diff --git a/src/game/game.c b/src/game/game.c index 8645c8c..6b5bd55 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -8,6 +8,7 @@ #include #include "../main.h" +#include "../window/colour.h" #include "./tetromino/shapes.h" #include "tetromino/placing.h" @@ -21,9 +22,9 @@ void game_init(GameData* const game_data) { // zero-initialize the game data *game_data = (GameData){0}; - // write the pointer information for the rows + // allocate size for each row for (int8_t i = 0; i < ROWS; i++) - game_data->row[i] = game_data->row_raw[i]; + game_data->rows[i] = calloc(COLUMNS, sizeof(Colour)); // set a random seed using the system clock srand(time(NULL)); @@ -48,3 +49,11 @@ void game_update(GameData* game_data, const uint8_t* keys) { // dbg_set_all(game_data); } + +void game_free(GameData* const game_data) { + // clear each row + for (int8_t i = 0; i < ROWS; i++) { + free(game_data->rows[i]); + game_data->rows[i] = NULL; + } +} diff --git a/src/game/game.h b/src/game/game.h index 2a92e2d..5b4cd68 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -19,8 +19,7 @@ typedef const Colour* CRow; typedef Colour* Row; typedef struct { - Colour* row[ROWS]; // stores how to interpert the raw level data - Colour row_raw[ROWS][COLUMNS]; // stores the raw level data + Row rows[ROWS]; SelectedShape selected; ShapeId next_shape; } GameData; @@ -28,3 +27,4 @@ typedef struct { void set_next_shape(GameData* game_data); void game_init(GameData* game_data); // initializes the game void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state +void game_free(GameData* game_data); // free all data stored with the game diff --git a/src/game/tetromino/placing.c b/src/game/tetromino/placing.c index 4679fe1..21b1943 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -93,9 +93,9 @@ void place_update(GameData* const game_data, const InputData move) { // set the shape if we moved vertically and intersected if (move & 4) { const int8_t y = selected->y + 1; - if (shape_intersects(game_data->row, selected->id, selected->x, y)) { - set_shape(game_data->row, selected->id, selected->x, selected->y); // if the shape intersects vertically, write the shape at the current position and return - clear_rows(game_data->row); // clear the rows that have been completed + if (shape_intersects(game_data->rows, selected->id, selected->x, y)) { + set_shape(game_data->rows, selected->id, selected->x, selected->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 game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0}; set_next_shape(game_data); @@ -109,7 +109,7 @@ void place_update(GameData* const game_data, const InputData move) { // update shape's X coordinate movement if ((move & 3) != 3 && (move & 3)) { const int8_t x = selected->x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x - if (shape_intersects(game_data->row, selected->id, x, selected->y) == false) { + if (shape_intersects(game_data->rows, selected->id, x, selected->y) == false) { selected->x = x; // set X if the shape does not intersect } } @@ -117,7 +117,7 @@ void place_update(GameData* const game_data, const InputData move) { // update the shape's rotation if (move & 8 || move & 16) { const ShapeId id = move & 8 ? rotate_id(selected->id, -8) : rotate_id(selected->id, 8); - if (shape_intersects(game_data->row, id, selected->x, selected->y) == false) { + if (shape_intersects(game_data->rows, id, selected->x, selected->y) == false) { selected->id = id; } } @@ -127,6 +127,6 @@ void place_update(GameData* const game_data, const InputData move) { void dbg_set_all(GameData* game_data) { for (uint8_t i = 0; i < TETROMINO_COUNT; i++) for (uint8_t r = 0; r < 4; r++) - set_shape(game_data->row, i | (r << 3), r * 4, i * 4); + set_shape(game_data->rows, i | (r << 3), r * 4, i * 4); } #endif diff --git a/src/main.c b/src/main.c index 8eab4ba..202247e 100644 --- a/src/main.c +++ b/src/main.c @@ -90,6 +90,7 @@ int main(int argc, char** argv) { } // cleanup of resources + game_free(&game_data); SDL_Quit(); return 0; diff --git a/src/window/renderer.c b/src/window/renderer.c index 3377612..3169df9 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -63,7 +63,7 @@ static void render_selected(SDL_Renderer* renderer, SelectedShape selected) { // draw the block data in the level static void render_level(SDL_Renderer* renderer, GameData* data) { for (int8_t y = 0; y < ROWS; y++) { - Row row = data->row[y]; + Row row = data->rows[y]; for (int8_t x = 0; x < COLUMNS; x++) { if (row[x].packed != 0) {