Remove caching from sf::Transformable and sf::View

This reduces the size of both classes by 130 bytes which has positive
impacts on cache locality. This change also removes all the internal
invariants from both classes allowing them to be converted to
aggregate types in the future.
This commit is contained in:
Chris Thrasher 2024-12-25 19:38:23 -06:00
parent dab1800f61
commit 4c685666c5
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C
4 changed files with 54 additions and 107 deletions

View File

@ -214,7 +214,7 @@ public:
/// \see `getInverseTransform`
///
////////////////////////////////////////////////////////////
[[nodiscard]] const Transform& getTransform() const;
[[nodiscard]] Transform getTransform() const;
////////////////////////////////////////////////////////////
/// \brief get the inverse of the combined transform of the object
@ -224,7 +224,7 @@ public:
/// \see `getTransform`
///
////////////////////////////////////////////////////////////
[[nodiscard]] const Transform& getInverseTransform() const;
[[nodiscard]] Transform getInverseTransform() const;
private:
////////////////////////////////////////////////////////////
@ -234,10 +234,6 @@ private:
Vector2f m_position; //!< Position of the object in the 2D world
Angle m_rotation; //!< Orientation of the object
Vector2f m_scale{1, 1}; //!< Scale of the object
mutable Transform m_transform; //!< Combined transformation of the object
mutable Transform m_inverseTransform; //!< Combined transformation of the object
mutable bool m_transformNeedUpdate{true}; //!< Does the transform need to be recomputed?
mutable bool m_inverseTransformNeedUpdate{true}; //!< Does the transform need to be recomputed?
};
} // namespace sf

View File

@ -240,7 +240,7 @@ public:
/// \see `getInverseTransform`
///
////////////////////////////////////////////////////////////
[[nodiscard]] const Transform& getTransform() const;
[[nodiscard]] Transform getTransform() const;
////////////////////////////////////////////////////////////
/// \brief Get the inverse projection transform of the view
@ -252,7 +252,7 @@ public:
/// \see `getTransform`
///
////////////////////////////////////////////////////////////
[[nodiscard]] const Transform& getInverseTransform() const;
[[nodiscard]] Transform getInverseTransform() const;
private:
////////////////////////////////////////////////////////////
@ -263,10 +263,6 @@ private:
Angle m_rotation; //!< Angle of rotation of the view rectangle
FloatRect m_viewport{{0, 0}, {1, 1}}; //!< Viewport rectangle, expressed as a factor of the render-target's size
FloatRect m_scissor{{0, 0}, {1, 1}}; //!< Scissor rectangle, expressed as a factor of the render-target's size
mutable Transform m_transform; //!< Precomputed projection transform corresponding to the view
mutable Transform m_inverseTransform; //!< Precomputed inverse projection transform corresponding to the view
mutable bool m_transformUpdated{}; //!< Internal state telling if the transform needs to be updated
mutable bool m_invTransformUpdated{}; //!< Internal state telling if the inverse transform needs to be updated
};
} // namespace sf

View File

@ -36,8 +36,6 @@ namespace sf
void Transformable::setPosition(Vector2f position)
{
m_position = position;
m_transformNeedUpdate = true;
m_inverseTransformNeedUpdate = true;
}
@ -45,9 +43,6 @@ void Transformable::setPosition(Vector2f position)
void Transformable::setRotation(Angle angle)
{
m_rotation = angle.wrapUnsigned();
m_transformNeedUpdate = true;
m_inverseTransformNeedUpdate = true;
}
@ -55,8 +50,6 @@ void Transformable::setRotation(Angle angle)
void Transformable::setScale(Vector2f factors)
{
m_scale = factors;
m_transformNeedUpdate = true;
m_inverseTransformNeedUpdate = true;
}
@ -64,8 +57,6 @@ void Transformable::setScale(Vector2f factors)
void Transformable::setOrigin(Vector2f origin)
{
m_origin = origin;
m_transformNeedUpdate = true;
m_inverseTransformNeedUpdate = true;
}
@ -119,11 +110,8 @@ void Transformable::scale(Vector2f factor)
////////////////////////////////////////////////////////////
const Transform& Transformable::getTransform() const
Transform Transformable::getTransform() const
{
// Recompute the combined transform if needed
if (m_transformNeedUpdate)
{
const float angle = -m_rotation.asRadians();
const float cosine = std::cos(angle);
const float sine = std::sin(angle);
@ -135,28 +123,17 @@ const Transform& Transformable::getTransform() const
const float ty = m_origin.x * sxs - m_origin.y * syc + m_position.y;
// clang-format off
m_transform = Transform( sxc, sys, tx,
return { sxc, sys, tx,
-sxs, syc, ty,
0.f, 0.f, 1.f);
0.f, 0.f, 1.f};
// clang-format on
m_transformNeedUpdate = false;
}
return m_transform;
}
////////////////////////////////////////////////////////////
const Transform& Transformable::getInverseTransform() const
Transform Transformable::getInverseTransform() const
{
// Recompute the inverse transform if needed
if (m_inverseTransformNeedUpdate)
{
m_inverseTransform = getTransform().getInverse();
m_inverseTransformNeedUpdate = false;
}
return m_inverseTransform;
return getTransform().getInverse();
}
} // namespace sf

View File

@ -49,8 +49,6 @@ View::View(Vector2f center, Vector2f size) : m_center(center), m_size(size)
void View::setCenter(Vector2f center)
{
m_center = center;
m_transformUpdated = false;
m_invTransformUpdated = false;
}
@ -58,9 +56,6 @@ void View::setCenter(Vector2f center)
void View::setSize(Vector2f size)
{
m_size = size;
m_transformUpdated = false;
m_invTransformUpdated = false;
}
@ -68,9 +63,6 @@ void View::setSize(Vector2f size)
void View::setRotation(Angle angle)
{
m_rotation = angle.wrapUnsigned();
m_transformUpdated = false;
m_invTransformUpdated = false;
}
@ -152,11 +144,8 @@ void View::zoom(float factor)
////////////////////////////////////////////////////////////
const Transform& View::getTransform() const
Transform View::getTransform() const
{
// Recompute the matrix if needed
if (!m_transformUpdated)
{
// Rotation components
const float angle = m_rotation.asRadians();
const float cosine = std::cos(angle);
@ -172,28 +161,17 @@ const Transform& View::getTransform() const
// Rebuild the projection matrix
// clang-format off
m_transform = Transform( a * cosine, a * sine, a * tx + c,
return { a * cosine, a * sine, a * tx + c,
-b * sine, b * cosine, b * ty + d,
0.f, 0.f, 1.f);
0.f, 0.f, 1.f};
// clang-format on
m_transformUpdated = true;
}
return m_transform;
}
////////////////////////////////////////////////////////////
const Transform& View::getInverseTransform() const
Transform View::getInverseTransform() const
{
// Recompute the matrix if needed
if (!m_invTransformUpdated)
{
m_inverseTransform = getTransform().getInverse();
m_invTransformUpdated = true;
}
return m_inverseTransform;
return getTransform().getInverse();
}
} // namespace sf