summaryrefslogtreecommitdiff
path: root/src/game/game.cpp
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2018-03-26 14:19:50 +0200
committerPaweł Redman <pawel.redman@gmail.com>2018-03-26 14:19:50 +0200
commita016a156e76f7394c5632da325ab0b453cfb3b37 (patch)
treed25fe29ebe10f0d921b06d1860565fedd577ca72 /src/game/game.cpp
parent2ae4b0ff77a2b2be2c485ac3ba6c1ef3facb874c (diff)
Independent game timing.
Diffstat (limited to 'src/game/game.cpp')
-rw-r--r--src/game/game.cpp65
1 files changed, 45 insertions, 20 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp
index f76b478..44ea95d 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -92,6 +92,8 @@ void state_t::start(void)
soldier = new unit_soldier_t(this);
soldier->place(&world, v2f_t(2.5, 0.5));
select(rectf_t(v2f_t(0.5, 0.5), v2f_t(2.5, 0.5)));
+
+ resume();
}
void state_t::stop(void)
@@ -179,34 +181,57 @@ void state_t::spawn_soldier(v2f_t x)
soldier->destroy();
}
-void state_t::tick(double now_, double dt_)
+void state_t::pause(void)
+{
+ paused = true;
+}
+
+void state_t::resume(void)
+{
+ t0 = nclock();
+ frames_since_t0 = 0;
+
+ paused = false;
+}
+
+
+#define TIME_DELTA ((ntime_t)1000000000 / 60)
+
+void state_t::tick(ntime_t time_)
{
- union {
- double d;
- uint32_t i;
- } u;
+ size_t target, frames_this_tick = 0;
if (paused)
return;
- if (dt > 0.05)
- dt = 0.05;
- else
- dt = dt_;
+ target = (time_ - t0) / TIME_DELTA;
+
+ while (frames_since_t0 < target) {
+ // FIXME: Is this non-deterministic enough?
+ dice_prng.seed(dice_prng.next() ^ time);
- now += dt;
+ // setting up old variables (refactor them out eventually)
+ now = time * 1.0e-9;
+ dt = TIME_DELTA * 1.0e-9;
- // FIXME: Is this non-deterministic enough?
- u.d = now;
- dice_prng.seed(dice_prng.next() ^ u.i);
- u.d = dt;
- dice_prng.seed(dice_prng.next() ^ u.i);
+ // on_think can insert/erase elements of awake_entities so iterate
+ // over a copy of it.
+ auto copy = awake_entities;
+ for (entity_t *ent : copy)
+ ent->on_think();
- // on_think can insert/erase elements of awake_entities so iterate
- // over a copy of it.
- auto copy = awake_entities;
- for (entity_t *ent : copy)
- ent->on_think();
+ frames++;
+ frames_since_t0++;
+ frames_this_tick++;
+ time += TIME_DELTA;
+
+ if (frames_this_tick == 3) {
+ t0 = time_;
+ frames_since_t0 = 0;
+ frames_behind++;
+ break;
+ }
+ }
}
die_t::die_t(size_t sides_)