mirror of
https://github.com/SFML/SFML.git
synced 2025-01-18 23:35:11 +08:00
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:
parent
dab1800f61
commit
4c685666c5
@ -214,7 +214,7 @@ public:
|
|||||||
/// \see `getInverseTransform`
|
/// \see `getInverseTransform`
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] const Transform& getTransform() const;
|
[[nodiscard]] Transform getTransform() const;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief get the inverse of the combined transform of the object
|
/// \brief get the inverse of the combined transform of the object
|
||||||
@ -224,20 +224,16 @@ public:
|
|||||||
/// \see `getTransform`
|
/// \see `getTransform`
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] const Transform& getInverseTransform() const;
|
[[nodiscard]] Transform getInverseTransform() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Vector2f m_origin; //!< Origin of translation/rotation/scaling of the object
|
Vector2f m_origin; //!< Origin of translation/rotation/scaling of the object
|
||||||
Vector2f m_position; //!< Position of the object in the 2D world
|
Vector2f m_position; //!< Position of the object in the 2D world
|
||||||
Angle m_rotation; //!< Orientation of the object
|
Angle m_rotation; //!< Orientation of the object
|
||||||
Vector2f m_scale{1, 1}; //!< Scale 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
|
} // namespace sf
|
||||||
|
@ -240,7 +240,7 @@ public:
|
|||||||
/// \see `getInverseTransform`
|
/// \see `getInverseTransform`
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] const Transform& getTransform() const;
|
[[nodiscard]] Transform getTransform() const;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Get the inverse projection transform of the view
|
/// \brief Get the inverse projection transform of the view
|
||||||
@ -252,21 +252,17 @@ public:
|
|||||||
/// \see `getTransform`
|
/// \see `getTransform`
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] const Transform& getInverseTransform() const;
|
[[nodiscard]] Transform getInverseTransform() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Vector2f m_center{500, 500}; //!< Center of the view, in scene coordinates
|
Vector2f m_center{500, 500}; //!< Center of the view, in scene coordinates
|
||||||
Vector2f m_size{1000, 1000}; //!< Size of the view, in scene coordinates
|
Vector2f m_size{1000, 1000}; //!< Size of the view, in scene coordinates
|
||||||
Angle m_rotation; //!< Angle of rotation of the view rectangle
|
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_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
|
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
|
} // namespace sf
|
||||||
|
@ -35,9 +35,7 @@ namespace sf
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Transformable::setPosition(Vector2f position)
|
void Transformable::setPosition(Vector2f position)
|
||||||
{
|
{
|
||||||
m_position = position;
|
m_position = position;
|
||||||
m_transformNeedUpdate = true;
|
|
||||||
m_inverseTransformNeedUpdate = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -45,27 +43,20 @@ void Transformable::setPosition(Vector2f position)
|
|||||||
void Transformable::setRotation(Angle angle)
|
void Transformable::setRotation(Angle angle)
|
||||||
{
|
{
|
||||||
m_rotation = angle.wrapUnsigned();
|
m_rotation = angle.wrapUnsigned();
|
||||||
|
|
||||||
m_transformNeedUpdate = true;
|
|
||||||
m_inverseTransformNeedUpdate = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Transformable::setScale(Vector2f factors)
|
void Transformable::setScale(Vector2f factors)
|
||||||
{
|
{
|
||||||
m_scale = factors;
|
m_scale = factors;
|
||||||
m_transformNeedUpdate = true;
|
|
||||||
m_inverseTransformNeedUpdate = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Transformable::setOrigin(Vector2f origin)
|
void Transformable::setOrigin(Vector2f origin)
|
||||||
{
|
{
|
||||||
m_origin = origin;
|
m_origin = origin;
|
||||||
m_transformNeedUpdate = true;
|
|
||||||
m_inverseTransformNeedUpdate = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -119,44 +110,30 @@ void Transformable::scale(Vector2f factor)
|
|||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const Transform& Transformable::getTransform() const
|
Transform Transformable::getTransform() const
|
||||||
{
|
{
|
||||||
// Recompute the combined transform if needed
|
const float angle = -m_rotation.asRadians();
|
||||||
if (m_transformNeedUpdate)
|
const float cosine = std::cos(angle);
|
||||||
{
|
const float sine = std::sin(angle);
|
||||||
const float angle = -m_rotation.asRadians();
|
const float sxc = m_scale.x * cosine;
|
||||||
const float cosine = std::cos(angle);
|
const float syc = m_scale.y * cosine;
|
||||||
const float sine = std::sin(angle);
|
const float sxs = m_scale.x * sine;
|
||||||
const float sxc = m_scale.x * cosine;
|
const float sys = m_scale.y * sine;
|
||||||
const float syc = m_scale.y * cosine;
|
const float tx = -m_origin.x * sxc - m_origin.y * sys + m_position.x;
|
||||||
const float sxs = m_scale.x * sine;
|
const float ty = m_origin.x * sxs - m_origin.y * syc + m_position.y;
|
||||||
const float sys = m_scale.y * sine;
|
|
||||||
const float tx = -m_origin.x * sxc - m_origin.y * sys + m_position.x;
|
|
||||||
const float ty = m_origin.x * sxs - m_origin.y * syc + m_position.y;
|
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
m_transform = Transform( sxc, sys, tx,
|
return { sxc, sys, tx,
|
||||||
-sxs, syc, ty,
|
-sxs, syc, ty,
|
||||||
0.f, 0.f, 1.f);
|
0.f, 0.f, 1.f};
|
||||||
// clang-format on
|
// clang-format on
|
||||||
m_transformNeedUpdate = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_transform;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const Transform& Transformable::getInverseTransform() const
|
Transform Transformable::getInverseTransform() const
|
||||||
{
|
{
|
||||||
// Recompute the inverse transform if needed
|
return getTransform().getInverse();
|
||||||
if (m_inverseTransformNeedUpdate)
|
|
||||||
{
|
|
||||||
m_inverseTransform = getTransform().getInverse();
|
|
||||||
m_inverseTransformNeedUpdate = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_inverseTransform;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
@ -48,9 +48,7 @@ View::View(Vector2f center, Vector2f size) : m_center(center), m_size(size)
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void View::setCenter(Vector2f center)
|
void View::setCenter(Vector2f center)
|
||||||
{
|
{
|
||||||
m_center = center;
|
m_center = center;
|
||||||
m_transformUpdated = false;
|
|
||||||
m_invTransformUpdated = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -58,9 +56,6 @@ void View::setCenter(Vector2f center)
|
|||||||
void View::setSize(Vector2f size)
|
void View::setSize(Vector2f size)
|
||||||
{
|
{
|
||||||
m_size = size;
|
m_size = size;
|
||||||
|
|
||||||
m_transformUpdated = false;
|
|
||||||
m_invTransformUpdated = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,9 +63,6 @@ void View::setSize(Vector2f size)
|
|||||||
void View::setRotation(Angle angle)
|
void View::setRotation(Angle angle)
|
||||||
{
|
{
|
||||||
m_rotation = angle.wrapUnsigned();
|
m_rotation = angle.wrapUnsigned();
|
||||||
|
|
||||||
m_transformUpdated = false;
|
|
||||||
m_invTransformUpdated = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -152,48 +144,34 @@ void View::zoom(float factor)
|
|||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const Transform& View::getTransform() const
|
Transform View::getTransform() const
|
||||||
{
|
{
|
||||||
// Recompute the matrix if needed
|
// Rotation components
|
||||||
if (!m_transformUpdated)
|
const float angle = m_rotation.asRadians();
|
||||||
{
|
const float cosine = std::cos(angle);
|
||||||
// Rotation components
|
const float sine = std::sin(angle);
|
||||||
const float angle = m_rotation.asRadians();
|
const float tx = -m_center.x * cosine - m_center.y * sine + m_center.x;
|
||||||
const float cosine = std::cos(angle);
|
const float ty = m_center.x * sine - m_center.y * cosine + m_center.y;
|
||||||
const float sine = std::sin(angle);
|
|
||||||
const float tx = -m_center.x * cosine - m_center.y * sine + m_center.x;
|
|
||||||
const float ty = m_center.x * sine - m_center.y * cosine + m_center.y;
|
|
||||||
|
|
||||||
// Projection components
|
// Projection components
|
||||||
const float a = 2.f / m_size.x;
|
const float a = 2.f / m_size.x;
|
||||||
const float b = -2.f / m_size.y;
|
const float b = -2.f / m_size.y;
|
||||||
const float c = -a * m_center.x;
|
const float c = -a * m_center.x;
|
||||||
const float d = -b * m_center.y;
|
const float d = -b * m_center.y;
|
||||||
|
|
||||||
// Rebuild the projection matrix
|
// Rebuild the projection matrix
|
||||||
// clang-format off
|
// 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,
|
-b * sine, b * cosine, b * ty + d,
|
||||||
0.f, 0.f, 1.f);
|
0.f, 0.f, 1.f};
|
||||||
// clang-format on
|
// clang-format on
|
||||||
m_transformUpdated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_transform;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const Transform& View::getInverseTransform() const
|
Transform View::getInverseTransform() const
|
||||||
{
|
{
|
||||||
// Recompute the matrix if needed
|
return getTransform().getInverse();
|
||||||
if (!m_invTransformUpdated)
|
|
||||||
{
|
|
||||||
m_inverseTransform = getTransform().getInverse();
|
|
||||||
m_invTransformUpdated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_inverseTransform;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
Loading…
Reference in New Issue
Block a user