Loading [MathJax]/extensions/MathMenu.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GameClock.cxx
Go to the documentation of this file.
1 #include <cassert>
2 #include "GameClock.hxx"
3 
4 template <typename Task, typename Cmp, typename Now> void GameClock::tickTask(PriorityQueue<Task, Cmp> &queue, const Now now)
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 }
18 
19 void GameClock::tick(void)
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 }
35 
36 template <typename Queue> static bool removeTaskFromQueue(const GameClock::ClockTaskHndl hndl, Queue &queue)
37 {
38  return queue.erase_if([hndl](const typename Queue::value_type &ct) { return ct.hndl == hndl; }) > 0;
39 }
40 
42 {
43  std::lock_guard<std::mutex> lock(m_lock);
44 
46  {
48  }
49 
50  return true;
51 }
52 
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 }
76 
77 void GameClock::setGameClockSpeed(float speedFactor)
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 }
83 
84 void GameClock::clear(void)
85 {
86  std::lock_guard<std::mutex> lock(m_lock);
87  m_gameTimeTasks.clear();
88  m_realTimeTasks.clear();
89 }
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::ClockTaskHndl
unsigned long ClockTaskHndl
Definition: GameClock.hxx:28
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
PriorityQueue
Priority queue with erase element functionality.
Definition: PriorityQueue.hxx:10
GameClock::DefaultGameTickDuration
static constexpr unsigned int DefaultGameTickDuration
Duration of default game timer tick in ms.
Definition: GameClock.hxx:102
GameClock::tick
void tick(void)
This function provides the tick for both clocks.
Definition: GameClock.cxx:19
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
GameClock::GameClockTime
unsigned long GameClockTime
Definition: GameClock.hxx:29
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::addGameTimeClockTask
GameClock::ClockTaskHndl addGameTimeClockTask(ClockCbk cbk, GameClockTime delay, GameClockTime period=0U)
Add new game time clock task.
Definition: GameClock.cxx:53
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::m_gameTicks
GameClockTime m_gameTicks
Current number of the game ticks.
Definition: GameClock.hxx:137
GameClock::removeClockTask
bool removeClockTask(ClockTaskHndl hndl)
Remove real/game time clock. After it is removed successfully it is guaranteed it will not trigger ca...
Definition: GameClock.cxx:41
GameClock::clear
void clear(void)
Remove all real time and game time clocks.
Definition: GameClock.cxx:84
PriorityQueue::top
reference top()
Get top element from the queue.
Definition: PriorityQueue.inl.hxx:7
GameClock.hxx
GameClock::setGameClockSpeed
void setGameClockSpeed(float speedFactor)
Set game clock speed.
Definition: GameClock.cxx:77
GameClock::ClockCbk
std::function< bool(void)> ClockCbk
Definition: GameClock.hxx:27