mpri-graphics-project/periodic_updates.cpp

32 lines
803 B
C++
Raw Normal View History

2018-02-12 18:40:05 +01:00
#include "periodic_updates.hpp"
#include <ctime>
#include <chrono>
2018-02-12 18:40:05 +01:00
#include <GL/glut.h>
static Ball* _ball = nullptr;
static std::chrono::time_point<std::chrono::steady_clock>
_last_time, _init_clocks;
2018-02-12 18:40:05 +01:00
void init_periodic_static(Ball* ball) {
_last_time = std::chrono::steady_clock::now();
_init_clocks = std::chrono::steady_clock::now();
2018-02-12 18:40:05 +01:00
_ball = ball;
}
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() {
auto now = std::chrono::steady_clock::now();
_ball->update_pos(ellapsed_double(_last_time, now));
_last_time = now;
2018-02-12 18:40:05 +01:00
glutPostRedisplay();
}