2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
2023-04-24 20:13:52 +08:00
|
|
|
#include "Effect.hpp"
|
|
|
|
|
2021-12-22 01:57:38 +08:00
|
|
|
#include <array>
|
2022-06-13 02:22:04 +08:00
|
|
|
#include <random>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
|
2022-06-13 02:22:04 +08:00
|
|
|
namespace
|
|
|
|
{
|
2022-07-05 00:20:58 +08:00
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 rng(rd());
|
|
|
|
} // namespace
|
2022-06-13 02:22:04 +08:00
|
|
|
|
2021-12-03 22:45:32 +08:00
|
|
|
const sf::Font* Effect::s_font = nullptr;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// "Pixelate" fragment shader
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class Pixelate : public Effect
|
|
|
|
{
|
|
|
|
public:
|
2022-07-05 00:20:58 +08:00
|
|
|
Pixelate() : Effect("Pixelate")
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
bool onLoad() override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Load the texture and initialize the sprite
|
|
|
|
if (!m_texture.loadFromFile("resources/background.jpg"))
|
|
|
|
return false;
|
2023-04-03 10:24:02 +08:00
|
|
|
m_sprite.emplace(m_texture);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Load the shader
|
2024-01-27 22:36:42 +08:00
|
|
|
if (!m_shader.loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment))
|
2014-09-30 22:07:25 +08:00
|
|
|
return false;
|
2015-10-12 13:54:21 +08:00
|
|
|
m_shader.setUniform("texture", sf::Shader::CurrentTexture);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
void onUpdate(float, float x, float y) override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2015-10-12 13:54:21 +08:00
|
|
|
m_shader.setUniform("pixel_threshold", (x + y) / 30);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:43:18 +08:00
|
|
|
void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2022-02-16 01:43:18 +08:00
|
|
|
sf::RenderStates statesCopy(states);
|
|
|
|
statesCopy.shader = &m_shader;
|
2023-04-03 10:24:02 +08:00
|
|
|
target.draw(*m_sprite, statesCopy);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-04-03 10:24:02 +08:00
|
|
|
sf::Texture m_texture;
|
|
|
|
std::optional<sf::Sprite> m_sprite;
|
|
|
|
sf::Shader m_shader;
|
2014-09-30 22:07:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// "Wave" vertex shader + "blur" fragment shader
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class WaveBlur : public Effect
|
|
|
|
{
|
|
|
|
public:
|
2022-09-06 13:18:29 +08:00
|
|
|
WaveBlur() : Effect("Wave + Blur"), m_text(getFont())
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
bool onLoad() override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Create the text
|
2022-07-05 00:20:58 +08:00
|
|
|
m_text.setString(
|
|
|
|
"Praesent suscipit augue in velit pulvinar hendrerit varius purus aliquam.\n"
|
|
|
|
"Mauris mi odio, bibendum quis fringilla a, laoreet vel orci. Proin vitae vulputate tortor.\n"
|
|
|
|
"Praesent cursus ultrices justo, ut feugiat ante vehicula quis.\n"
|
|
|
|
"Donec fringilla scelerisque mauris et viverra.\n"
|
|
|
|
"Maecenas adipiscing ornare scelerisque. Nullam at libero elit.\n"
|
|
|
|
"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\n"
|
|
|
|
"Nullam leo urna, tincidunt id semper eget, ultricies sed mi.\n"
|
|
|
|
"Morbi mauris massa, commodo id dignissim vel, lobortis et elit.\n"
|
|
|
|
"Fusce vel libero sed neque scelerisque venenatis.\n"
|
|
|
|
"Integer mattis tincidunt quam vitae iaculis.\n"
|
|
|
|
"Vivamus fringilla sem non velit venenatis fermentum.\n"
|
|
|
|
"Vivamus varius tincidunt nisi id vehicula.\n"
|
|
|
|
"Integer ullamcorper, enim vitae euismod rutrum, massa nisl semper ipsum,\n"
|
|
|
|
"vestibulum sodales sem ante in massa.\n"
|
|
|
|
"Vestibulum in augue non felis convallis viverra.\n"
|
|
|
|
"Mauris ultricies dolor sed massa convallis sed aliquet augue fringilla.\n"
|
|
|
|
"Duis erat eros, porta in accumsan in, blandit quis sem.\n"
|
|
|
|
"In hac habitasse platea dictumst. Etiam fringilla est id odio dapibus sit amet semper dui laoreet.\n");
|
2014-09-30 22:07:25 +08:00
|
|
|
m_text.setCharacterSize(22);
|
2021-12-15 17:29:34 +08:00
|
|
|
m_text.setPosition({30.f, 20.f});
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Load the shader
|
2023-01-20 09:53:53 +08:00
|
|
|
return m_shader.loadFromFile("resources/wave.vert", "resources/blur.frag");
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
void onUpdate(float time, float x, float y) override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2015-10-12 13:54:21 +08:00
|
|
|
m_shader.setUniform("wave_phase", time);
|
|
|
|
m_shader.setUniform("wave_amplitude", sf::Vector2f(x * 40, y * 40));
|
|
|
|
m_shader.setUniform("blur_radius", (x + y) * 0.008f);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:43:18 +08:00
|
|
|
void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2022-02-16 01:43:18 +08:00
|
|
|
sf::RenderStates statesCopy(states);
|
|
|
|
statesCopy.shader = &m_shader;
|
|
|
|
target.draw(m_text, statesCopy);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-07-05 00:20:58 +08:00
|
|
|
sf::Text m_text;
|
2014-09-30 22:07:25 +08:00
|
|
|
sf::Shader m_shader;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// "Storm" vertex shader + "blink" fragment shader
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class StormBlink : public Effect
|
|
|
|
{
|
|
|
|
public:
|
2022-07-05 00:20:58 +08:00
|
|
|
StormBlink() : Effect("Storm + Blink")
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
bool onLoad() override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2022-10-24 06:53:36 +08:00
|
|
|
std::uniform_real_distribution<float> xDistribution(0, 800);
|
|
|
|
std::uniform_real_distribution<float> yDistribution(0, 600);
|
|
|
|
std::uniform_int_distribution<std::uint16_t> colorDistribution(0, 255);
|
2022-06-13 02:22:04 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Create the points
|
2022-11-28 03:17:27 +08:00
|
|
|
m_points.setPrimitiveType(sf::PrimitiveType::Points);
|
2014-09-30 22:07:25 +08:00
|
|
|
for (int i = 0; i < 40000; ++i)
|
|
|
|
{
|
2023-02-15 11:42:12 +08:00
|
|
|
const auto x = xDistribution(rng);
|
|
|
|
const auto y = yDistribution(rng);
|
|
|
|
const auto r = static_cast<std::uint8_t>(colorDistribution(rng));
|
|
|
|
const auto g = static_cast<std::uint8_t>(colorDistribution(rng));
|
|
|
|
const auto b = static_cast<std::uint8_t>(colorDistribution(rng));
|
2014-09-30 22:07:25 +08:00
|
|
|
m_points.append(sf::Vertex(sf::Vector2f(x, y), sf::Color(r, g, b)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the shader
|
2023-01-20 09:53:53 +08:00
|
|
|
return m_shader.loadFromFile("resources/storm.vert", "resources/blink.frag");
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
void onUpdate(float time, float x, float y) override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2023-02-15 11:42:12 +08:00
|
|
|
const float radius = 200 + std::cos(time) * 150;
|
2015-10-12 13:54:21 +08:00
|
|
|
m_shader.setUniform("storm_position", sf::Vector2f(x * 800, y * 600));
|
|
|
|
m_shader.setUniform("storm_inner_radius", radius / 3);
|
|
|
|
m_shader.setUniform("storm_total_radius", radius);
|
|
|
|
m_shader.setUniform("blink_alpha", 0.5f + std::cos(time * 3) * 0.25f);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:43:18 +08:00
|
|
|
void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2022-02-16 01:43:18 +08:00
|
|
|
sf::RenderStates statesCopy(states);
|
|
|
|
statesCopy.shader = &m_shader;
|
|
|
|
target.draw(m_points, statesCopy);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sf::VertexArray m_points;
|
2022-07-05 00:20:58 +08:00
|
|
|
sf::Shader m_shader;
|
2014-09-30 22:07:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// "Edge" post-effect fragment shader
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class Edge : public Effect
|
|
|
|
{
|
|
|
|
public:
|
2022-07-05 00:20:58 +08:00
|
|
|
Edge() : Effect("Edge Post-effect")
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
bool onLoad() override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Create the off-screen surface
|
2022-03-30 08:53:17 +08:00
|
|
|
if (!m_surface.create({800, 600}))
|
2014-09-30 22:07:25 +08:00
|
|
|
return false;
|
|
|
|
m_surface.setSmooth(true);
|
|
|
|
|
|
|
|
// Load the textures
|
|
|
|
if (!m_backgroundTexture.loadFromFile("resources/sfml.png"))
|
|
|
|
return false;
|
|
|
|
m_backgroundTexture.setSmooth(true);
|
|
|
|
if (!m_entityTexture.loadFromFile("resources/devices.png"))
|
|
|
|
return false;
|
|
|
|
m_entityTexture.setSmooth(true);
|
|
|
|
|
|
|
|
// Initialize the background sprite
|
2023-04-03 10:24:02 +08:00
|
|
|
m_backgroundSprite.emplace(m_backgroundTexture);
|
|
|
|
m_backgroundSprite->setPosition({135.f, 100.f});
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Load the moving entities
|
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
{
|
2023-02-15 11:42:12 +08:00
|
|
|
const sf::Sprite entity(m_entityTexture, sf::IntRect({96 * i, 0}, {96, 96}));
|
2014-09-30 22:07:25 +08:00
|
|
|
m_entities.push_back(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the shader
|
2024-01-27 22:36:42 +08:00
|
|
|
if (!m_shader.loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment))
|
2014-09-30 22:07:25 +08:00
|
|
|
return false;
|
2015-10-12 13:54:21 +08:00
|
|
|
m_shader.setUniform("texture", sf::Shader::CurrentTexture);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
void onUpdate(float time, float x, float y) override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2015-10-12 13:54:21 +08:00
|
|
|
m_shader.setUniform("edge_threshold", 1 - (x + y) / 2);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Update the position of the moving entities
|
|
|
|
for (std::size_t i = 0; i < m_entities.size(); ++i)
|
|
|
|
{
|
|
|
|
sf::Vector2f position;
|
2022-07-05 00:20:58 +08:00
|
|
|
position.x = std::cos(0.25f * (time * static_cast<float>(i) + static_cast<float>(m_entities.size() - i))) * 300 +
|
|
|
|
350;
|
|
|
|
position.y = std::sin(0.25f * (time * static_cast<float>(m_entities.size() - i) + static_cast<float>(i))) * 200 +
|
|
|
|
250;
|
2014-09-30 22:07:25 +08:00
|
|
|
m_entities[i].setPosition(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the updated scene to the off-screen surface
|
|
|
|
m_surface.clear(sf::Color::White);
|
2023-04-03 10:24:02 +08:00
|
|
|
m_surface.draw(*m_backgroundSprite);
|
2021-12-09 03:24:23 +08:00
|
|
|
for (const sf::Sprite& entity : m_entities)
|
|
|
|
m_surface.draw(entity);
|
2014-09-30 22:07:25 +08:00
|
|
|
m_surface.display();
|
|
|
|
}
|
|
|
|
|
2022-02-16 01:43:18 +08:00
|
|
|
void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2022-02-16 01:43:18 +08:00
|
|
|
sf::RenderStates statesCopy(states);
|
|
|
|
statesCopy.shader = &m_shader;
|
|
|
|
target.draw(sf::Sprite(m_surface.getTexture()), statesCopy);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-04-03 10:24:02 +08:00
|
|
|
sf::RenderTexture m_surface;
|
|
|
|
sf::Texture m_backgroundTexture;
|
|
|
|
sf::Texture m_entityTexture;
|
|
|
|
std::optional<sf::Sprite> m_backgroundSprite;
|
|
|
|
std::vector<sf::Sprite> m_entities;
|
|
|
|
sf::Shader m_shader;
|
2014-09-30 22:07:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-15 21:29:23 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// "Geometry" geometry shader example
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class Geometry : public Effect
|
|
|
|
{
|
|
|
|
public:
|
2022-11-28 03:17:27 +08:00
|
|
|
Geometry() : Effect("Geometry Shader Billboards"), m_pointCloud(sf::PrimitiveType::Points, 10000)
|
2014-05-15 21:29:23 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
bool onLoad() override
|
2014-05-15 21:29:23 +08:00
|
|
|
{
|
|
|
|
// Check if geometry shaders are supported
|
|
|
|
if (!sf::Shader::isGeometryAvailable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Move the points in the point cloud to random positions
|
2022-02-17 08:14:30 +08:00
|
|
|
for (std::size_t i = 0; i < 10000; ++i)
|
2014-05-15 21:29:23 +08:00
|
|
|
{
|
|
|
|
// Spread the coordinates from -480 to +480
|
|
|
|
// So they'll always fill the viewport at 800x600
|
2023-03-04 12:40:36 +08:00
|
|
|
std::uniform_real_distribution<float> positionDistribution(-480, 480);
|
2023-05-03 03:04:33 +08:00
|
|
|
m_pointCloud[i].position = {positionDistribution(rng), positionDistribution(rng)};
|
2014-05-15 21:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the texture
|
|
|
|
if (!m_logoTexture.loadFromFile("resources/logo.png"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Load the shader
|
|
|
|
if (!m_shader.loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag"))
|
|
|
|
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));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:37:49 +08:00
|
|
|
void onUpdate(float /*time*/, float x, float y) override
|
2014-05-15 21:29:23 +08:00
|
|
|
{
|
|
|
|
// Reset our transformation matrix
|
|
|
|
m_transform = sf::Transform::Identity;
|
|
|
|
// Move to the center of the window
|
2021-12-15 17:29:34 +08:00
|
|
|
m_transform.translate({400.f, 300.f});
|
2014-05-15 21:29:23 +08:00
|
|
|
// Rotate everything based on cursor position
|
2022-01-23 09:11:08 +08:00
|
|
|
m_transform.rotate(sf::degrees(x * 360.f));
|
2014-05-15 21:29:23 +08:00
|
|
|
|
|
|
|
// Adjust billboard size to scale between 25 and 75
|
2023-02-15 11:42:12 +08:00
|
|
|
const float size = 25 + std::abs(y) * 50;
|
2014-05-15 21:29:23 +08:00
|
|
|
|
|
|
|
// Update the shader parameter
|
|
|
|
m_shader.setUniform("size", sf::Vector2f(size, size));
|
|
|
|
}
|
|
|
|
|
2022-02-16 01:43:18 +08:00
|
|
|
void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const override
|
2014-05-15 21:29:23 +08:00
|
|
|
{
|
2022-02-16 01:43:18 +08:00
|
|
|
sf::RenderStates statesCopy(states);
|
|
|
|
|
2014-05-15 21:29:23 +08:00
|
|
|
// Prepare the render state
|
2022-07-05 00:20:58 +08:00
|
|
|
statesCopy.shader = &m_shader;
|
|
|
|
statesCopy.texture = &m_logoTexture;
|
2022-02-16 01:43:18 +08:00
|
|
|
statesCopy.transform = m_transform;
|
2014-05-15 21:29:23 +08:00
|
|
|
|
|
|
|
// Draw the point cloud
|
2022-02-16 01:43:18 +08:00
|
|
|
target.draw(m_pointCloud, statesCopy);
|
2014-05-15 21:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-07-05 00:20:58 +08:00
|
|
|
sf::Texture m_logoTexture;
|
|
|
|
sf::Transform m_transform;
|
|
|
|
sf::Shader m_shader;
|
2014-05-15 21:29:23 +08:00
|
|
|
sf::VertexArray m_pointCloud;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Create the main window
|
2022-07-05 00:20:58 +08:00
|
|
|
sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML Shader", sf::Style::Titlebar | sf::Style::Close);
|
2014-09-30 22:07:25 +08:00
|
|
|
window.setVerticalSyncEnabled(true);
|
|
|
|
|
|
|
|
// Load the application font and pass it to the Effect class
|
|
|
|
sf::Font font;
|
2020-12-13 04:40:52 +08:00
|
|
|
if (!font.loadFromFile("resources/tuffy.ttf"))
|
2014-09-30 22:07:25 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
Effect::setFont(font);
|
|
|
|
|
|
|
|
// Create the effects
|
2022-07-05 00:20:58 +08:00
|
|
|
Pixelate pixelateEffect;
|
|
|
|
WaveBlur waveBlurEffect;
|
2021-12-22 01:57:38 +08:00
|
|
|
StormBlink stormBlinkEffect;
|
2022-07-05 00:20:58 +08:00
|
|
|
Edge edgeEffect;
|
|
|
|
Geometry geometryEffect;
|
2021-12-22 01:57:38 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
const std::array<Effect*, 5> effects{&pixelateEffect, &waveBlurEffect, &stormBlinkEffect, &edgeEffect, &geometryEffect};
|
2021-12-22 01:57:38 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
std::size_t current = 0;
|
|
|
|
|
|
|
|
// Initialize them
|
2021-12-09 03:24:23 +08:00
|
|
|
for (Effect* effect : effects)
|
|
|
|
effect->load();
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create the messages background
|
|
|
|
sf::Texture textBackgroundTexture;
|
|
|
|
if (!textBackgroundTexture.loadFromFile("resources/text-background.png"))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
sf::Sprite textBackground(textBackgroundTexture);
|
2021-12-15 17:29:34 +08:00
|
|
|
textBackground.setPosition({0.f, 520.f});
|
2014-09-30 22:07:25 +08:00
|
|
|
textBackground.setColor(sf::Color(255, 255, 255, 200));
|
|
|
|
|
|
|
|
// Create the description text
|
2022-09-06 13:18:29 +08:00
|
|
|
sf::Text description(font, "Current effect: " + effects[current]->getName(), 20);
|
2021-12-15 17:29:34 +08:00
|
|
|
description.setPosition({10.f, 530.f});
|
2015-12-29 21:14:43 +08:00
|
|
|
description.setFillColor(sf::Color(80, 80, 80));
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create the instructions text
|
2022-09-06 13:18:29 +08:00
|
|
|
sf::Text instructions(font, "Press left and right arrows to change the current shader", 20);
|
2021-12-15 17:29:34 +08:00
|
|
|
instructions.setPosition({280.f, 555.f});
|
2015-12-29 21:14:43 +08:00
|
|
|
instructions.setFillColor(sf::Color(80, 80, 80));
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Start the game loop
|
2023-02-15 11:42:12 +08:00
|
|
|
const sf::Clock clock;
|
2014-09-30 22:07:25 +08:00
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
// Process events
|
2022-06-07 12:48:48 +08:00
|
|
|
for (sf::Event event; window.pollEvent(event);)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Close window: exit
|
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
window.close();
|
|
|
|
|
|
|
|
if (event.type == sf::Event::KeyPressed)
|
|
|
|
{
|
|
|
|
switch (event.key.code)
|
|
|
|
{
|
|
|
|
// Escape key: exit
|
2023-12-20 06:45:33 +08:00
|
|
|
case sf::Keyboard::Key::Escape:
|
2014-09-30 22:07:25 +08:00
|
|
|
window.close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Left arrow key: previous shader
|
2023-12-20 06:45:33 +08:00
|
|
|
case sf::Keyboard::Key::Left:
|
2014-09-30 22:07:25 +08:00
|
|
|
if (current == 0)
|
|
|
|
current = effects.size() - 1;
|
|
|
|
else
|
2022-02-17 08:14:30 +08:00
|
|
|
--current;
|
2014-09-30 22:07:25 +08:00
|
|
|
description.setString("Current effect: " + effects[current]->getName());
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Right arrow key: next shader
|
2023-12-20 06:45:33 +08:00
|
|
|
case sf::Keyboard::Key::Right:
|
2014-09-30 22:07:25 +08:00
|
|
|
if (current == effects.size() - 1)
|
|
|
|
current = 0;
|
|
|
|
else
|
2022-02-17 08:14:30 +08:00
|
|
|
++current;
|
2014-09-30 22:07:25 +08:00
|
|
|
description.setString("Current effect: " + effects[current]->getName());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the current example
|
2023-03-02 13:50:45 +08:00
|
|
|
const auto [x, y] = sf::Vector2f(sf::Mouse::getPosition(window)).cwiseDiv(sf::Vector2f(window.getSize()));
|
2014-09-30 22:07:25 +08:00
|
|
|
effects[current]->update(clock.getElapsedTime().asSeconds(), x, y);
|
|
|
|
|
|
|
|
// Clear the window
|
2022-07-05 00:20:58 +08:00
|
|
|
if (effects[current]->getName() == "Edge Post-effect")
|
|
|
|
{
|
2020-12-13 04:40:52 +08:00
|
|
|
window.clear(sf::Color::White);
|
2022-07-05 00:20:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-13 04:40:52 +08:00
|
|
|
window.clear(sf::Color(50, 50, 50));
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Draw the current example
|
|
|
|
window.draw(*effects[current]);
|
|
|
|
|
|
|
|
// Draw the text
|
|
|
|
window.draw(textBackground);
|
|
|
|
window.draw(instructions);
|
|
|
|
window.draw(description);
|
|
|
|
|
|
|
|
// Finally, display the rendered frame on screen
|
|
|
|
window.display();
|
|
|
|
}
|
|
|
|
}
|