-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_state.cpp
130 lines (99 loc) · 3.58 KB
/
menu_state.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "menu_state.h"
const size_t CHARACTER_SIZE = 80;
const float MENU_POSITION_X = 350.f;
const float MENU_POSITION_Y = 150.f;
namespace OptionNames {
const std::string Play = "Play";
const std::string Scores = "Scores";
const std::string Exit = "Exit";
}
MenuState::MenuState(Game *state_holder) :
GameState(state_holder), selected(0) {
sf::Texture *texture = game->getResourseManager().getTexture(Textures::Code::MenuBackground);
sf::Font *font = game->getResourseManager().getFont(Fonts::Code::Menu);
backgroundSprite.setTexture(*texture);
sf::Text playOption;
playOption.setFont(*font);
playOption.setString(OptionNames::Play);
playOption.setCharacterSize(CHARACTER_SIZE);
playOption.setPosition(sf::Vector2f(MENU_POSITION_X, MENU_POSITION_Y));
options.push_back(playOption);
sf::Text scoresOption;
scoresOption.setFont(*font);
scoresOption.setString(OptionNames::Scores);
scoresOption.setCharacterSize(CHARACTER_SIZE);
scoresOption.setPosition(playOption.getPosition() + sf::Vector2f(0.f, CHARACTER_SIZE));
options.push_back(scoresOption);
sf::Text exitOption;
exitOption.setFont(*font);
exitOption.setCharacterSize(CHARACTER_SIZE);
exitOption.setString(OptionNames::Exit);
exitOption.setPosition(scoresOption.getPosition() + sf::Vector2f(0.f, CHARACTER_SIZE));
options.push_back(exitOption);
updateOptions();
}
MenuState::~MenuState() {
delete options[0].getFont();
delete backgroundSprite.getTexture();
}
void MenuState::updateOptions() {
for(sf::Text &option : options)
option.setColor(sf::Color::Blue);
options[selected].setColor(sf::Color::Yellow);
}
/** Nothing to do in update, cause we must update text only in case
* when user provide input.
* Not on every loop iteration **/
void MenuState::update() { }
void MenuState::render() {
game->getWindow()->clear();
game->getWindow()->draw(backgroundSprite);
for(sf::Text &option : options) {
game->getWindow()->draw(option);
}
game->getWindow()->display();
}
void MenuState::processEvents() {
sf::Event event;
while (game->getWindow()->pollEvent(event)) {
switch (event.type)
{
case sf::Event::Closed:
game->popState();
return;
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Up:
selected = selected > 0 ? selected - 1 :
options.size() - 1;
updateOptions();
break;
case sf::Keyboard::Down:
selected = (selected + 1) % options.size();
updateOptions();
break;
case sf::Keyboard::Return:
switch (selected)
{
case Play:
game->changeState(States::Game);
return;
case Scores:
game->addState(States::Scores);
break;
case Exit:
game->popState();
return;
default:
break;
}
default:
break;
}
break;
default:
break;
}
}
}