mirror of
https://github.com/SFML/SFML.git
synced 2024-11-24 20:31:05 +08:00
Restore Shader and Island examples back to equivalent pre-API-change functionality.
This commit is contained in:
parent
7987d3cedc
commit
59414d5bfd
@ -97,6 +97,7 @@ int main()
|
|||||||
sf::Text hudText(font);
|
sf::Text hudText(font);
|
||||||
sf::Text statusText(font);
|
sf::Text statusText(font);
|
||||||
std::optional<sf::Shader> terrainShader;
|
std::optional<sf::Shader> terrainShader;
|
||||||
|
sf::RenderStates terrainStates;
|
||||||
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Usage::Static);
|
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Usage::Static);
|
||||||
|
|
||||||
// Set up our text drawables
|
// Set up our text drawables
|
||||||
@ -145,6 +146,9 @@ int main()
|
|||||||
generateTerrain(terrainStagingBuffer.data());
|
generateTerrain(terrainStagingBuffer.data());
|
||||||
|
|
||||||
statusText.setString("Generating Terrain...");
|
statusText.setString("Generating Terrain...");
|
||||||
|
|
||||||
|
// Set up the render states
|
||||||
|
terrainStates = sf::RenderStates(&*terrainShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Center the status text
|
// Center the status text
|
||||||
@ -233,7 +237,7 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
terrainShader->setUniform("lightFactor", lightFactor);
|
terrainShader->setUniform("lightFactor", lightFactor);
|
||||||
window.draw(terrain, sf::RenderStates(&*terrainShader));
|
window.draw(terrain, terrainStates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@ public:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Clear the target to grey to make sure the text is always readable
|
||||||
|
target.clear(sf::Color(50, 50, 50));
|
||||||
sf::Text error(getFont(), "Shader not\nsupported");
|
sf::Text error(getFont(), "Shader not\nsupported");
|
||||||
error.setPosition({320.f, 200.f});
|
error.setPosition({320.f, 200.f});
|
||||||
error.setCharacterSize(36);
|
error.setCharacterSize(36);
|
||||||
|
@ -38,26 +38,29 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
m_sprite.emplace(m_texture);
|
m_sprite.emplace(m_texture);
|
||||||
|
|
||||||
m_shader.setUniform("texture", sf::Shader::CurrentTexture);
|
// Load the shader
|
||||||
|
if (!(m_shader = sf::Shader::loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment)))
|
||||||
|
return false;
|
||||||
|
m_shader->setUniform("texture", sf::Shader::CurrentTexture);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onUpdate(float, float x, float y) override
|
void onUpdate(float, float x, float y) override
|
||||||
{
|
{
|
||||||
m_shader.setUniform("pixel_threshold", (x + y) / 30);
|
m_shader->setUniform("pixel_threshold", (x + y) / 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override
|
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override
|
||||||
{
|
{
|
||||||
states.shader = &m_shader;
|
states.shader = &*m_shader;
|
||||||
target.draw(*m_sprite, states);
|
target.draw(*m_sprite, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Texture m_texture;
|
sf::Texture m_texture;
|
||||||
std::optional<sf::Sprite> m_sprite;
|
std::optional<sf::Sprite> m_sprite;
|
||||||
sf::Shader m_shader{sf::Shader::loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment).value()};
|
std::optional<sf::Shader> m_shader;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -96,25 +99,26 @@ public:
|
|||||||
m_text.setCharacterSize(22);
|
m_text.setCharacterSize(22);
|
||||||
m_text.setPosition({30.f, 20.f});
|
m_text.setPosition({30.f, 20.f});
|
||||||
|
|
||||||
return true;
|
// Load the shader
|
||||||
|
return (m_shader = sf::Shader::loadFromFile("resources/wave.vert", "resources/blur.frag")).has_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onUpdate(float time, float x, float y) override
|
void onUpdate(float time, float x, float y) override
|
||||||
{
|
{
|
||||||
m_shader.setUniform("wave_phase", time);
|
m_shader->setUniform("wave_phase", time);
|
||||||
m_shader.setUniform("wave_amplitude", sf::Vector2f(x * 40, y * 40));
|
m_shader->setUniform("wave_amplitude", sf::Vector2f(x * 40, y * 40));
|
||||||
m_shader.setUniform("blur_radius", (x + y) * 0.008f);
|
m_shader->setUniform("blur_radius", (x + y) * 0.008f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override
|
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override
|
||||||
{
|
{
|
||||||
states.shader = &m_shader;
|
states.shader = &*m_shader;
|
||||||
target.draw(m_text, states);
|
target.draw(m_text, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Text m_text;
|
sf::Text m_text;
|
||||||
sf::Shader m_shader{sf::Shader::loadFromFile("resources/wave.vert", "resources/blur.frag").value()};
|
std::optional<sf::Shader> m_shader;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -146,27 +150,28 @@ public:
|
|||||||
m_points.append({{x, y}, {r, g, b}});
|
m_points.append({{x, y}, {r, g, b}});
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// Load the shader
|
||||||
|
return (m_shader = sf::Shader::loadFromFile("resources/storm.vert", "resources/blink.frag")).has_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onUpdate(float time, float x, float y) override
|
void onUpdate(float time, float x, float y) override
|
||||||
{
|
{
|
||||||
const float radius = 200 + std::cos(time) * 150;
|
const float radius = 200 + std::cos(time) * 150;
|
||||||
m_shader.setUniform("storm_position", sf::Vector2f(x * 800, y * 600));
|
m_shader->setUniform("storm_position", sf::Vector2f(x * 800, y * 600));
|
||||||
m_shader.setUniform("storm_inner_radius", radius / 3);
|
m_shader->setUniform("storm_inner_radius", radius / 3);
|
||||||
m_shader.setUniform("storm_total_radius", radius);
|
m_shader->setUniform("storm_total_radius", radius);
|
||||||
m_shader.setUniform("blink_alpha", 0.5f + std::cos(time * 3) * 0.25f);
|
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, sf::RenderStates states) const override
|
||||||
{
|
{
|
||||||
states.shader = &m_shader;
|
states.shader = &*m_shader;
|
||||||
target.draw(m_points, states);
|
target.draw(m_points, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::VertexArray m_points;
|
sf::VertexArray m_points;
|
||||||
sf::Shader m_shader{sf::Shader::loadFromFile("resources/storm.vert", "resources/blink.frag").value()};
|
std::optional<sf::Shader> m_shader;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -182,7 +187,10 @@ public:
|
|||||||
|
|
||||||
bool onLoad() override
|
bool onLoad() override
|
||||||
{
|
{
|
||||||
m_surface.setSmooth(true);
|
// Create the off-screen surface
|
||||||
|
if (!(m_surface = sf::RenderTexture::create({800, 600})))
|
||||||
|
return false;
|
||||||
|
m_surface->setSmooth(true);
|
||||||
|
|
||||||
// Load the textures
|
// Load the textures
|
||||||
if (!m_backgroundTexture.loadFromFile("resources/sfml.png"))
|
if (!m_backgroundTexture.loadFromFile("resources/sfml.png"))
|
||||||
@ -203,15 +211,17 @@ public:
|
|||||||
m_entities.push_back(entity);
|
m_entities.push_back(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the shader uniform
|
// Load the shader
|
||||||
m_shader.setUniform("texture", sf::Shader::CurrentTexture);
|
if (!(m_shader = sf::Shader::loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment)))
|
||||||
|
return false;
|
||||||
|
m_shader->setUniform("texture", sf::Shader::CurrentTexture);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onUpdate(float time, float x, float y) override
|
void onUpdate(float time, float x, float y) override
|
||||||
{
|
{
|
||||||
m_shader.setUniform("edge_threshold", 1 - (x + y) / 2);
|
m_shader->setUniform("edge_threshold", 1 - (x + y) / 2);
|
||||||
|
|
||||||
// Update the position of the moving entities
|
// Update the position of the moving entities
|
||||||
for (std::size_t i = 0; i < m_entities.size(); ++i)
|
for (std::size_t i = 0; i < m_entities.size(); ++i)
|
||||||
@ -225,26 +235,26 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render the updated scene to the off-screen surface
|
// Render the updated scene to the off-screen surface
|
||||||
m_surface.clear(sf::Color::White);
|
m_surface->clear(sf::Color::White);
|
||||||
m_surface.draw(*m_backgroundSprite);
|
m_surface->draw(*m_backgroundSprite);
|
||||||
for (const sf::Sprite& entity : m_entities)
|
for (const sf::Sprite& entity : m_entities)
|
||||||
m_surface.draw(entity);
|
m_surface->draw(entity);
|
||||||
m_surface.display();
|
m_surface->display();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override
|
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const override
|
||||||
{
|
{
|
||||||
states.shader = &m_shader;
|
states.shader = &*m_shader;
|
||||||
target.draw(sf::Sprite(m_surface.getTexture()), states);
|
target.draw(sf::Sprite(m_surface->getTexture()), states);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::RenderTexture m_surface{sf::RenderTexture::create({800, 600}).value()};
|
std::optional<sf::RenderTexture> m_surface;
|
||||||
sf::Texture m_backgroundTexture;
|
sf::Texture m_backgroundTexture;
|
||||||
sf::Texture m_entityTexture;
|
sf::Texture m_entityTexture;
|
||||||
std::optional<sf::Sprite> m_backgroundSprite;
|
std::optional<sf::Sprite> m_backgroundSprite;
|
||||||
std::vector<sf::Sprite> m_entities;
|
std::vector<sf::Sprite> m_entities;
|
||||||
sf::Shader m_shader{sf::Shader::loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment).value()};
|
std::optional<sf::Shader> m_shader;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -278,10 +288,9 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Load the shader
|
// Load the shader
|
||||||
m_shader = sf::Shader::loadFromFile("resources/billboard.vert",
|
if (!(m_shader = sf::Shader::loadFromFile("resources/billboard.vert",
|
||||||
"resources/billboard.geom",
|
"resources/billboard.geom",
|
||||||
"resources/billboard.frag");
|
"resources/billboard.frag")))
|
||||||
if (!m_shader)
|
|
||||||
return false;
|
return false;
|
||||||
m_shader->setUniform("texture", sf::Shader::CurrentTexture);
|
m_shader->setUniform("texture", sf::Shader::CurrentTexture);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user