mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
code cleanup:
- rename typedefs to a lowercase variant - change variable names to shorter variants
This commit is contained in:
@@ -16,39 +16,39 @@
|
||||
|
||||
|
||||
// shuffle the array using a Fisher–Yates 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]);
|
||||
|
||||
Reference in New Issue
Block a user