![]() |
Cytopia
0.3
A city building simulation game
|
Game clock service. Implement two timers one real time timer and other game time timer. More...
#include <GameClock.hxx>
Classes | |
struct | ClockTask |
Template structure provide base for different clock tasks. More... | |
Public Types | |
using | TimePoint = std::chrono::time_point< Clock > |
using | TimeDuration = Clock::duration |
using | ClockCbk = std::function< bool(void)> |
using | ClockTaskHndl = unsigned long |
using | GameClockTime = unsigned long |
using | GameClockDuration = unsigned long |
Public Member Functions | |
void | tick (void) |
This function provides the tick for both clocks. More... | |
template<typename DelayType , typename PeriodType = TimePoint> | |
GameClock::ClockTaskHndl | addRealTimeClockTask (ClockCbk cbk, DelayType delay, PeriodType period=TimePointZero) |
Add new real time clock task. More... | |
GameClock::ClockTaskHndl | addGameTimeClockTask (ClockCbk cbk, GameClockTime delay, GameClockTime period=0U) |
Add new game time clock task. More... | |
void | setGameClockSpeed (float speedFactor) |
Set game clock speed. More... | |
float | getGameClockSpeed () const |
void | clear (void) |
Remove all real time and game time clocks. More... | |
bool | removeClockTask (ClockTaskHndl hndl) |
Remove real/game time clock. After it is removed successfully it is guaranteed it will not trigger callback. More... | |
Static Public Attributes | |
static constexpr GameClockTime | GameMinute = 1 |
Represent 1 minute of game time. More... | |
static constexpr GameClockTime | GameHour = 60 * GameMinute |
Represent 1 hour of game time. More... | |
static constexpr GameClockTime | GameDay = 24 * GameHour |
Represent 1 day of game time. More... | |
Private Types | |
using | Clock = std::chrono::high_resolution_clock |
using | RealTimeClockTask = ClockTask< TimePoint, TimeDuration > |
using | GameTimeClockTask = ClockTask< GameClockTime, GameClockDuration > |
Private Member Functions | |
template<typename Task , typename Cmp , typename Now > | |
void | tickTask (PriorityQueue< Task, Cmp > &queue, Now now) |
Tick clock for given task type. More... | |
Private Attributes | |
PriorityQueue< RealTimeClockTask, std::greater< RealTimeClockTask > > | m_realTimeTasks |
PriorityQueue< GameTimeClockTask, std::greater< GameTimeClockTask > > | m_gameTimeTasks |
std::mutex | m_lock |
ClockTaskHndl | m_unique_handle = 0U |
GameClockTime | m_gameTicks = 0U |
Current number of the game ticks. More... | |
TimePoint | m_lastGameTickTime = Clock::now() |
Last time of the game tick. More... | |
float | m_speedFactor = 1.f |
The current game tick duration on milliseconds. More... | |
Clock::duration | m_gameTickDuration = std::chrono::milliseconds(DefaultGameTickDuration) |
Static Private Attributes | |
static constexpr unsigned int | DefaultGameTickDuration = 2000 |
Duration of default game timer tick in ms. More... | |
static constexpr TimePoint | TimePointZero = TimePoint{0s} |
static constexpr ClockTaskHndl | ClockTaskHndlInvalid = ClockTaskHndl{0} |
Invalid task handler. In case that clock is not added. More... | |
Friends | |
template<typename T , typename Comparator > | |
class | PriorityQueue |
Additional Inherited Members | |
![]() | |
static GameClock & | instance (void) |
Get an instance of the singleton. More... | |
![]() | |
Singleton () noexcept=default | |
~Singleton () noexcept=default | |
Game clock service. Implement two timers one real time timer and other game time timer.
Both timers provide possibility to add task which will be triggered after delay time run out. Game timer represent timer running in game time. The game time can be scaled providing possibility to speed up or slow down game.
Definition at line 18 of file GameClock.hxx.
|
private |
Definition at line 21 of file GameClock.hxx.
using GameClock::ClockCbk = std::function<bool(void)> |
Definition at line 27 of file GameClock.hxx.
using GameClock::ClockTaskHndl = unsigned long |
Definition at line 28 of file GameClock.hxx.
using GameClock::GameClockDuration = unsigned long |
Definition at line 30 of file GameClock.hxx.
using GameClock::GameClockTime = unsigned long |
Definition at line 29 of file GameClock.hxx.
|
private |
Definition at line 129 of file GameClock.hxx.
|
private |
Definition at line 128 of file GameClock.hxx.
using GameClock::TimeDuration = Clock::duration |
Definition at line 26 of file GameClock.hxx.
using GameClock::TimePoint = std::chrono::time_point<Clock> |
Definition at line 25 of file GameClock.hxx.
GameClock::ClockTaskHndl GameClock::addGameTimeClockTask | ( | ClockCbk | cbk, |
GameClockTime | delay, | ||
GameClockTime | period = 0U |
||
) |
Add new game time clock task.
cbk | Callback function to be called after delay time is passed. |
delay | Delay in game timer ticks. Use provided values GameDay, GameHour, GameMinute and scale it as necessary. Callback function will be called after delay timer is passed. |
period | Repeat period in game timer ticks. Use provided values GameDay, GameHour, GameMinute and scale it as necessary. The timer will be reset again with new delay ticks in amount of period ticks. |
Definition at line 53 of file GameClock.cxx.
|
inline |
Add new real time clock task.
cbk | Callback function to be called after delay time is passed. |
delay | Delay in chrono literals (e.g. 1h, 2min, 3s ...), callback function will be called after delay timer is passed. |
period | Repeat period in chrono literals (e.g. 1h, 2min, 3s ...) the timer will be reset again with new delay time in amount of period time. |
Definition at line 5 of file GameClock.inl.hxx.
void GameClock::clear | ( | void | ) |
|
inline |
bool GameClock::removeClockTask | ( | ClockTaskHndl | hndl | ) |
Remove real/game time clock. After it is removed successfully it is guaranteed it will not trigger callback.
hndl | Handle of clock which should be removed. |
Definition at line 41 of file GameClock.cxx.
void GameClock::setGameClockSpeed | ( | float | speedFactor | ) |
Set game clock speed.
speedFactor | Game clock scale factor. E.g. to run game clock 4 time faster provide 4.0f. |
Definition at line 77 of file GameClock.cxx.
void GameClock::tick | ( | void | ) |
This function provides the tick for both clocks.
It must be called frequently. Call frequency determines clock precision.
Definition at line 19 of file GameClock.cxx.
|
private |
Tick clock for given task type.
queue | Priority queue of tasks. |
now | The time point when tick occurred. |
Definition at line 4 of file GameClock.cxx.
|
friend |
Definition at line 22 of file GameClock.hxx.
|
staticconstexprprivate |
Invalid task handler. In case that clock is not added.
Definition at line 107 of file GameClock.hxx.
|
staticconstexprprivate |
Duration of default game timer tick in ms.
Definition at line 102 of file GameClock.hxx.
|
staticconstexpr |
Represent 1 day of game time.
Definition at line 45 of file GameClock.hxx.
|
staticconstexpr |
Represent 1 hour of game time.
Definition at line 40 of file GameClock.hxx.
|
staticconstexpr |
Represent 1 minute of game time.
Definition at line 35 of file GameClock.hxx.
|
private |
Definition at line 143 of file GameClock.hxx.
|
private |
Current number of the game ticks.
Definition at line 137 of file GameClock.hxx.
|
private |
Definition at line 132 of file GameClock.hxx.
|
private |
Last time of the game tick.
Definition at line 139 of file GameClock.hxx.
|
private |
Definition at line 133 of file GameClock.hxx.
|
private |
Definition at line 131 of file GameClock.hxx.
|
private |
The current game tick duration on milliseconds.
Definition at line 142 of file GameClock.hxx.
|
private |
Definition at line 135 of file GameClock.hxx.
Definition at line 103 of file GameClock.hxx.