From 949e7aecae1101348308433ab3758f85ca5e2b9e Mon Sep 17 00:00:00 2001 From: binary1248 Date: Wed, 21 Mar 2018 22:14:06 +0100 Subject: [PATCH] Fixed strict aliasing punning warning when generating the key of a glyph in Font.cpp. Fixes #1187 --- src/SFML/Graphics/Font.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index e487e0732..45f6baf87 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -61,6 +61,21 @@ namespace void close(FT_Stream) { } + + // Helper to intepret memory as a specific type + template + inline T reinterpret(const U& input) + { + T output; + std::memcpy(&output, &input, sizeof(U)); + return output; + } + + // Combine outline thickness, boldness and codepoint into a single 64-bit key + sf::Uint64 combine(float outlineThickness, bool bold, sf::Uint32 codePoint) + { + return (static_cast(reinterpret(outlineThickness)) << 32) | (static_cast(bold) << 31) | codePoint; + } } @@ -332,9 +347,7 @@ const Glyph& Font::getGlyph(Uint32 codePoint, unsigned int characterSize, bool b GlyphTable& glyphs = m_pages[characterSize].glyphs; // Build the key by combining the code point, bold flag, and outline thickness - Uint64 key = (static_cast(*reinterpret_cast(&outlineThickness)) << 32) - | (static_cast(bold ? 1 : 0) << 31) - | static_cast(codePoint); + Uint64 key = combine(outlineThickness, bold, codePoint); // Search the glyph into the cache GlyphTable::const_iterator it = glyphs.find(key);