fix: two gametimes were present

This commit is contained in:
2025-03-22 12:57:12 +01:00
parent 7e5236db7c
commit 68c61a04be
4 changed files with 15 additions and 12 deletions

View File

@@ -8,15 +8,15 @@
#include "gametime.h"
void game_init(gamedata* dat) {
void game_init(gamedata* dat, gametime* gt) {
*dat = (gamedata){
gametime_new(),
gt,
true,
};
}
void game_update(gamedata* dat) {
gametime_update(&dat->time);
gametime_update(dat->time);
uint8_t const* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE])
@@ -24,5 +24,6 @@ void game_update(gamedata* dat) {
}
void game_free(gamedata* dat) {
gametime_free(dat->time);
*dat = (gamedata){0};
}

View File

@@ -12,10 +12,10 @@
#define TAUf (M_PIf * 2.0F) // τ constant as a 32-bit floating point
typedef struct {
gametime time;
gametime* time;
bool run;
} gamedata;
void game_init(gamedata*); // initializes everything needed to start the game; outputs to game_data
void game_init(gamedata*, gametime*); // initializes everything needed to start the game; outputs to game_data
void game_update(gamedata*); // causes an update to occur within the game
void game_free(gamedata*); // frees the resources associated with the game

View File

@@ -1,5 +1,6 @@
#pragma once
#include <stdlib.h>
#include <time.h>
typedef struct {
@@ -22,6 +23,10 @@ static inline gametime gametime_new(void) {
};
}
static inline void gametime_free(gametime* gt) {
free(gt);
}
// updates the internal variables
static inline void gametime_update(gametime* gt) {
struct timespec ts;

View File

@@ -21,7 +21,7 @@ static void init(void) {
// initialize other game components
gt = gametime_new();
game_init(&gdat);
game_init(&gdat, &gt);
render_init(&rdat, &gdat);
}
@@ -33,7 +33,7 @@ static void update(void) {
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
gdat->run = false;
gdat.run = false;
break;
}
}
@@ -52,15 +52,12 @@ int32_t main(int32_t argc, char** argv) {
init();
debug("successfully initialized!");
while (gdat->run == true)
while (gdat.run == true)
update();
debug("done! starting to free resources...");
game_free(&gdat);
render_free(&rdat);
SDL_Quit();
gamestatus exit_code = get_gamestatus();
debug("quitting with an exit code of %u", exit_code);
return exit_code;
return 0;
}