Add equality comparison to sf::Transform and avoid uploading 64 bytes of data every time we want to reset the OpenGL matrix back to identity.

This commit is contained in:
binary1248 2017-10-01 18:16:41 +02:00 committed by Lukas Dürrenberger
parent 516678fe1f
commit 898c2350f7
3 changed files with 54 additions and 3 deletions

View File

@ -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

View File

@ -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
{
@ -393,6 +393,7 @@ 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));
@ -400,7 +401,6 @@ void RenderTarget::resetGLStates()
// Apply the default SFML states
applyBlendMode(BlendAlpha);
applyTransform(Transform::Identity);
applyTexture(NULL);
if (shaderAvailable)
applyShader(NULL);
@ -499,7 +499,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()));
}

View File

@ -269,4 +269,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