2018-02-12 18:40:05 +01:00
|
|
|
#include "periodic_updates.hpp"
|
|
|
|
|
|
|
|
#include <ctime>
|
2018-02-12 21:53:35 +01:00
|
|
|
#include <chrono>
|
2018-02-12 18:40:05 +01:00
|
|
|
#include <GL/glut.h>
|
|
|
|
|
|
|
|
static Ball* _ball = nullptr;
|
2018-02-12 21:53:35 +01:00
|
|
|
static std::chrono::time_point<std::chrono::steady_clock>
|
|
|
|
_last_time, _init_clocks;
|
2018-02-12 18:40:05 +01:00
|
|
|
|
2018-02-14 11:18:36 +01:00
|
|
|
static bool is_paused = false;
|
|
|
|
static double speed_factor = 1.;
|
|
|
|
|
2018-02-12 18:40:05 +01:00
|
|
|
void init_periodic_static(Ball* ball) {
|
2018-02-12 21:53:35 +01:00
|
|
|
_last_time = std::chrono::steady_clock::now();
|
|
|
|
_init_clocks = std::chrono::steady_clock::now();
|
2018-02-12 18:40:05 +01:00
|
|
|
_ball = ball;
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:53:35 +01:00
|
|
|
double ellapsed_double(
|
|
|
|
std::chrono::time_point<std::chrono::steady_clock> beg,
|
|
|
|
std::chrono::time_point<std::chrono::steady_clock> end)
|
|
|
|
{
|
|
|
|
std::chrono::duration<double> ellapsed_db = end - beg;
|
|
|
|
return ellapsed_db.count();
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:40:05 +01:00
|
|
|
void periodic_update() {
|
2018-02-14 11:18:36 +01:00
|
|
|
if(is_paused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%lf\n", speed_factor);
|
2018-02-12 21:53:35 +01:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2018-02-14 11:18:36 +01:00
|
|
|
_ball->update_pos(speed_factor * ellapsed_double(_last_time, now));
|
2018-02-12 21:53:35 +01:00
|
|
|
|
|
|
|
_last_time = now;
|
2018-02-12 18:40:05 +01:00
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
2018-02-14 11:18:36 +01:00
|
|
|
|
|
|
|
void periodic_kb_handler(unsigned char key, int, int) {
|
|
|
|
if(key == ' ') {
|
|
|
|
if(is_paused)
|
|
|
|
_last_time = std::chrono::steady_clock::now();
|
|
|
|
is_paused = !is_paused;
|
|
|
|
}
|
|
|
|
else if(key == '<') {
|
|
|
|
speed_factor -= .1;
|
|
|
|
if(speed_factor <= 0.05)
|
|
|
|
speed_factor = .1;
|
|
|
|
}
|
|
|
|
else if(key == '>') {
|
|
|
|
speed_factor += .1;
|
|
|
|
}
|
|
|
|
else if(key == '0') {
|
|
|
|
speed_factor = 1.;
|
|
|
|
}
|
|
|
|
}
|