Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed strict aliasing punning warning when generating the key of a glyph in Font.cpp #1396

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/SFML/Graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ namespace
void close(FT_Stream)
{
}

// Helper to intepret memory as a specific type
template <typename T, typename U>
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<sf::Uint64>(reinterpret<sf::Uint32>(outlineThickness)) << 32) | (static_cast<sf::Uint64>(bold) << 31) | codePoint;
}
}


Expand Down Expand Up @@ -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<Uint64>(*reinterpret_cast<Uint32*>(&outlineThickness)) << 32)
| (static_cast<Uint64>(bold ? 1 : 0) << 31)
| static_cast<Uint64>(codePoint);
Uint64 key = combine(outlineThickness, bold, codePoint);

// Search the glyph into the cache
GlyphTable::const_iterator it = glyphs.find(key);
Expand Down