Skip to content
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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

ncerroneumich
Copy link
Contributor

For #21395, Create a new strong type for game ticks

@ncerroneumich
Copy link
Contributor Author

Due to complexity of integrating the strong type, for now it is left unimplemented

Copy link
Member

@ZehMatt ZehMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments.

Comment on lines 21 to 137
{
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;
}
};
Copy link
Member

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.

Copy link
Member

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.

Comment on lines 39 to 41
GameTicks<T, Tag> result;
result.Value = seconds * TickConversion::kGameUpdateFPS;
return result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GameTicks<T, Tag> result;
result.Value = seconds * TickConversion::kGameUpdateFPS;
return result;
return FromMilliseconds(seconds * 1000);

We don't need to implement this twice.

namespace TickConversion
{
constexpr uint32_t kGameUpdateFPS = 40;
constexpr float kGameUpdateFPMS = kGameUpdateFPS / 1000.0f;
Copy link
Member

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

Suggested change
constexpr float kGameUpdateFPMS = kGameUpdateFPS / 1000.0f;
constexpr float kGameUpdateFPMS = kGameUpdateFPS / 1000.0f;

static constexpr GameTicks FromMilliseconds(uint32_t milliseconds)
{
GameTicks<T, Tag> result;
result.Value = milliseconds * TickConversion::kGameUpdateFPMS;
Copy link
Member

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:

Suggested change
result.Value = milliseconds * TickConversion::kGameUpdateFPMS;
result.Value = (milliseconds + (TickConversion::kGameUpdateTimeMs - 1)) / TickConversion::kGameUpdateTimeMs /* 25 */;

Comment on lines 106 to 129
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;
}
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants