2022-12-21 12:17:38 +08:00
|
|
|
#pragma once
|
2011-12-10 20:02:38 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Graphics.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
2012-08-04 05:50:34 +08:00
|
|
|
#include <cassert>
|
2011-12-10 20:02:38 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Base class for effects
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class Effect : public sf::Drawable
|
|
|
|
{
|
2014-09-30 22:07:25 +08:00
|
|
|
public:
|
2023-01-12 15:27:56 +08:00
|
|
|
~Effect() override = default;
|
2011-12-10 20:02:38 +08:00
|
|
|
|
2012-08-04 05:50:34 +08:00
|
|
|
static void setFont(const sf::Font& font)
|
|
|
|
{
|
|
|
|
s_font = &font;
|
|
|
|
}
|
|
|
|
|
2012-03-12 02:10:37 +08:00
|
|
|
const std::string& getName() const
|
2011-12-10 20:02:38 +08:00
|
|
|
{
|
2012-03-09 08:22:47 +08:00
|
|
|
return m_name;
|
2011-12-10 20:02:38 +08:00
|
|
|
}
|
|
|
|
|
2012-03-12 02:10:37 +08:00
|
|
|
void load()
|
2011-12-10 20:02:38 +08:00
|
|
|
{
|
2012-03-12 02:10:37 +08:00
|
|
|
m_isLoaded = sf::Shader::isAvailable() && onLoad();
|
2011-12-10 20:02:38 +08:00
|
|
|
}
|
|
|
|
|
2012-03-12 02:10:37 +08:00
|
|
|
void update(float time, float x, float y)
|
2011-12-10 20:02:38 +08:00
|
|
|
{
|
2012-03-09 08:22:47 +08:00
|
|
|
if (m_isLoaded)
|
2012-03-12 02:10:37 +08:00
|
|
|
onUpdate(time, x, y);
|
2011-12-10 20:02:38 +08:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:43:18 +08:00
|
|
|
void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override
|
2011-12-10 20:02:38 +08:00
|
|
|
{
|
2012-03-09 08:22:47 +08:00
|
|
|
if (m_isLoaded)
|
2011-12-10 20:02:38 +08:00
|
|
|
{
|
2012-03-12 02:10:37 +08:00
|
|
|
onDraw(target, states);
|
2011-12-10 20:02:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-04 05:50:34 +08:00
|
|
|
sf::Text error("Shader not\nsupported", getFont());
|
2021-12-15 17:29:34 +08:00
|
|
|
error.setPosition({320.f, 200.f});
|
2012-03-12 02:10:37 +08:00
|
|
|
error.setCharacterSize(36);
|
|
|
|
target.draw(error, states);
|
2011-12-10 20:02:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
protected:
|
2022-07-05 00:20:58 +08:00
|
|
|
Effect(const std::string& name) : m_name(name), m_isLoaded(false)
|
2011-12-10 20:02:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
private:
|
2011-12-10 20:02:38 +08:00
|
|
|
// 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;
|
2022-02-16 01:43:18 +08:00
|
|
|
virtual void onDraw(sf::RenderTarget& target, const sf::RenderStates& states) const = 0;
|
2011-12-10 20:02:38 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
private:
|
2012-03-09 08:22:47 +08:00
|
|
|
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;
|
2011-12-10 20:02:38 +08:00
|
|
|
};
|