-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
35 lines (30 loc) · 756 Bytes
/
Timer.cpp
File metadata and controls
35 lines (30 loc) · 756 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
32
33
34
35
#include "Timer.h"
#include <SDL/SDL.h>
void Timer::initialize( int updates_per_second ) {
my_ticks_per_update = int( 1000.0 / updates_per_second + 0.5 );
printf( "Timer initialized:\n"
"Updates per second: %d =>\n"
"Ticks (milliseconds) per update: %d\n",
updates_per_second,
my_ticks_per_update );
}
void Timer::set_mark() {
my_ticks_at_mark = SDL_GetTicks();
}
inline
int Timer::get_delta_ticks() {
return ( SDL_GetTicks() - my_ticks_at_mark );
}
void Timer::await_next_update() {
int dt = get_delta_ticks();
if( dt > my_ticks_per_update ) {
return;
}
SDL_Delay( my_ticks_per_update - dt );
}
bool Timer::threshold_reached() {
if( get_delta_ticks() > my_ticks_per_update )
return true;
else
return false;
}