mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
synchronise with the template
- use the version of audio management which doesn't crash the application if something goes wrong - include colour32 - update error management to be more reflective of the one defined in the template - modify the renderer functions to be more reflective of the template - modify the game functions to be more reflective of the template (& use a more failproof method of initializing) - add gametime - remove set_gamestatus logic and just use a run boolean in gamedata - remove emscripten preprocessors as those haven't really been used
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
#include "game.h"
|
||||
|
||||
#include <SDL_audio.h>
|
||||
#include <SDL_keyboard.h>
|
||||
#include <SDL_scancode.h>
|
||||
#include <SDL_timer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../error.h"
|
||||
#include "../window/audio.h"
|
||||
#include "../window/colour8.h"
|
||||
#include "../window/colour/colour8.h"
|
||||
#include "./tetromino/shapes.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_timer.h"
|
||||
#include "tetromino/placing.h"
|
||||
|
||||
// shuffle the array using a Fisher–Yates shuffle
|
||||
@@ -25,7 +25,7 @@ static inline void shuffle(uint8_t const size, shape_id* const elmnts) {
|
||||
}
|
||||
}
|
||||
|
||||
void next_shape(game_data* const dat) {
|
||||
void next_shape(gamedata* 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;
|
||||
@@ -42,19 +42,34 @@ void next_shape(game_data* const dat) {
|
||||
dat->nxt[TETROMINO_COUNT - 1] = cache;
|
||||
}
|
||||
|
||||
void game_init(game_data* const dat) {
|
||||
// zero-initialize the game data
|
||||
*dat = (game_data){0};
|
||||
|
||||
// allocate size for each row
|
||||
for (int8_t i = 0; i < ROWS; i++) {
|
||||
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
|
||||
}
|
||||
|
||||
void game_init(gamedata* const dat, gametime* gt) {
|
||||
// set a random seed using the system clock
|
||||
srand(time(NULL));
|
||||
|
||||
// initialize audio device
|
||||
audiodevice* ad = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||
|
||||
*dat = (gamedata){
|
||||
{0}, // row
|
||||
gt, // time
|
||||
ad, // audio_device
|
||||
audio_wav_load(ad, "korobeiniki.wav"), // music
|
||||
0, // timer_music
|
||||
0, // timer_update
|
||||
0, // timer_input
|
||||
0, // score
|
||||
{0}, // nxt
|
||||
0, // curr_idx
|
||||
0, // sel_x
|
||||
0, // sel_y
|
||||
true, // run
|
||||
};
|
||||
|
||||
// initialize the rows within the game data
|
||||
for (int8_t i = 0; i < ROWS; i++) {
|
||||
dat->rows[i] = calloc(COLUMNS, sizeof(colour8)); // TODO: add memory safety check
|
||||
}
|
||||
|
||||
// set the shape data in each slot to it's corrsponding ID
|
||||
for (shape_id i = 0; i < TETROMINO_COUNT; i++)
|
||||
dat->nxt[i] = i;
|
||||
@@ -62,16 +77,14 @@ void game_init(game_data* const dat) {
|
||||
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
|
||||
dat->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||
dat->music = audio_wav_load(dat->audio_device, "korobeiniki.wav");
|
||||
}
|
||||
|
||||
// called every time the game's state is updated
|
||||
void game_update(game_data* const dat, uint8_t const* const keys) {
|
||||
void game_update(gamedata* const dat) {
|
||||
uint8_t const* keys = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (keys[SDL_SCANCODE_ESCAPE])
|
||||
set_gamestatus(STATUS_SUCCESS);
|
||||
dat->run = false;
|
||||
|
||||
input_data move = MOVE_NONE; // contains the move data
|
||||
uint32_t ctime = SDL_GetTicks();
|
||||
@@ -107,7 +120,7 @@ void game_update(game_data* const dat, uint8_t const* const keys) {
|
||||
place_update(dat, move);
|
||||
}
|
||||
|
||||
void game_free(game_data* const dat) {
|
||||
void game_free(gamedata* const dat) {
|
||||
audio_wav_unload(&dat->music);
|
||||
audio_device_free(dat->audio_device);
|
||||
|
||||
@@ -116,4 +129,7 @@ void game_free(game_data* const dat) {
|
||||
free(dat->rows[i]);
|
||||
dat->rows[i] = NULL;
|
||||
}
|
||||
|
||||
// zero-out the rest of the data
|
||||
*dat = (gamedata){0};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user