-
Notifications
You must be signed in to change notification settings - Fork 1
/
menuManager.cpp
245 lines (233 loc) · 6.82 KB
/
menuManager.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <cmath>
#include <sstream>
#include "sound.h"
#include "menuManager.h"
#include "manager.h"
MenuManager::MenuManager() :
env( SDL_putenv(const_cast<char*>("SDL_VIDEO_CENTERED=center")) ),
screen( IOManager::getInstance().getScreen() ),
clock( Clock::getInstance() ),
io( IOManager::getInstance() ),
backColor(),
menu(),
numberOfOrbs(0),
soundEnabled(true)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw string("Unable to initialize SDL: ");
}
backColor.r = Gamedata::getInstance()->getXmlInt("backRed");
backColor.g = Gamedata::getInstance()->getXmlInt("backGreen");
backColor.b = Gamedata::getInstance()->getXmlInt("backBlue");
atexit(SDL_Quit);
}
void MenuManager::drawBackground() const {
menu.drawBG();
}
void MenuManager::getNumberOfItems() {
// IOManager& io = IOManager::getInstance().getInstance();
SDL_Event* event = new SDL_Event();
bool done = false;
bool nameDone = false;
const string msg("How many Pokeballs: ");
io.clearString();
while ( not done ) {
Uint8 *keystate = SDL_GetKeyState(NULL);
if ( SDL_PollEvent(event) )
switch (event->type) {
case SDL_KEYDOWN: {
if (keystate[SDLK_ESCAPE] || keystate[SDLK_q]) {
done = true;
}
if (keystate[SDLK_RETURN]) {
nameDone = true;
}
io.buildString(event);
}
}
drawBackground();
io.printStringAfterMessage(msg, 20, 200);
if ( nameDone ) {
std::string number = io.getString();
std::stringstream strm;
strm << number;
strm >> numberOfOrbs;
strm.clear(); // clear error flags
strm.str(std::string()); // clear contents
strm << "Okay -- you'll see " << numberOfOrbs << " pokeballs";
// cout << strm.str() << endl;
io.printMessageAt(strm.str(), 20, 240);
SDL_Flip(screen);
SDL_Delay(1000);
done = true;
}
if ( !done ) {
SDL_Flip(screen);
}
}
}
void MenuManager::getSoundOptions() {
// IOManager& io = IOManager::getInstance().getInstance();
SDL_Event* event = new SDL_Event();
bool done = false;
bool nameDone = false;
const string msg("Sound? (on/off) ");
io.clearString();
while ( not done ) {
Uint8 *keystate = SDL_GetKeyState(NULL);
if ( SDL_PollEvent(event) )
switch (event->type) {
case SDL_KEYDOWN: {
if (keystate[SDLK_ESCAPE] || keystate[SDLK_q]) {
done = true;
}
if (keystate[SDLK_RETURN]) {
nameDone = true;
}
io.buildString(event);
}
}
drawBackground();
io.printStringAfterMessage(msg, 20, 200);
if ( nameDone ) {
std::string answer = io.getString();
std::stringstream strm;
if(answer=="on") {
strm << "Okay -- you'll hear music!";
soundEnabled = true;
} else if(answer=="off") {
strm << "Okay -- we'll keep things quiet...";
soundEnabled = false;
} else {
strm << "That response doesn't look like an 'on' or 'off'...";
soundEnabled = true;
}
io.printMessageAt(strm.str(), 20, 240);
SDL_Flip(screen);
SDL_Delay(1000);
done = true;
}
if ( !done ) {
SDL_Flip(screen);
}
}
}
void MenuManager::displayHelp() {
// IOManager& io = IOManager::getInstance().getInstance();
SDL_Event* event = new SDL_Event();
bool done = false;
// bool nameDone = false;
io.clearString();
while ( not done ) {
Uint8 *keystate = SDL_GetKeyState(NULL);
if ( SDL_PollEvent(event) )
switch (event->type) {
case SDL_KEYDOWN: {
if (keystate[SDLK_ESCAPE] || keystate[SDLK_q]) {
done = true;
}
io.buildString(event);
}
}
drawBackground();
io.printMessageCenteredAt("Press p to pause game", 150);
io.printMessageCenteredAt("Press ESC to return to menu", 170);
io.printMessageCenteredAt("Press F1 to see in game help", 190);
io.printMessageAt("MARIO CONTROLS", 375, 230);
io.printMessageAt("Left: LEFT arrow ", 375, 270);
io.printMessageAt("Right: RIGHT arrow", 375, 290);
io.printMessageAt("Jump: UP arrow ", 375, 310);
io.printMessageAt("Sprint: RSHIFT", 375, 330);
io.printMessageAt("Pick/Throw Item: /", 375, 350);
io.printMessageAt("LUIGI CONTROLS", 75, 230);
io.printMessageAt("Left: a ", 75, 270);
io.printMessageAt("Right: d", 75, 290);
io.printMessageAt("Jump: w ", 75, 310);
io.printMessageAt("Sprint: e", 75, 330);
io.printMessageAt("Pick/Throw Item: q", 75, 350);
if ( !done ) {
SDL_Flip(screen);
}
}
}
void MenuManager::play() {
bool keyCatch = false; // To get only 1 key per keydown
SDL_Event* event = new SDL_Event();
SDLSound sound;
sound.toggleMusic();
bool done = false;
// Here, we need to create an instance of the Manager,
// the one that manages the game:
Manager manager;
while ( not done ) {
drawBackground();
menu.draw();
SDL_Flip(screen);
SDL_PollEvent(event);
if (event->type == SDL_QUIT) { break; }
if(event->type == SDL_KEYDOWN) {
switch ( event->key.keysym.sym ) {
case SDLK_ESCAPE :
case SDLK_q : {
done = true;
break;
}
case SDLK_RETURN: case SDLK_RIGHT: {
if ( !keyCatch ) {
menu.lightOn();
if ( menu.getIconClicked() == "Start Game") {
// Here is where we call the play() function in Manager:
manager.play();
menu.lightOff();
// std::cout << "Starting game ..." << std::endl;
}
if ( menu.getIconClicked() == "Parameters" ) {
getNumberOfItems();
manager.setNumberOfItems( numberOfOrbs );
getSoundOptions();
manager.setSoundEnabled( soundEnabled );
}
if ( menu.getIconClicked() == "Help" ) {
// Here is where we explain how to play the game"
//std::cout << "Give help ..." << std::endl;
displayHelp();
}
if ( menu.getIconClicked() == "Exit" ) {
drawBackground();
menu.draw();
SDL_Flip(screen);
SDL_Delay(250);
done = true;
}
}
break;
}
case SDLK_DOWN : {
if ( !keyCatch ) {
menu.increment();
io.clearString();
io.buildString(event);
if(soundEnabled) sound[1];
}
break;
}
case SDLK_UP : {
if ( !keyCatch ) {
menu.decrement();
io.clearString();
io.buildString(event);
if(soundEnabled) sound[1];
}
break;
}
default : break;
}
keyCatch = true;
}
if(event->type == SDL_KEYUP) {
keyCatch = false;
menu.lightOff();
}
}
delete event;
}