From f6cdc77a59c2c936e6f93bed64a9b2b280d09de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 8 Sep 2018 15:53:11 +0200 Subject: [PATCH] Use delta timing --- demo1.cpp | 32 +++++++++++++++++++++----------- general.hpp | 12 ++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/demo1.cpp b/demo1.cpp index 1bc0a50..d3c885e 100644 --- a/demo1.cpp +++ b/demo1.cpp @@ -699,14 +699,24 @@ int main() pokitto.setFrameRate(FPS); pokitto.display.setFont(fontTiny); +uint32_t previousTime = 0; +uint32_t dt; + while (pokitto.isRunning()) { if (pokitto.update()) { + +uint32_t timeNow = pokitto.getTime(); +dt = timeNow - previousTime; +previousTime = timeNow; + + + draw(); - int16_t step = max(1,UNITS_PER_SQUARE / 7); - const int16_t step2 = max(UNITS_PER_SQUARE / 25,1); + int16_t horizontalStep = (dt * PLAYER_SPEED) / 1000; + const int16_t rotationStep = (dt * PLAYER_ROTATION_SPEED) / 1000; Vector2D d = angleToDirection(player.mCamera.direction); @@ -715,13 +725,13 @@ int main() moveOffset.x = 0; moveOffset.y = 0; - d.x = (d.x * step) / UNITS_PER_SQUARE; - d.y = (d.y * step) / UNITS_PER_SQUARE; + d.x = (d.x * horizontalStep) / UNITS_PER_SQUARE; + d.y = (d.y * horizontalStep) / UNITS_PER_SQUARE; if (d.x == 0 && d.y == 0) { - d.x = d.x > 0 ? step : -step; - d.y = d.y > 0 ? step : -step; + d.x = d.x > 0 ? horizontalStep : -1 * horizontalStep; + d.y = d.y > 0 ? horizontalStep : -1 * horizontalStep; } bool strafe = pokitto.aBtn(); @@ -760,13 +770,13 @@ int main() if (strafe) { d = angleToDirection(player.mCamera.direction + UNITS_PER_SQUARE / 4); - d.x = (d.x * step * addition) / UNITS_PER_SQUARE; - d.y = (d.y * step * addition) / UNITS_PER_SQUARE; + d.x = (d.x * horizontalStep * addition) / UNITS_PER_SQUARE; + d.y = (d.y * horizontalStep * addition) / UNITS_PER_SQUARE; moveOffset = d; } else - player.mCamera.direction += addition * step2; + player.mCamera.direction += addition * rotationStep; Unit prevHeight = player.mCamera.height; @@ -786,9 +796,9 @@ int main() if (player.mCamera.height - CAMERA_COLL_HEIGHT_BELOW - floorHeightAt(camX,camY) < 2) player.mVericalSpeed = 500; // jump - } + } - player.mVericalSpeed -= 80; // gravity + player.mVericalSpeed -= (dt * GRAVITY_ACCELERATION) / 1000; // gravity } } diff --git a/general.hpp b/general.hpp index 48ddfaf..db2534f 100644 --- a/general.hpp +++ b/general.hpp @@ -21,6 +21,18 @@ Pokitto::Core pokitto; #define FPS 30 #endif +#ifndef PLAYER_SPEED +#define PLAYER_SPEED (2 * UNITS_PER_SQUARE) +#endif + +#ifndef PLAYER_ROTATION_SPEED +#define PLAYER_ROTATION_SPEED (UNITS_PER_SQUARE / 2) +#endif + +#ifndef GRAVITY_ACCELERATION +#define GRAVITY_ACCELERATION ((3 * UNITS_PER_SQUARE) / 2) +#endif + #define SCREEN_WIDTH 110 #define SCREEN_HEIGHT 88