mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
code cleanup + call clear rows when placing a block
This commit is contained in:
@@ -8,7 +8,6 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "../main.h"
|
#include "../main.h"
|
||||||
#include "../window/colour.h"
|
|
||||||
#include "./tetromino/shapes.h"
|
#include "./tetromino/shapes.h"
|
||||||
#include "tetromino/placing.h"
|
#include "tetromino/placing.h"
|
||||||
|
|
||||||
@@ -27,51 +26,6 @@ void game_init(GameData* const game_data) {
|
|||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_filled(CRow row) {
|
|
||||||
for (uint8_t x = 0; x < COLUMNS; x++) {
|
|
||||||
if (row[x].packed == NONE) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void set_row(Row* rows, const uint8_t y, Row row) {
|
|
||||||
rows[(ROWS - 1) - y] = row;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void clear_rows(Row* row) {
|
|
||||||
uint8_t filled_rows = 0;
|
|
||||||
|
|
||||||
// loop through each row
|
|
||||||
for (uint8_t y = 0; y < ROWS; y++) {
|
|
||||||
// zero out the row instead if we've reached the top rows
|
|
||||||
if ((ROWS - y) <= filled_rows) {
|
|
||||||
for (uint8_t x = 0; x < COLUMNS; x++)
|
|
||||||
row[filled_rows--][x].packed = 0; // use filled_rows as the y index and decrease them as we go.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the row we currently need to check
|
|
||||||
Row crow = filled_rows > 0
|
|
||||||
? row[y + filled_rows]
|
|
||||||
: row[y];
|
|
||||||
|
|
||||||
// set the current y to the current row
|
|
||||||
Row row_cache = row[y];
|
|
||||||
set_row(row, y, crow);
|
|
||||||
|
|
||||||
if (!is_filled(crow)) continue;
|
|
||||||
|
|
||||||
// write the cached row to the top if we know that the current row pointer will be lost
|
|
||||||
row[filled_rows] = row_cache;
|
|
||||||
filled_rows++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// called every time the game's state is updated
|
// called every time the game's state is updated
|
||||||
void game_update(GameData* game_data, const uint8_t* keys) {
|
void game_update(GameData* game_data, const uint8_t* keys) {
|
||||||
if (keys[SDL_SCANCODE_ESCAPE])
|
if (keys[SDL_SCANCODE_ESCAPE])
|
||||||
|
|||||||
@@ -1,13 +1,55 @@
|
|||||||
#include "placing.h"
|
#include "placing.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "../../window/colour.h"
|
#include "../../window/colour.h"
|
||||||
#include "../game.h"
|
#include "../game.h"
|
||||||
#include "SDL_scancode.h"
|
|
||||||
#include "shapes.h"
|
#include "shapes.h"
|
||||||
|
|
||||||
|
|
||||||
|
static bool is_filled(CRow row) {
|
||||||
|
for (uint8_t x = 0; x < COLUMNS; x++) {
|
||||||
|
if (row[x].packed == NONE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void set_row(Row* rows, const uint8_t y, Row row) {
|
||||||
|
rows[(ROWS - 1) - y] = row;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clear_rows(Row* row) {
|
||||||
|
uint8_t filled_rows = 0;
|
||||||
|
|
||||||
|
// loop through each row
|
||||||
|
for (uint8_t y = 0; y < ROWS; y++) {
|
||||||
|
// zero out the row instead if we've reached the top rows
|
||||||
|
if ((ROWS - y) <= filled_rows) {
|
||||||
|
for (uint8_t x = 0; x < COLUMNS; x++)
|
||||||
|
row[filled_rows--][x].packed = 0; // use filled_rows as the y index and decrease them as we go.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the row we currently need to check
|
||||||
|
Row crow = filled_rows > 0
|
||||||
|
? row[y + filled_rows]
|
||||||
|
: row[y];
|
||||||
|
|
||||||
|
// set the current y to the current row
|
||||||
|
Row row_cache = row[y];
|
||||||
|
set_row(row, y, crow);
|
||||||
|
|
||||||
|
if (!is_filled(crow)) continue;
|
||||||
|
|
||||||
|
// write the cached row to the top if we know that the current row pointer will be lost
|
||||||
|
row[filled_rows] = row_cache;
|
||||||
|
filled_rows++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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 uint8_t pos_x) {
|
||||||
const Shape shape = shape_from_id(id);
|
const Shape shape = shape_from_id(id);
|
||||||
@@ -59,6 +101,7 @@ void place_update(GameData* const game_data, const InputData move) {
|
|||||||
const uint8_t y = selected->y + 1;
|
const uint8_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
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "../game.h"
|
#include "../game.h"
|
||||||
#include "shapes.h"
|
|
||||||
|
|
||||||
typedef uint8_t InputData;
|
typedef uint8_t InputData;
|
||||||
enum {
|
enum {
|
||||||
|
|||||||
Reference in New Issue
Block a user