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 | return false; |
43 | } |
44 |
|
45 | // ITimer |
46 | void TimerSDL2::Reset() |
47 | { |
48 | m_instanceExpired = false; |
49 | m_instanceRunning = false; |
50 | m_instanceStartTimeMs = -1; |
51 | } |
52 |
|
53 | void TimerSDL2::SetTimeout(uint64_t timeout_ms) |
54 | { |
55 | m_instanceTimeoutMs = timeout_ms; |
56 | } |
57 |
|
58 | void TimerSDL2::Start() |
59 | { |
60 | if (!m_instanceRunning) |
61 | { |
62 | m_instanceStartTimeMs = s_globalCounterMs; |
63 | m_instanceRunning = true; |
64 | } |
65 | } |
66 |
|
67 | void TimerSDL2::Stop() |
68 | { |
69 | if (m_instanceRunning) |
70 | { |
71 | m_instanceRunning = false; |
72 | m_instanceExpired = (s_globalCounterMs >= (m_instanceStartTimeMs + m_instanceTimeoutMs)); |
73 | } |
74 | } |
75 |
|
76 | bool TimerSDL2::IsExpired() |
77 | { |
78 | if (m_instanceExpired) { return true; } |
79 | if (s_globalCounterMs >= (m_instanceStartTimeMs + m_instanceTimeoutMs)) |
80 | { |
81 | m_instanceRunning = false; |
82 | m_instanceExpired = true; |
83 | return true; |
84 | } |
85 | return false; |
86 | } |
87 |
|
88 | uint64_t *TimerSDL2::GetGlobalTimePointer() |
89 | { |
90 | return &s_globalCounterMs; |
91 | } |
92 |
|