Skip to content

Commit

Permalink
Simplify implementations of types without an empty state
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChrisThrasher committed May 24, 2024
1 parent eb41707 commit 0d29d3d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 48 deletions.
9 changes: 7 additions & 2 deletions src/SFML/Audio/InputSoundFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <ostream>
#include <utility>

#include <cassert>
#include <cstdint>


Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/SFML/Audio/OutputSoundFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <SFML/Audio/SoundFileFactory.hpp>
#include <SFML/Audio/SoundFileWriter.hpp>

#include <cassert>


namespace sf
{
Expand Down Expand Up @@ -57,7 +59,9 @@ std::optional<OutputSoundFile> 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);
}

Expand Down
33 changes: 19 additions & 14 deletions src/SFML/Graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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))
{
Expand Down Expand Up @@ -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<float>(face->size->metrics.height) / static_cast<float>(1 << 6);
}
Expand All @@ -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))
Expand All @@ -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))
{
Expand Down Expand Up @@ -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)
Expand Down
61 changes: 30 additions & 31 deletions src/SFML/Graphics/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <utility>
#include <vector>

#include <cassert>
#include <cstdint>

#ifndef SFML_OPENGL_ES
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
{
const TransientContextLock lock;
assert(m_shaderProgram);

// 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())
{
err() << "Impossible to use texture " << std::quoted(name)
<< " for shader: all available texture units are used" << std::endl;
return;
}

m_textures[location] = &texture;
}
else
// New entry, make sure there are enough texture units
if (m_textures.size() + 1 >= getMaxTextureUnits())
{
// Location already used, just replace the texture
it->second = &texture;
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;
}
}
}
Expand All @@ -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);
}


Expand Down

0 comments on commit 0d29d3d

Please sign in to comment.