Add gametime system

This commit is contained in:
Edgaru089 2024-04-22 05:11:11 +08:00
parent 4acd6c0710
commit 787f94a8b2
5 changed files with 91 additions and 4 deletions

5
app.c
View File

@ -2,6 +2,7 @@
#include "app.h" #include "app.h"
#include "camera.h" #include "camera.h"
#include "entity.h" #include "entity.h"
#include "gametime.h"
#include "input.h" #include "input.h"
#include "particle.h" #include "particle.h"
#include "physics.h" #include "physics.h"
@ -25,6 +26,7 @@ App *app_NewApp() {
app->entity = entity_NewSystem(app); app->entity = entity_NewSystem(app);
app->camera = camera_NewSystem(app); app->camera = camera_NewSystem(app);
app->particle = particle_NewSystem(app); app->particle = particle_NewSystem(app);
app->time = gametime_NewSystem(app);
app->switch_level = NULL; app->switch_level = NULL;
app->clear_color = 0; app->clear_color = 0;
@ -51,6 +53,8 @@ void app_DeleteApp(App *app) {
physics_DeleteSystem(app->physics); physics_DeleteSystem(app->physics);
player_DeleteSystem(app->player); player_DeleteSystem(app->player);
camera_DeleteSystem(app->camera); camera_DeleteSystem(app->camera);
particle_DeleteSystem(app->particle);
gametime_DeleteSystem(app->time);
free(app); free(app);
} }
@ -64,6 +68,7 @@ void app_Advance(App *app, Duration deltaTime) {
if (app->switch_level != NULL) if (app->switch_level != NULL)
_app_SwitchLevel(app); _app_SwitchLevel(app);
gametime_Advance(app->time, deltaTime);
particle_Advance(app->particle, deltaTime); particle_Advance(app->particle, deltaTime);
input_Advance(app->input); input_Advance(app->input);
player_Advance(app->player, deltaTime); player_Advance(app->player, deltaTime);

2
app.h
View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "entity.h" #include "entity.h"
#include "gametime.h"
#include "physics.h" #include "physics.h"
#include "player.h" #include "player.h"
#include "input.h" #include "input.h"
@ -23,6 +24,7 @@ typedef struct _App {
System_Entity *entity; System_Entity *entity;
System_Camera *camera; System_Camera *camera;
System_Particle *particle; System_Particle *particle;
System_GameTime *time;
char *switch_level; char *switch_level;
uint32_t clear_color; uint32_t clear_color;

36
gametime.c Normal file
View File

@ -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;
}

40
gametime.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include "types.h"
#include <stdint.h>
#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

12
types.h
View File

@ -107,11 +107,15 @@ typedef struct {
int64_t microseconds; int64_t microseconds;
} TimePoint; } 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); TimePoint time_After(TimePoint now, Duration after);
Duration time_Since(TimePoint prev); // Don't use this!
Duration time_Difference(TimePoint now, TimePoint prev); Duration time_Since(TimePoint prev);
Duration time_Reset(TimePoint *prev); Duration time_Difference(TimePoint now, TimePoint prev);
// Don't use this!
Duration time_Reset(TimePoint *prev);
// 1e-6 // 1e-6
extern const double EPS; extern const double EPS;