mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
fix: not able to place towards the far left
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user