-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
345 lines (300 loc) · 13.9 KB
/
Menu.cpp
File metadata and controls
345 lines (300 loc) · 13.9 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <iostream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Character.h"
#include "MenuController.h"
using namespace std;
//Width and height
#define SCREEN_W 800
#define SCREEN_H 600
//Selected options and current menu
int selected=0;
int current_menu=0;
//Difficulty and music state
int difficulty=0;
bool music_state=true;
//Draw texts in the window
void draw(sf::RenderWindow& w, sf::Text* t, int n){
for(int i=0; i<n; i++){
w.draw(t[i]);
}
}
int main(void){
//800x600 window entitled LGGP Project with a fix size
//Screen frame rate limited at 60Hz
sf::RenderWindow window(sf::VideoMode(SCREEN_W,SCREEN_H), "LGGP Project", sf::Style::Close);
window.setFramerateLimit(60);
//Scott head selected as icon for the window
sf::Image icon;
icon.loadFromFile("img/icon.png");
window.setIcon(100,100,icon.getPixelsPtr());
//Select the background
sf::Texture bgt;
bgt.loadFromFile("img/background.jpg");
sf::RectangleShape bg = sf::RectangleShape(sf::Vector2f(800,600));
bg.setTexture(&bgt);
//Music by Toby Fox
sf::Music music;
music.openFromFile("audio/menu.wav");
music.setLoop(true);
music.setVolume(50.);
music.play();
//Validation song
sf::SoundBuffer buffer;
buffer.loadFromFile("audio/validation.wav");
sf::Sound sound;
sound.setBuffer(buffer);
//Clear the window with a black screen
window.clear(sf::Color::Black);
sf::Event event;
int limit;
sf::Font font1, font2;
font1.loadFromFile("font/impact.ttf");
font2.loadFromFile("font/Minecraft.ttf"); //Craftron Gaming
sf::Text* titles;
sf::Text* options;
ifstream scores_file("scores.txt");
string main_string[MenuController::MAIN_LIMIT] = {"PLAY", "OPTIONS", "SCORES", "CREDITS", "QUIT"};
string options_string[MenuController::OP_LIMIT]= {"DIFFICULTY : NOOB", "MUSIC : ON", "BACK"};
string scores_string[MenuController::SC_LIMIT]= {"", "", "", "", "BACK"};
string credits_string[MenuController::CR_LIMIT]= {"DEVELOPER : LEO GARREAU",
"DEVELOPER : GAETAN PUGET",
"SPRITES : MAVERICK PK",
"MUSIC : TOBY FOX",
"FONT : CRAFTRON GAMING",
"BACK"
};
while(window.isOpen()){
switch(current_menu){
case MenuController::MAIN_ID :
limit=MenuController::MAIN_LIMIT;
//Window is listening events
while(window.pollEvent(event)){
//Window closed
if(event.type==sf::Event::Closed) window.close();
//Key pressed
if(event.type==sf::Event::KeyPressed){
switch(event.key.code){
//Previous selection
case sf::Keyboard::Up : selected = (selected+(limit-1))%limit;
break;
//Next selection
case sf::Keyboard::Down : selected = (selected+1)%limit;
break;
//Press ENTER
case sf::Keyboard::Return : sound.play();
switch(selected){
//Quit
case MenuController::MAIN_QUIT :
window.close();
break;
//Options menu
case MenuController::MAIN_OPTIONS :
current_menu=MenuController::OP_ID;
selected=0;
break;
//Scores menu
case MenuController::MAIN_SCORES :
current_menu=MenuController::SC_ID;
selected=0;
break;
//Credits menu
case MenuController::MAIN_CREDITS :
current_menu=MenuController::CR_ID;
selected=0;
break;
}
break;
}
}
}
//Draw the background
window.draw(bg);
//Mark title
titles=new sf::Text[3];
MenuController::printTitles(titles, 3, font1, SCREEN_W, SCREEN_H);
//Mark options
options = new sf::Text[limit];
MenuController::printOptions(main_string, options, limit, font2, SCREEN_W, SCREEN_H, selected);
//Draw titles and options
draw(window, titles, 3);
draw(window, options, limit);
window.display();
break;
//Options menu
case MenuController::OP_ID :
limit=MenuController::OP_LIMIT;
//Window is listening events
while(window.pollEvent(event)){
//Window closed
if(event.type==sf::Event::Closed) window.close();
//Key pressed
if(event.type==sf::Event::KeyPressed){
switch(event.key.code){
//Previous selection
case sf::Keyboard::Up : selected = (selected+(limit-1))%limit;
break;
//Next selection
case sf::Keyboard::Down : selected = (selected+1)%limit;
break;
case sf::Keyboard::Left :
switch(selected){
//Change difficulty
case MenuController::OP_DIFFICULTY :
if(difficulty==2){
difficulty--;
options_string[MenuController::OP_DIFFICULTY]="DIFFICULTY : NORMAL";
sound.play();
}else if(difficulty==1){
difficulty--;
options_string[MenuController::OP_DIFFICULTY]="DIFFICULTY : NOOB";
sound.play();
}
break;
//Change music state
case MenuController::OP_MUSIC :
if(!music_state){
music_state=!music_state;
options_string[MenuController::OP_MUSIC]="MUSIC : ON";
music.play();
}
break;
}
break;
case sf::Keyboard::Right :
switch(selected){
//Change difficulty
case MenuController::OP_DIFFICULTY :
if(difficulty==0){
difficulty++;
options_string[MenuController::OP_DIFFICULTY]="DIFFICULTY : NORMAL";
sound.play();
}else if(difficulty==1){
difficulty++;
options_string[MenuController::OP_DIFFICULTY]="DIFFICULTY : TRY HARDER";
sound.play();
}
break;
//Change music state
case MenuController::OP_MUSIC :
if(music_state){
music_state=!music_state;
options_string[MenuController::OP_MUSIC]="MUSIC : OFF";
music.stop();
}
break;
}
break;
//Press ENTER
case sf::Keyboard::Return :
switch(selected){
//Go to Main menu
case MenuController::OP_BACK :
current_menu=MenuController::MAIN_ID;
selected=0;
sound.play();
break;
}
break;
}
}
}
//Draw the background
window.draw(bg);
//Mark title
MenuController::printTitles(titles, 3, font1, SCREEN_W, SCREEN_H);
//Mark options
options = new sf::Text[limit];
MenuController::printOptions(options_string, options, limit, font2, SCREEN_W, SCREEN_H, selected);
//Draw titles and options
draw(window, titles, 3);
draw(window, options, limit);
window.display();
break;
//Scores menu
case MenuController::SC_ID :
limit=MenuController::SC_LIMIT;
//Window is listening events
while(window.pollEvent(event)){
//Window closed
if(event.type==sf::Event::Closed) window.close();
//Key pressed
if(event.type==sf::Event::KeyPressed){
switch(event.key.code){
//Previous selection
case sf::Keyboard::Up : selected = (selected+(limit-1))%limit;
break;
//Next selection
case sf::Keyboard::Down : selected = (selected+1)%limit;
break;
//Press ENTER
case sf::Keyboard::Return : sound.play();
switch(selected){
//Go to Main menu
case MenuController::SC_BACK :
current_menu=MenuController::MAIN_ID;
selected=0;
break;
}
break;
}
}
}
//Draw the background
window.draw(bg);
//Mark title
MenuController::printTitles(titles, 3, font1, SCREEN_W, SCREEN_H);
//Mark options
options = new sf::Text[limit];
for(int i=0;i<limit-1;i++) getline(scores_file, scores_string[i]);
MenuController::printOptions(scores_string, options, limit, font2, SCREEN_W, SCREEN_H, selected);
//Draw titles and options
draw(window, titles, 3);
draw(window, options, limit);
window.display();
break;
//Credits menu
case MenuController::CR_ID :
limit=MenuController::CR_LIMIT;
//Window is listening events
while(window.pollEvent(event)){
//Window closed
if(event.type==sf::Event::Closed) window.close();
//Key pressed
if(event.type==sf::Event::KeyPressed){
switch(event.key.code){
//Previous selection
case sf::Keyboard::Up : selected = (selected+(limit-1))%limit;
break;
//Next selection
case sf::Keyboard::Down : selected = (selected+1)%limit;
break;
//Press ENTER
case sf::Keyboard::Return : sound.play();
switch(selected){
//Go to Main menu
case MenuController::CR_BACK :
current_menu=MenuController::MAIN_ID;
selected=0;
break;
}
break;
}
}
}
//Draw the background
window.draw(bg);
//Mark options
options = new sf::Text[limit];
MenuController::printOptions(credits_string, options, limit, font2, SCREEN_W, SCREEN_H, selected);
//Draw titles and options
draw(window, options, limit);
window.display();
break;
}
}
free(titles);
free(options);
return 0;
}