Skip to content

Commit

Permalink
Add equality comparison to sf::Transform and avoid uploading 64 bytes…
Browse files Browse the repository at this point in the history
… of data every time we want to reset the OpenGL matrix back to identity.
  • Loading branch information
binary1248 committed Oct 1, 2017
1 parent 2aa70de commit 6e0d213
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
29 changes: 29 additions & 0 deletions include/SFML/Graphics/Transform.hpp
Expand Up @@ -403,6 +403,35 @@ SFML_GRAPHICS_API Transform& operator *=(Transform& left, const Transform& right
////////////////////////////////////////////////////////////
SFML_GRAPHICS_API Vector2f operator *(const Transform& left, const Vector2f& right);

////////////////////////////////////////////////////////////
/// \relates sf::Transform
/// \brief Overload of binary operator == to compare two transforms
///
/// Performs an element-wise comparison of the elements of the
/// left transform with the elements of the right transform.
///
/// \param left Left operand (the first transform)
/// \param right Right operand (the second transform)
///
/// \return true if the transforms are equal, false otherwise
///
////////////////////////////////////////////////////////////
SFML_GRAPHICS_API bool operator ==(const Transform& left, const Transform& right);

////////////////////////////////////////////////////////////
/// \relates sf::Transform
/// \brief Overload of binary operator != to compare two transforms
///
/// This call is equivalent to !(left == right).
///
/// \param left Left operand (the first transform)
/// \param right Right operand (the second transform)
///
/// \return true if the transforms are not equal, false otherwise
///
////////////////////////////////////////////////////////////
SFML_GRAPHICS_API bool operator !=(const Transform& left, const Transform& right);

} // namespace sf


Expand Down
9 changes: 6 additions & 3 deletions src/SFML/Graphics/RenderTarget.cpp
Expand Up @@ -235,7 +235,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount,

// Since vertices are transformed, we must use an identity transform to render them
if (!m_cache.useVertexCache)
applyTransform(Transform::Identity);
glCheck(glLoadIdentity());
}
else
{
Expand Down Expand Up @@ -390,14 +390,14 @@ void RenderTarget::resetGLStates()
glCheck(glEnable(GL_TEXTURE_2D));
glCheck(glEnable(GL_BLEND));
glCheck(glMatrixMode(GL_MODELVIEW));
glCheck(glLoadIdentity());
glCheck(glEnableClientState(GL_VERTEX_ARRAY));
glCheck(glEnableClientState(GL_COLOR_ARRAY));
glCheck(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
m_cache.glStatesSet = true;

// Apply the default SFML states
applyBlendMode(BlendAlpha);
applyTransform(Transform::Identity);
applyTexture(NULL);
if (shaderAvailable)
applyShader(NULL);
Expand Down Expand Up @@ -496,7 +496,10 @@ void RenderTarget::applyTransform(const Transform& transform)
{
// No need to call glMatrixMode(GL_MODELVIEW), it is always the
// current mode (for optimization purpose, since it's the most used)
glCheck(glLoadMatrixf(transform.getMatrix()));
if (transform == Transform::Identity)
glCheck(glLoadIdentity());
else
glCheck(glLoadMatrixf(transform.getMatrix()));
}


Expand Down
20 changes: 20 additions & 0 deletions src/SFML/Graphics/Transform.cpp
Expand Up @@ -26,6 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Transform.hpp>
#include <algorithm>
#include <cmath>


Expand Down Expand Up @@ -269,4 +270,23 @@ Vector2f operator *(const Transform& left, const Vector2f& right)
return left.transformPoint(right);
}


////////////////////////////////////////////////////////////
bool operator ==(const Transform& left, const Transform& right)
{
const float* a = left.getMatrix();
const float* b = right.getMatrix();

return ((a[0] == b[0]) && (a[1] == b[1]) && (a[3] == b[3]) &&
(a[4] == b[4]) && (a[5] == b[5]) && (a[7] == b[7]) &&
(a[12] == b[12]) && (a[13] == b[13]) && (a[15] == b[15]));
}


////////////////////////////////////////////////////////////
bool operator !=(const Transform& left, const Transform& right)
{
return !(left == right);
}

} // namespace sf

0 comments on commit 6e0d213

Please sign in to comment.