SFML/examples/shader/Effect.hpp

80 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
2022-07-05 00:20:58 +08:00
2012-08-04 05:50:34 +08:00
#include <cassert>
#include <string>
////////////////////////////////////////////////////////////
// Base class for effects
////////////////////////////////////////////////////////////
class Effect : public sf::Drawable
{
public:
~Effect() override = default;
2012-08-04 05:50:34 +08:00
static void setFont(const sf::Font& font)
{
s_font = &font;
}
const std::string& getName() const
{
return m_name;
}
void load()
{
m_isLoaded = sf::Shader::isAvailable() && onLoad();
}
void update(float time, float x, float y)
{
if (m_isLoaded)
onUpdate(time, x, y);
}
void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override
{
if (m_isLoaded)
{
onDraw(target, states);
}
else
{
2012-08-04 05:50:34 +08:00
sf::Text error("Shader not\nsupported", getFont());
error.setPosition({320.f, 200.f});
error.setCharacterSize(36);
target.draw(error, states);
}
}
protected:
2022-07-05 00:20:58 +08:00
Effect(const std::string& name) : m_name(name), m_isLoaded(false)
{
}
2012-08-04 05:50:34 +08:00
static const sf::Font& getFont()
{
2021-12-03 22:45:32 +08:00
assert(s_font != nullptr);
2012-08-04 05:50:34 +08:00
return *s_font;
}
private:
// Virtual functions to be implemented in derived effects
2022-07-05 00:20:58 +08:00
virtual bool onLoad() = 0;
virtual void onUpdate(float time, float x, float y) = 0;
virtual void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const = 0;
private:
std::string m_name;
2022-07-05 00:20:58 +08:00
bool m_isLoaded;
2012-08-04 05:50:34 +08:00
2022-10-24 06:53:36 +08:00
// NOLINTNEXTLINE(readability-identifier-naming)
2012-08-04 05:50:34 +08:00
static const sf::Font* s_font;
};