Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GameClock Class Reference

Game clock service. Implement two timers one real time timer and other game time timer. More...

#include <GameClock.hxx>

+ Inheritance diagram for GameClock:
+ Collaboration diagram for GameClock:

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 Public Member Functions inherited from Singleton< GameClock >
static GameClockinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< GameClock >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

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.

Member Typedef Documentation

◆ Clock

using GameClock::Clock = std::chrono::high_resolution_clock
private

Definition at line 21 of file GameClock.hxx.

◆ ClockCbk

using GameClock::ClockCbk = std::function<bool(void)>

Definition at line 27 of file GameClock.hxx.

◆ ClockTaskHndl

using GameClock::ClockTaskHndl = unsigned long

Definition at line 28 of file GameClock.hxx.

◆ GameClockDuration

using GameClock::GameClockDuration = unsigned long

Definition at line 30 of file GameClock.hxx.

◆ GameClockTime

using GameClock::GameClockTime = unsigned long

Definition at line 29 of file GameClock.hxx.

◆ GameTimeClockTask

◆ RealTimeClockTask

Definition at line 128 of file GameClock.hxx.

◆ TimeDuration

using GameClock::TimeDuration = Clock::duration

Definition at line 26 of file GameClock.hxx.

◆ TimePoint

using GameClock::TimePoint = std::chrono::time_point<Clock>

Definition at line 25 of file GameClock.hxx.

Member Function Documentation

◆ addGameTimeClockTask()

GameClock::ClockTaskHndl GameClock::addGameTimeClockTask ( ClockCbk  cbk,
GameClockTime  delay,
GameClockTime  period = 0U 
)

Add new game time clock task.

Parameters
cbkCallback function to be called after delay time is passed.
delayDelay 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.
periodRepeat 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.
Returns
Game time clock task handle. Can be used to remove clock and verify whether it is started correctly. In case of failure ClockTaskHndlInvalid will be return.

Definition at line 53 of file GameClock.cxx.

54 {
55  assert(cbk != nullptr);
56 
57  std::lock_guard<std::mutex> lock(m_lock);
58 
59  // If no delay required call callback immediately
60  if (delay == 0)
61  {
62  const bool isCanceled = cbk();
63 
64  if (isCanceled || (period == 0))
65  {
66  return ClockTaskHndlInvalid;
67  }
68 
69  delay = period;
70  }
71 
72  // Add +1 tick just to be sure timer is not fire before its timeout.
73  m_gameTimeTasks.push(GameTimeClockTask(cbk, delay + m_gameTicks + 1U, period, ++m_unique_handle));
74  return m_unique_handle;
75 }

◆ addRealTimeClockTask()

template<typename DelayType , typename PeriodType >
GameClock::ClockTaskHndl GameClock::addRealTimeClockTask ( ClockCbk  cbk,
DelayType  delay,
PeriodType  period = TimePointZero 
)
inline

Add new real time clock task.

Parameters
cbkCallback function to be called after delay time is passed.
delayDelay in chrono literals (e.g. 1h, 2min, 3s ...), callback function will be called after delay timer is passed.
periodRepeat period in chrono literals (e.g. 1h, 2min, 3s ...) the timer will be reset again with new delay time in amount of period time.
Returns
Real time clock task handle. Can be used to remove clock and verify whether it is started correctly. In case of failure ClockTaskHndlInvalid will be return.

Definition at line 5 of file GameClock.inl.hxx.

6 {
7  assert((cbk != nullptr) && (TimePoint(delay) >= TimePointZero) && (TimePoint(period) >= TimePointZero));
8 
9  std::lock_guard<std::mutex> lock(m_lock);
10  TimePoint delayConverted = (TimePoint)delay;
11 
12  // If no delay required call callback immediately
13  if (delayConverted == TimePointZero)
14  {
15  const bool isCanceled = cbk();
16 
17  if (isCanceled || ((TimePoint)period == TimePointZero))
18  {
19  return ClockTaskHndlInvalid;
20  }
21 
22  delayConverted = (TimePoint)period;
23  }
24 
25  m_realTimeTasks.push(RealTimeClockTask(cbk, Clock::now() + (delayConverted - TimePointZero), (TimePoint)period - TimePointZero,
26  ++m_unique_handle));
27  return m_unique_handle;
28 }
+ Here is the caller graph for this function:

◆ clear()

void GameClock::clear ( void  )

Remove all real time and game time clocks.

Definition at line 84 of file GameClock.cxx.

85 {
86  std::lock_guard<std::mutex> lock(m_lock);
87  m_gameTimeTasks.clear();
88  m_realTimeTasks.clear();
89 }

◆ getGameClockSpeed()

float GameClock::getGameClockSpeed ( ) const
inline

Definition at line 83 of file GameClock.hxx.

83 { return m_speedFactor; }
+ Here is the caller graph for this function:

◆ removeClockTask()

bool GameClock::removeClockTask ( ClockTaskHndl  hndl)

Remove real/game time clock. After it is removed successfully it is guaranteed it will not trigger callback.

Parameters
hndlHandle of clock which should be removed.
Returns
true in case clock is successfully removed, otherwise false.

Definition at line 41 of file GameClock.cxx.

42 {
43  std::lock_guard<std::mutex> lock(m_lock);
44 
46  {
48  }
49 
50  return true;
51 }
+ Here is the call graph for this function:

◆ setGameClockSpeed()

void GameClock::setGameClockSpeed ( float  speedFactor)

Set game clock speed.

Parameters
speedFactorGame clock scale factor. E.g. to run game clock 4 time faster provide 4.0f.

Definition at line 77 of file GameClock.cxx.

78 {
79  std::lock_guard<std::mutex> lock(m_lock);
80  m_speedFactor = speedFactor;
81  m_gameTickDuration = std::chrono::milliseconds((unsigned int)(GameClock::DefaultGameTickDuration / speedFactor));
82 }
+ Here is the caller graph for this function:

◆ tick()

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.

20 {
21  std::lock_guard<std::mutex> lock(m_lock);
22 
23  const auto now = Clock::now();
24 
26 
28  {
29  m_lastGameTickTime = now;
30  m_gameTicks++;
31 
33  }
34 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tickTask()

template<typename Task , typename Cmp , typename Now >
void GameClock::tickTask ( PriorityQueue< Task, Cmp > &  queue,
Now  now 
)
private

Tick clock for given task type.

Parameters
queuePriority queue of tasks.
nowThe time point when tick occurred.

Definition at line 4 of file GameClock.cxx.

5 {
6  while (!queue.empty() && (now >= queue.top().m_waketime))
7  {
8  const auto task = queue.top();
9  queue.pop();
10  const bool isCanceled = task.callback();
11 
12  if (!isCanceled && (task.m_period != decltype(task.m_period){0}))
13  {
14  queue.push(Task(task.callback, now + task.m_period, task.m_period, task.hndl));
15  }
16  }
17 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ PriorityQueue

template<typename T , typename Comparator >
friend class PriorityQueue
friend

Definition at line 22 of file GameClock.hxx.

Member Data Documentation

◆ ClockTaskHndlInvalid

constexpr ClockTaskHndl GameClock::ClockTaskHndlInvalid = ClockTaskHndl{0}
staticconstexprprivate

Invalid task handler. In case that clock is not added.

Definition at line 107 of file GameClock.hxx.

◆ DefaultGameTickDuration

constexpr unsigned int GameClock::DefaultGameTickDuration = 2000
staticconstexprprivate

Duration of default game timer tick in ms.

Definition at line 102 of file GameClock.hxx.

◆ GameDay

constexpr GameClockTime GameClock::GameDay = 24 * GameHour
staticconstexpr

Represent 1 day of game time.

Definition at line 45 of file GameClock.hxx.

◆ GameHour

constexpr GameClockTime GameClock::GameHour = 60 * GameMinute
staticconstexpr

Represent 1 hour of game time.

Definition at line 40 of file GameClock.hxx.

◆ GameMinute

constexpr GameClockTime GameClock::GameMinute = 1
staticconstexpr

Represent 1 minute of game time.

Definition at line 35 of file GameClock.hxx.

◆ m_gameTickDuration

Clock::duration GameClock::m_gameTickDuration = std::chrono::milliseconds(DefaultGameTickDuration)
private

Definition at line 143 of file GameClock.hxx.

◆ m_gameTicks

GameClockTime GameClock::m_gameTicks = 0U
private

Current number of the game ticks.

Definition at line 137 of file GameClock.hxx.

◆ m_gameTimeTasks

PriorityQueue<GameTimeClockTask, std::greater<GameTimeClockTask> > GameClock::m_gameTimeTasks
private

Definition at line 132 of file GameClock.hxx.

◆ m_lastGameTickTime

TimePoint GameClock::m_lastGameTickTime = Clock::now()
private

Last time of the game tick.

Definition at line 139 of file GameClock.hxx.

◆ m_lock

std::mutex GameClock::m_lock
private

Definition at line 133 of file GameClock.hxx.

◆ m_realTimeTasks

PriorityQueue<RealTimeClockTask, std::greater<RealTimeClockTask> > GameClock::m_realTimeTasks
private

Definition at line 131 of file GameClock.hxx.

◆ m_speedFactor

float GameClock::m_speedFactor = 1.f
private

The current game tick duration on milliseconds.

Definition at line 142 of file GameClock.hxx.

◆ m_unique_handle

ClockTaskHndl GameClock::m_unique_handle = 0U
private

Definition at line 135 of file GameClock.hxx.

◆ TimePointZero

constexpr TimePoint GameClock::TimePointZero = TimePoint{0s}
staticconstexprprivate

Definition at line 103 of file GameClock.hxx.


The documentation for this class was generated from the following files:
GameClock::m_speedFactor
float m_speedFactor
The current game tick duration on milliseconds.
Definition: GameClock.hxx:142
GameClock::tickTask
void tickTask(PriorityQueue< Task, Cmp > &queue, Now now)
Tick clock for given task type.
Definition: GameClock.cxx:4
GameClock::m_unique_handle
ClockTaskHndl m_unique_handle
Definition: GameClock.hxx:135
GameClock::m_lock
std::mutex m_lock
Definition: GameClock.hxx:133
GameClock::m_realTimeTasks
PriorityQueue< RealTimeClockTask, std::greater< RealTimeClockTask > > m_realTimeTasks
Definition: GameClock.hxx:131
GameClock::ClockTaskHndlInvalid
static constexpr ClockTaskHndl ClockTaskHndlInvalid
Invalid task handler. In case that clock is not added.
Definition: GameClock.hxx:107
GameClock::RealTimeClockTask
ClockTask< TimePoint, TimeDuration > RealTimeClockTask
Definition: GameClock.hxx:128
PriorityQueue::empty
bool empty() const noexcept
Check whether queue is empty.
Definition: PriorityQueue.inl.hxx:1
removeTaskFromQueue
static bool removeTaskFromQueue(const GameClock::ClockTaskHndl hndl, Queue &queue)
Definition: GameClock.cxx:36
GameClock::DefaultGameTickDuration
static constexpr unsigned int DefaultGameTickDuration
Duration of default game timer tick in ms.
Definition: GameClock.hxx:102
PriorityQueue::pop
void pop()
Remove first element from the queue (from top).
Definition: PriorityQueue.inl.hxx:30
GameClock::m_gameTickDuration
Clock::duration m_gameTickDuration
Definition: GameClock.hxx:143
PriorityQueue::push
void push(value_type &&element)
Add new element to the queue.
Definition: PriorityQueue.inl.hxx:18
GameClock::GameTimeClockTask
ClockTask< GameClockTime, GameClockDuration > GameTimeClockTask
Definition: GameClock.hxx:129
GameClock::m_lastGameTickTime
TimePoint m_lastGameTickTime
Last time of the game tick.
Definition: GameClock.hxx:139
GameClock::m_gameTimeTasks
PriorityQueue< GameTimeClockTask, std::greater< GameTimeClockTask > > m_gameTimeTasks
Definition: GameClock.hxx:132
GameClock::TimePoint
std::chrono::time_point< Clock > TimePoint
Definition: GameClock.hxx:25
GameClock::m_gameTicks
GameClockTime m_gameTicks
Current number of the game ticks.
Definition: GameClock.hxx:137
GameClock::TimePointZero
static constexpr TimePoint TimePointZero
Definition: GameClock.hxx:103
PriorityQueue::top
reference top()
Get top element from the queue.
Definition: PriorityQueue.inl.hxx:7