mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
fix: rows are now being cleared properly and should no longer have any problems.
This commit is contained in:
@@ -23,8 +23,10 @@ void game_init(GameData* const game_data) {
|
|||||||
*game_data = (GameData){0};
|
*game_data = (GameData){0};
|
||||||
|
|
||||||
// allocate size for each row
|
// allocate size for each row
|
||||||
for (int8_t i = 0; i < ROWS; i++)
|
for (int8_t i = 0; i < ROWS; i++) {
|
||||||
game_data->rows[i] = calloc(COLUMNS, sizeof(Colour));
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// set a random seed using the system clock
|
// set a random seed using the system clock
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "placing.h"
|
#include "placing.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "../../window/colour.h"
|
#include "../../window/colour.h"
|
||||||
#include "../game.h"
|
#include "../game.h"
|
||||||
@@ -18,30 +17,39 @@ static bool is_filled(CRow row) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear_rows(Row* row) {
|
static void clear_rows(Row* rows) {
|
||||||
int8_t filled = 0;
|
Row cache[4] = {0}; // you can only clear four rows at a time
|
||||||
|
struct {
|
||||||
|
uint8_t filled : 3; // values will only ever be 0..4 (use extra bit for carry)
|
||||||
|
uint8_t checked : 3; // values will only ever be 0..4 (use extra bit for carry)
|
||||||
|
} dat = {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 (int8_t y = 0; y < (ROWS - filled); y++) {
|
for (int8_t y = 0; y < (ROWS - dat.filled); y++) {
|
||||||
const int8_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
|
rows[i] = rows[i - dat.filled]; // set the row to the new or same address
|
||||||
row[i] = row[i - filled]; // set the row to the new or same address
|
|
||||||
|
|
||||||
if (!is_filled(row[i])) continue;
|
// continue if the line isn't filled or the max amount has been reached
|
||||||
|
if (dat.checked >= 4 || !is_filled(rows[i])) {
|
||||||
// zero out the cache
|
if (dat.filled > 0 && dat.checked < 4) dat.checked++;
|
||||||
for (int8_t x = 0; x < COLUMNS; x++)
|
continue; // continue to the next line
|
||||||
row[i][x].packed = 0;
|
|
||||||
|
|
||||||
// write the cached address to a row starting from the top
|
|
||||||
row[filled++] = row_cache;
|
|
||||||
row[i] = row[i - filled];
|
|
||||||
y--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filled > 0)
|
cache[dat.filled] = rows[i]; // cache the current row address
|
||||||
printf("filled: %i\n", filled);
|
dat.filled++; // increase filled, and keep the row in the cache
|
||||||
|
dat.checked++; // increase the checked count
|
||||||
|
y--; // decrease y to check this line again
|
||||||
|
}
|
||||||
|
|
||||||
|
while (dat.filled > 0) {
|
||||||
|
dat.filled--;
|
||||||
|
rows[dat.filled] = cache[dat.filled];
|
||||||
|
|
||||||
|
// zero out the filled row
|
||||||
|
for (int8_t x = 0; x < COLUMNS; x++)
|
||||||
|
cache[dat.filled][x].packed = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sets a shape to the screen
|
// sets a shape to the screen
|
||||||
|
|||||||
Reference in New Issue
Block a user