From 359fe9088ceb372b8e3bcadb181cf34c903f3afd Mon Sep 17 00:00:00 2001 From: binary1248 Date: Tue, 15 Feb 2022 18:43:18 +0100 Subject: [PATCH] Fixed warnings reported by LGTM and Coverity Scan. --- examples/shader/Effect.hpp | 4 +-- examples/shader/Shader.cpp | 40 +++++++++++++---------- include/SFML/Graphics/Drawable.hpp | 9 ++--- include/SFML/Graphics/Glyph.hpp | 2 +- include/SFML/Graphics/Shape.hpp | 2 +- include/SFML/Graphics/Sprite.hpp | 2 +- include/SFML/Graphics/Text.hpp | 2 +- include/SFML/Graphics/Transformable.hpp | 7 ++-- include/SFML/Graphics/VertexArray.hpp | 2 +- include/SFML/Graphics/VertexBuffer.hpp | 2 +- src/SFML/Audio/Music.cpp | 2 +- src/SFML/Audio/SoundRecorder.cpp | 2 +- src/SFML/Audio/SoundStream.cpp | 13 ++++++-- src/SFML/Graphics/Font.cpp | 2 +- src/SFML/Graphics/Image.cpp | 2 +- src/SFML/Graphics/Shape.cpp | 14 ++++---- src/SFML/Graphics/Sprite.cpp | 10 +++--- src/SFML/Graphics/Text.cpp | 12 ++++--- src/SFML/Graphics/Texture.cpp | 4 +-- src/SFML/Graphics/VertexArray.cpp | 2 +- src/SFML/Graphics/VertexBuffer.cpp | 2 +- src/SFML/Window/Unix/CursorImpl.cpp | 2 +- src/SFML/Window/Unix/VulkanImplX11.cpp | 5 ++- src/SFML/Window/Unix/WindowImplX11.cpp | 6 ++-- src/SFML/Window/Win32/VulkanImplWin32.cpp | 5 ++- src/SFML/Window/WindowBase.cpp | 2 +- test/TestUtilities/GraphicsUtil.hpp | 2 +- 27 files changed, 93 insertions(+), 66 deletions(-) diff --git a/examples/shader/Effect.hpp b/examples/shader/Effect.hpp index 20bd1f38..9571604f 100644 --- a/examples/shader/Effect.hpp +++ b/examples/shader/Effect.hpp @@ -41,7 +41,7 @@ public: onUpdate(time, x, y); } - void draw(sf::RenderTarget& target, sf::RenderStates states) const override + void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override { if (m_isLoaded) { @@ -75,7 +75,7 @@ private: // Virtual functions to be implemented in derived effects virtual bool onLoad() = 0; virtual void onUpdate(float time, float x, float y) = 0; - virtual void onDraw(sf::RenderTarget& target, sf::RenderStates states) const = 0; + virtual void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const = 0; private: diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index f979f1c4..1a4250f6 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -41,10 +41,11 @@ public: m_shader.setUniform("pixel_threshold", (x + y) / 30); } - void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override + void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override { - states.shader = &m_shader; - target.draw(m_sprite, states); + sf::RenderStates statesCopy(states); + statesCopy.shader = &m_shader; + target.draw(m_sprite, statesCopy); } private: @@ -106,10 +107,11 @@ public: m_shader.setUniform("blur_radius", (x + y) * 0.008f); } - void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override + void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override { - states.shader = &m_shader; - target.draw(m_text, states); + sf::RenderStates statesCopy(states); + statesCopy.shader = &m_shader; + target.draw(m_text, statesCopy); } private: @@ -161,10 +163,11 @@ public: m_shader.setUniform("blink_alpha", 0.5f + std::cos(time * 3) * 0.25f); } - void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override + void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override { - states.shader = &m_shader; - target.draw(m_points, states); + sf::RenderStates statesCopy(states); + statesCopy.shader = &m_shader; + target.draw(m_points, statesCopy); } private: @@ -241,10 +244,11 @@ public: m_surface.display(); } - void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override + void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override { - states.shader = &m_shader; - target.draw(sf::Sprite(m_surface.getTexture()), states); + sf::RenderStates statesCopy(states); + statesCopy.shader = &m_shader; + target.draw(sf::Sprite(m_surface.getTexture()), statesCopy); } private: @@ -317,15 +321,17 @@ public: m_shader.setUniform("size", sf::Vector2f(size, size)); } - void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override + void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override { + sf::RenderStates statesCopy(states); + // Prepare the render state - states.shader = &m_shader; - states.texture = &m_logoTexture; - states.transform = m_transform; + statesCopy.shader = &m_shader; + statesCopy.texture = &m_logoTexture; + statesCopy.transform = m_transform; // Draw the point cloud - target.draw(m_pointCloud, states); + target.draw(m_pointCloud, statesCopy); } private: diff --git a/include/SFML/Graphics/Drawable.hpp b/include/SFML/Graphics/Drawable.hpp index 1ebcfbb9..5c1ca736 100644 --- a/include/SFML/Graphics/Drawable.hpp +++ b/include/SFML/Graphics/Drawable.hpp @@ -66,7 +66,7 @@ protected: /// \param states Current render states /// //////////////////////////////////////////////////////////// - virtual void draw(RenderTarget& target, RenderStates states) const = 0; + virtual void draw(RenderTarget& target, const RenderStates& states) const = 0; }; } // namespace sf @@ -100,14 +100,15 @@ protected: /// /// private: /// -/// void draw(sf::RenderTarget& target, sf::RenderStates states) const override +/// void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override /// { /// // You can draw other high-level objects /// target.draw(m_sprite, states); /// /// // ... or use the low-level API -/// states.texture = &m_texture; -/// target.draw(m_vertices, states); +/// sf::RenderStates statesCopy(states); +/// statesCopy.texture = &m_texture; +/// target.draw(m_vertices, statesCopy); /// /// // ... or draw with OpenGL directly /// glBegin(GL_TRIANGLES); diff --git a/include/SFML/Graphics/Glyph.hpp b/include/SFML/Graphics/Glyph.hpp index 32e45bc6..3160c6e2 100644 --- a/include/SFML/Graphics/Glyph.hpp +++ b/include/SFML/Graphics/Glyph.hpp @@ -46,7 +46,7 @@ public: /// \brief Default constructor /// //////////////////////////////////////////////////////////// - Glyph() : advance(0) {} + Glyph() : advance(0), lsbDelta(0), rsbDelta(0) {} //////////////////////////////////////////////////////////// // Member data diff --git a/include/SFML/Graphics/Shape.hpp b/include/SFML/Graphics/Shape.hpp index b2434ef8..895ff5c3 100644 --- a/include/SFML/Graphics/Shape.hpp +++ b/include/SFML/Graphics/Shape.hpp @@ -276,7 +276,7 @@ private: /// \param states Current render states /// //////////////////////////////////////////////////////////// - void draw(RenderTarget& target, RenderStates states) const override; + void draw(RenderTarget& target, const RenderStates& states) const override; //////////////////////////////////////////////////////////// /// \brief Update the fill vertices' color diff --git a/include/SFML/Graphics/Sprite.hpp b/include/SFML/Graphics/Sprite.hpp index deb60b19..6df99c2c 100644 --- a/include/SFML/Graphics/Sprite.hpp +++ b/include/SFML/Graphics/Sprite.hpp @@ -198,7 +198,7 @@ private: /// \param states Current render states /// //////////////////////////////////////////////////////////// - void draw(RenderTarget& target, RenderStates states) const override; + void draw(RenderTarget& target, const RenderStates& states) const override; //////////////////////////////////////////////////////////// /// \brief Update the vertices' positions diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index 0d9228d5..083d6956 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -390,7 +390,7 @@ private: /// \param states Current render states /// //////////////////////////////////////////////////////////// - void draw(RenderTarget& target, RenderStates states) const override; + void draw(RenderTarget& target, const RenderStates& states) const override; //////////////////////////////////////////////////////////// /// \brief Make sure the text's geometry is updated diff --git a/include/SFML/Graphics/Transformable.hpp b/include/SFML/Graphics/Transformable.hpp index edd1c4b9..759fa7ba 100644 --- a/include/SFML/Graphics/Transformable.hpp +++ b/include/SFML/Graphics/Transformable.hpp @@ -292,10 +292,11 @@ private: /// \code /// class MyEntity : public sf::Transformable, public sf::Drawable /// { -/// void draw(sf::RenderTarget& target, sf::RenderStates states) const override +/// void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override /// { -/// states.transform *= getTransform(); -/// target.draw(..., states); +/// sf::RenderStates statesCopy(states); +/// statesCopy.transform *= getTransform(); +/// target.draw(..., statesCopy); /// } /// }; /// diff --git a/include/SFML/Graphics/VertexArray.hpp b/include/SFML/Graphics/VertexArray.hpp index 405f3747..2166b516 100644 --- a/include/SFML/Graphics/VertexArray.hpp +++ b/include/SFML/Graphics/VertexArray.hpp @@ -179,7 +179,7 @@ private: /// \param states Current render states /// //////////////////////////////////////////////////////////// - void draw(RenderTarget& target, RenderStates states) const override; + void draw(RenderTarget& target, const RenderStates& states) const override; private: diff --git a/include/SFML/Graphics/VertexBuffer.hpp b/include/SFML/Graphics/VertexBuffer.hpp index edb523b0..9d79add1 100644 --- a/include/SFML/Graphics/VertexBuffer.hpp +++ b/include/SFML/Graphics/VertexBuffer.hpp @@ -328,7 +328,7 @@ private: /// \param states Current render states /// //////////////////////////////////////////////////////////// - void draw(RenderTarget& target, RenderStates states) const override; + void draw(RenderTarget& target, const RenderStates& states) const override; private: diff --git a/src/SFML/Audio/Music.cpp b/src/SFML/Audio/Music.cpp index ef5ba31e..ad96a2b3 100644 --- a/src/SFML/Audio/Music.cpp +++ b/src/SFML/Audio/Music.cpp @@ -248,7 +248,7 @@ void Music::initialize() m_loopSpan.length = m_file.getSampleCount(); // Resize the internal buffer so that it can contain 1 second of audio samples - m_samples.resize(m_file.getSampleRate() * m_file.getChannelCount()); + m_samples.resize(static_cast(m_file.getSampleRate()) * static_cast(m_file.getChannelCount())); // Initialize the stream SoundStream::initialize(m_file.getChannelCount(), m_file.getSampleRate()); diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp index 0391e36e..2cc6360c 100644 --- a/src/SFML/Audio/SoundRecorder.cpp +++ b/src/SFML/Audio/SoundRecorder.cpp @@ -303,7 +303,7 @@ void SoundRecorder::processCapturedSamples() if (samplesAvailable > 0) { // Get the recorded samples - m_samples.resize(static_cast(samplesAvailable) * getChannelCount()); + m_samples.resize(static_cast(samplesAvailable) * getChannelCount()); alcCaptureSamples(captureDevice, m_samples.data(), samplesAvailable); // Forward them to the derived class diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index aad23d73..3af3ed7f 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -82,7 +82,11 @@ void SoundStream::initialize(unsigned int channelCount, unsigned int sampleRate) m_channelCount = channelCount; m_sampleRate = sampleRate; m_samplesProcessed = 0; - m_isStreaming = false; + + { + std::scoped_lock lock(m_threadMutex); + m_isStreaming = false; + } // Deduce the format from the number of channels m_format = priv::AudioDevice::getFormatFromChannelCount(channelCount); @@ -494,8 +498,11 @@ void SoundStream::clearQueue() //////////////////////////////////////////////////////////// void SoundStream::launchStreamingThread(Status threadStartState) { - m_isStreaming = true; - m_threadStartState = threadStartState; + { + std::scoped_lock lock(m_threadMutex); + m_isStreaming = true; + m_threadStartState = threadStartState; + } assert(!m_thread.joinable()); m_thread = std::thread(&SoundStream::streamData, this); diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 993c140c..c54ecdc4 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -629,7 +629,7 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f glyph.bounds.height = static_cast( bitmap.rows); // Resize the pixel buffer to the new size and fill it with transparent white pixels - m_pixelBuffer.resize(width * height * 4); + m_pixelBuffer.resize(static_cast(width) * static_cast(height) * 4); Uint8* current = m_pixelBuffer.data(); Uint8* end = current + width * height * 4; diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 7677a45a..a9b7c6f2 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -58,7 +58,7 @@ void Image::create(unsigned int width, unsigned int height, const Color& color) if (width && height) { // Create a new pixel buffer first for exception safety's sake - std::vector newPixels(width * height * 4); + std::vector newPixels(static_cast(width) * static_cast(height) * 4); // Fill it with the specified color Uint8* ptr = newPixels.data(); diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index 90654a31..a7726827 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -209,19 +209,21 @@ void Shape::update() //////////////////////////////////////////////////////////// -void Shape::draw(RenderTarget& target, RenderStates states) const +void Shape::draw(RenderTarget& target, const RenderStates& states) const { - states.transform *= getTransform(); + RenderStates statesCopy(states); + + statesCopy.transform *= getTransform(); // Render the inside - states.texture = m_texture; - target.draw(m_vertices, states); + statesCopy.texture = m_texture; + target.draw(m_vertices, statesCopy); // Render the outline if (m_outlineThickness != 0) { - states.texture = nullptr; - target.draw(m_outlineVertices, states); + statesCopy.texture = nullptr; + target.draw(m_outlineVertices, statesCopy); } } diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index 0e7e2532..a3bdefa4 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -138,13 +138,15 @@ FloatRect Sprite::getGlobalBounds() const //////////////////////////////////////////////////////////// -void Sprite::draw(RenderTarget& target, RenderStates states) const +void Sprite::draw(RenderTarget& target, const RenderStates& states) const { if (m_texture) { - states.transform *= getTransform(); - states.texture = m_texture; - target.draw(m_vertices, 4, TriangleStrip, states); + RenderStates statesCopy(states); + + statesCopy.transform *= getTransform(); + statesCopy.texture = m_texture; + target.draw(m_vertices, 4, TriangleStrip, statesCopy); } } diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 57355c59..b12a3ab7 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -359,20 +359,22 @@ FloatRect Text::getGlobalBounds() const //////////////////////////////////////////////////////////// -void Text::draw(RenderTarget& target, RenderStates states) const +void Text::draw(RenderTarget& target, const RenderStates& states) const { if (m_font) { ensureGeometryUpdate(); - states.transform *= getTransform(); - states.texture = &m_font->getTexture(m_characterSize); + RenderStates statesCopy(states); + + statesCopy.transform *= getTransform(); + statesCopy.texture = &m_font->getTexture(m_characterSize); // Only draw the outline if there is something to draw if (m_outlineThickness != 0) - target.draw(m_outlineVertices, states); + target.draw(m_outlineVertices, statesCopy); - target.draw(m_vertices, states); + target.draw(m_vertices, statesCopy); } } diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index ad11d2a9..eacfec91 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -330,7 +330,7 @@ Image Texture::copyToImage() const priv::TextureSaver save; // Create an array of pixels - std::vector pixels(m_size.x * m_size.y * 4); + std::vector pixels(static_cast(m_size.x) * static_cast(m_size.y) * 4); #ifdef SFML_OPENGL_ES @@ -364,7 +364,7 @@ Image Texture::copyToImage() const // Texture is either padded or flipped, we have to use a slower algorithm // All the pixels will first be copied to a temporary array - std::vector allPixels(m_actualSize.x * m_actualSize.y * 4); + std::vector allPixels(static_cast(m_actualSize.x) * static_cast(m_actualSize.y) * 4); glCheck(glBindTexture(GL_TEXTURE_2D, m_texture)); glCheck(glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, allPixels.data())); diff --git a/src/SFML/Graphics/VertexArray.cpp b/src/SFML/Graphics/VertexArray.cpp index 154159fc..fe39c6e6 100644 --- a/src/SFML/Graphics/VertexArray.cpp +++ b/src/SFML/Graphics/VertexArray.cpp @@ -141,7 +141,7 @@ FloatRect VertexArray::getBounds() const //////////////////////////////////////////////////////////// -void VertexArray::draw(RenderTarget& target, RenderStates states) const +void VertexArray::draw(RenderTarget& target, const RenderStates& states) const { if (!m_vertices.empty()) target.draw(m_vertices.data(), m_vertices.size(), m_primitiveType, states); diff --git a/src/SFML/Graphics/VertexBuffer.cpp b/src/SFML/Graphics/VertexBuffer.cpp index 5bfcd69f..a3be35d6 100644 --- a/src/SFML/Graphics/VertexBuffer.cpp +++ b/src/SFML/Graphics/VertexBuffer.cpp @@ -360,7 +360,7 @@ bool VertexBuffer::isAvailable() //////////////////////////////////////////////////////////// -void VertexBuffer::draw(RenderTarget& target, RenderStates states) const +void VertexBuffer::draw(RenderTarget& target, const RenderStates& states) const { if (m_buffer && m_size) target.draw(*this, 0, m_size, states); diff --git a/src/SFML/Window/Unix/CursorImpl.cpp b/src/SFML/Window/Unix/CursorImpl.cpp index b032b632..d8f979f0 100644 --- a/src/SFML/Window/Unix/CursorImpl.cpp +++ b/src/SFML/Window/Unix/CursorImpl.cpp @@ -77,7 +77,7 @@ bool CursorImpl::loadFromPixelsARGB(const Uint8* pixels, Vector2u size, Vector2u cursorImage->xhot = hotspot.x; cursorImage->yhot = hotspot.y; - const std::size_t numPixels = size.x * size.y; + const std::size_t numPixels = static_cast(size.x) * static_cast(size.y); for (std::size_t pixelIndex = 0; pixelIndex < numPixels; ++pixelIndex) { cursorImage->pixels[pixelIndex] = static_cast(pixels[pixelIndex * 4 + 2] + diff --git a/src/SFML/Window/Unix/VulkanImplX11.cpp b/src/SFML/Window/Unix/VulkanImplX11.cpp index 2b6ae7df..0c5dc1dd 100644 --- a/src/SFML/Window/Unix/VulkanImplX11.cpp +++ b/src/SFML/Window/Unix/VulkanImplX11.cpp @@ -41,7 +41,10 @@ namespace struct VulkanLibraryWrapper { VulkanLibraryWrapper() : - library(nullptr) + library(nullptr), + vkGetInstanceProcAddr(nullptr), + vkEnumerateInstanceLayerProperties(nullptr), + vkEnumerateInstanceExtensionProperties(nullptr) { } diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 100cd2b1..4b293ab7 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -1009,8 +1009,8 @@ void WindowImplX11::setIcon(unsigned int width, unsigned int height, const Uint8 { // X11 wants BGRA pixels: swap red and blue channels // Note: this memory will be freed by XDestroyImage - auto* iconPixels = static_cast(std::malloc(width * height * 4)); - for (std::size_t i = 0; i < width * height; ++i) + auto* iconPixels = static_cast(std::malloc(static_cast(width) * static_cast(height) * 4)); + for (std::size_t i = 0; i < static_cast(width) * static_cast(height); ++i) { iconPixels[i * 4 + 0] = pixels[i * 4 + 2]; iconPixels[i * 4 + 1] = pixels[i * 4 + 1]; @@ -1079,7 +1079,7 @@ void WindowImplX11::setIcon(unsigned int width, unsigned int height, const Uint8 *ptr++ = height; #pragma GCC diagnostic pop - for (std::size_t i = 0; i < width * height; ++i) + for (std::size_t i = 0; i < static_cast(width) * static_cast(height); ++i) { *ptr++ = static_cast((pixels[i * 4 + 2] << 0 ) | (pixels[i * 4 + 1] << 8 ) | diff --git a/src/SFML/Window/Win32/VulkanImplWin32.cpp b/src/SFML/Window/Win32/VulkanImplWin32.cpp index 50e509d9..3e135029 100644 --- a/src/SFML/Window/Win32/VulkanImplWin32.cpp +++ b/src/SFML/Window/Win32/VulkanImplWin32.cpp @@ -40,7 +40,10 @@ namespace struct VulkanLibraryWrapper { VulkanLibraryWrapper() : - library(nullptr) + library(nullptr), + vkGetInstanceProcAddr(nullptr), + vkEnumerateInstanceLayerProperties(nullptr), + vkEnumerateInstanceExtensionProperties(nullptr) { } diff --git a/src/SFML/Window/WindowBase.cpp b/src/SFML/Window/WindowBase.cpp index 1e455b98..5d890e83 100644 --- a/src/SFML/Window/WindowBase.cpp +++ b/src/SFML/Window/WindowBase.cpp @@ -73,7 +73,7 @@ m_size(0, 0) //////////////////////////////////////////////////////////// WindowBase::~WindowBase() { - close(); + WindowBase::close(); } diff --git a/test/TestUtilities/GraphicsUtil.hpp b/test/TestUtilities/GraphicsUtil.hpp index ac5a5f86..d631e543 100644 --- a/test/TestUtilities/GraphicsUtil.hpp +++ b/test/TestUtilities/GraphicsUtil.hpp @@ -10,7 +10,7 @@ namespace sf { - class BlendMode; + struct BlendMode; class Color; class Transform;