diff --git a/src/game/game.c b/src/game/game.c index 031c47c..8645c8c 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -22,7 +22,7 @@ void game_init(GameData* const game_data) { *game_data = (GameData){0}; // write the pointer information for the rows - for (uint8_t i = 0; i < ROWS; i++) + for (int8_t i = 0; i < ROWS; i++) game_data->row[i] = game_data->row_raw[i]; // set a random seed using the system clock diff --git a/src/game/game.h b/src/game/game.h index 8c6f6ec..2a92e2d 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -6,13 +6,13 @@ #include "tetromino/shapes.h" // stores the data used in the game -#define COLUMNS ((uint8_t)16) -#define ROWS ((uint8_t)(COLUMNS * 2)) +#define COLUMNS ((int8_t)16) +#define ROWS ((int8_t)(COLUMNS * 2)) typedef struct { ShapeId id; - uint8_t x; - uint8_t y; + int8_t x; + int8_t y; } SelectedShape; typedef const Colour* CRow; diff --git a/src/game/tetromino/placing.c b/src/game/tetromino/placing.c index 8fb37da..82a4178 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -8,7 +8,7 @@ static bool is_filled(CRow row) { - for (uint8_t x = 0; x < COLUMNS; x++) { + for (int8_t x = 0; x < COLUMNS; x++) { if (row[x].packed == NONE) { return false; } @@ -18,11 +18,11 @@ static bool is_filled(CRow row) { } static void clear_rows(Row* row) { - uint8_t filled = 0; + int8_t filled = 0; // loop through each row (excluding the empty rows at the top when clearing a line) - for (uint8_t y = 0; y < (ROWS - filled); y++) { - const uint8_t i = (ROWS - 1) - y; // get the index starting from the bottom + for (int8_t y = 0; y < (ROWS - filled); y++) { + const int8_t i = (ROWS - 1) - y; // get the index starting from the bottom Row row_cache = row[i]; // cache this row address in case the row is filled row[i] = row[i - filled]; // set the row to the new or same address @@ -30,7 +30,7 @@ static void clear_rows(Row* row) { if (!is_filled(row[i])) continue; // zero out the cache - for (uint8_t x = 0; x < COLUMNS; x++) + for (int8_t x = 0; x < COLUMNS; x++) row_cache[x].packed = 0; // write the cached address to a row starting from the top @@ -41,36 +41,36 @@ static void clear_rows(Row* row) { } // sets a shape to the screen -static void set_shape_i(Row* row, const ShapeId id, const uint8_t pos_x) { +static void set_shape_i(Row* row, const ShapeId id, const int8_t pos_x) { const Shape shape = shape_from_id(id); const Colour colour = colour_from_id(id); - for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) { + for (int8_t y = 0; y < SHAPE_HEIGHT; y++) { ShapeRow shape_row = shape_get_row(shape, y); if (shape_row == 0) continue; - for (uint8_t x = 0; x < SHAPE_WIDTH; x++) + for (int8_t x = 0; x < SHAPE_WIDTH; x++) if (is_set(shape_row, x)) row[y][x + pos_x] = colour; } } -static inline void set_shape(Row* row, const ShapeId id, const uint8_t pos_x, const uint8_t pos_y) { +static inline void set_shape(Row* row, const ShapeId id, const int8_t pos_x, const int8_t pos_y) { set_shape_i(&row[pos_y], id, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row } -static bool shape_intersects(const Row* rows, const ShapeId id, const uint8_t x, const uint8_t y) { +static bool shape_intersects(const Row* rows, const ShapeId id, const int8_t x, const int8_t y) { const Shape shape = shape_from_id(id); - for (uint8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) { + for (int8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) { const ShapeRow shape_row = shape_get_row(shape, y0); // get the shape row if (shape_row == 0) continue; // if the row doesn't contain data; continue - for (uint8_t x0 = 0; x0 < SHAPE_WIDTH; x0++) { + for (int8_t x0 = 0; x0 < SHAPE_WIDTH; x0++) { if (is_set(shape_row, x0) == false) continue; // if the bit isn't set at this index; continue - const uint8_t x1 = x + x0; - const uint8_t y1 = y + y0; + const int8_t x1 = x + x0; + const int8_t y1 = y + y0; if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds if (y1 < 0 || y1 >= ROWS) return true; // if Y is out of bounds @@ -88,7 +88,7 @@ void place_update(GameData* const game_data, const InputData move) { SelectedShape* const selected = &game_data->selected; // set the shape if we moved vertically and intersected if (move & 4) { - const uint8_t y = selected->y + 1; + 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 @@ -104,7 +104,7 @@ void place_update(GameData* const game_data, const InputData move) { // update shape's X coordinate movement if ((move & 3) != 3 && (move & 3)) { - const uint8_t x = selected->x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x + 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) { selected->x = x; // set X if the shape does not intersect } diff --git a/src/window/renderer.c b/src/window/renderer.c index 9e83955..3377612 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -34,7 +34,7 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) { } // draws a block at the specified position -static inline int draw_block(SDL_Renderer* renderer, uint8_t x, uint8_t y) { +static inline int draw_block(SDL_Renderer* renderer, int8_t x, int8_t y) { return SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1}); } @@ -48,13 +48,13 @@ 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)); - for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) { + for (int8_t y = 0; y < SHAPE_HEIGHT; y++) { ShapeRow shape_row = shape_get_row(selected_shape, y); if (shape_row == 0) continue; - for (uint8_t x = 0; x < SHAPE_WIDTH; x++) + for (int8_t x = 0; x < SHAPE_WIDTH; x++) if (is_set(shape_row, x)) draw_block(renderer, selected.x + x, selected.y + y); } @@ -62,10 +62,10 @@ 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 (uint8_t y = 0; y < ROWS; y++) { + for (int8_t y = 0; y < ROWS; y++) { Row row = data->row[y]; - for (uint8_t x = 0; x < COLUMNS; x++) { + for (int8_t x = 0; x < COLUMNS; x++) { if (row[x].packed != 0) { set_colour(renderer, row[x]); draw_block(renderer, x, y);