use new time system rather than the badly written old one

This commit is contained in:
2025-03-22 23:51:51 +01:00
parent 14c62914f0
commit f72949dc38
4 changed files with 40 additions and 47 deletions

View File

@@ -43,10 +43,13 @@ void next_shape(gamedata* const dat) {
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
srand(time(NULL));
struct gametime gt = {{0}, 0};
gametime_get(&gt.ts);
// initialize audio device
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
}
// 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
void game_update(gamedata* const dat) {
update_gametime(dat);
uint8_t const* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE])
dat->run = false;
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) {
dat->timer_update = ctime + 500;