From 787f94a8b2d7e17e2b7841dad7a492d62c6f2796 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Mon, 22 Apr 2024 05:11:11 +0800 Subject: [PATCH] Add gametime system --- app.c | 5 +++++ app.h | 2 ++ gametime.c | 36 ++++++++++++++++++++++++++++++++++++ gametime.h | 40 ++++++++++++++++++++++++++++++++++++++++ types.h | 12 ++++++++---- 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 gametime.c create mode 100644 gametime.h diff --git a/app.c b/app.c index 548c053..9f575ec 100644 --- a/app.c +++ b/app.c @@ -2,6 +2,7 @@ #include "app.h" #include "camera.h" #include "entity.h" +#include "gametime.h" #include "input.h" #include "particle.h" #include "physics.h" @@ -25,6 +26,7 @@ App *app_NewApp() { app->entity = entity_NewSystem(app); app->camera = camera_NewSystem(app); app->particle = particle_NewSystem(app); + app->time = gametime_NewSystem(app); app->switch_level = NULL; app->clear_color = 0; @@ -51,6 +53,8 @@ void app_DeleteApp(App *app) { physics_DeleteSystem(app->physics); player_DeleteSystem(app->player); camera_DeleteSystem(app->camera); + particle_DeleteSystem(app->particle); + gametime_DeleteSystem(app->time); free(app); } @@ -64,6 +68,7 @@ void app_Advance(App *app, Duration deltaTime) { if (app->switch_level != NULL) _app_SwitchLevel(app); + gametime_Advance(app->time, deltaTime); particle_Advance(app->particle, deltaTime); input_Advance(app->input); player_Advance(app->player, deltaTime); diff --git a/app.h b/app.h index e73f9fc..a0ee901 100644 --- a/app.h +++ b/app.h @@ -1,6 +1,7 @@ #pragma once #include "entity.h" +#include "gametime.h" #include "physics.h" #include "player.h" #include "input.h" @@ -23,6 +24,7 @@ typedef struct _App { System_Entity *entity; System_Camera *camera; System_Particle *particle; + System_GameTime *time; char *switch_level; uint32_t clear_color; diff --git a/gametime.c b/gametime.c new file mode 100644 index 0000000..73d744a --- /dev/null +++ b/gametime.c @@ -0,0 +1,36 @@ + +#include "gametime.h" +#include "types.h" + + +System_GameTime *gametime_NewSystem(App *super) { + System_GameTime *sys = zero_malloc(sizeof(System_GameTime)); + + sys->super = super; + sys->now = GAMETIME_EPOCH; + return sys; +} +void gametime_DeleteSystem(System_GameTime *sys) { + free(sys); +} + +void gametime_Advance(System_GameTime *sys, Duration deltaTime) { + sys->now += deltaTime.microseconds; +} + + +TimePoint gametime_Now(System_GameTime *sys) { + TimePoint now = {.microseconds = sys->now}; + return now; +} + +Duration gametime_Since(System_GameTime *sys, TimePoint prev) { + return time_Difference(gametime_Now(sys), prev); +} + +Duration gametime_Reset(System_GameTime *sys, TimePoint *prev) { + TimePoint now = gametime_Now(sys); + Duration d = time_Difference(now, *prev); + *prev = now; + return d; +} diff --git a/gametime.h b/gametime.h new file mode 100644 index 0000000..f10587f --- /dev/null +++ b/gametime.h @@ -0,0 +1,40 @@ +#pragma once + +#include "types.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +typedef struct _App App; + + +// GameTime starts counting at 100 seconds. +#define GAMETIME_EPOCH ((int64_t)(100 * 1000 * 1000)) + + +// GameTime keeps track of running (or paused) time of the game. +typedef struct { + App *super; + int64_t now; // Microseconds since object creation, adding GAMETIME_EPOCH. +} System_GameTime; + + +System_GameTime *gametime_NewSystem(App *super); +void gametime_DeleteSystem(System_GameTime *sys); + +// Advance pushes the clock forwards by DeltaTime. +void gametime_Advance(System_GameTime *sys, Duration deltaTime); + + +// time_XXX functions, ported to GameTime. +TimePoint gametime_Now(System_GameTime *sys); +Duration gametime_Since(System_GameTime *sys, TimePoint prev); +Duration gametime_Reset(System_GameTime *sys, TimePoint *prev); + + +#ifdef __cplusplus +} +#endif diff --git a/types.h b/types.h index 32cdec3..9b76c29 100644 --- a/types.h +++ b/types.h @@ -107,11 +107,15 @@ typedef struct { int64_t microseconds; } TimePoint; -TimePoint time_Now(); +// No game logic should use the time_XXX functions for time. +// Use gametime_XXX instead. +TimePoint time_Now(); // Don't use this! TimePoint time_After(TimePoint now, Duration after); -Duration time_Since(TimePoint prev); -Duration time_Difference(TimePoint now, TimePoint prev); -Duration time_Reset(TimePoint *prev); +// Don't use this! +Duration time_Since(TimePoint prev); +Duration time_Difference(TimePoint now, TimePoint prev); +// Don't use this! +Duration time_Reset(TimePoint *prev); // 1e-6 extern const double EPS;