summaryrefslogtreecommitdiff
path: root/src/main.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/main.cpp
parent2ae4b0ff77a2b2be2c485ac3ba6c1ef3facb874c (diff)
Independent game timing.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp29
1 files changed, 16 insertions, 13 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 71e47be..3f60f25 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -24,20 +24,23 @@ bool debug_draw_tile_coords = false;
bool debug_AI = false;
render::state_t *debug_render;
-uint64_t nano_clock(void)
+static ntime_t nclock_ref;
+
+// always > 0
+ntime_t nclock(void)
{
- return std::chrono::high_resolution_clock::now().time_since_epoch() / std::chrono::nanoseconds(1);
+ return std::chrono::high_resolution_clock::now().time_since_epoch() /
+ std::chrono::nanoseconds(1) - nclock_ref;
}
int main()
{
- uint64_t t0 = nano_clock();
+ nclock_ref = nclock() - 1;
+
sf::RenderWindow window(sf::VideoMode(800, 600), "Minitrem");
render::state_t render(&window);
game::state_t game;
interface::state_t interface(&window, &game);
- size_t frame = 0;
- double before = NAN;
game.interface = &interface;
debug_render = &render;
@@ -47,14 +50,17 @@ int main()
game.start();
while (1) {
- double now = (nano_clock() - t0) * 1.0e-9, dt;
+ static ntime_t before = 0;
+ ntime_t now = nclock();
+ double dt;
- if (frame)
- dt = now - before;
+ if (before)
+ dt = (now - before) * 1.0e-9;
else
- dt = 0.01;
+ dt = 0.1;
+ before = now;
- game.tick(now, dt);
+ game.tick(now);
interface.tick(dt);
if (!window.isOpen())
break;
@@ -64,9 +70,6 @@ int main()
render.render(&game);
interface.render_to(&render);
render.end_frame();
-
- before = now;
- frame++;
}
game.stop();