Compare commits
No commits in common. "main" and "v0.0.5" have entirely different histories.
54
src/main.cpp
54
src/main.cpp
@ -34,39 +34,26 @@
|
|||||||
// 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 pauseTime;
|
bool cycleAnimation;
|
||||||
} 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_SPACE && action == GLFW_PRESS)
|
if (key == GLFW_KEY_C && action == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
input.pauseTime = true;
|
input.cycleAnimation = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearInput()
|
void clearInput()
|
||||||
{
|
{
|
||||||
input.pauseTime = false;
|
input.cycleAnimation = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now should always increase linearly with real world time, this should not be modified by input
|
double getTime()
|
||||||
// for animations etc
|
|
||||||
struct Time
|
|
||||||
{
|
{
|
||||||
double now;
|
auto now = std::chrono::steady_clock::now().time_since_epoch();
|
||||||
double dt;
|
return std::chrono::duration<double>(now).count();
|
||||||
} 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()
|
||||||
@ -113,12 +100,11 @@ int main()
|
|||||||
glfwSetKeyCallback(window, keyCallback);
|
glfwSetKeyCallback(window, keyCallback);
|
||||||
|
|
||||||
// TODO: convert these to an enum
|
// TODO: convert these to an enum
|
||||||
const int MODE_ORBIT = 0;
|
const int ANIM_ORBITING = 0;
|
||||||
const int MODE_MANEOUVRE = 1;
|
const int ANIM_ECCENTRICITY = 1;
|
||||||
int mode = 0;
|
int animation = 0;
|
||||||
|
|
||||||
// simulation time. this can be paused or whatever as animation demands.
|
double time = getTime();
|
||||||
double time = 0;
|
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
@ -127,15 +113,14 @@ 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.pauseTime)
|
if (input.cycleAnimation)
|
||||||
{
|
{
|
||||||
mode++;
|
animation++;
|
||||||
if (mode > MODE_MANEOUVRE)
|
if (animation > ANIM_ECCENTRICITY)
|
||||||
{
|
{
|
||||||
mode = 0;
|
animation = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,14 +128,15 @@ 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;
|
||||||
|
|
||||||
// orbit modifications need to be instanteneous
|
// only update time if playing the orbiting animation
|
||||||
if (mode == MODE_ORBIT)
|
if (animation == ANIM_ORBITING)
|
||||||
{
|
{
|
||||||
time += engineTime.dt * speed;
|
time = getTime() * speed;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("maneouvre mode!\n");
|
double e = .25 + .2 * sin(getTime());
|
||||||
|
moonOrbit.setEccentricity(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// rendering
|
// rendering
|
||||||
|
Loading…
x
Reference in New Issue
Block a user