forked from protoman/rockbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timerlib.cpp
69 lines (58 loc) · 1.22 KB
/
timerlib.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cmath>
#include <iostream>
#include "timerlib.h"
#include <SDL/SDL.h>
#include <unistd.h>
timerLib::timerLib()
{
_paused_timer = 0;
_ticks = 0;
_paused_ticks = 0;
_paused = false;
}
void timerLib::delay(int waitMS) const
{
SDL_Delay(waitMS);
}
void timerLib::udelay(int useconds)
{
#ifndef PC
SDL_Delay(useconds);
#else
usleep(useconds);
#endif
}
unsigned long timerLib::getTimer() const
{
return SDL_GetTicks() - _paused_ticks;
}
void timerLib::start_ticker()
{
_ticks = getTimer();
}
long timerLib::get_ticks()
{
return getTimer() - _ticks;
}
void timerLib::pause()
{
_paused = true;
_paused_timer = getTimer();
//std::cout << "PAUSE - SDL_GetTicks(): " << _paused_timer << std::endl;
}
void timerLib::unpause()
{
// unpausing without pause being called
if (_paused == false) {
return;
}
_paused = false;
long paused_ticks = getTimer() - _paused_timer;
//std::cout << "UNPAUSE - _paused_ticks: " << _paused_ticks << ", paused_ticks: " << paused_ticks;
_paused_ticks += paused_ticks;
//std::cout << ", SDL_GetTicks(): " << SDL_GetTicks() << ", timer: " << getTimer() << std::endl;
}
bool timerLib::is_paused()
{
return _paused;
}