diff --git a/examples/X11/X11.cpp b/examples/X11/X11.cpp index 979cc7d69..9aa37db2e 100644 --- a/examples/X11/X11.cpp +++ b/examples/X11/X11.cpp @@ -10,13 +10,15 @@ #include #include #include - +#include //////////////////////////////////////////////////////////// /// Initialize OpenGL states into the specified view /// /// \param Window Target window to initialize /// +/// \return True if operation was successful, false otherwise +/// //////////////////////////////////////////////////////////// [[nodiscard]] bool initialize(sf::Window& window) { @@ -68,6 +70,8 @@ /// \param window Target window for rendering /// \param elapsedTime Time elapsed since the last draw /// +/// \return True if operation was successful, false otherwise +/// //////////////////////////////////////////////////////////// [[nodiscard]] bool draw(sf::Window& window, float elapsedTime) { @@ -261,7 +265,6 @@ int main() return EXIT_FAILURE; } - // Display the views on screen sfmlView1.display(); sfmlView2.display(); diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index f79bafe32..01f2510eb 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -4,13 +4,18 @@ //////////////////////////////////////////////////////////// #define STB_PERLIN_IMPLEMENTATION #include + #include -#include -#include -#include + #include -#include +#include +#include +#include +#include + #include +#include +#include namespace @@ -136,7 +141,11 @@ int main() } // Create our VertexBuffer with enough space to hold all the terrain geometry - terrain.create(resolutionX * resolutionY * 6); + if (!terrain.create(resolutionX * resolutionY * 6)) + { + std::cerr << "Failed to create vertex buffer" << std::endl; + return EXIT_FAILURE; + } // Resize the staging buffer to be able to hold all the terrain geometry terrainStagingBuffer.resize(resolutionX * resolutionY * 6); @@ -215,7 +224,12 @@ int main() // If there is new data pending to be uploaded to the VertexBuffer, do it now if (bufferUploadPending) { - terrain.update(terrainStagingBuffer.data()); + if (!terrain.update(terrainStagingBuffer.data())) + { + std::cerr << "Failed to update vertex buffer" << std::endl; + return EXIT_FAILURE; + } + bufferUploadPending = false; } diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index 3f726feed..f68a271ef 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -3,6 +3,8 @@ // Headers //////////////////////////////////////////////////////////// #include +#include +#include #define GLAD_GL_IMPLEMENTATION #include @@ -57,6 +59,7 @@ int main() sf::Font font; if (!font.loadFromFile(resourcesDir() + "tuffy.ttf")) return EXIT_FAILURE; + sf::Text text("SFML / OpenGL demo", font); sf::Text sRgbInstructions("Press space to toggle sRGB conversion", font); sf::Text mipmapInstructions("Press return to toggle mipmapping", font); @@ -75,10 +78,14 @@ int main() // Attempt to generate a mipmap for our cube texture // We don't check the return value here since // mipmapping is purely optional in this example - texture.generateMipmap(); + (void) texture.generateMipmap(); // Make the window the active window for OpenGL calls - window.setActive(true); + if (!window.setActive(true)) + { + std::cerr << "Failed to set window to active" << std::endl; + return EXIT_FAILURE; + } // Load OpenGL or OpenGL ES entry points using glad #ifdef SFML_OPENGL_ES @@ -174,7 +181,11 @@ int main() glDisableClientState(GL_COLOR_ARRAY); // Make the window no longer the active window for OpenGL calls - window.setActive(false); + if (!window.setActive(false)) + { + std::cerr << "Failed to set window to inactive" << std::endl; + return EXIT_FAILURE; + } // Create a clock for measuring the time elapsed sf::Clock clock; @@ -214,10 +225,8 @@ int main() mipmapEnabled = false; } - else + else if (texture.generateMipmap()) { - texture.generateMipmap(); - mipmapEnabled = true; } } @@ -235,7 +244,11 @@ int main() sf::Vector2u textureSize = backgroundTexture.getSize(); // Make the window the active window for OpenGL calls - window.setActive(true); + if (!window.setActive(true)) + { + std::cerr << "Failed to set window to active" << std::endl; + return EXIT_FAILURE; + } glViewport(0, 0, static_cast(event.size.width), static_cast(event.size.height)); glMatrixMode(GL_PROJECTION); @@ -248,7 +261,11 @@ int main() #endif // Make the window no longer the active window for OpenGL calls - window.setActive(false); + if (!window.setActive(false)) + { + std::cerr << "Failed to set window to inactive" << std::endl; + return EXIT_FAILURE; + } sf::View view; view.setSize(sf::Vector2f(textureSize)); @@ -263,7 +280,11 @@ int main() window.popGLStates(); // Make the window the active window for OpenGL calls - window.setActive(true); + if (!window.setActive(true)) + { + std::cerr << "Failed to set window to active" << std::endl; + return EXIT_FAILURE; + } // Clear the depth buffer glClear(GL_DEPTH_BUFFER_BIT); @@ -292,7 +313,11 @@ int main() glDrawArrays(GL_TRIANGLES, 0, 36); // Make the window no longer the active window for OpenGL calls - window.setActive(false); + if (!window.setActive(false)) + { + std::cerr << "Failed to set window to inactive" << std::endl; + return EXIT_FAILURE; + } // Draw some text on top of our OpenGL object window.pushGLStates(); diff --git a/examples/window/Window.cpp b/examples/window/Window.cpp index 44bb0a52c..83c84a565 100644 --- a/examples/window/Window.cpp +++ b/examples/window/Window.cpp @@ -12,6 +12,7 @@ #endif #include +#include //////////////////////////////////////////////////////////// /// Entry point of application diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 963e83dd3..2d377e6e1 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -104,7 +104,7 @@ public: /// \see loadFromMemory, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromFile(const std::string& filename); + [[nodiscard]] bool loadFromFile(const std::string& filename); //////////////////////////////////////////////////////////// /// \brief Load the font from a file in memory @@ -125,7 +125,7 @@ public: /// \see loadFromFile, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromMemory(const void* data, std::size_t sizeInBytes); + [[nodiscard]] bool loadFromMemory(const void* data, std::size_t sizeInBytes); //////////////////////////////////////////////////////////// /// \brief Load the font from a custom stream @@ -147,7 +147,7 @@ public: /// \see loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool loadFromStream(InputStream& stream); + [[nodiscard]] bool loadFromStream(InputStream& stream); //////////////////////////////////////////////////////////// /// \brief Get the font information @@ -382,7 +382,7 @@ private: /// \return True on success, false if any error happened /// //////////////////////////////////////////////////////////// - bool setCurrentSize(unsigned int characterSize) const; + [[nodiscard]] bool setCurrentSize(unsigned int characterSize) const; //////////////////////////////////////////////////////////// // Types diff --git a/include/SFML/Graphics/Image.hpp b/include/SFML/Graphics/Image.hpp index edd1404fe..31cb22399 100644 --- a/include/SFML/Graphics/Image.hpp +++ b/include/SFML/Graphics/Image.hpp @@ -101,7 +101,7 @@ public: /// \see loadFromMemory, loadFromStream, saveToFile /// //////////////////////////////////////////////////////////// - bool loadFromFile(const std::string& filename); + [[nodiscard]] bool loadFromFile(const std::string& filename); //////////////////////////////////////////////////////////// /// \brief Load the image from a file in memory @@ -119,7 +119,7 @@ public: /// \see loadFromFile, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromMemory(const void* data, std::size_t size); + [[nodiscard]] bool loadFromMemory(const void* data, std::size_t size); //////////////////////////////////////////////////////////// /// \brief Load the image from a custom stream @@ -136,7 +136,7 @@ public: /// \see loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool loadFromStream(InputStream& stream); + [[nodiscard]] bool loadFromStream(InputStream& stream); //////////////////////////////////////////////////////////// /// \brief Save the image to a file on disk @@ -153,7 +153,7 @@ public: /// \see create, loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool saveToFile(const std::string& filename) const; + [[nodiscard]] bool saveToFile(const std::string& filename) const; //////////////////////////////////////////////////////////// /// \brief Save the image to a buffer in memory @@ -171,7 +171,7 @@ public: /// \see create, loadFromFile, loadFromMemory, saveToFile /// //////////////////////////////////////////////////////////// - bool saveToMemory(std::vector& output, const std::string& format) const; + [[nodiscard]] bool saveToMemory(std::vector& output, const std::string& format) const; //////////////////////////////////////////////////////////// /// \brief Return the size (width and height) of the image diff --git a/include/SFML/Graphics/RenderTarget.hpp b/include/SFML/Graphics/RenderTarget.hpp index f9320af66..80347af41 100644 --- a/include/SFML/Graphics/RenderTarget.hpp +++ b/include/SFML/Graphics/RenderTarget.hpp @@ -304,7 +304,7 @@ public: /// \return True if operation was successful, false otherwise /// //////////////////////////////////////////////////////////// - virtual bool setActive(bool active = true); + [[nodiscard]] virtual bool setActive(bool active = true); //////////////////////////////////////////////////////////// /// \brief Save the current OpenGL render states and matrices diff --git a/include/SFML/Graphics/RenderTexture.hpp b/include/SFML/Graphics/RenderTexture.hpp index 64c6261b0..3f5717284 100644 --- a/include/SFML/Graphics/RenderTexture.hpp +++ b/include/SFML/Graphics/RenderTexture.hpp @@ -84,7 +84,7 @@ public: /// \return True if creation has been successful /// //////////////////////////////////////////////////////////// - bool create(unsigned int width, unsigned int height, const ContextSettings& settings = ContextSettings()); + [[nodiscard]] bool create(unsigned int width, unsigned int height, const ContextSettings& settings = ContextSettings()); //////////////////////////////////////////////////////////// /// \brief Get the maximum anti-aliasing level supported by the system @@ -154,7 +154,7 @@ public: /// \return True if mipmap generation was successful, false if unsuccessful /// //////////////////////////////////////////////////////////// - bool generateMipmap(); + [[nodiscard]] bool generateMipmap(); //////////////////////////////////////////////////////////// /// \brief Activate or deactivate the render-texture for rendering @@ -171,7 +171,7 @@ public: /// \return True if operation was successful, false otherwise /// //////////////////////////////////////////////////////////// - bool setActive(bool active = true) override; + [[nodiscard]] bool setActive(bool active = true) override; //////////////////////////////////////////////////////////// /// \brief Update the contents of the target texture diff --git a/include/SFML/Graphics/RenderWindow.hpp b/include/SFML/Graphics/RenderWindow.hpp index 8793b0626..0c7bdab7e 100644 --- a/include/SFML/Graphics/RenderWindow.hpp +++ b/include/SFML/Graphics/RenderWindow.hpp @@ -139,7 +139,7 @@ public: /// \return True if operation was successful, false otherwise /// //////////////////////////////////////////////////////////// - bool setActive(bool active = true) override; + [[nodiscard]] bool setActive(bool active = true) override; protected: diff --git a/include/SFML/Graphics/Shader.hpp b/include/SFML/Graphics/Shader.hpp index 3db037536..d7a3634b5 100644 --- a/include/SFML/Graphics/Shader.hpp +++ b/include/SFML/Graphics/Shader.hpp @@ -116,7 +116,7 @@ public: /// \see loadFromMemory, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromFile(const std::string& filename, Type type); + [[nodiscard]] bool loadFromFile(const std::string& filename, Type type); //////////////////////////////////////////////////////////// /// \brief Load both the vertex and fragment shaders from files @@ -137,7 +137,7 @@ public: /// \see loadFromMemory, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromFile(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename); + [[nodiscard]] bool loadFromFile(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename); //////////////////////////////////////////////////////////// /// \brief Load the vertex, geometry and fragment shaders from files @@ -159,7 +159,7 @@ public: /// \see loadFromMemory, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromFile(const std::string& vertexShaderFilename, const std::string& geometryShaderFilename, const std::string& fragmentShaderFilename); + [[nodiscard]] bool loadFromFile(const std::string& vertexShaderFilename, const std::string& geometryShaderFilename, const std::string& fragmentShaderFilename); //////////////////////////////////////////////////////////// /// \brief Load the vertex, geometry or fragment shader from a source code in memory @@ -179,7 +179,7 @@ public: /// \see loadFromFile, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromMemory(const std::string& shader, Type type); + [[nodiscard]] bool loadFromMemory(const std::string& shader, Type type); //////////////////////////////////////////////////////////// /// \brief Load both the vertex and fragment shaders from source codes in memory @@ -200,7 +200,7 @@ public: /// \see loadFromFile, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromMemory(const std::string& vertexShader, const std::string& fragmentShader); + [[nodiscard]] bool loadFromMemory(const std::string& vertexShader, const std::string& fragmentShader); //////////////////////////////////////////////////////////// /// \brief Load the vertex, geometry and fragment shaders from source codes in memory @@ -222,7 +222,7 @@ public: /// \see loadFromFile, loadFromStream /// //////////////////////////////////////////////////////////// - bool loadFromMemory(const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader); + [[nodiscard]] bool loadFromMemory(const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader); //////////////////////////////////////////////////////////// /// \brief Load the vertex, geometry or fragment shader from a custom stream @@ -242,7 +242,7 @@ public: /// \see loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool loadFromStream(InputStream& stream, Type type); + [[nodiscard]] bool loadFromStream(InputStream& stream, Type type); //////////////////////////////////////////////////////////// /// \brief Load both the vertex and fragment shaders from custom streams @@ -263,7 +263,7 @@ public: /// \see loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream); + [[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream); //////////////////////////////////////////////////////////// /// \brief Load the vertex, geometry and fragment shaders from custom streams @@ -285,7 +285,7 @@ public: /// \see loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool loadFromStream(InputStream& vertexShaderStream, InputStream& geometryShaderStream, InputStream& fragmentShaderStream); + [[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream, InputStream& geometryShaderStream, InputStream& fragmentShaderStream); //////////////////////////////////////////////////////////// /// \brief Specify value for \p float uniform @@ -626,7 +626,7 @@ private: /// \return True on success, false if any error happened /// //////////////////////////////////////////////////////////// - bool compile(const char* vertexShaderCode, const char* geometryShaderCode, const char* fragmentShaderCode); + [[nodiscard]] bool compile(const char* vertexShaderCode, const char* geometryShaderCode, const char* fragmentShaderCode); //////////////////////////////////////////////////////////// /// \brief Bind all the textures used by the shader diff --git a/include/SFML/Graphics/Texture.hpp b/include/SFML/Graphics/Texture.hpp index 8214f3f0b..7f4e33e36 100644 --- a/include/SFML/Graphics/Texture.hpp +++ b/include/SFML/Graphics/Texture.hpp @@ -94,7 +94,7 @@ public: /// \return True if creation was successful /// //////////////////////////////////////////////////////////// - bool create(unsigned int width, unsigned int height); + [[nodiscard]] bool create(unsigned int width, unsigned int height); //////////////////////////////////////////////////////////// /// \brief Load the texture from a file on disk @@ -125,7 +125,7 @@ public: /// \see loadFromMemory, loadFromStream, loadFromImage /// //////////////////////////////////////////////////////////// - bool loadFromFile(const std::string& filename, const IntRect& area = IntRect()); + [[nodiscard]] bool loadFromFile(const std::string& filename, const IntRect& area = IntRect()); //////////////////////////////////////////////////////////// /// \brief Load the texture from a file in memory @@ -157,7 +157,7 @@ public: /// \see loadFromFile, loadFromStream, loadFromImage /// //////////////////////////////////////////////////////////// - bool loadFromMemory(const void* data, std::size_t size, const IntRect& area = IntRect()); + [[nodiscard]] bool loadFromMemory(const void* data, std::size_t size, const IntRect& area = IntRect()); //////////////////////////////////////////////////////////// /// \brief Load the texture from a custom stream @@ -188,7 +188,7 @@ public: /// \see loadFromFile, loadFromMemory, loadFromImage /// //////////////////////////////////////////////////////////// - bool loadFromStream(InputStream& stream, const IntRect& area = IntRect()); + [[nodiscard]] bool loadFromStream(InputStream& stream, const IntRect& area = IntRect()); //////////////////////////////////////////////////////////// /// \brief Load the texture from an image @@ -212,7 +212,7 @@ public: /// \see loadFromFile, loadFromMemory /// //////////////////////////////////////////////////////////// - bool loadFromImage(const Image& image, const IntRect& area = IntRect()); + [[nodiscard]] bool loadFromImage(const Image& image, const IntRect& area = IntRect()); //////////////////////////////////////////////////////////// /// \brief Return the size of the texture @@ -506,7 +506,7 @@ public: /// \return True if mipmap generation was successful, false if unsuccessful /// //////////////////////////////////////////////////////////// - bool generateMipmap(); + [[nodiscard]] bool generateMipmap(); //////////////////////////////////////////////////////////// /// \brief Overload of assignment operator diff --git a/include/SFML/Graphics/VertexBuffer.hpp b/include/SFML/Graphics/VertexBuffer.hpp index 642f82bb3..f0b655768 100644 --- a/include/SFML/Graphics/VertexBuffer.hpp +++ b/include/SFML/Graphics/VertexBuffer.hpp @@ -134,7 +134,7 @@ public: /// \return True if creation was successful /// //////////////////////////////////////////////////////////// - bool create(std::size_t vertexCount); + [[nodiscard]] bool create(std::size_t vertexCount); //////////////////////////////////////////////////////////// /// \brief Return the vertex count @@ -162,7 +162,7 @@ public: /// \return True if the update was successful /// //////////////////////////////////////////////////////////// - bool update(const Vertex* vertices); + [[nodiscard]] bool update(const Vertex* vertices); //////////////////////////////////////////////////////////// /// \brief Update a part of the buffer from an array of vertices @@ -195,7 +195,7 @@ public: /// \return True if the update was successful /// //////////////////////////////////////////////////////////// - bool update(const Vertex* vertices, std::size_t vertexCount, unsigned int offset); + [[nodiscard]] bool update(const Vertex* vertices, std::size_t vertexCount, unsigned int offset); //////////////////////////////////////////////////////////// /// \brief Copy the contents of another buffer into this buffer @@ -205,7 +205,7 @@ public: /// \return True if the copy was successful /// //////////////////////////////////////////////////////////// - bool update(const VertexBuffer& vertexBuffer); + [[nodiscard]] bool update(const VertexBuffer& vertexBuffer); //////////////////////////////////////////////////////////// /// \brief Overload of assignment operator diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 40636fad2..ab7a136f9 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -770,7 +770,12 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height) { // Make the texture 2 times bigger Texture newTexture; - newTexture.create(textureWidth * 2, textureHeight * 2); + if (!newTexture.create(textureWidth * 2, textureHeight * 2)) + { + err() << "Failed to create new page texture" << std::endl; + return IntRect(0, 0, 2, 2); + } + newTexture.setSmooth(m_isSmooth); newTexture.update(page.texture); page.texture.swap(newTexture); @@ -854,7 +859,11 @@ nextRow(3) image.setPixel(x, y, Color(255, 255, 255, 255)); // Create the texture - texture.loadFromImage(image); + if (!texture.loadFromImage(image)) + { + err() << "Failed to load font page texture" << std::endl; + } + texture.setSmooth(true); } diff --git a/src/SFML/Graphics/RenderTexture.cpp b/src/SFML/Graphics/RenderTexture.cpp index 0243d29e3..e894e320c 100644 --- a/src/SFML/Graphics/RenderTexture.cpp +++ b/src/SFML/Graphics/RenderTexture.cpp @@ -143,13 +143,11 @@ bool RenderTexture::generateMipmap() //////////////////////////////////////////////////////////// bool RenderTexture::setActive(bool active) { - bool result = m_impl && m_impl->activate(active); - // Update RenderTarget tracking - if (result) - RenderTarget::setActive(active); + if (m_impl && m_impl->activate(active)) + return RenderTarget::setActive(active); - return result; + return false; } diff --git a/src/SFML/Graphics/RenderWindow.cpp b/src/SFML/Graphics/RenderWindow.cpp index 179b17892..bc2531c57 100644 --- a/src/SFML/Graphics/RenderWindow.cpp +++ b/src/SFML/Graphics/RenderWindow.cpp @@ -87,7 +87,7 @@ bool RenderWindow::setActive(bool active) // Update RenderTarget tracking if (result) - RenderTarget::setActive(active); + result = RenderTarget::setActive(active); // If FBOs are available, make sure none are bound when we // try to draw to the default framebuffer of the RenderWindow