diff --git a/src/game/game.c b/src/game/game.c index 9c9ef8c..5fe4843 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -13,9 +13,24 @@ #include "tetromino/placing.h" -void set_next_shape(GameData* const game_data) { - ShapeId id = (rand() % TETROMINO_COUNT) | (rand() % 4 << 3); - game_data->next_shape = id; +void next_shape(GameData* const game_data) { + game_data->curr_idx++; // increase the current shape index + game_data->sel_x = COLUMNS / 2 - SHAPE_WIDTH / 2; // move the shape position to the centre + game_data->sel_y = 0; + + // return if know which shape is next + if (game_data->curr_idx < (TETROMINO_COUNT - 1)) + return; + + game_data->curr_idx = 0; + + // shuffle the next shapes using a Fisher–Yates shuffle + for (uint8_t i = 0; i < (TETROMINO_COUNT - 1); i++) { + const uint8_t j = i + rand() % (TETROMINO_COUNT - i); + const ShapeId cache = game_data->nxt[i]; + game_data->nxt[i] = game_data->nxt[j]; + game_data->nxt[j] = cache; + } } void game_init(GameData* const game_data) { @@ -25,15 +40,18 @@ void game_init(GameData* const game_data) { // allocate size for each row for (int8_t i = 0; i < ROWS; i++) { game_data->rows[i] = calloc(COLUMNS, sizeof(Colour)); - //game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging + // game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging } // set a random seed using the system clock srand(time(NULL)); - set_next_shape(game_data); - game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0}; - set_next_shape(game_data); + // set the shape data in each slot to it's corrsponding ID + for (ShapeId i = 0; i < TETROMINO_COUNT; i++) + game_data->nxt[i] = i; + + game_data->curr_idx = ~1; // set the current index to the max (well, so it becomes the max after increment) + next_shape(game_data); // select the next shape (a refresh is triggered due to the current index being too high) } // called every time the game's state is updated diff --git a/src/game/game.h b/src/game/game.h index 2f2fb5a..3cb948a 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -9,22 +9,18 @@ #define COLUMNS ((int8_t)10) #define ROWS ((int8_t)(COLUMNS * 2)) -typedef struct { - ShapeId id; - int8_t x; - int8_t y; -} SelectedShape; - typedef const Colour* CRow; typedef Colour* Row; typedef struct { Row rows[ROWS]; - SelectedShape selected; - ShapeId next_shape; + ShapeId nxt[7]; // the order of the shape ids that they should appear in + uint8_t curr_idx; // current shape index + int8_t sel_x; // selected shape x position + int8_t sel_y; // selected shape y position } GameData; -void set_next_shape(GameData* game_data); +void 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 d86467a..3f9e1c8 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -97,36 +97,39 @@ static inline ShapeId rotate_id(const ShapeId id, const int dir) { } void place_update(GameData* const game_data, const InputData move) { - SelectedShape* const selected = &game_data->selected; + // store the current index, only changes when placed (which yields no movement) and rotation (which occurs last) + ShapeId const curr_idx = game_data->curr_idx; + // set the shape if we moved vertically and intersected if (move & 4) { - const int8_t y = selected->y + 1; - 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 + const int8_t y = game_data->sel_y + 1; + if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, y)) { + set_shape(game_data->rows, game_data->nxt[curr_idx], 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 - game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0}; - set_next_shape(game_data); + next_shape(game_data); return; } // otherwise, just set Y - selected->y = y; + game_data->sel_y = y; } // 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->rows, selected->id, x, selected->y) == false) { - selected->x = x; // set X if the shape does not intersect + const int8_t x = game_data->sel_x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x + if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], x, game_data->sel_y) == false) { + game_data->sel_x = x; // set X if the shape does not intersect } } // 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->rows, id, selected->x, selected->y) == false) { - selected->id = id; + const ShapeId id = move & 8 // check which direction we should move + ? rotate_id(game_data->nxt[curr_idx], -8) + : rotate_id(game_data->nxt[curr_idx], 8); + if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) { + game_data->nxt[curr_idx] = id; } } } diff --git a/src/window/renderer.c b/src/window/renderer.c index 3169df9..ce8af65 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -44,19 +44,20 @@ static inline void set_colour(SDL_Renderer* renderer, Colour c) { } // draw the selected block -static void render_selected(SDL_Renderer* renderer, SelectedShape selected) { - Shape selected_shape = shape_from_id(selected.id); - set_colour(renderer, colour_from_id(selected.id)); +static void render_selected(SDL_Renderer* renderer, GameData* const game_data) { + const ShapeId id = game_data->nxt[game_data->curr_idx]; + const Shape shape = shape_from_id(id); + set_colour(renderer, colour_from_id(id)); for (int8_t y = 0; y < SHAPE_HEIGHT; y++) { - ShapeRow shape_row = shape_get_row(selected_shape, y); + const ShapeRow shape_row = shape_get_row(shape, y); if (shape_row == 0) continue; for (int8_t x = 0; x < SHAPE_WIDTH; x++) if (is_set(shape_row, x)) - draw_block(renderer, selected.x + x, selected.y + y); + draw_block(renderer, game_data->sel_x + x, game_data->sel_y + y); } } @@ -83,7 +84,7 @@ void renderer_update(const RenderData* render_data) { success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); success |= SDL_RenderClear(renderer); - render_selected(renderer, render_data->game_data->selected); + render_selected(renderer, render_data->game_data); render_level(renderer, render_data->game_data); if (success < 0) {