From 30ddb673ca2ca22bb3a69e952b4614374a604fcf Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Mon, 23 Sep 2024 22:57:57 +0100 Subject: [PATCH] fix: add engine time --- src/main.cpp | 54 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4307cad..891b0fa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,26 +34,39 @@ // stuff to a library, since it keeps input a separate concern from the physics model struct Input { - bool cycleAnimation; + bool pauseTime; } input; + void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { - if (key == GLFW_KEY_C && action == GLFW_PRESS) + if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { - input.cycleAnimation = true; + input.pauseTime = true; } } void clearInput() { - input.cycleAnimation = false; + input.pauseTime = false; } -double getTime() +// now should always increase linearly with real world time, this should not be modified by input +// for animations etc +struct Time { - auto now = std::chrono::steady_clock::now().time_since_epoch(); - return std::chrono::duration(now).count(); + double now; + double dt; +} engineTime; + +void updateTime() +{ + std::chrono::duration timeSinceEpoch = std::chrono::steady_clock::now().time_since_epoch(); + double now = std::chrono::duration(timeSinceEpoch).count(); + double dt = now - engineTime.now; + + engineTime.dt = dt; + engineTime.now = now; } int main() @@ -100,11 +113,12 @@ int main() glfwSetKeyCallback(window, keyCallback); // TODO: convert these to an enum - const int ANIM_ORBITING = 0; - const int ANIM_ECCENTRICITY = 1; - int animation = 0; + const int MODE_ORBIT = 0; + const int MODE_MANEOUVRE = 1; + int mode = 0; - double time = getTime(); + // simulation time. this can be paused or whatever as animation demands. + double time = 0; // Main loop while (!glfwWindowShouldClose(window)) @@ -113,14 +127,15 @@ int main() // clearInput() needs to be called to clear input from previous frame clearInput(); glfwPollEvents(); + updateTime(); // apply input - if (input.cycleAnimation) + if (input.pauseTime) { - animation++; - if (animation > ANIM_ECCENTRICITY) + mode++; + if (mode > MODE_MANEOUVRE) { - animation = 0; + mode = 0; } } @@ -128,15 +143,14 @@ int main() // increase the speed of time by 60 * 60 * 24 to see 1 day per second. const double speed = 60 * 60 * 24; - // only update time if playing the orbiting animation - if (animation == ANIM_ORBITING) + // orbit modifications need to be instanteneous + if (mode == MODE_ORBIT) { - time = getTime() * speed; + time += engineTime.dt * speed; } else { - double e = .25 + .2 * sin(getTime()); - moonOrbit.setEccentricity(e); + printf("maneouvre mode!\n"); } // rendering