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
|
||||
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<double>(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<double>(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
|
||||
|
|
Loading…
Reference in New Issue