fix: add engine time
This commit is contained in:
parent
da3a917896
commit
30ddb673ca
54
src/main.cpp
54
src/main.cpp
|
@ -34,26 +34,39 @@
|
||||||
// stuff to a library, since it keeps input a separate concern from the physics model
|
// stuff to a library, since it keeps input a separate concern from the physics model
|
||||||
struct Input
|
struct Input
|
||||||
{
|
{
|
||||||
bool cycleAnimation;
|
bool pauseTime;
|
||||||
} input;
|
} input;
|
||||||
|
|
||||||
|
|
||||||
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
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()
|
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();
|
double now;
|
||||||
return std::chrono::duration<double>(now).count();
|
double dt;
|
||||||
|
} engineTime;
|
||||||
|
|
||||||
|
void updateTime()
|
||||||
|
{
|
||||||
|
std::chrono::duration timeSinceEpoch = std::chrono::steady_clock::now().time_since_epoch();
|
||||||
|
double now = std::chrono::duration<double>(timeSinceEpoch).count();
|
||||||
|
double dt = now - engineTime.now;
|
||||||
|
|
||||||
|
engineTime.dt = dt;
|
||||||
|
engineTime.now = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
@ -100,11 +113,12 @@ int main()
|
||||||
glfwSetKeyCallback(window, keyCallback);
|
glfwSetKeyCallback(window, keyCallback);
|
||||||
|
|
||||||
// TODO: convert these to an enum
|
// TODO: convert these to an enum
|
||||||
const int ANIM_ORBITING = 0;
|
const int MODE_ORBIT = 0;
|
||||||
const int ANIM_ECCENTRICITY = 1;
|
const int MODE_MANEOUVRE = 1;
|
||||||
int animation = 0;
|
int mode = 0;
|
||||||
|
|
||||||
double time = getTime();
|
// simulation time. this can be paused or whatever as animation demands.
|
||||||
|
double time = 0;
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
|
@ -113,14 +127,15 @@ int main()
|
||||||
// clearInput() needs to be called to clear input from previous frame
|
// clearInput() needs to be called to clear input from previous frame
|
||||||
clearInput();
|
clearInput();
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
updateTime();
|
||||||
|
|
||||||
// apply input
|
// apply input
|
||||||
if (input.cycleAnimation)
|
if (input.pauseTime)
|
||||||
{
|
{
|
||||||
animation++;
|
mode++;
|
||||||
if (animation > ANIM_ECCENTRICITY)
|
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.
|
// increase the speed of time by 60 * 60 * 24 to see 1 day per second.
|
||||||
const double speed = 60 * 60 * 24;
|
const double speed = 60 * 60 * 24;
|
||||||
|
|
||||||
// only update time if playing the orbiting animation
|
// orbit modifications need to be instanteneous
|
||||||
if (animation == ANIM_ORBITING)
|
if (mode == MODE_ORBIT)
|
||||||
{
|
{
|
||||||
time = getTime() * speed;
|
time += engineTime.dt * speed;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
double e = .25 + .2 * sin(getTime());
|
printf("maneouvre mode!\n");
|
||||||
moonOrbit.setEccentricity(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// rendering
|
// rendering
|
||||||
|
|
Loading…
Reference in New Issue