mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
use new time system rather than the badly written old one
This commit is contained in:
@@ -43,10 +43,13 @@ void next_shape(gamedata* const dat) {
|
|||||||
dat->nxt[TETROMINO_COUNT - 1] = cache;
|
dat->nxt[TETROMINO_COUNT - 1] = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_init(gamedata* const dat, gametime* gt) {
|
void game_init(gamedata* const dat) {
|
||||||
// set a random seed using the system clock
|
// set a random seed using the system clock
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
|
struct gametime gt = {{0}, 0};
|
||||||
|
gametime_get(>.ts);
|
||||||
|
|
||||||
// initialize audio device
|
// initialize audio device
|
||||||
audiodevice* ad = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
audiodevice* ad = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||||
|
|
||||||
@@ -83,15 +86,24 @@ void game_init(gamedata* const dat, gametime* gt) {
|
|||||||
shuffle(TETROMINO_COUNT, dat->nxt); // manually trigger a shuffle
|
shuffle(TETROMINO_COUNT, dat->nxt); // manually trigger a shuffle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updates the gametime
|
||||||
|
static inline void update_gametime(gamedata* dat) {
|
||||||
|
struct timespec ts;
|
||||||
|
gametime_get(&ts);
|
||||||
|
dat->time.ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||||
|
dat->time.ts = ts;
|
||||||
|
}
|
||||||
|
|
||||||
// called every time the game's state is updated
|
// called every time the game's state is updated
|
||||||
void game_update(gamedata* const dat) {
|
void game_update(gamedata* const dat) {
|
||||||
|
update_gametime(dat);
|
||||||
uint8_t const* keys = SDL_GetKeyboardState(NULL);
|
uint8_t const* keys = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_ESCAPE])
|
if (keys[SDL_SCANCODE_ESCAPE])
|
||||||
dat->run = false;
|
dat->run = false;
|
||||||
|
|
||||||
input_data move = MOVE_NONE; // contains the move data
|
input_data move = MOVE_NONE; // contains the move data
|
||||||
uint32_t ctime = SDL_GetTicks();
|
time_t ctime = dat->time.ms;
|
||||||
|
|
||||||
if (ctime > dat->timer_update) {
|
if (ctime > dat->timer_update) {
|
||||||
dat->timer_update = ctime + 500;
|
dat->timer_update = ctime + 500;
|
||||||
|
|||||||
@@ -24,12 +24,12 @@ typedef colour8* row;
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
row rows[ROWS];
|
row rows[ROWS];
|
||||||
gametime* time;
|
struct gametime time;
|
||||||
audiodevice* audio_device;
|
audiodevice* audio_device;
|
||||||
audiodata music;
|
audiodata music;
|
||||||
uint32_t timer_music;
|
time_t timer_music;
|
||||||
uint32_t timer_update;
|
time_t timer_update;
|
||||||
uint32_t timer_input;
|
time_t timer_input;
|
||||||
uint16_t score;
|
uint16_t score;
|
||||||
shape_id 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
|
uint8_t curr_idx; // current shape index
|
||||||
@@ -39,6 +39,6 @@ typedef struct {
|
|||||||
} gamedata;
|
} gamedata;
|
||||||
|
|
||||||
void next_shape(gamedata*); // initializes everything needed to start the game; outputs to gamedata
|
void next_shape(gamedata*); // initializes everything needed to start the game; outputs to gamedata
|
||||||
void game_init(gamedata*, gametime*); // initializes the game
|
void game_init(gamedata*); // initializes the game
|
||||||
void game_update(gamedata*); // causes an update to occur within 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
|
void game_free(gamedata*); // frees the resources associated with the game
|
||||||
|
|||||||
@@ -1,39 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "../util/attributes.h"
|
struct gametime {
|
||||||
|
|
||||||
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;
|
struct timespec ts;
|
||||||
timespec_get(&ts, TIME_UTC);
|
time_t ms;
|
||||||
|
};
|
||||||
|
|
||||||
return (gametime){
|
#if _POSIX_C_SOURCE >= 199309L
|
||||||
ts,
|
static inline void gametime_get(struct timespec* ts) {
|
||||||
0.0,
|
clock_gettime(CLOCK_MONOTONIC, ts);
|
||||||
1.0F,
|
|
||||||
0.0F,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
#elif defined _WIN32
|
||||||
// updates the internal variables
|
# include <windows.h>
|
||||||
static inline void gametime_update(gametime* gt) {
|
static inline void gametime_get(struct timespec* ts) {
|
||||||
struct timespec ts;
|
LARGE_INTEGER cnt, frq;
|
||||||
timespec_get(&ts, TIME_UTC);
|
QueryPerformanceCounter(&cnt);
|
||||||
gt->sec = (double)ts.tv_nsec * 1e-9; // calculate the current time in seconds
|
QueryPerformanceFrequency(&frq);
|
||||||
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
|
ts->tv_sec = (time_t)(cnt.QuadPart / frq.QuadPart);
|
||||||
gt->ts = ts; // update the game's timespec
|
ts->tv_nsec = (time_t)((cnt.QuadPart % frq.QuadPart) * 1000000000 / frq.QuadPart);
|
||||||
}
|
|
||||||
|
|
||||||
// gets how many times the game updates per second
|
|
||||||
atrb_const static inline float gametime_get_ups(gametime* gt) {
|
|
||||||
return 1.0F / gt->delta;
|
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
# error platform not supported
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -6,11 +6,9 @@
|
|||||||
|
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "game/game.h"
|
#include "game/game.h"
|
||||||
#include "game/gametime.h"
|
|
||||||
#include "window/renderer.h"
|
#include "window/renderer.h"
|
||||||
|
|
||||||
// initialized in init(), reading beforehand is undefined behaviour
|
// initialized in init(), reading beforehand is undefined behaviour
|
||||||
static gametime gt;
|
|
||||||
static gamedata gdat;
|
static gamedata gdat;
|
||||||
static renderdata rdat;
|
static renderdata rdat;
|
||||||
|
|
||||||
@@ -23,8 +21,7 @@ static void init(void) {
|
|||||||
fatal(ERROR_SDL_FONT_INIT, __FILE_NAME__, __LINE__, "the TTF module of SDL could not initialize! TTF Error: %s", TTF_GetError());
|
fatal(ERROR_SDL_FONT_INIT, __FILE_NAME__, __LINE__, "the TTF module of SDL could not initialize! TTF Error: %s", TTF_GetError());
|
||||||
|
|
||||||
// initialize other game components
|
// initialize other game components
|
||||||
gt = gametime_new();
|
game_init(&gdat);
|
||||||
game_init(&gdat, >);
|
|
||||||
render_init(&rdat, &gdat);
|
render_init(&rdat, &gdat);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +40,6 @@ static void update(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform updates
|
// perform updates
|
||||||
gametime_update(>);
|
|
||||||
game_update(&gdat);
|
game_update(&gdat);
|
||||||
render_update(&rdat);
|
render_update(&rdat);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user