mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
4315c3d290
This change was made in 359fe90
due to recommendations from tooling.
On its face this change makes sense since it removes a copy that
isn't always necessary. In practice it caused ergonomic issues due
to now being forced to make a copy of the render states when needed.
The performance gains of eliding this copy are unsubstantiated. We
have not done any profiling to measure its impact. For lack of such
measurements I'd rather err on the side of improved user experience.
If future benchmarks prove this copy is rather expensive then we
can reconsider removing it with that evidence in mind.
79 lines
1.9 KiB
C++
79 lines
1.9 KiB
C++
#pragma once
|
|
|
|
////////////////////////////////////////////////////////////
|
|
// Headers
|
|
////////////////////////////////////////////////////////////
|
|
#include <SFML/Graphics.hpp>
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
// Base class for effects
|
|
////////////////////////////////////////////////////////////
|
|
class Effect : public sf::Drawable
|
|
{
|
|
public:
|
|
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, sf::RenderStates states) const override
|
|
{
|
|
if (m_isLoaded)
|
|
{
|
|
onDraw(target, states);
|
|
}
|
|
else
|
|
{
|
|
sf::Text error(getFont(), "Shader not\nsupported");
|
|
error.setPosition({320.f, 200.f});
|
|
error.setCharacterSize(36);
|
|
target.draw(error, states);
|
|
}
|
|
}
|
|
|
|
protected:
|
|
Effect(std::string name) : m_name(std::move(name))
|
|
{
|
|
}
|
|
|
|
static const sf::Font& getFont()
|
|
{
|
|
assert(s_font != nullptr && "Cannot get font until setFont() is called");
|
|
return *s_font;
|
|
}
|
|
|
|
private:
|
|
// Virtual functions to be implemented in derived effects
|
|
virtual bool onLoad() = 0;
|
|
virtual void onUpdate(float time, float x, float y) = 0;
|
|
virtual void onDraw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
|
|
|
|
std::string m_name;
|
|
bool m_isLoaded{};
|
|
|
|
// NOLINTNEXTLINE(readability-identifier-naming)
|
|
static inline const sf::Font* s_font{};
|
|
};
|