Index

zydeco / 1467546

Experiment in graphics programming, C++, OpenGL, simulation techniques.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
916 Nov 2023 11:11a4d3636Fractal ViewerJosh Stockin101G

Blob @ zydeco / src / sdl2 / TimerSDL2.cpp

text/plain2039 bytesdownload raw
1#include <SDL2/SDL.h>
2
3#include "ZydecoCommon.hpp"
4#include "TimerSDL2.hpp"
5
6
7static Logger LOGGER("TimerSDL2");
8
9// static initialize members
10uint64_t TimerSDL2::s_globalCounterMs = 0;
11bool TimerSDL2::s_globalCounterInitialized = false;
12
13
14TimerSDL2::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
34bool 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
46void TimerSDL2::Reset()
47{
48 m_instanceExpired = false;
49 m_instanceRunning = false;
50 m_instanceStartTimeMs = -1;
51}
52
53void TimerSDL2::SetTimeout(uint64_t timeout_ms)
54{
55 m_instanceTimeoutMs = timeout_ms;
56}
57
58void TimerSDL2::Start()
59{
60 if (!m_instanceRunning)
61 {
62 m_instanceStartTimeMs = s_globalCounterMs;
63 m_instanceRunning = true;
64 }
65}
66
67void TimerSDL2::Stop()
68{
69 if (m_instanceRunning)
70 {
71 m_instanceRunning = false;
72 m_instanceExpired = (s_globalCounterMs >= (m_instanceStartTimeMs + m_instanceTimeoutMs));
73 }
74}
75
76bool 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
88uint64_t *TimerSDL2::GetGlobalTimePointer()
89{
90 return &s_globalCounterMs;
91}
92