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};
|
||||
}
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL_scancode.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../window/audio.h"
|
||||
#include "../window/colour8.h"
|
||||
#include "tetromino/shapes.h"
|
||||
#include "../window/colour/colour8.h"
|
||||
#include "./tetromino/shapes.h"
|
||||
#include "gametime.h"
|
||||
|
||||
// constants for pi(π) and tau(τ)
|
||||
#define PI (M_PI) // π constant
|
||||
#define TAU (M_PI * 2.0) // τ constant
|
||||
#define PIf (M_PIf) // π constant as a 32-bit floating point
|
||||
#define TAUf (M_PIf * 2.0F) // τ constant as a 32-bit floating point
|
||||
|
||||
// stores the data used in the game
|
||||
#define COLUMNS ((int8_t)10)
|
||||
@@ -15,8 +24,9 @@ typedef colour8* row;
|
||||
|
||||
typedef struct {
|
||||
row rows[ROWS];
|
||||
audio_device* audio_device;
|
||||
audio_data music;
|
||||
gametime* time;
|
||||
audiodevice* audio_device;
|
||||
audiodata music;
|
||||
uint32_t timer_music;
|
||||
uint32_t timer_update;
|
||||
uint32_t timer_input;
|
||||
@@ -25,9 +35,10 @@ typedef struct {
|
||||
uint8_t curr_idx; // current shape index
|
||||
int8_t sel_x; // selected shape x position
|
||||
int8_t sel_y; // selected shape y position
|
||||
} game_data;
|
||||
bool run;
|
||||
} gamedata;
|
||||
|
||||
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
|
||||
void next_shape(gamedata*); // initializes everything needed to start the game; outputs to gamedata
|
||||
void game_init(gamedata*, gametime*); // initializes the game
|
||||
void game_update(gamedata*); // causes an update to occur within the game
|
||||
void game_free(gamedata*); // frees the resources associated with the game
|
||||
|
||||
39
src/game/gametime.h
Normal file
39
src/game/gametime.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include "../util/attributes.h"
|
||||
|
||||
typedef struct {
|
||||
struct timespec ts; // stores the time at the current update
|
||||
double sec; // stores the current time in seconds
|
||||
float scale; // multiplier for the time calculation, default value is 1.0
|
||||
float delta; // the time that it took between updates
|
||||
} gametime;
|
||||
|
||||
// initializes the gametime struct
|
||||
atrb_const static inline gametime gametime_new(void) {
|
||||
struct timespec ts;
|
||||
timespec_get(&ts, TIME_UTC);
|
||||
|
||||
return (gametime){
|
||||
ts,
|
||||
0.0,
|
||||
1.0F,
|
||||
0.0F,
|
||||
};
|
||||
}
|
||||
|
||||
// updates the internal variables
|
||||
static inline void gametime_update(gametime* gt) {
|
||||
struct timespec ts;
|
||||
timespec_get(&ts, TIME_UTC);
|
||||
gt->sec = (double)ts.tv_nsec * 1e-9; // calculate the current time in seconds
|
||||
gt->delta = ((double)(ts.tv_nsec - gt->ts.tv_nsec) * 1e-9) * gt->scale; // calculate how much time has passed between this and last frame
|
||||
gt->ts = ts; // update the game's timespec
|
||||
}
|
||||
|
||||
// gets how many times the game updates per second
|
||||
atrb_const static inline float gametime_get_ups(gametime* gt) {
|
||||
return 1.0F / gt->delta;
|
||||
}
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../../error.h"
|
||||
#include "../../window/colour8.h"
|
||||
#include "../../window/colour/colour8.h"
|
||||
#include "../game.h"
|
||||
#include "shapes.h"
|
||||
|
||||
@@ -101,7 +100,7 @@ static inline shape_id rotate_id(shape_id const id, int const dir) {
|
||||
return (id + dir) & 31;
|
||||
}
|
||||
|
||||
void place_update(game_data* const game_data, input_data const move) {
|
||||
void place_update(gamedata* const game_data, input_data 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;
|
||||
shape_id const curr_id = game_data->nxt[curr_idx];
|
||||
@@ -116,7 +115,7 @@ void place_update(game_data* const game_data, input_data const move) {
|
||||
|
||||
next_shape(game_data);
|
||||
if (shape_intersects(game_data->rows, game_data->curr_idx, game_data->sel_x, game_data->sel_y))
|
||||
set_gamestatus(STATUS_SUCCESS);
|
||||
game_data->run = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ enum {
|
||||
MOVE_ROTRIGHT = 16,
|
||||
};
|
||||
|
||||
void place_update(game_data* game_data, input_data move);
|
||||
void place_update(gamedata* game_data, input_data move);
|
||||
|
||||
#ifdef DEBUG
|
||||
void dbg_set_all(game_data* game_data);
|
||||
void dbg_set_all(gamedata* game_data);
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "shapes.h"
|
||||
|
||||
#include "../../window/colour8.h"
|
||||
#include "../../window/colour/colour8.h"
|
||||
|
||||
/* 0 1 2 3 */
|
||||
#define SHAPE_O ((shape)0x0660) // 0000 0110 0110 0000 the O tetromino with no rotation
|
||||
@@ -55,13 +55,13 @@ shape shape_from_id(shape_id 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;
|
||||
case TETROMINO_S: return COLOUR_RED;
|
||||
case TETROMINO_Z: return COLOUR_GREEN;
|
||||
case TETROMINO_T: return COLOUR_MAGENTA;
|
||||
case TETROMINO_L: return COLOUR_ORANGE;
|
||||
case TETROMINO_J: return COLOUR_BLUE;
|
||||
default: return COLOUR_BLACK;
|
||||
case TETROMINO_O: return COLOUR8_YELLOW;
|
||||
case TETROMINO_I: return COLOUR8_CYAN;
|
||||
case TETROMINO_S: return COLOUR8_RED;
|
||||
case TETROMINO_Z: return COLOUR8_GREEN;
|
||||
case TETROMINO_T: return COLOUR8_MAGENTA;
|
||||
case TETROMINO_L: return COLOUR8_ORANGE;
|
||||
case TETROMINO_J: return COLOUR8_BLUE;
|
||||
default: return COLOUR8_BLACK;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../../window/colour8.h"
|
||||
#include "../../window/colour/colour8.h"
|
||||
|
||||
typedef uint16_t shape;
|
||||
typedef uint8_t shape_row;
|
||||
|
||||
Reference in New Issue
Block a user