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.
This commit is contained in:
Chris Thrasher 2024-05-23 13:22:43 -06:00
parent fdcd899d2b
commit eb4170796a

View File

@ -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;
@ -315,8 +321,7 @@ 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()};
std::optional<sf::Shader> m_shader;
sf::VertexArray m_pointCloud;
};