From eb4170796ac2b9d23a21cf003d9deda30a50dad7 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 23 May 2024 13:22:43 -0600 Subject: [PATCH] Fix geometry shader example This example is optional since it may be skipped on hardware where geometry shaders are not supported. The way I rewrote it in 7234fc1 resulted in the shader still being attempted to be loaded even when geometry shaders were not supported leading to the whole program crashing. My bad. --- examples/shader/Shader.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index 151dc8d18..ad33ee984 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -277,10 +277,16 @@ public: if (!m_logoTexture.loadFromFile("resources/logo.png")) return false; - m_shader.setUniform("texture", sf::Shader::CurrentTexture); + // Load the shader + m_shader = sf::Shader::loadFromFile("resources/billboard.vert", + "resources/billboard.geom", + "resources/billboard.frag"); + if (!m_shader) + return false; + m_shader->setUniform("texture", sf::Shader::CurrentTexture); // Set the render resolution (used for proper scaling) - m_shader.setUniform("resolution", sf::Vector2f(800, 600)); + m_shader->setUniform("resolution", sf::Vector2f(800, 600)); return true; } @@ -298,13 +304,13 @@ public: const float size = 25 + std::abs(y) * 50; // Update the shader parameter - m_shader.setUniform("size", sf::Vector2f(size, size)); + m_shader->setUniform("size", sf::Vector2f(size, size)); } void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override { // Prepare the render state - states.shader = &m_shader; + states.shader = &*m_shader; states.texture = &m_logoTexture; states.transform = m_transform; @@ -313,11 +319,10 @@ public: } private: - sf::Texture m_logoTexture; - sf::Transform m_transform; - sf::Shader m_shader{ - sf::Shader::loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag").value()}; - sf::VertexArray m_pointCloud; + sf::Texture m_logoTexture; + sf::Transform m_transform; + std::optional m_shader; + sf::VertexArray m_pointCloud; };