refactor: comments and small semantic fixes

This commit is contained in:
2025-07-03 13:32:17 +02:00
parent ede32b4ff7
commit baf59fa84e
2 changed files with 13 additions and 18 deletions

View File

@@ -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);

View File

@@ -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);