New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add templated GameTicks strong type #21868
base: develop
Are you sure you want to change the base?
Conversation
Due to complexity of integrating the strong type, for now it is left unimplemented |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments.
src/openrct2/Game.h
Outdated
{ | ||
GameTicks<T, Tag> result; | ||
result.Value = seconds * TickConversion::kGameUpdateFPS; | ||
return result; | ||
} | ||
static constexpr GameTicks FromMilliseconds(uint32_t milliseconds) | ||
{ | ||
GameTicks<T, Tag> result; | ||
result.Value = milliseconds * TickConversion::kGameUpdateFPMS; | ||
return result; | ||
} | ||
|
||
constexpr void operator=(T rhs) | ||
{ | ||
Value = rhs; | ||
} | ||
|
||
constexpr GameTicks& operator++() | ||
{ | ||
++Value; | ||
return *this; | ||
} | ||
constexpr GameTicks operator++(int) | ||
{ | ||
GameTicks<T, Tag> temp = *this; | ||
Value++; | ||
return temp; | ||
} | ||
constexpr GameTicks& operator--() | ||
{ | ||
--Value; | ||
return *this; | ||
} | ||
constexpr GameTicks operator--(int) | ||
{ | ||
GameTicks<T, Tag> temp = *this; | ||
Value--; | ||
return temp; | ||
} | ||
|
||
constexpr GameTicks operator+(GameTicks rhs) const | ||
{ | ||
return GameTicks(Value + rhs.Value); | ||
} | ||
constexpr GameTicks operator-(GameTicks rhs) const | ||
{ | ||
return GameTicks(Value - rhs.Value); | ||
} | ||
constexpr GameTicks operator*(GameTicks rhs) const | ||
{ | ||
return GameTicks(Value * rhs.Value); | ||
} | ||
constexpr GameTicks operator/(GameTicks rhs) const | ||
{ | ||
return GameTicks(Value / rhs.GameTicks); | ||
} | ||
|
||
constexpr GameTicks& operator+=(GameTicks rhs) | ||
{ | ||
Value += rhs.Value; | ||
return *this; | ||
} | ||
constexpr GameTicks& operator-=(GameTicks rhs) | ||
{ | ||
Value -= rhs.Value; | ||
return *this; | ||
} | ||
|
||
constexpr bool operator==(const GameTicks& rhs) | ||
{ | ||
return Value == rhs.Value; | ||
} | ||
constexpr bool operator<(const GameTicks& rhs) | ||
{ | ||
return Value < rhs.Value; | ||
} | ||
constexpr bool operator>(const GameTicks& rhs) | ||
{ | ||
return Value > rhs.Value; | ||
} | ||
constexpr bool operator<=(const GameTicks& rhs) | ||
{ | ||
return Value <= rhs.Value; | ||
} | ||
constexpr bool operator>=(const GameTicks& rhs) | ||
{ | ||
return Value >= rhs.Value; | ||
} | ||
constexpr bool operator!=(const GameTicks& rhs) | ||
{ | ||
return Value != rhs.Value; | ||
} | ||
|
||
constexpr GameTicks& operator%(const GameTicks& rhs) | ||
{ | ||
GameTicks result; | ||
result.Value = Value % rhs.Value; | ||
return result; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its better to move all of this into a new header file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also would like to see some tests added for this new logic, before implementing it we should ensure it actually works the way we expect.
src/openrct2/Game.h
Outdated
GameTicks<T, Tag> result; | ||
result.Value = seconds * TickConversion::kGameUpdateFPS; | ||
return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GameTicks<T, Tag> result; | |
result.Value = seconds * TickConversion::kGameUpdateFPS; | |
return result; | |
return FromMilliseconds(seconds * 1000); |
We don't need to implement this twice.
src/openrct2/Game.h
Outdated
namespace TickConversion | ||
{ | ||
constexpr uint32_t kGameUpdateFPS = 40; | ||
constexpr float kGameUpdateFPMS = kGameUpdateFPS / 1000.0f; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need floats, the value should be milliseconds as integer, so this should result in 25, 1000 / 40 = 25
constexpr float kGameUpdateFPMS = kGameUpdateFPS / 1000.0f; | |
constexpr float kGameUpdateFPMS = kGameUpdateFPS / 1000.0f; |
src/openrct2/Game.h
Outdated
static constexpr GameTicks FromMilliseconds(uint32_t milliseconds) | ||
{ | ||
GameTicks<T, Tag> result; | ||
result.Value = milliseconds * TickConversion::kGameUpdateFPMS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to use floating point math here, see my remark on kGameUpdateFPMS
. We should also round it because 1 millisecond would result to 0 otherwise, in this case we have to represent a single tick starting at 0 ms to 25 ms. The formula should be something like:
result.Value = milliseconds * TickConversion::kGameUpdateFPMS; | |
result.Value = (milliseconds + (TickConversion::kGameUpdateTimeMs - 1)) / TickConversion::kGameUpdateTimeMs /* 25 */; |
src/openrct2/Game.h
Outdated
constexpr bool operator==(const GameTicks& rhs) | ||
{ | ||
return Value == rhs.Value; | ||
} | ||
constexpr bool operator<(const GameTicks& rhs) | ||
{ | ||
return Value < rhs.Value; | ||
} | ||
constexpr bool operator>(const GameTicks& rhs) | ||
{ | ||
return Value > rhs.Value; | ||
} | ||
constexpr bool operator<=(const GameTicks& rhs) | ||
{ | ||
return Value <= rhs.Value; | ||
} | ||
constexpr bool operator>=(const GameTicks& rhs) | ||
{ | ||
return Value >= rhs.Value; | ||
} | ||
constexpr bool operator!=(const GameTicks& rhs) | ||
{ | ||
return Value != rhs.Value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use C++20 now so we can probably use the spaceship operator for all of this which is just a single function then.
For #21395, Create a new strong type for game ticks