From 09a6684f41c82b352a81ed114d774c4eecd5e0f4 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 28 Jan 2017 15:22:07 +1300 Subject: Style adjustment --- plot.c | 24 ++++++------ plot.h | 6 +-- tetris.c | 132 +++++++++++++++++++++++++++++++-------------------------------- 3 files changed, 80 insertions(+), 82 deletions(-) diff --git a/plot.c b/plot.c index 419513e..25bca51 100644 --- a/plot.c +++ b/plot.c @@ -6,12 +6,14 @@ static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; -void plot_update() +void +plot_update(void) { SDL_RenderPresent(renderer); } -void plot_rect(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct colour *c) +void +plot_rect(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct colour *c) { SDL_Rect rect; @@ -25,7 +27,8 @@ void plot_rect(unsigned int x, unsigned int y, unsigned int width, unsigned int SDL_RenderFillRect(renderer, &rect); } -void plot_cell(unsigned int x, unsigned int y, struct colour *c) +void +plot_cell(unsigned int x, unsigned int y, struct colour *c) { plot_rect( x*CELL_SIZE+(x-1)*BORDER_THICKNESS, @@ -36,17 +39,18 @@ void plot_cell(unsigned int x, unsigned int y, struct colour *c) ); } -void plot_clear() +void +plot_clear(void) { /* blank out the background with black */ SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_RenderFillRect(renderer, NULL); } -int plot_init() +int +plot_init(void) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Failed to init SDL video: %s\n", SDL_GetError()); return 1; } @@ -60,15 +64,13 @@ int plot_init() SDL_WINDOW_SHOWN ); - if (window == NULL) - { + if (window == NULL) { fprintf(stderr, "Failed to create window: %s\n", SDL_GetError()); return 1; } renderer = SDL_CreateRenderer(window, 0, 0); - if (renderer == NULL) - { + if (renderer == NULL) { fprintf(stderr, "Failed to get window renderer: %s\n", SDL_GetError()); return 1; } diff --git a/plot.h b/plot.h index e73c481..edde581 100644 --- a/plot.h +++ b/plot.h @@ -1,8 +1,8 @@ void plot_rect(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct colour *c); void plot_cell(unsigned int x, unsigned int y, struct colour *c); -void plot_clear(); -int plot_init(); -void plot_update(); +void plot_clear(void); +int plot_init(void); +void plot_update(void); #define CELL_SIZE 25 #define BORDER_THICKNESS 2 diff --git a/tetris.c b/tetris.c index ce60f21..3218e66 100644 --- a/tetris.c +++ b/tetris.c @@ -24,20 +24,20 @@ struct colour palette[] = { {.r=0xFF,.g=0xFF,.b=0xFF} }; -void draw_board(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) +void +draw_board(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) { unsigned int x,y; plot_clear(); - for (y = 0; y < HEIGHT_CELLS; y++) - { - for (x = 0; x < WIDTH_CELLS; x++) - { + for (y = 0; y < HEIGHT_CELLS; y++) { + for (x = 0; x < WIDTH_CELLS; x++) { plot_cell(x,y, (*board)[x][y]); } } } -void draw_piece(int x, int y, struct colour *c, char (*piece)[4][4]) +void +draw_piece(int x, int y, struct colour *c, char (*piece)[4][4]) { int px, py, ipx, ipy, end_x, end_y, wx, wy; ipx = (x < 0)? -x : 0; @@ -51,7 +51,8 @@ void draw_piece(int x, int y, struct colour *c, char (*piece)[4][4]) plot_cell(wx, wy, c); } -void drop_piece(int x, int y, struct piece *piece, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) +void +drop_piece(int x, int y, struct piece *piece, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) { int px, py, ipx, ipy, end_x, end_y, wx, wy; ipx = (x < 0)? -x : 0; @@ -65,7 +66,8 @@ void drop_piece(int x, int y, struct piece *piece, struct colour* (*board)[WIDTH (*board)[wx][wy] = piece->colour; } -Uint32 gravity_callback(Uint32 interval, void *param) +Uint32 +gravity_callback(Uint32 interval, void *param) { (void)param; /* solves unused parameter warn+error */ SDL_Event e; @@ -82,11 +84,11 @@ Uint32 gravity_callback(Uint32 interval, void *param) return interval; } -int hit_floor(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) +int +hit_floor(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) { int px,py; - for (px = 0; px < 4; px++) - { + for (px = 0; px < 4; px++) { /* seek to first cell of column */ py = 0; while (py < 4 && (*held->bitmap)[px][py] == 0) @@ -106,13 +108,12 @@ int hit_floor(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CE return 0; } -int hit_side(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) +int +hit_side(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) { int px,py; - for (py = 0; py < 4; py++) - { - for (px = 0; px < 4; px++) - { + for (py = 0; py < 4; py++) { + for (px = 0; px < 4; px++) { if ((*held->bitmap)[px][py] && ( x + px >= WIDTH_CELLS @@ -125,12 +126,14 @@ int hit_side(int x, int y, struct piece *held, struct colour* (*board)[WIDTH_CEL return 0; } -void update_bitmap(struct piece *held) +void +update_bitmap(struct piece *held) { held->bitmap = &(tetrominoes[0][held->type][held->rotation]); } -void new_piece(struct piece *held) +void +new_piece(struct piece *held) { held->colour = &palette[rand() % 7 + 1]; held->type = rand()%7; @@ -139,7 +142,8 @@ void new_piece(struct piece *held) } -void rotate(struct piece *held, int direction) +void +rotate(struct piece *held, int direction) { held->rotation += direction; /* FIXME need to handle direction not in [-4, 4] */ @@ -153,17 +157,15 @@ void rotate(struct piece *held, int direction) } -void clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) +void +clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) { char row; int x, y, x1, y1; - for (y = 0; y < HEIGHT_CELLS; y++) - { + for (y = 0; y < HEIGHT_CELLS; y++) { row = 1; - for (x = 0; x < WIDTH_CELLS; x++) - { - if ((*board)[x][y] == &(palette[0])) - { + for (x = 0; x < WIDTH_CELLS; x++) { + if ((*board)[x][y] == &(palette[0])) { row = 0; break; } @@ -180,7 +182,8 @@ void clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) } } -void main_loop() +void +main_loop() { struct colour *board[WIDTH_CELLS][HEIGHT_CELLS]; SDL_Event e = {0}; @@ -202,8 +205,7 @@ void main_loop() SDL_AddTimer(500, &gravity_callback, NULL); char lockout; - while (running) - { + while (running) { lockout = 0; clear_rows(&board); @@ -217,54 +219,48 @@ void main_loop() draw_piece(x, y, held.colour, held.bitmap); plot_update(); SDL_WaitEvent(&e); - switch (e.type) - { - case SDL_USEREVENT: - if (lockout) { - drop_piece(x, y, &held, &board); - last_x = last_y = x = y = 0; - new_piece(&held); - lockout = 0; - } else { - last_y = y++; /* gravity */ - last_x = x; - } + switch (e.type) { + case SDL_USEREVENT: + if (lockout) { + drop_piece(x, y, &held, &board); + last_x = last_y = x = y = 0; + new_piece(&held); + lockout = 0; + } else { + last_y = y++; /* gravity */ + last_x = x; + } + break; + case SDL_QUIT: + fprintf(stderr, "quit\n"); + running = false; + break; + case SDL_KEYDOWN: + switch (e.key.keysym.sym) + { + case SDLK_a: last_x = x--; break; + case SDLK_d: last_x = x++; break; + case SDLK_w: + i = 0; + do { + rotate(&held, 1); + } while(hit_side(x, y, &held, &board) && i++ < 4); break; - case SDL_QUIT: - fprintf(stderr, "quit\n"); + case SDLK_q: running = false; break; - - case SDL_KEYDOWN: - switch (e.key.keysym.sym) - { - case SDLK_a: last_x = x--; break; - case SDLK_d: last_x = x++; break; - case SDLK_w: - i = 0; - do - { - rotate(&held, 1); - } while(hit_side(x, y, &held, &board) && i++ < 4); - break; - case SDLK_q: - running = false; - break; - } - break; - default: - break; + } + break; + default: + break; } - //} } } -int main() +int +main(int argc, char **argv) { srand(time(NULL)); plot_init(); main_loop(); } - - - -- cgit v1.1