Strategic use of '[[nodiscard]]' in 'Graphics' module

This commit is contained in:
Vittorio Romeo 2021-12-09 19:55:33 +00:00
parent d02d3590fd
commit 363e964acc
15 changed files with 110 additions and 60 deletions

View File

@ -10,13 +10,15 @@
#include <X11/Xlib.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
////////////////////////////////////////////////////////////
/// 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();

View File

@ -4,13 +4,18 @@
////////////////////////////////////////////////////////////
#define STB_PERLIN_IMPLEMENTATION
#include <stb_perlin.h>
#include <SFML/Graphics.hpp>
#include <vector>
#include <deque>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cstring>
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;
}

View File

@ -3,6 +3,8 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
#define GLAD_GL_IMPLEMENTATION
#include <gl.h>
@ -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<GLsizei>(event.size.width), static_cast<GLsizei>(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();

View File

@ -12,6 +12,7 @@
#endif
#include <iostream>
#include <cstdlib>
////////////////////////////////////////////////////////////
/// Entry point of application

View File

@ -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

View File

@ -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<sf::Uint8>& output, const std::string& format) const;
[[nodiscard]] bool saveToMemory(std::vector<sf::Uint8>& output, const std::string& format) const;
////////////////////////////////////////////////////////////
/// \brief Return the size (width and height) of the image

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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