Add delta parameter to Gamestate_Logic calls

This commit is contained in:
Sebastian Krzyszkowiak 2018-02-03 03:37:44 +01:00
parent 749bb1ef50
commit c6b71f1686
5 changed files with 13 additions and 8 deletions

View file

@ -25,7 +25,7 @@
struct Gamestate_API { struct Gamestate_API {
void (*Gamestate_Draw)(struct Game* game, void* data); void (*Gamestate_Draw)(struct Game* game, void* data);
void (*Gamestate_Logic)(struct Game* game, void* data); void (*Gamestate_Logic)(struct Game* game, void* data, double delta);
void* (*Gamestate_Load)(struct Game* game, void (*progress)(struct Game* game)); void* (*Gamestate_Load)(struct Game* game, void (*progress)(struct Game* game));
void (*Gamestate_Start)(struct Game* game, void* data); void (*Gamestate_Start)(struct Game* game, void* data);

View file

@ -37,12 +37,12 @@ SYMBOL_INTERNAL void DrawGamestates(struct Game* game) {
} }
} }
SYMBOL_INTERNAL void LogicGamestates(struct Game* game) { SYMBOL_INTERNAL void LogicGamestates(struct Game* game, double delta) {
struct Gamestate* tmp = game->_priv.gamestates; struct Gamestate* tmp = game->_priv.gamestates;
while (tmp) { while (tmp) {
if ((tmp->loaded) && (tmp->started) && (!tmp->paused)) { if ((tmp->loaded) && (tmp->started) && (!tmp->paused)) {
game->_priv.current_gamestate = tmp; game->_priv.current_gamestate = tmp;
(*tmp->api->Gamestate_Logic)(game, tmp->data); (*tmp->api->Gamestate_Logic)(game, tmp->data, delta);
} }
tmp = tmp->next; tmp = tmp->next;
} }

View file

@ -63,7 +63,7 @@ struct ScreenshotThreadData {
}; };
void DrawGamestates(struct Game* game); void DrawGamestates(struct Game* game);
void LogicGamestates(struct Game* game); void LogicGamestates(struct Game* game, double delta);
void EventGamestates(struct Game* game, ALLEGRO_EVENT* ev); void EventGamestates(struct Game* game, ALLEGRO_EVENT* ev);
void ReloadGamestates(struct Game* game); void ReloadGamestates(struct Game* game);
void FreezeGamestates(struct Game* game); void FreezeGamestates(struct Game* game);

View file

@ -277,8 +277,8 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game* game) {
game->_priv.loading.gamestate->data = (*game->_priv.loading.gamestate->api->Gamestate_Load)(game, NULL); game->_priv.loading.gamestate->data = (*game->_priv.loading.gamestate->api->Gamestate_Load)(game, NULL);
PrintConsole(game, "Loading screen registered."); PrintConsole(game, "Loading screen registered.");
game->_priv.timestamp = al_get_time();
game->_priv.draw = true; game->_priv.draw = true;
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
void libsuperderpy_mainloop(void* game); void libsuperderpy_mainloop(void* game);
emscripten_set_main_loop_arg(libsuperderpy_mainloop, game, 0, true); emscripten_set_main_loop_arg(libsuperderpy_mainloop, game, 0, true);
@ -389,6 +389,7 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void* g) {
(*game->_priv.loading.gamestate->api->Gamestate_Stop)(game, game->_priv.loading.gamestate->data); (*game->_priv.loading.gamestate->api->Gamestate_Stop)(game, game->_priv.loading.gamestate->data);
} }
al_resume_timer(game->_priv.timer); al_resume_timer(game->_priv.timer);
game->_priv.timestamp = al_get_time();
} }
tmp = tmp->next; tmp = tmp->next;
@ -445,7 +446,9 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void* g) {
} }
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->_priv.timer)) { if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->_priv.timer)) {
LogicGamestates(game); double delta = al_get_time() - game->_priv.timestamp;
game->_priv.timestamp += delta;
LogicGamestates(game, delta);
redraw = true; redraw = true;
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
@ -483,7 +486,7 @@ SYMBOL_INTERNAL void libsuperderpy_mainloop(void* g) {
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F1)) { } else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F1)) {
int i; int i;
for (i = 0; i < 512; i++) { for (i = 0; i < 512; i++) {
LogicGamestates(game); LogicGamestates(game, ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer)));
} }
game->_priv.showconsole = true; game->_priv.showconsole = true;
PrintConsole(game, "DEBUG: 512 frames skipped..."); PrintConsole(game, "DEBUG: 512 frames skipped...");

View file

@ -120,6 +120,8 @@ struct Game {
bool draw; bool draw;
double timestamp;
#ifdef ALLEGRO_MACOSX #ifdef ALLEGRO_MACOSX
char cwd[MAXPATHLEN]; char cwd[MAXPATHLEN];
#endif #endif
@ -153,7 +155,7 @@ void libsuperderpy_destroy(struct Game* game);
struct GamestateResources; struct GamestateResources;
extern int Gamestate_ProgressCount; extern int Gamestate_ProgressCount;
void Gamestate_ProcessEvent(struct Game* game, struct GamestateResources* data, ALLEGRO_EVENT* ev); void Gamestate_ProcessEvent(struct Game* game, struct GamestateResources* data, ALLEGRO_EVENT* ev);
void Gamestate_Logic(struct Game* game, struct GamestateResources* data); void Gamestate_Logic(struct Game* game, struct GamestateResources* data, double delta);
void Gamestate_Draw(struct Game* game, struct GamestateResources* data); void Gamestate_Draw(struct Game* game, struct GamestateResources* data);
void* Gamestate_Load(struct Game* game, void (*progress)(struct Game*)); void* Gamestate_Load(struct Game* game, void (*progress)(struct Game*));
void Gamestate_Unload(struct Game* game, struct GamestateResources* data); void Gamestate_Unload(struct Game* game, struct GamestateResources* data);