1 | #ifndef EFFECT_HPP |
2 | #define EFFECT_HPP |
3 | |
4 | //////////////////////////////////////////////////////////// |
5 | // Headers |
6 | //////////////////////////////////////////////////////////// |
7 | #include <SFML/Graphics.hpp> |
8 | #include <cassert> |
9 | #include <string> |
10 | |
11 | |
12 | //////////////////////////////////////////////////////////// |
13 | // Base class for effects |
14 | //////////////////////////////////////////////////////////// |
15 | class Effect : public sf::Drawable |
16 | { |
17 | public: |
18 | |
19 | virtual ~Effect() |
20 | { |
21 | } |
22 | |
23 | static void setFont(const sf::Font& font) |
24 | { |
25 | s_font = &font; |
26 | } |
27 | |
28 | const std::string& getName() const |
29 | { |
30 | return m_name; |
31 | } |
32 | |
33 | void load() |
34 | { |
35 | m_isLoaded = sf::Shader::isAvailable() && onLoad(); |
36 | } |
37 | |
38 | void update(float time, float x, float y) |
39 | { |
40 | if (m_isLoaded) |
41 | onUpdate(time, x, y); |
42 | } |
43 | |
44 | void draw(sf::RenderTarget& target, sf::RenderStates states) const |
45 | { |
46 | if (m_isLoaded) |
47 | { |
48 | onDraw(target, states); |
49 | } |
50 | else |
51 | { |
52 | sf::Text error("Shader not\nsupported" , getFont()); |
53 | error.setPosition(320.f, 200.f); |
54 | error.setCharacterSize(36); |
55 | target.draw(error, states); |
56 | } |
57 | } |
58 | |
59 | protected: |
60 | |
61 | Effect(const std::string& name) : |
62 | m_name(name), |
63 | m_isLoaded(false) |
64 | { |
65 | } |
66 | |
67 | static const sf::Font& getFont() |
68 | { |
69 | assert(s_font != NULL); |
70 | return *s_font; |
71 | } |
72 | |
73 | private: |
74 | |
75 | // Virtual functions to be implemented in derived effects |
76 | virtual bool onLoad() = 0; |
77 | virtual void onUpdate(float time, float x, float y) = 0; |
78 | virtual void onDraw(sf::RenderTarget& target, sf::RenderStates states) const = 0; |
79 | |
80 | private: |
81 | |
82 | std::string m_name; |
83 | bool m_isLoaded; |
84 | |
85 | static const sf::Font* s_font; |
86 | }; |
87 | |
88 | #endif // EFFECT_HPP |
89 | |