-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.cc
More file actions
31 lines (27 loc) · 966 Bytes
/
clock.cc
File metadata and controls
31 lines (27 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "clock.hh"
#include <thread>
namespace SDL {
Clock::Clock() {
last = std::chrono::system_clock::now();
first = true;
}
std::chrono::milliseconds Clock::tick() {
const auto current = std::chrono::system_clock::now();
std::chrono::milliseconds delta = std::chrono::duration_cast<std::chrono::milliseconds>(
current - this->last
);
this->last = current;
return delta;
}
std::chrono::milliseconds Clock::tick(const float fps) {
const auto delta = this->tick();
const auto min_delta = std::chrono::duration<double>(1.0 / double(fps));
const auto delta_delta = delta - min_delta;
if (delta_delta > std::chrono::duration<double>(0.)) {
std::this_thread::sleep_for(delta_delta);
return std::chrono::duration_cast<std::chrono::milliseconds>(min_delta);
} else {
return delta;
}
}
}