diff options
author | cflip <cflip@cflip.net> | 2022-04-01 21:23:01 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-04-01 21:23:01 -0600 |
commit | 3ee74cdb1b4fd2bf9eefdf6bcd8439b27cbb7336 (patch) | |
tree | 4c58b1cae03df912d2be17b077ab1810fcb3627b /src/main.cpp | |
parent | 7803be09b7c0a133b1009408fea2fb1f1474e786 (diff) |
This adds a delta time that ensures that everything moves at the same
speed regardless of framerate or system.
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp index 25e6fea..b417b45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "window.h" #include <SDL_keycode.h> +#include <chrono> int main(int argc, char** argv) { @@ -107,7 +108,12 @@ int main(int argc, char** argv) } }); + auto lastTime = std::chrono::high_resolution_clock::now(); while (!window.shouldClose()) { + auto now = std::chrono::high_resolution_clock::now(); + float deltaTime = std::chrono::duration_cast<std::chrono::duration<float>>(now - lastTime).count() * 100; + lastTime = now; + window.update(); bitmap.clear(0xff224466); @@ -116,7 +122,7 @@ int main(int argc, char** argv) if (left) xOffs -= cameraMoveSpeed; if (right) xOffs += cameraMoveSpeed; - level.update(); + level.update(deltaTime); level.draw(bitmap, xOffs, yOffs); int xx = (hoveredTile.x - hoveredTile.y) * (TileSize / 2) - xOffs; |