add score increasement

This commit is contained in:
2025-02-04 15:20:20 +01:00
parent 319282b7c8
commit 65bc44ef12
2 changed files with 13 additions and 7 deletions

View File

@@ -17,11 +17,11 @@ static bool is_filled(CRow const row) {
return true;
}
static void clear_rows(Row* const rows) {
static void clear_rows(Row* const rows, uint16_t* const score) {
Row cache[4] = {0}; // you can only clear four rows at a time
struct {
uint8_t filled : 3; // values will only ever be 0..4 (use extra bit for carry)
uint8_t checked : 3; // values will only ever be 0..4 (use extra bit for carry)
uint8_t filled : 3; // values will only ever be 0..4
uint8_t checked : 3; // values will only ever be 0..4
} dat = {0};
// loop through each row (excluding the empty rows at the top when clearing a line)
@@ -42,14 +42,18 @@ static void clear_rows(Row* const rows) {
y--; // decrease y to check this line again
}
while (dat.filled > 0) {
if (dat.filled == 0) return;
*score += 8 << dat.filled;
// do while, as we already know that entering the loop that filled is non-zero
do {
dat.filled--;
rows[dat.filled] = cache[dat.filled];
// zero out the filled row
for (int8_t x = 0; x < COLUMNS; x++)
cache[dat.filled][x].packed = 0;
}
} while (dat.filled > 0);
}
// sets a shape to the screen
@@ -107,7 +111,7 @@ void place_update(GameData* const game_data, InputData const move) {
int8_t const y = game_data->sel_y + 1;
if (shape_intersects(game_data->rows, curr_id, game_data->sel_x, y)) {
set_shape(game_data->rows, curr_id, game_data->sel_x, game_data->sel_y); // if the shape intersects vertically, write the shape at the current position and return
clear_rows(game_data->rows); // clear the rows that have been completed
clear_rows(game_data->rows, &game_data->score); // clear the rows that have been completed
next_shape(game_data);
return;

View File

@@ -47,7 +47,9 @@ static inline int32_t get_row_pos(uint8_t row) {
static void draw_score_text(SDL_Renderer* const renderer, TTF_Font* const font, uint16_t const score) {
char score_text[5]; // max digits of a uint16
sprintf(score_text, "%hu", score);
if (!score) sprintf(score_text, "0");
else sprintf(score_text, "%hu0", score);
SDL_Surface* const txt_surface = TTF_RenderText_Solid(font, score_text, (SDL_Colour){colour8_red32(COLOUR_SCORE), colour8_green32(COLOUR_SCORE), colour8_blue32(COLOUR_SCORE), 0xFF});
SDL_Texture* const txt_texture = SDL_CreateTextureFromSurface(renderer, txt_surface);