1 | #include <SDL2/SDL.h> |
2 |
|
3 | #include "ZydecoCommon.hpp" |
4 | #include "TimerSDL2.hpp" |
5 |
|
6 |
|
7 | static Logger LOGGER("TimerSDL2"); |
8 |
|
9 | // static initialize members |
10 | uint64_t TimerSDL2::s_globalCounterMs = 0; |
11 | bool TimerSDL2::s_globalCounterInitialized = false; |
12 |
|
13 |
|
14 | TimerSDL2::TimerSDL2() |
15 | { |
16 | LOGGER.Log(Logger::TRACE, "TimerSDL2()"); |
17 | if (!s_globalCounterInitialized) |
18 | { |
19 | LOGGER.Log(Logger::DEBUG, "TimerSDL2(): First TimerSDL2 instance created..."); |
20 | if (SDL_WasInit(SDL_INIT_EVENTS) & SDL_INIT_EVENTS) |
21 | { |
22 | LOGGER.Log(Logger::DEBUG, "TimerSDL2(): SDL timer was initialized correctly"); |
23 | s_globalCounterInitialized = true; |
24 | s_globalCounterMs = SDL_GetTicks64(); |
25 | } |
26 | else |
27 | { |
28 | LOGGER.Log(Logger::DEBUG, "TimerSDL2(): SDL timer was not initialized correctly"); |
29 | } |
30 | } |
31 | } |
32 |
|
33 | // IUpdateable |
34 | bool TimerSDL2::Update() |
35 | { |
36 | if (!s_globalCounterInitialized) |
37 | { |
38 | LOGGER.Log(Logger::ERROR, "Update(): SDL timer was not initialized correctly"); |
39 | return true; |
40 | } |
41 | s_globalCounterMs = SDL_GetTicks64(); |
42 | SDL_Delay(1); |
43 | return false; |
44 | } |
45 |
|
46 | // ITimer |
47 | void TimerSDL2::Reset() |
48 | { |
49 | m_instanceExpired = false; |
50 | m_instanceRunning = false; |
51 | m_instanceStartTimeMs = -1; |
52 | } |
53 |
|
54 | void TimerSDL2::SetTimeout(uint64_t timeout_ms) |
55 | { |
56 | m_instanceTimeoutMs = timeout_ms; |
57 | } |
58 |
|
59 | void TimerSDL2::Start() |
60 | { |
61 | if (!m_instanceRunning) |
62 | { |
63 | m_instanceStartTimeMs = s_globalCounterMs; |
64 | m_instanceRunning = true; |
65 | } |
66 | } |
67 |
|
68 | void TimerSDL2::Stop() |
69 | { |
70 | if (m_instanceRunning) |
71 | { |
72 | m_instanceRunning = false; |
73 | m_instanceExpired = (s_globalCounterMs >= (m_instanceStartTimeMs + m_instanceTimeoutMs)); |
74 | } |
75 | } |
76 |
|
77 | bool TimerSDL2::IsExpired() |
78 | { |
79 | if (m_instanceExpired) { return true; } |
80 | if (s_globalCounterMs >= (m_instanceStartTimeMs + m_instanceTimeoutMs)) |
81 | { |
82 | m_instanceRunning = false; |
83 | m_instanceExpired = true; |
84 | return true; |
85 | } |
86 | return false; |
87 | } |
88 |
|
89 | uint64_t *TimerSDL2::GetGlobalTimePointer() |
90 | { |
91 | return &s_globalCounterMs; |
92 | } |
93 |
|