fix: not able to place towards the far left

This commit is contained in:
2025-01-29 17:46:10 +01:00
parent 48117451cd
commit 8d7434af28
4 changed files with 26 additions and 26 deletions

View File

@@ -22,7 +22,7 @@ void game_init(GameData* const game_data) {
*game_data = (GameData){0}; *game_data = (GameData){0};
// write the pointer information for the rows // 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]; game_data->row[i] = game_data->row_raw[i];
// set a random seed using the system clock // set a random seed using the system clock

View File

@@ -6,13 +6,13 @@
#include "tetromino/shapes.h" #include "tetromino/shapes.h"
// stores the data used in the game // stores the data used in the game
#define COLUMNS ((uint8_t)16) #define COLUMNS ((int8_t)16)
#define ROWS ((uint8_t)(COLUMNS * 2)) #define ROWS ((int8_t)(COLUMNS * 2))
typedef struct { typedef struct {
ShapeId id; ShapeId id;
uint8_t x; int8_t x;
uint8_t y; int8_t y;
} SelectedShape; } SelectedShape;
typedef const Colour* CRow; typedef const Colour* CRow;

View File

@@ -8,7 +8,7 @@
static bool is_filled(CRow row) { 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) { if (row[x].packed == NONE) {
return false; return false;
} }
@@ -18,11 +18,11 @@ static bool is_filled(CRow row) {
} }
static void clear_rows(Row* 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) // loop through each row (excluding the empty rows at the top when clearing a line)
for (uint8_t y = 0; y < (ROWS - filled); y++) { for (int8_t y = 0; y < (ROWS - filled); y++) {
const uint8_t i = (ROWS - 1) - y; // get the index starting from the bottom 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 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 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; if (!is_filled(row[i])) continue;
// zero out the cache // 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; row_cache[x].packed = 0;
// write the cached address to a row starting from the top // 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 // 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 Shape shape = shape_from_id(id);
const Colour colour = colour_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); ShapeRow shape_row = shape_get_row(shape, y);
if (shape_row == 0) if (shape_row == 0)
continue; 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)) if (is_set(shape_row, x))
row[y][x + pos_x] = colour; 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 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); 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 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 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 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 int8_t x1 = x + x0;
const uint8_t y1 = y + y0; const int8_t y1 = y + y0;
if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds 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 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; SelectedShape* const selected = &game_data->selected;
// set the shape if we moved vertically and intersected // set the shape if we moved vertically and intersected
if (move & 4) { 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)) { 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 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 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 // update shape's X coordinate movement
if ((move & 3) != 3 && (move & 3)) { 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) { if (shape_intersects(game_data->row, selected->id, x, selected->y) == false) {
selected->x = x; // set X if the shape does not intersect selected->x = x; // set X if the shape does not intersect
} }

View File

@@ -34,7 +34,7 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
} }
// draws a block at the specified position // 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}); 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); Shape selected_shape = shape_from_id(selected.id);
set_colour(renderer, colour_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); ShapeRow shape_row = shape_get_row(selected_shape, y);
if (shape_row == 0) if (shape_row == 0)
continue; 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)) if (is_set(shape_row, x))
draw_block(renderer, selected.x + x, selected.y + y); 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 // draw the block data in the level
static void render_level(SDL_Renderer* renderer, GameData* data) { 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]; 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) { if (row[x].packed != 0) {
set_colour(renderer, row[x]); set_colour(renderer, row[x]);
draw_block(renderer, x, y); draw_block(renderer, x, y);