mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
optimize clearing lines by using pointers to define the row order
This commit is contained in:
@@ -3,7 +3,9 @@
|
|||||||
#include <SDL_scancode.h>
|
#include <SDL_scancode.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "../main.h"
|
#include "../main.h"
|
||||||
#include "../window/colour.h"
|
#include "../window/colour.h"
|
||||||
@@ -11,32 +13,58 @@
|
|||||||
#include "tetromino/placing.h"
|
#include "tetromino/placing.h"
|
||||||
|
|
||||||
|
|
||||||
static void clear_row(Row* row, uint8_t y) {
|
void game_init(GameData* const game_data) {
|
||||||
// move the rows down from the specified height
|
// zero-initialize the game data
|
||||||
for (; y > 0; y--)
|
*game_data = (GameData){0};
|
||||||
memcpy(row[y].columns, row[y - 1].columns, COLUMNS * sizeof(Colour));
|
|
||||||
|
|
||||||
memset(row[0].columns, NONE, sizeof(Colour));
|
// write the pointer information for the rows
|
||||||
|
for (uint8_t i = 0; i < ROWS; i++)
|
||||||
|
game_data->row[i] = game_data->row_raw[i];
|
||||||
|
|
||||||
|
// set a random seed using the system clock
|
||||||
|
srand(time(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool is_filled(CRow row) {
|
||||||
|
for (uint8_t x = 0; x < COLUMNS; x++) {
|
||||||
|
if (row[x].packed == NONE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void set_row(Row* rows, const uint8_t y, Row row) {
|
||||||
|
rows[(ROWS - 1) - y] = row;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear_rows(Row* row) {
|
static void clear_rows(Row* row) {
|
||||||
// start at the lowest row
|
uint8_t filled_rows = 0;
|
||||||
// integer will wrap
|
|
||||||
for (uint8_t y = ROWS - 1; y < ROWS; y--) {
|
|
||||||
Row* crow = &row[y]; // the current row
|
|
||||||
bool nfilled = false; // whether this row is filled
|
|
||||||
|
|
||||||
// loop through all the columns or till nfilled is true
|
// loop through each row
|
||||||
for (uint8_t x = 0; x < COLUMNS; x++) {
|
for (uint8_t y = 0; y < ROWS; y++) {
|
||||||
if (crow->columns[x].packed == NONE) {
|
// zero out the row instead if we've reached the top rows
|
||||||
nfilled = true;
|
if ((ROWS - y) <= filled_rows) {
|
||||||
break;
|
for (uint8_t x = 0; x < COLUMNS; x++)
|
||||||
}
|
row[filled_rows--][x].packed = 0; // use filled_rows as the y index and decrease them as we go.
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// continue if the row isn;t filled
|
// get the row we currently need to check
|
||||||
if (!nfilled)
|
Row crow = filled_rows > 0
|
||||||
clear_row(crow, y);
|
? row[y + filled_rows]
|
||||||
|
: row[y];
|
||||||
|
|
||||||
|
// set the current y to the current row
|
||||||
|
Row row_cache = row[y];
|
||||||
|
set_row(row, y, crow);
|
||||||
|
|
||||||
|
if (!is_filled(crow)) continue;
|
||||||
|
|
||||||
|
// write the cached row to the top if we know that the current row pointer will be lost
|
||||||
|
row[filled_rows] = row_cache;
|
||||||
|
filled_rows++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,5 +94,5 @@ void game_update(GameData* game_data, const uint8_t* keys) {
|
|||||||
update_input(game_data, keys);
|
update_input(game_data, keys);
|
||||||
|
|
||||||
// tmp_set_random(game_data);
|
// tmp_set_random(game_data);
|
||||||
// tmp_set_all(game_data);
|
tmp_set_all(game_data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,15 +15,14 @@ typedef struct {
|
|||||||
uint8_t y;
|
uint8_t y;
|
||||||
} SelectedShape;
|
} SelectedShape;
|
||||||
|
|
||||||
typedef struct {
|
typedef const Colour* CRow;
|
||||||
Colour columns[COLUMNS];
|
typedef Colour* Row;
|
||||||
} Row;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SelectedShape selected;
|
SelectedShape selected;
|
||||||
Row row[ROWS];
|
Colour* row[ROWS]; // stores how to interpert the raw level data
|
||||||
|
Colour row_raw[ROWS][COLUMNS]; // stores the raw level data
|
||||||
} GameData;
|
} GameData;
|
||||||
|
|
||||||
// updates the game's state
|
void game_init(GameData* game_data); // initializes the game
|
||||||
void game_update(GameData* game_data, const uint8_t* keys);
|
void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ static void set_shape_i(Row* row, const ShapeId id, const uint8_t pos_x) {
|
|||||||
|
|
||||||
for (uint8_t x = 0; x < SHAPE_WIDTH; x++)
|
for (uint8_t x = 0; x < SHAPE_WIDTH; x++)
|
||||||
if (is_set(shape_row, x))
|
if (is_set(shape_row, x))
|
||||||
row[y].columns[x + pos_x] = colour;
|
row[y][x + pos_x] = colour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ static void init(void) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set a random seed using the system clock
|
// initialize the game
|
||||||
srand(time(NULL));
|
game_init(&game_data);
|
||||||
|
|
||||||
// initialize audio
|
// initialize audio
|
||||||
// AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
// AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||||
|
|||||||
@@ -74,8 +74,8 @@ void renderer_update(const RenderData* render_data) {
|
|||||||
Row row = data->row[y];
|
Row row = data->row[y];
|
||||||
|
|
||||||
for (uint8_t x = 0; x < COLUMNS; x++) {
|
for (uint8_t x = 0; x < COLUMNS; x++) {
|
||||||
if (row.columns[x].packed != 0) {
|
if (row[x].packed != 0) {
|
||||||
set_colour(renderer, row.columns[x]);
|
set_colour(renderer, row[x]);
|
||||||
success |= draw_block(renderer, x, y);
|
success |= draw_block(renderer, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user