World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
Time.h
См. документацию.
1 #pragma once
2 
3 #include <cstdint>
4 
5 #define TIME_QUANT 128
6 #define TIME_SECONDS_PER_QUANT 30
7 #define TIME_UNPACK_GAME_SECONDS (uint64_t)TIME_SECONDS_PER_QUANT / (double)TIME_QUANT
8 // TIME_UNPACK_GAME_SECONDS = 0.234375
9 // 30 game seconds per one time quant (128ms) [128 * 0.234375 = 30]
10 // seconds = game_time * TIME_UNPACK_GAME_SECONDS
11 #define TIME_PACK_GAME_SECONDS (uint64_t)TIME_QUANT / (double)TIME_SECONDS_PER_QUANT
12 // game_time += seconds * TIME_PACK_GAME_SECONDS
13 
14 struct GameTime {
15  GameTime() : value(0) {}
16  explicit GameTime(uint64_t val) : value(val) {}
17  GameTime(int seconds, int minutes, int hours = 0, int days = 0,
18  int weeks = 0, int months = 0, int years = 0) {
19  auto converted = (seconds + (uint64_t)60 * minutes + (uint64_t)3600 * hours + (uint64_t)86400 * days +
20  (uint64_t)604800 * weeks + (uint64_t)2419200 * months +
21  (uint64_t)29030400 * years) *
22  TIME_PACK_GAME_SECONDS;
23 
24  this->value = (uint64_t)converted;
25  }
26 
27  uint64_t GetSeconds() const {
28  return (uint64_t)(this->value * TIME_UNPACK_GAME_SECONDS);
29  }
30  uint64_t GetMinutes() const { return this->GetSeconds() / 60; }
31  uint64_t GetHours() const { return this->GetMinutes() / 60; }
32  int GetDays() const { return (int)(this->GetHours() / 24); }
33  int GetWeeks() const { return this->GetDays() / 7; }
34  int GetMonths() const { return this->GetWeeks() / 4; }
35  int GetYears() const { return this->GetMonths() / 12; }
36 
37  int GetSecondsFraction() const { return this->GetSeconds() % 60; }
38  int GetMinutesFraction() const { return (this->GetSeconds() / 60) % 60; }
39  int GetHoursOfDay() const { return (this->GetSeconds() / 3600) % 24; }
40  int GetDaysOfWeek() const { return this->GetDays() % 7; }
41  int GetDaysOfMonth() const { return this->GetDays() % 28; }
42  int GetWeeksOfMonth() const { return this->GetWeeks() % 4; }
43  int GetMonthsOfYear() const { return this->GetMonths() % 12; }
44 
45  void AddMinutes(int minutes) {
46  this->value += ((uint64_t)60 * minutes * TIME_PACK_GAME_SECONDS);
47  }
48  void SubtractHours(int hours) {
49  this->value -= ((uint64_t)60 * 60 * hours * TIME_PACK_GAME_SECONDS);
50  }
51  void AddDays(int days) {
52  this->value += ((uint64_t)60 * 60 * 24 * days * TIME_PACK_GAME_SECONDS);
53  }
54  void AddYears(int years) {
55  this->value += ((uint64_t)60 * 60 * 24 * 7 * 4 * 12 * years * TIME_PACK_GAME_SECONDS);
56  }
57 
58  void Reset() { this->value = 0; }
59  bool Valid() const { return this->value > 0; }
60 
62  return GameTime(this->value + rhs.value);
63  }
65  return GameTime(this->value - rhs.value);
66  }
68  this->value += rhs.value;
69  return *this;
70  }
71 
72  bool operator>(const GameTime &rhs) const { return this->value > rhs.value; }
73  bool operator>=(const GameTime &rhs) const { return this->value >= rhs.value; }
74  bool operator<(const GameTime &rhs) const { return this->value < rhs.value; }
75  bool operator<=(const GameTime &rhs) const { return this->value <= rhs.value; }
76 
77  explicit operator bool() {
78  return this->Valid();
79  } // unsafe bool was casuing many problems
80 
81  operator int() { return static_cast<int>(this->value); } // cast operator conversion require
82 
83  static GameTime FromSeconds(int seconds) {
84  return GameTime(seconds, 0, 0, 0, 0, 0, 0);
85  }
86  static GameTime FromMinutes(int minutes) {
87  return GameTime(0, minutes, 0, 0, 0, 0, 0);
88  }
89  static GameTime FromHours(int hours) {
90  return GameTime(0, 0, hours, 0, 0, 0, 0);
91  }
92  static GameTime FromDays(int days) {
93  return GameTime(0, 0, 0, days, 0, 0, 0);
94  }
95  static GameTime FromYears(int years) {
96  return GameTime(0, 0, 0, 0, 0, 0, years);
97  }
98 
100 };
101 
102 /* 61 */
103 #pragma pack(push, 1)
104 struct Timer {
105  static Timer *Create() { return new Timer; }
106 
107  Timer() : bReady(false), bPaused(false) {
108  bTackGameTime = 0;
109  uStartTime = 0;
110  uStopTime = 0;
111  uGameTimeStart = 0;
112  field_18 = 0;
113  uTimeElapsed = 0;
114  dt_in_some_format = 0;
116  }
117 
118  void Initialize();
119  uint64_t Time();
120  void Update();
121  void Pause();
122  void Resume();
123  void TrackGameTime();
124  void StopGameTime();
125 
126  unsigned int bReady;
127  unsigned int bPaused;
129  unsigned int uStartTime;
130  unsigned int uStopTime;
132  int field_18;
133  unsigned int uTimeElapsed;
135  unsigned int uTotalGameTimeElapsed;
136 
137  static const unsigned int Minute = 2 * TIME_QUANT;
138  static const unsigned int Hour = 60 * Minute;
139  static const unsigned int Day = 24 * Hour;
140  static const unsigned int Week = 7 * Day;
141  static const unsigned int Month = 4 * Week;
142  static const unsigned int Year = 12 * Month;
143 };
144 #pragma pack(pop)
145 
146 extern Timer *pMiscTimer;
147 extern Timer *pEventTimer;
Timer::TrackGameTime
void TrackGameTime()
Definition: Time.cpp:37
Timer::Time
uint64_t Time()
Definition: Time.cpp:11
Timer::uTimeElapsed
unsigned int uTimeElapsed
Definition: Time.h:133
Timer::Update
void Update()
Definition: Time.cpp:53
GameTime::GetDays
int GetDays() const
Definition: Time.h:32
Timer::Initialize
void Initialize()
Definition: Time.cpp:72
Timer::Resume
void Resume()
Definition: Time.cpp:27
Timer::Minute
static const unsigned int Minute
Definition: Time.h:137
pMiscTimer
Timer * pMiscTimer
Definition: Time.cpp:7
GameTime::GetDaysOfWeek
int GetDaysOfWeek() const
Definition: Time.h:40
GameTime::GetDaysOfMonth
int GetDaysOfMonth() const
Definition: Time.h:41
Timer::Create
static Timer * Create()
Definition: Time.h:105
Timer::field_18
int field_18
Definition: Time.h:132
Timer::bPaused
unsigned int bPaused
Definition: Time.h:127
int64_t
__int64 int64_t
Definition: alext.h:31
GameTime::FromYears
static GameTime FromYears(int years)
Definition: Time.h:95
GameTime::GetYears
int GetYears() const
Definition: Time.h:35
uint64_t
unsigned __int64 uint64_t
Definition: alext.h:32
GameTime::value
int64_t value
Definition: Time.h:99
Timer::bTackGameTime
int bTackGameTime
Definition: Time.h:128
GameTime::FromHours
static GameTime FromHours(int hours)
Definition: Time.h:89
GameTime::GetMinutesFraction
int GetMinutesFraction() const
Definition: Time.h:38
GameTime::Reset
void Reset()
Definition: Time.h:58
GameTime::operator+=
GameTime & operator+=(GameTime &rhs)
Definition: Time.h:67
GameTime::GetSecondsFraction
int GetSecondsFraction() const
Definition: Time.h:37
GameTime::GetWeeks
int GetWeeks() const
Definition: Time.h:33
Timer::uStopTime
unsigned int uStopTime
Definition: Time.h:130
GameTime::Valid
bool Valid() const
Definition: Time.h:59
Timer::Year
static const unsigned int Year
Definition: Time.h:142
GameTime::FromMinutes
static GameTime FromMinutes(int minutes)
Definition: Time.h:86
GameTime::GetMinutes
uint64_t GetMinutes() const
Definition: Time.h:30
Timer::dt_in_some_format
int dt_in_some_format
Definition: Time.h:134
Timer::uGameTimeStart
int uGameTimeStart
Definition: Time.h:131
Timer::Day
static const unsigned int Day
Definition: Time.h:139
GameTime::GetHoursOfDay
int GetHoursOfDay() const
Definition: Time.h:39
GameTime::GameTime
GameTime(int seconds, int minutes, int hours=0, int days=0, int weeks=0, int months=0, int years=0)
Definition: Time.h:17
Timer::Pause
void Pause()
Definition: Time.cpp:19
Timer::bReady
unsigned int bReady
Definition: Time.h:126
value
EGLSyncKHR EGLint EGLint * value
Definition: SDL_egl.h:899
GameTime::operator<
bool operator<(const GameTime &rhs) const
Definition: Time.h:74
GameTime::GetWeeksOfMonth
int GetWeeksOfMonth() const
Definition: Time.h:42
Timer::StopGameTime
void StopGameTime()
Definition: Time.cpp:45
Timer::Month
static const unsigned int Month
Definition: Time.h:141
GameTime::GetMonthsOfYear
int GetMonthsOfYear() const
Definition: Time.h:43
pEventTimer
Timer * pEventTimer
Definition: Time.cpp:8
GameTime::AddMinutes
void AddMinutes(int minutes)
Definition: Time.h:45
val
GLuint GLfloat * val
Definition: SDL_opengl_glext.h:1495
Timer
Definition: Time.h:104
GameTime::GetSeconds
uint64_t GetSeconds() const
Definition: Time.h:27
GameTime::GetMonths
int GetMonths() const
Definition: Time.h:34
Timer::Week
static const unsigned int Week
Definition: Time.h:140
GameTime::AddYears
void AddYears(int years)
Definition: Time.h:54
Timer::Hour
static const unsigned int Hour
Definition: Time.h:138
GameTime::operator-
GameTime operator-(GameTime &rhs)
Definition: Time.h:64
Timer::Timer
Timer()
Definition: Time.h:107
GameTime::FromDays
static GameTime FromDays(int days)
Definition: Time.h:92
GameTime::GetHours
uint64_t GetHours() const
Definition: Time.h:31
Timer::uStartTime
unsigned int uStartTime
Definition: Time.h:129
GameTime::SubtractHours
void SubtractHours(int hours)
Definition: Time.h:48
GameTime::operator+
GameTime operator+(GameTime &rhs)
Definition: Time.h:61
GameTime::GameTime
GameTime()
Definition: Time.h:15
GameTime::operator>
bool operator>(const GameTime &rhs) const
Definition: Time.h:72
GameTime::AddDays
void AddDays(int days)
Definition: Time.h:51
Timer::uTotalGameTimeElapsed
unsigned int uTotalGameTimeElapsed
Definition: Time.h:135
GameTime
Definition: Time.h:14
GameTime::operator>=
bool operator>=(const GameTime &rhs) const
Definition: Time.h:73
GameTime::FromSeconds
static GameTime FromSeconds(int seconds)
Definition: Time.h:83
GameTime::GameTime
GameTime(uint64_t val)
Definition: Time.h:16
GameTime::operator<=
bool operator<=(const GameTime &rhs) const
Definition: Time.h:75