From baf59fa84e7d14cb95fe48363255113609300d46 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 3 Jul 2025 13:32:17 +0200 Subject: [PATCH] refactor: comments and small semantic fixes --- src/game/tetromino/placing.h | 1 - src/io/render.c | 30 +++++++++++++----------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/game/tetromino/placing.h b/src/game/tetromino/placing.h index 9ac59f1..a990e48 100644 --- a/src/game/tetromino/placing.h +++ b/src/game/tetromino/placing.h @@ -5,7 +5,6 @@ #include "../game.h" -// TODO: this singular function does too much, break it up via different returns and such. /* updates the movement of the pdat structure, updating the rows when colliding downwards. * closes window when the next shape intersects with the current one */ void place_update(struct gamedata* gdat, int movdat); diff --git a/src/io/render.c b/src/io/render.c index ca06dd3..1c45e0c 100644 --- a/src/io/render.c +++ b/src/io/render.c @@ -29,29 +29,19 @@ struct gamedata const* gdat = NULL; static SDL_Surface* score_surface = NULL; static SDL_Texture* score_texture = NULL; -void render_init(SDL_Window* win, struct gamedata const* game_data) { - rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); - if (rend == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); - - font = TTF_OpenFont("pixeldroid_botic-regular.ttf", PX_DENS); - if (font == NULL) error("Failed to open font! TTF Error: %s", TTF_GetError()); - - gdat = game_data; -} - static inline i32 colpos(uint column) { return column * BLOCK_WIDTH + 1 + TET_PADDING; } -static inline int32_t get_row_pos(uint row) { +static inline i32 rowpos(uint row) { return row * BLOCK_HEIGHT + 1 + TET_PADDING; } static void draw_score_text(void) { - static u16 prev_pts = 0xFFFF; + static u16 prev_pts = 0xFFFF; // initialise to maximum, so the code below is triggered on first run if (prev_pts ^ gdat->pnts) { - char score_text[6] = "0"; // max digits of a u16 + null terminator + char score_text[6] = "0"; // max (base 10) digits of a u16 + null terminator prev_pts = gdat->pnts; if (gdat->pnts) sprintf(score_text, "%hu0", gdat->pnts); @@ -66,14 +56,11 @@ static void draw_score_text(void) { SDL_RenderCopy(rend, score_texture, NULL, &text_rect); } -// TODO: this is suboptimal, since each block will be 2 triangles, wasting perf. Consider using switch...case hard-coded drawing -// draws a block at the specified position static inline int draw_block(SDL_Renderer* const renderer, i8vec2 pos) { SDL_Rect const block = {colpos(pos[VX]), rowpos(pos[VY]), BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1}; return SDL_RenderFillRect(renderer, &block); } -// draws a shape at the specified position static void draw_shape(u8 const id, i8vec2 pos) { set_colour8(rend, colour_from_id(id)); i8vec2 bpos[4]; @@ -84,7 +71,6 @@ static void draw_shape(u8 const id, i8vec2 pos) { draw_block(rend, pos + bpos[3]); } -// draw the block data in the level static void render_level(void) { for (int y = 0; y < ROWS; y++) { u8 const* row = gdat->rows[y]; @@ -98,6 +84,16 @@ static void render_level(void) { } } +void render_init(SDL_Window* win, struct gamedata const* game_data) { + rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); + if (rend == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); + + font = TTF_OpenFont("pixeldroid_botic-regular.ttf", PX_DENS); + if (font == NULL) error("Failed to open font! TTF Error: %s", TTF_GetError()); + + gdat = game_data; +} + void render_update(void) { set_colour32(rend, COLOUR32_BLACK); SDL_RenderClear(rend);