Add gametime system
This commit is contained in:
parent
4acd6c0710
commit
787f94a8b2
5
app.c
5
app.c
@ -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
2
app.h
@ -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
36
gametime.c
Normal 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
40
gametime.h
Normal 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
|
6
types.h
6
types.h
@ -107,10 +107,14 @@ 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);
|
||||||
|
// Don't use this!
|
||||||
Duration time_Since(TimePoint prev);
|
Duration time_Since(TimePoint prev);
|
||||||
Duration time_Difference(TimePoint now, TimePoint prev);
|
Duration time_Difference(TimePoint now, TimePoint prev);
|
||||||
|
// Don't use this!
|
||||||
Duration time_Reset(TimePoint *prev);
|
Duration time_Reset(TimePoint *prev);
|
||||||
|
|
||||||
// 1e-6
|
// 1e-6
|
||||||
|
Loading…
Reference in New Issue
Block a user