From 51a707628b5538076efb5bc1237dcbafc0a9956b Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 22 May 2024 21:18:25 -0600 Subject: [PATCH] Simplify implementations of types without an empty state Now that these types have no default empty state, we can make assumptions about certain data members which allow us to skip doing certain checks. --- src/SFML/Audio/InputSoundFile.cpp | 9 ++++- src/SFML/Audio/OutputSoundFile.cpp | 6 ++- src/SFML/Graphics/Font.cpp | 33 +++++++++------- src/SFML/Graphics/Shader.cpp | 61 +++++++++++++++--------------- 4 files changed, 61 insertions(+), 48 deletions(-) diff --git a/src/SFML/Audio/InputSoundFile.cpp b/src/SFML/Audio/InputSoundFile.cpp index cc0fb3203..8d5aa44e4 100644 --- a/src/SFML/Audio/InputSoundFile.cpp +++ b/src/SFML/Audio/InputSoundFile.cpp @@ -38,6 +38,7 @@ #include #include +#include #include @@ -197,7 +198,9 @@ std::uint64_t InputSoundFile::getSampleOffset() const //////////////////////////////////////////////////////////// void InputSoundFile::seek(std::uint64_t sampleOffset) { - if (m_reader && !m_channelMap.empty()) + assert(m_reader); + + if (!m_channelMap.empty()) { // The reader handles an overrun gracefully, but we // pre-check to keep our known position consistent @@ -217,8 +220,10 @@ void InputSoundFile::seek(Time timeOffset) //////////////////////////////////////////////////////////// std::uint64_t InputSoundFile::read(std::int16_t* samples, std::uint64_t maxCount) { + assert(m_reader); + std::uint64_t readSamples = 0; - if (m_reader && samples && maxCount) + if (samples && maxCount) readSamples = m_reader->read(samples, maxCount); m_sampleOffset += readSamples; return readSamples; diff --git a/src/SFML/Audio/OutputSoundFile.cpp b/src/SFML/Audio/OutputSoundFile.cpp index b5982a50d..4ab0c81cb 100644 --- a/src/SFML/Audio/OutputSoundFile.cpp +++ b/src/SFML/Audio/OutputSoundFile.cpp @@ -29,6 +29,8 @@ #include #include +#include + namespace sf { @@ -57,7 +59,9 @@ std::optional OutputSoundFile::openFromFile( //////////////////////////////////////////////////////////// void OutputSoundFile::write(const std::int16_t* samples, std::uint64_t count) { - if (m_writer && samples && count) + assert(m_writer); + + if (samples && count) m_writer->write(samples, count); } diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 2b9eaa2f2..c052dc1e9 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -301,13 +301,13 @@ const Font::Info& Font::getInfo() const //////////////////////////////////////////////////////////// const Glyph& Font::getGlyph(std::uint32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness) const { + assert(m_fontHandles); + // Get the page corresponding to the character size GlyphTable& glyphs = loadPage(characterSize).glyphs; // Build the key by combining the glyph index (based on code point), bold flag, and outline thickness - const std::uint64_t key = combine(outlineThickness, - bold, - FT_Get_Char_Index(m_fontHandles ? m_fontHandles->face : nullptr, codePoint)); + const std::uint64_t key = combine(outlineThickness, bold, FT_Get_Char_Index(m_fontHandles->face, codePoint)); // Search the glyph into the cache if (const auto it = glyphs.find(key); it != glyphs.end()) @@ -327,18 +327,21 @@ const Glyph& Font::getGlyph(std::uint32_t codePoint, unsigned int characterSize, //////////////////////////////////////////////////////////// bool Font::hasGlyph(std::uint32_t codePoint) const { - return FT_Get_Char_Index(m_fontHandles ? m_fontHandles->face : nullptr, codePoint) != 0; + assert(m_fontHandles); + return FT_Get_Char_Index(m_fontHandles->face, codePoint) != 0; } //////////////////////////////////////////////////////////// float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold) const { + assert(m_fontHandles); + // Special case where first or second is 0 (null character) if (first == 0 || second == 0) return 0.f; - FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr; + FT_Face face = m_fontHandles->face; if (face && setCurrentSize(characterSize)) { @@ -375,9 +378,11 @@ float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int c //////////////////////////////////////////////////////////// float Font::getLineSpacing(unsigned int characterSize) const { - FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr; + assert(m_fontHandles); - if (face && setCurrentSize(characterSize)) + FT_Face face = m_fontHandles->face; + + if (setCurrentSize(characterSize)) { return static_cast(face->size->metrics.height) / static_cast(1 << 6); } @@ -391,9 +396,11 @@ float Font::getLineSpacing(unsigned int characterSize) const //////////////////////////////////////////////////////////// float Font::getUnderlinePosition(unsigned int characterSize) const { - FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr; + assert(m_fontHandles); - if (face && setCurrentSize(characterSize)) + FT_Face face = m_fontHandles->face; + + if (setCurrentSize(characterSize)) { // Return a fixed position if font is a bitmap font if (!FT_IS_SCALABLE(face)) @@ -412,7 +419,9 @@ float Font::getUnderlinePosition(unsigned int characterSize) const //////////////////////////////////////////////////////////// float Font::getUnderlineThickness(unsigned int characterSize) const { - FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr; + assert(m_fontHandles); + + FT_Face face = m_fontHandles->face; if (face && setCurrentSize(characterSize)) { @@ -470,10 +479,6 @@ Glyph Font::loadGlyph(std::uint32_t codePoint, unsigned int characterSize, bool // The glyph to return Glyph glyph; - // Stop if no font is loaded - if (!m_fontHandles) - return glyph; - // Get our FT_Face FT_Face face = m_fontHandles->face; if (!face) diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index e06f2e00d..09e5d3604 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -45,6 +45,7 @@ #include #include +#include #include #ifndef SFML_OPENGL_ES @@ -256,8 +257,8 @@ Shader& Shader::operator=(Shader&& right) noexcept { // Destroy effect program const TransientContextLock lock; - if (m_shaderProgram) - glCheck(GLEXT_glDeleteObject(castToGlHandle(m_shaderProgram))); + assert(m_shaderProgram); + glCheck(GLEXT_glDeleteObject(castToGlHandle(m_shaderProgram))); } // Move the contents of right. @@ -579,33 +580,32 @@ void Shader::setUniform(const std::string& name, const Glsl::Mat4& matrix) //////////////////////////////////////////////////////////// void Shader::setUniform(const std::string& name, const Texture& texture) { - if (m_shaderProgram) + assert(m_shaderProgram); + + const TransientContextLock lock; + + // Find the location of the variable in the shader + const int location = getUniformLocation(name); + if (location != -1) { - const TransientContextLock lock; - - // Find the location of the variable in the shader - const int location = getUniformLocation(name); - if (location != -1) + // Store the location -> texture mapping + const auto it = m_textures.find(location); + if (it == m_textures.end()) { - // Store the location -> texture mapping - const auto it = m_textures.find(location); - if (it == m_textures.end()) + // New entry, make sure there are enough texture units + if (m_textures.size() + 1 >= getMaxTextureUnits()) { - // New entry, make sure there are enough texture units - if (m_textures.size() + 1 >= getMaxTextureUnits()) - { - err() << "Impossible to use texture " << std::quoted(name) - << " for shader: all available texture units are used" << std::endl; - return; - } + err() << "Impossible to use texture " << std::quoted(name) + << " for shader: all available texture units are used" << std::endl; + return; + } - m_textures[location] = &texture; - } - else - { - // Location already used, just replace the texture - it->second = &texture; - } + m_textures[location] = &texture; + } + else + { + // Location already used, just replace the texture + it->second = &texture; } } } @@ -614,13 +614,12 @@ void Shader::setUniform(const std::string& name, const Texture& texture) //////////////////////////////////////////////////////////// void Shader::setUniform(const std::string& name, CurrentTextureType) { - if (m_shaderProgram) - { - const TransientContextLock lock; + assert(m_shaderProgram); - // Find the location of the variable in the shader - m_currentTexture = getUniformLocation(name); - } + const TransientContextLock lock; + + // Find the location of the variable in the shader + m_currentTexture = getUniformLocation(name); }