mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
add score increasement
This commit is contained in:
@@ -17,11 +17,11 @@ static bool is_filled(CRow const row) {
|
|||||||
return true;
|
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
|
Row cache[4] = {0}; // you can only clear four rows at a time
|
||||||
struct {
|
struct {
|
||||||
uint8_t filled : 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 (use extra bit for carry)
|
uint8_t checked : 3; // values will only ever be 0..4
|
||||||
} dat = {0};
|
} dat = {0};
|
||||||
|
|
||||||
// loop through each row (excluding the empty rows at the top when clearing a line)
|
// 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
|
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--;
|
dat.filled--;
|
||||||
rows[dat.filled] = cache[dat.filled];
|
rows[dat.filled] = cache[dat.filled];
|
||||||
|
|
||||||
// zero out the filled row
|
// zero out the filled row
|
||||||
for (int8_t x = 0; x < COLUMNS; x++)
|
for (int8_t x = 0; x < COLUMNS; x++)
|
||||||
cache[dat.filled][x].packed = 0;
|
cache[dat.filled][x].packed = 0;
|
||||||
}
|
} while (dat.filled > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// sets a shape to the screen
|
// 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;
|
int8_t const y = game_data->sel_y + 1;
|
||||||
if (shape_intersects(game_data->rows, curr_id, game_data->sel_x, y)) {
|
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
|
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);
|
next_shape(game_data);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -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) {
|
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
|
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_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);
|
SDL_Texture* const txt_texture = SDL_CreateTextureFromSurface(renderer, txt_surface);
|
||||||
|
|||||||
Reference in New Issue
Block a user