/
screen.hpp
69 lines (55 loc) · 1.94 KB
/
screen.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <functional>
#include <SFML/Graphics.hpp>
struct Event {
enum Type {
MENU_START_NEW_GAME,
MENU_TRY_AGAIN,
MENU_QUIT,
MENU_LEVEL_CODE,
MENU_CREDITS,
LEVEL_FINISHED,
SHOW_MENU_WITH_TRY_AGAIN,
SHOW_CLEAN_MENU,
SHOW_MENU
} type;
void *data = 0;
};
class Screen : public sf::Drawable {
protected:
std::function<void (Event)> eventReceiver;
public:
Screen(const sf::Color &fillColor, const sf::Color &outlineColor, const sf::Font &font)
: eventReceiver([](Event){}),
fillColor(fillColor),
outlineColor(outlineColor),
font(font)
{ }
void setEventReceiver(std::function<void (Event)> receiver) {
eventReceiver = receiver;
}
void drawText(sf::RenderTarget &target, sf::RenderStates states, sf::String text, unsigned int characterSize, float outlineThickness, float x, float y) const {
sf::Text sfText(text, font, characterSize);
sfText.setOutlineThickness(outlineThickness);
sfText.setOutlineColor(outlineColor);
sfText.setFillColor(fillColor);
sfText.setPosition(x, y);
target.draw(sfText, states);
}
void drawCenteredText(sf::RenderTarget &target, sf::RenderStates states, sf::String text, unsigned int characterSize, float outlineThickness, float y) const {
sf::Text sfText(text, font, characterSize);
sfText.setOutlineThickness(outlineThickness);
sfText.setOutlineColor(outlineColor);
sfText.setFillColor(fillColor);
sfText.setPosition((target.getSize().x - sfText.getGlobalBounds().width) / 2, y);
target.draw(sfText, states);
}
virtual void update() { }
virtual void onActivated() { }
virtual void processEvent(const sf::Event &event) = 0;
virtual ~Screen() = default;
private:
const sf::Color &fillColor;
const sf::Color &outlineColor;
const sf::Font &font;
};