Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GameClock.hxx
Go to the documentation of this file.
1 #ifndef GAME_CLOCK_HXX_
2 #define GAME_CLOCK_HXX_
3 
4 #include <chrono>
5 #include <mutex>
6 #include <functional>
7 #include "../util/PriorityQueue.hxx"
8 #include "../util/Singleton.hxx"
9 
10 using namespace std::chrono_literals;
11 
18 class GameClock : public Singleton<GameClock>
19 {
20 private:
21  using Clock = std::chrono::high_resolution_clock;
22  template <typename T, typename Comparator> friend class PriorityQueue;
23 
24 public:
25  using TimePoint = std::chrono::time_point<Clock>;
26  using TimeDuration = Clock::duration;
27  using ClockCbk = std::function<bool(void)>;
28  using ClockTaskHndl = unsigned long;
29  using GameClockTime = unsigned long;
30  using GameClockDuration = unsigned long;
31 
35  static constexpr GameClockTime GameMinute = 1;
36 
40  static constexpr GameClockTime GameHour = 60 * GameMinute;
41 
45  static constexpr GameClockTime GameDay = 24 * GameHour;
46 
51  void tick(void);
52 
61  template <typename DelayType, typename PeriodType = TimePoint>
62  GameClock::ClockTaskHndl addRealTimeClockTask(ClockCbk cbk, DelayType delay, PeriodType period = TimePointZero);
63 
74  GameClock::ClockTaskHndl addGameTimeClockTask(ClockCbk cbk, GameClockTime delay, GameClockTime period = 0U);
75 
81  void setGameClockSpeed(float speedFactor);
82 
83  float getGameClockSpeed() const { return m_speedFactor; }
84 
88  void clear(void);
89 
96  bool removeClockTask(ClockTaskHndl hndl);
97 
98 private:
102  static constexpr unsigned int DefaultGameTickDuration = 2000;
103  static constexpr TimePoint TimePointZero = TimePoint{0s};
107  static constexpr ClockTaskHndl ClockTaskHndlInvalid = ClockTaskHndl{0};
108 
112  template <typename Time, typename Duration> struct ClockTask
113  {
116  Duration m_period;
118 
119  explicit ClockTask(ClockCbk cbk, Time delay, Duration period, ClockTaskHndl hndl)
120  : callback{cbk}, m_waketime{delay}, m_period{period}, hndl{hndl}
121  {
122  }
123 
124  bool operator==(const ClockTask &task2) const { return hndl == task2.hndl; };
125  bool operator>(const ClockTask &task2) const { return m_waketime > task2.m_waketime; }
126  };
127 
130 
133  std::mutex m_lock;
134  // Provide way to return unique handle for each task.
135  ClockTaskHndl m_unique_handle = 0U;
137  GameClockTime m_gameTicks = 0U;
139  TimePoint m_lastGameTickTime = Clock::now();
141 
142  float m_speedFactor = 1.f;
143  Clock::duration m_gameTickDuration = std::chrono::milliseconds(DefaultGameTickDuration);
144 
150  template <typename Task, typename Cmp, typename Now> void tickTask(PriorityQueue<Task, Cmp> &queue, Now now);
151 };
152 
153 #include "GameClock.inl.hxx"
154 
155 #endif // GAME_CLOCK_HXX_
GameClock
Game clock service. Implement two timers one real time timer and other game time timer.
Definition: GameClock.hxx:18
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::ClockTaskHndl
unsigned long ClockTaskHndl
Definition: GameClock.hxx:28
GameClock.inl.hxx
GameClock::ClockTask::operator>
bool operator>(const ClockTask &task2) const
Definition: GameClock.hxx:125
GameClock::ClockTask::hndl
ClockTaskHndl hndl
Definition: GameClock.hxx:117
GameClock::GameClockDuration
unsigned long GameClockDuration
Definition: GameClock.hxx:30
PriorityQueue::clear
void clear(void) noexcept
Remove all elements from queue.
Definition: PriorityQueue.inl.hxx:53
PriorityQueue
Priority queue with erase element functionality.
Definition: PriorityQueue.hxx:10
GameClock::ClockTask
Template structure provide base for different clock tasks.
Definition: GameClock.hxx:112
GameClock::TimeDuration
Clock::duration TimeDuration
Definition: GameClock.hxx:26
GameClock::GameClockTime
unsigned long GameClockTime
Definition: GameClock.hxx:29
Singleton
Abstract Singleton implementation.
Definition: Singleton.hxx:8
GameClock::ClockTask::callback
ClockCbk callback
Definition: GameClock.hxx:114
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::ClockTask::m_waketime
Time m_waketime
Definition: GameClock.hxx:115
GameClock::ClockTask::m_period
Duration m_period
Definition: GameClock.hxx:116
GameClock::Clock
std::chrono::high_resolution_clock Clock
Definition: GameClock.hxx:21
GameClock::getGameClockSpeed
float getGameClockSpeed() const
Definition: GameClock.hxx:83
GameClock::ClockTask::operator==
bool operator==(const ClockTask &task2) const
Definition: GameClock.hxx:124
GameClock::ClockCbk
std::function< bool(void)> ClockCbk
Definition: GameClock.hxx:27
GameClock::ClockTask::ClockTask
ClockTask(ClockCbk cbk, Time delay, Duration period, ClockTaskHndl hndl)
Definition: GameClock.hxx:119