From a016a156e76f7394c5632da325ab0b453cfb3b37 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Mon, 26 Mar 2018 14:19:50 +0200 Subject: Independent game timing. --- src/common.hpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/common.hpp') diff --git a/src/common.hpp b/src/common.hpp index 846b166..df9c8d1 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -35,6 +35,11 @@ extern bool debug_draw_paths; extern bool debug_draw_tile_coords; extern bool debug_AI; +// time in nanoseconds +// nclock() never returns 0, so it can be used as a null value +typedef uint64_t ntime_t; +ntime_t nclock(void); + namespace procgen { class prng_t { uint32_t state = 0; @@ -240,15 +245,23 @@ namespace game { public: world::world_t world; interface::state_t *interface; - double now = 0.0, dt; - bool paused = false; procgen::prng_t dice_prng; std::unordered_set awake_entities; std::unordered_set selected_units; + ntime_t time; // game time + double now, dt; // FIXME: refactor the code, use ntime_t everywhere + + // frame timing data (game runs at a different frequency than the renderer) + bool paused; + ntime_t t0; + size_t frames = 0, frames_since_t0, frames_behind = 0; + void start(void); void stop(void); - void tick(double now_, double dt_); + void tick(ntime_t time); + void pause(void); + void resume(void); // These are called by the interface. void select(rectf_t rect); @@ -355,14 +368,15 @@ namespace render { void drender_text(rectf_t rect, std::string str); void drender_entity(world::entity_t *ent); public: - double now, dt; + ntime_t time; + double dt; struct { size_t sectors, tiles, entities; } stats; state_t(sf::RenderWindow *window_); - void begin_frame(double time_, double dt_); + void begin_frame(ntime_t time_, double dt); void end_frame(void); void render(game::state_t *game); -- cgit