code cleanup:

- rename typedefs to a lowercase variant
- change variable names to shorter variants
This commit is contained in:
2025-02-10 12:28:05 +01:00
parent 94ca7e3d08
commit 3fd7806e5f
14 changed files with 121 additions and 119 deletions

View File

@@ -16,39 +16,39 @@
// shuffle the array using a FisherYates shuffle
static inline void shuffle(uint8_t const size, ShapeId* const elmnts) {
static inline void shuffle(uint8_t const size, shape_id* const elmnts) {
for (uint8_t i = 0; i < (size - 1); i++) {
uint8_t const j = i + rand() % (size - i);
ShapeId const cache = elmnts[i];
shape_id const cache = elmnts[i];
elmnts[i] = elmnts[j];
elmnts[j] = cache;
}
}
void next_shape(GameData* const game_data) {
game_data->curr_idx++; // increase the current shape index
game_data->sel_x = COLUMNS / 2 - SHAPE_WIDTH / 2; // move the shape position to the centre
game_data->sel_y = 0;
void next_shape(game_data* const dat) {
dat->curr_idx++; // increase the current shape index
dat->sel_x = COLUMNS / 2 - SHAPE_WIDTH / 2; // move the shape position to the centre
dat->sel_y = 0;
// return if know which shape is next
if (game_data->curr_idx < (TETROMINO_COUNT - 1))
if (dat->curr_idx < (TETROMINO_COUNT - 1))
return;
game_data->curr_idx = 0;
dat->curr_idx = 0;
shuffle(TETROMINO_COUNT - 1, game_data->nxt);
ShapeId cache = game_data->nxt[0];
game_data->nxt[0] = game_data->nxt[TETROMINO_COUNT - 1];
game_data->nxt[TETROMINO_COUNT - 1] = cache;
shuffle(TETROMINO_COUNT - 1, dat->nxt);
shape_id cache = dat->nxt[0];
dat->nxt[0] = dat->nxt[TETROMINO_COUNT - 1];
dat->nxt[TETROMINO_COUNT - 1] = cache;
}
void game_init(GameData* const game_data) {
void game_init(game_data* const dat) {
// zero-initialize the game data
*game_data = (GameData){0};
*dat = (game_data){0};
// allocate size for each row
for (int8_t i = 0; i < ROWS; i++) {
game_data->rows[i] = calloc(COLUMNS, sizeof(colour8));
dat->rows[i] = calloc(COLUMNS, sizeof(colour8));
// game_data->rows[i][0] = (colour8){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging rows
}
@@ -56,21 +56,21 @@ void game_init(GameData* const game_data) {
srand(time(NULL));
// set the shape data in each slot to it's corrsponding ID
for (ShapeId i = 0; i < TETROMINO_COUNT; i++)
game_data->nxt[i] = i;
for (shape_id i = 0; i < TETROMINO_COUNT; i++)
dat->nxt[i] = i;
game_data->curr_idx = -1; // set the current index to the max so it becomes zero after increasement
next_shape(game_data); // select the next shape (shuffle should not be triggered)
shuffle(TETROMINO_COUNT, game_data->nxt); // manually trigger a shuffle
dat->curr_idx = -1; // set the current index to the max so it becomes zero after increasement
next_shape(dat); // select the next shape (shuffle should not be triggered)
shuffle(TETROMINO_COUNT, dat->nxt); // manually trigger a shuffle
// initialize audio
game_data->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
game_data->music = audio_load_wav(game_data->audio_device, "korobeiniki.wav");
audio_play(game_data->audio_device, game_data->music);
dat->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
dat->music = audio_load_wav(dat->audio_device, "korobeiniki.wav");
audio_play(dat->audio_device, dat->music);
}
// called every time the game's state is updated
void game_update(GameData* const game_data, uint8_t const* const keys) {
void game_update(game_data* const dat, uint8_t const* const keys) {
if (keys[SDL_SCANCODE_ESCAPE])
stop();
@@ -80,10 +80,10 @@ void game_update(GameData* const game_data, uint8_t const* const keys) {
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_SPACE]) move |= MOVE_DOWN;
if (keys[SDL_SCANCODE_Q]) move |= MOVE_ROTLEFT;
if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT;
place_update(game_data, move);
place_update(dat, move);
}
void game_free(GameData* const game_data) {
void game_free(game_data* const game_data) {
// clear each row
for (int8_t i = 0; i < ROWS; i++) {
free(game_data->rows[i]);

View File

@@ -10,21 +10,21 @@
#define COLUMNS ((int8_t)10)
#define ROWS ((int8_t)24)
typedef const colour8* const CRow;
typedef colour8* Row;
typedef colour8 const* const row_const;
typedef colour8* row;
typedef struct {
Row rows[ROWS];
AudioDevice const* audio_device;
AudioData music;
row rows[ROWS];
audio_device const* audio_device;
audio_data music;
uint16_t score;
ShapeId nxt[7]; // the order of the shape ids that they should appear in
shape_id nxt[7]; // the order of the shape ids that they should appear in
uint8_t curr_idx; // current shape index
int8_t sel_x; // selected shape x position
int8_t sel_y; // selected shape y position
} GameData;
} game_data;
void next_shape(GameData* game_data);
void game_init(GameData* game_data); // initializes the game
void game_update(GameData* game_data, uint8_t const* keys); // updates the game's state
void game_free(GameData* game_data); // free all data stored with the game
void next_shape(game_data* dat);
void game_init(game_data* dat); // initializes the game
void game_update(game_data* dat, uint8_t const* keys); // updates the game's state
void game_free(game_data* dat); // free all data stored with the game

View File

@@ -7,7 +7,7 @@
#include "shapes.h"
static bool is_filled(CRow const row) {
static bool is_filled(row_const const row) {
for (int8_t x = 0; x < COLUMNS; x++) {
if (row[x].packed == 0) {
return false;
@@ -17,8 +17,8 @@ static bool is_filled(CRow const row) {
return true;
}
static void clear_rows(Row* const rows, uint16_t* const score) {
Row cache[4] = {0}; // you can only clear four rows at a time
static void clear_rows(row* const rows, uint16_t* const score) {
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
uint8_t checked : 3; // values will only ever be 0..4
@@ -57,7 +57,7 @@ static void clear_rows(Row* const rows, uint16_t* const score) {
}
// sets a shape to the screen
static void set_shape_i(Row const* const row, ShapeId const id, int8_t const pos_x) {
static void set_shape_i(row const* const row, shape_id const id, int8_t const pos_x) {
Shape const shape = shape_from_id(id);
colour8 const colour = colour_from_id(id);
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
@@ -72,11 +72,11 @@ static void set_shape_i(Row const* const row, ShapeId const id, int8_t const pos
}
}
static inline void set_shape(Row const* const row, ShapeId const id, int8_t const pos_x, int8_t const pos_y) {
static inline void set_shape(row const* const row, shape_id const id, int8_t const pos_x, int8_t const 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(Row const* const rows, ShapeId const id, int8_t const x, int8_t const y) {
static bool shape_intersects(row const* const rows, shape_id const id, int8_t const x, int8_t const y) {
Shape const shape = shape_from_id(id);
for (int8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) {
@@ -96,14 +96,14 @@ static bool shape_intersects(Row const* const rows, ShapeId const id, int8_t con
return false;
}
static inline ShapeId rotate_id(ShapeId const id, int const dir) {
static inline shape_id rotate_id(shape_id const id, int const dir) {
return (id + dir) & 31;
}
void place_update(GameData* const game_data, InputData const move) {
void place_update(game_data* const game_data, InputData const move) {
// store the current index and ID, only changes when placed (which yields no movement) and rotation (which occurs last)
uint8_t const curr_idx = game_data->curr_idx;
ShapeId const curr_id = game_data->nxt[curr_idx];
shape_id const curr_id = game_data->nxt[curr_idx];
// set the shape if we moved vertically and intersected
@@ -131,7 +131,7 @@ void place_update(GameData* const game_data, InputData const move) {
// update the shape's rotation
if (move & 8 || move & 16) {
ShapeId const id = move & 8 // check which direction we should move
shape_id const id = move & 8 // check which direction we should move
? rotate_id(curr_id, -8)
: rotate_id(curr_id, 8);
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {

View File

@@ -15,8 +15,8 @@ enum {
MOVE_ROTRIGHT = 16,
};
void place_update(GameData* game_data, InputData move);
void place_update(game_data* game_data, InputData move);
#ifdef DEBUG
void dbg_set_all(GameData* game_data);
void dbg_set_all(game_data* game_data);
#endif

View File

@@ -35,7 +35,7 @@
#define SHAPE_J_180 ((Shape)0x6440) // 0110 0100 0100 0000 the J tetromino with a 180° rotation
#define SHAPE_J_270 ((Shape)0x0E20) // 0000 1110 0010 0000 the J tetromino with a 270° rotation
Shape shape_from_id(ShapeId const id) {
Shape shape_from_id(shape_id const id) {
// clang-format off
static Shape const shapes[TETROMINO_COUNT][4] = {
// 0° 90° 180° 170°
@@ -53,7 +53,7 @@ Shape shape_from_id(ShapeId const id) {
return shapes[id & 7][id >> 3];
}
colour8 colour_from_id(ShapeId const id) {
colour8 colour_from_id(shape_id const id) {
switch (id & 7) {
case TETROMINO_O: return COLOUR_YELLOW;
case TETROMINO_I: return COLOUR_CYAN;

View File

@@ -7,7 +7,7 @@
typedef uint16_t Shape;
typedef uint8_t ShapeRow;
typedef uint8_t ShapeId;
typedef uint8_t shape_id;
enum {
TETROMINO_O = 0,
TETROMINO_I = 1,
@@ -35,5 +35,5 @@ static inline bool is_set(ShapeRow const row, uint8_t const index) {
return (row >> ((SHAPE_WIDTH - 1) - index) & 1) != 0;
}
Shape shape_from_id(ShapeId id);
colour8 colour_from_id(ShapeId id);
Shape shape_from_id(shape_id id);
colour8 colour_from_id(shape_id id);