]> git.jsancho.org Git - lugaru.git/blob - Source/Menu/Menu.cpp
Added braces to all statements with clang-tidy and ran clang-format again
[lugaru.git] / Source / Menu / Menu.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Menu/Menu.hpp"
22
23 #include "Audio/openal_wrapper.hpp"
24 #include "Graphic/gamegl.hpp"
25 #include "Level/Campaign.hpp"
26 #include "User/Settings.hpp"
27 #include "Utils/Input.hpp"
28
29 // Should not be needed, Menu should call methods from other classes to launch maps and challenges and so on
30 #include "Level/Awards.hpp"
31
32 #include <set>
33 #include <string>
34 #include <vector>
35
36 using namespace Game;
37
38 extern float multiplier;
39 extern std::set<std::pair<int, int>> resolutions;
40 extern int mainmenu;
41 extern std::vector<CampaignLevel> campaignlevels;
42 extern float musicvolume[4];
43 extern float oldmusicvolume[4];
44 extern bool stillloading;
45 extern bool visibleloading;
46 extern int whichchoice;
47 extern int leveltheme;
48
49 extern void toggleFullscreen();
50
51 int entername = 0;
52 std::string newusername = "";
53 unsigned newuserselected = 0;
54 float newuserblinkdelay = 0;
55 bool newuserblink = false;
56
57 std::vector<MenuItem> Menu::items;
58
59 MenuItem::MenuItem(MenuItemType _type, int _id, const string& _text, Texture _texture,
60                    int _x, int _y, int _w, int _h, float _r, float _g, float _b,
61                    float _linestartsize, float _lineendsize)
62     : type(_type)
63     , id(_id)
64     , text(_text)
65     , texture(_texture)
66     , x(_x)
67     , y(_y)
68     , w(_w)
69     , h(_h)
70     , r(_r)
71     , g(_g)
72     , b(_b)
73     , effectfade(0)
74     , linestartsize(_linestartsize)
75     , lineendsize(_lineendsize)
76 {
77     if (type == MenuItem::BUTTON) {
78         if (w == -1) {
79             w = text.length() * 10;
80         }
81         if (h == -1) {
82             h = 20;
83         }
84     }
85 }
86
87 void Menu::clearMenu()
88 {
89     items.clear();
90 }
91
92 void Menu::addLabel(int id, const string& text, int x, int y, float r, float g, float b)
93 {
94     items.emplace_back(MenuItem::LABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
95 }
96 void Menu::addButton(int id, const string& text, int x, int y, float r, float g, float b)
97 {
98     items.emplace_back(MenuItem::BUTTON, id, text, Texture(), x, y, -1, -1, r, g, b);
99 }
100 void Menu::addImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
101 {
102     items.emplace_back(MenuItem::IMAGE, id, "", texture, x, y, w, h, r, g, b);
103 }
104 void Menu::addButtonImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
105 {
106     items.emplace_back(MenuItem::IMAGEBUTTON, id, "", texture, x, y, w, h, r, g, b);
107 }
108 void Menu::addMapLine(int x, int y, int w, int h, float startsize, float endsize, float r, float g, float b)
109 {
110     items.emplace_back(MenuItem::MAPLINE, -1, "", Texture(), x, y, w, h, r, g, b, startsize, endsize);
111 }
112 void Menu::addMapMarker(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
113 {
114     items.emplace_back(MenuItem::MAPMARKER, id, "", texture, x, y, w, h, r, g, b);
115 }
116 void Menu::addMapLabel(int id, const string& text, int x, int y, float r, float g, float b)
117 {
118     items.emplace_back(MenuItem::MAPLABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
119 }
120
121 void Menu::setText(int id, const string& text)
122 {
123     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
124         if (it->id == id) {
125             it->text = text;
126             it->w = it->text.length() * 10;
127             break;
128         }
129     }
130 }
131
132 void Menu::setText(int id, const string& text, int x, int y, int w, int h)
133 {
134     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
135         if (it->id == id) {
136             it->text = text;
137             it->x = x;
138             it->y = y;
139             if (w == -1) {
140                 it->w = it->text.length() * 10;
141             }
142             if (h == -1) {
143                 it->h = 20;
144             }
145             break;
146         }
147     }
148 }
149
150 int Menu::getSelected(int mousex, int mousey)
151 {
152     for (vector<MenuItem>::reverse_iterator it = items.rbegin(); it != items.rend(); it++) {
153         if (it->type == MenuItem::BUTTON || it->type == MenuItem::IMAGEBUTTON || it->type == MenuItem::MAPMARKER) {
154             int mx = mousex;
155             int my = mousey;
156             if (it->type == MenuItem::MAPMARKER) {
157                 mx -= 1;
158                 my += 2;
159             }
160             if (mx >= it->x && mx < it->x + it->w && my >= it->y && my < it->y + it->h) {
161                 return it->id;
162             }
163         }
164     }
165     return -1;
166 }
167
168 void Menu::handleFadeEffect()
169 {
170     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
171         if (it->id == Game::selected) {
172             it->effectfade += multiplier * 5;
173             if (it->effectfade > 1) {
174                 it->effectfade = 1;
175             }
176         } else {
177             it->effectfade -= multiplier * 5;
178             if (it->effectfade < 0) {
179                 it->effectfade = 0;
180             }
181         }
182     }
183 }
184
185 void Menu::drawItems()
186 {
187     handleFadeEffect();
188     glEnable(GL_TEXTURE_2D);
189     glEnable(GL_ALPHA_TEST);
190     glEnable(GL_BLEND);
191     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
192         switch (it->type) {
193             case MenuItem::IMAGE:
194             case MenuItem::IMAGEBUTTON:
195             case MenuItem::MAPMARKER:
196                 glColor4f(it->r, it->g, it->b, 1);
197                 glPushMatrix();
198                 if (it->type == MenuItem::MAPMARKER) {
199                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
200                     glTranslatef(2.5, -4.5, 0); //from old code
201                 } else {
202                     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
203                 }
204                 it->texture.bind();
205                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
206                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
207                 glBegin(GL_QUADS);
208                 glTexCoord2f(0, 0);
209                 glVertex3f(it->x, it->y, 0);
210                 glTexCoord2f(1, 0);
211                 glVertex3f(it->x + it->w, it->y, 0);
212                 glTexCoord2f(1, 1);
213                 glVertex3f(it->x + it->w, it->y + it->h, 0);
214                 glTexCoord2f(0, 1);
215                 glVertex3f(it->x, it->y + it->h, 0);
216                 glEnd();
217                 if (it->type != MenuItem::IMAGE) {
218                     //mouseover highlight
219                     for (int i = 0; i < 10; i++) {
220                         if (1 - ((float)i) / 10 - (1 - it->effectfade) > 0) {
221                             glColor4f(it->r, it->g, it->b, (1 - ((float)i) / 10 - (1 - it->effectfade)) * .25);
222                             glBegin(GL_QUADS);
223                             glTexCoord2f(0, 0);
224                             glVertex3f(it->x - ((float)i) * 1 / 2, it->y - ((float)i) * 1 / 2, 0);
225                             glTexCoord2f(1, 0);
226                             glVertex3f(it->x + it->w + ((float)i) * 1 / 2, it->y - ((float)i) * 1 / 2, 0);
227                             glTexCoord2f(1, 1);
228                             glVertex3f(it->x + it->w + ((float)i) * 1 / 2, it->y + it->h + ((float)i) * 1 / 2, 0);
229                             glTexCoord2f(0, 1);
230                             glVertex3f(it->x - ((float)i) * 1 / 2, it->y + it->h + ((float)i) * 1 / 2, 0);
231                             glEnd();
232                         }
233                     }
234                 }
235                 glPopMatrix();
236                 break;
237             case MenuItem::LABEL:
238             case MenuItem::BUTTON:
239                 glColor4f(it->r, it->g, it->b, 1);
240                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
241                 Game::text->glPrint(it->x, it->y, it->text.c_str(), 0, 1, 640, 480);
242                 if (it->type != MenuItem::LABEL) {
243                     //mouseover highlight
244                     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
245                     for (int i = 0; i < 15; i++) {
246                         if (1 - ((float)i) / 15 - (1 - it->effectfade) > 0) {
247                             glColor4f(it->r, it->g, it->b, (1 - ((float)i) / 10 - (1 - it->effectfade)) * .25);
248                             Game::text->glPrint(it->x - ((float)i), it->y, it->text.c_str(), 0, 1 + ((float)i) / 70, 640, 480);
249                         }
250                     }
251                 }
252                 break;
253             case MenuItem::MAPLABEL:
254                 Game::text->glPrintOutlined(0.9, 0, 0, 1, it->x, it->y, it->text.c_str(), 0, 0.6, 640, 480);
255                 break;
256             case MenuItem::MAPLINE: {
257                 XYZ linestart;
258                 linestart.x = it->x;
259                 linestart.y = it->y;
260                 linestart.z = 0;
261                 XYZ lineend;
262                 lineend.x = it->x + it->w;
263                 lineend.y = it->y + it->h;
264                 lineend.z = 0;
265                 XYZ offset = lineend - linestart;
266                 XYZ fac = offset;
267                 Normalise(&fac);
268                 offset = DoRotation(offset, 0, 0, 90);
269                 Normalise(&offset);
270
271                 linestart += fac * 4 * it->linestartsize;
272                 lineend -= fac * 4 * it->lineendsize;
273
274                 glDisable(GL_TEXTURE_2D);
275                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
276                 glColor4f(it->r, it->g, it->b, 1);
277                 glPushMatrix();
278                 glTranslatef(2, -5, 0); //from old code
279                 glBegin(GL_QUADS);
280                 glVertex3f(linestart.x - offset.x * it->linestartsize, linestart.y - offset.y * it->linestartsize, 0.0f);
281                 glVertex3f(linestart.x + offset.x * it->linestartsize, linestart.y + offset.y * it->linestartsize, 0.0f);
282                 glVertex3f(lineend.x + offset.x * it->lineendsize, lineend.y + offset.y * it->lineendsize, 0.0f);
283                 glVertex3f(lineend.x - offset.x * it->lineendsize, lineend.y - offset.y * it->lineendsize, 0.0f);
284                 glEnd();
285                 glPopMatrix();
286                 glEnable(GL_TEXTURE_2D);
287             } break;
288             default:
289             case MenuItem::NONE:
290                 break;
291         }
292     }
293 }
294
295 void Menu::updateSettingsMenu()
296 {
297     std::string sbuf = std::string("Resolution: ") + to_string(newscreenwidth) + "*" + to_string(newscreenheight);
298     if (((float)newscreenwidth <= (float)newscreenheight * 1.61) && ((float)newscreenwidth >= (float)newscreenheight * 1.59)) {
299         sbuf += " (widescreen)";
300     }
301     setText(0, sbuf);
302     setText(14, fullscreen ? "Fullscreen: On" : "Fullscreen: Off");
303     if (newdetail == 0) {
304         setText(1, "Detail: Low");
305     }
306     if (newdetail == 1) {
307         setText(1, "Detail: Medium");
308     }
309     if (newdetail == 2) {
310         setText(1, "Detail: High");
311     }
312     if (bloodtoggle == 0) {
313         setText(2, "Blood: Off");
314     }
315     if (bloodtoggle == 1) {
316         setText(2, "Blood: On, low detail");
317     }
318     if (bloodtoggle == 2) {
319         setText(2, "Blood: On, high detail (slower)");
320     }
321     setText(4, ismotionblur ? "Blur Effects: Enabled (less compatible)" : "Blur Effects: Disabled (more compatible)");
322     setText(5, decalstoggle ? "Decals: Enabled (slower)" : "Decals: Disabled");
323     setText(6, musictoggle ? "Music: Enabled" : "Music: Disabled");
324     setText(9, invertmouse ? "Invert mouse: Yes" : "Invert mouse: No");
325     setText(10, std::string("Mouse Speed: ") + to_string(int(usermousesensitivity * 5)));
326     setText(11, std::string("Volume: ") + to_string(int(volume * 100)) + "%");
327     setText(13, showdamagebar ? "Damage Bar: On" : "Damage Bar: Off");
328     if ((newdetail == detail) && (newscreenheight == (int)screenheight) && (newscreenwidth == (int)screenwidth)) {
329         setText(8, "Back");
330     } else {
331         setText(8, "Back (some changes take effect next time Lugaru is opened)");
332     }
333 }
334
335 void Menu::updateStereoConfigMenu()
336 {
337     setText(0, std::string("Stereo mode: ") + StereoModeName(newstereomode));
338     setText(1, std::string("Stereo separation: ") + to_string(stereoseparation));
339     setText(2, std::string("Reverse stereo: ") + (stereoreverse ? "Yes" : "No"));
340 }
341
342 void Menu::updateControlsMenu()
343 {
344     setText(0, (string) "Forwards: " + (keyselect == 0 ? "_" : Input::keyToChar(forwardkey)));
345     setText(1, (string) "Back: " + (keyselect == 1 ? "_" : Input::keyToChar(backkey)));
346     setText(2, (string) "Left: " + (keyselect == 2 ? "_" : Input::keyToChar(leftkey)));
347     setText(3, (string) "Right: " + (keyselect == 3 ? "_" : Input::keyToChar(rightkey)));
348     setText(4, (string) "Crouch: " + (keyselect == 4 ? "_" : Input::keyToChar(crouchkey)));
349     setText(5, (string) "Jump: " + (keyselect == 5 ? "_" : Input::keyToChar(jumpkey)));
350     setText(6, (string) "Draw: " + (keyselect == 6 ? "_" : Input::keyToChar(drawkey)));
351     setText(7, (string) "Throw: " + (keyselect == 7 ? "_" : Input::keyToChar(throwkey)));
352     setText(8, (string) "Attack: " + (keyselect == 8 ? "_" : Input::keyToChar(attackkey)));
353     if (devtools) {
354         setText(9, (string) "Console: " + (keyselect == 9 ? "_" : Input::keyToChar(consolekey)));
355     }
356 }
357
358 /*
359 Values of mainmenu :
360 1 Main menu
361 2 Menu pause (resume/end game)
362 3 Option menu
363 4 Controls configuration menu
364 5 Main game menu (choose level or challenge)
365 6 Deleting user menu
366 7 User managment menu (select/add)
367 8 Choose difficulty menu
368 9 Challenge level selection menu
369 10 End of the campaign congratulation (is that really a menu?)
370 11 Same that 9 ??? => unused
371 18 stereo configuration
372 */
373
374 void Menu::Load()
375 {
376     clearMenu();
377     switch (mainmenu) {
378         case 1:
379         case 2:
380             addImage(0, Mainmenuitems[0], 150, 480 - 128, 256, 128);
381             addButtonImage(1, Mainmenuitems[mainmenu == 1 ? 1 : 5], 18, 480 - 152 - 32, 128, 32);
382             addButtonImage(2, Mainmenuitems[2], 18, 480 - 228 - 32, 112, 32);
383             addButtonImage(3, Mainmenuitems[mainmenu == 1 ? 3 : 6], 18, 480 - 306 - 32, mainmenu == 1 ? 68 : 132, 32);
384             break;
385         case 3:
386             addButton(0, "", 10 + 20, 440);
387             addButton(14, "", 10 + 400, 440);
388             addButton(1, "", 10 + 60, 405);
389             addButton(2, "", 10 + 70, 370);
390             addButton(4, "", 10, 335);
391             addButton(5, "", 10 + 60, 300);
392             addButton(6, "", 10 + 70, 265);
393             addButton(9, "", 10, 230);
394             addButton(10, "", 20, 195);
395             addButton(11, "", 10 + 60, 160);
396             addButton(13, "", 30, 125);
397             addButton(7, "-Configure Controls-", 10 + 15, 90);
398             addButton(12, "-Configure Stereo -", 10 + 15, 55);
399             addButton(8, "Back", 10, 10);
400             updateSettingsMenu();
401             break;
402         case 4:
403             addButton(0, "", 10, 400);
404             addButton(1, "", 10 + 40, 360);
405             addButton(2, "", 10 + 40, 320);
406             addButton(3, "", 10 + 30, 280);
407             addButton(4, "", 10 + 20, 240);
408             addButton(5, "", 10 + 40, 200);
409             addButton(6, "", 10 + 40, 160);
410             addButton(7, "", 10 + 30, 120);
411             addButton(8, "", 10 + 20, 80);
412             if (devtools) {
413                 addButton(9, "", 10 + 10, 40);
414             }
415             addButton(devtools ? 10 : 9, "Back", 10, 10);
416             updateControlsMenu();
417             break;
418         case 5: {
419             LoadCampaign();
420             addLabel(-1, Account::active().getName(), 5, 400);
421             addButton(1, "Tutorial", 5, 300);
422             addButton(2, "Challenge", 5, 240);
423             addButton(3, "Delete User", 400, 10);
424             addButton(4, "Main Menu", 5, 10);
425             addButton(5, "Change User", 5, 180);
426             addButton(6, "Campaign : " + Account::active().getCurrentCampaign(), 200, 420);
427
428             //show campaign map
429             //with (2,-5) offset from old code
430             addImage(-1, Mainmenuitems[7], 150 + 2, 60 - 5, 400, 400);
431             //show levels
432             int numlevels = Account::active().getCampaignChoicesMade();
433             numlevels += numlevels > 0 ? campaignlevels[numlevels - 1].nextlevel.size() : 1;
434             for (int i = 0; i < numlevels; i++) {
435                 XYZ midpoint = campaignlevels[i].getCenter();
436                 float itemsize = campaignlevels[i].getWidth();
437                 const bool active = (i >= Account::active().getCampaignChoicesMade());
438                 if (!active) {
439                     itemsize /= 2;
440                 }
441
442                 if (i >= 1) {
443                     XYZ start = campaignlevels[i - 1].getCenter();
444                     addMapLine(start.x, start.y, midpoint.x - start.x, midpoint.y - start.y, 0.5, active ? 1 : 0.5, active ? 1 : 0.5, 0, 0);
445                 }
446                 addMapMarker(NB_CAMPAIGN_MENU_ITEM + i, Mapcircletexture,
447                              midpoint.x - itemsize / 2, midpoint.y - itemsize / 2, itemsize, itemsize, active ? 1 : 0.5, 0, 0);
448
449                 if (active) {
450                     addMapLabel(-2, campaignlevels[i].description,
451                                 campaignlevels[i].getStartX() + 10,
452                                 campaignlevels[i].getStartY() - 4);
453                 }
454             }
455         } break;
456         case 6:
457             addLabel(-1, "Are you sure you want to delete this user?", 10, 400);
458             addButton(1, "Yes", 10, 360);
459             addButton(2, "No", 10, 320);
460             break;
461         case 7:
462             if (Account::getNbAccounts() < 8) {
463                 addButton(0, "New User", 10, 400);
464             } else {
465                 addLabel(0, "No More Users", 10, 400);
466             }
467             addLabel(-2, "", 20, 400);
468             addButton(Account::getNbAccounts() + 1, "Back", 10, 10);
469             for (int i = 0; i < Account::getNbAccounts(); i++) {
470                 addButton(i + 1, Account::get(i).getName(), 10, 340 - 20 * (i + 1));
471             }
472             break;
473         case 8:
474             addButton(0, "Easier", 10, 400);
475             addButton(1, "Difficult", 10, 360);
476             addButton(2, "Insane", 10, 320);
477             break;
478         case 9:
479             for (int i = 0; i < numchallengelevels; i++) {
480                 string name = "Level ";
481                 name += to_string(i + 1);
482                 if (name.size() < 17) {
483                     name.append((17 - name.size()), ' ');
484                 }
485                 name += to_string(int(Account::active().getHighScore(i)));
486                 if (name.size() < 32) {
487                     name.append((32 - name.size()), ' ');
488                 }
489                 int fasttime = (int)round(Account::active().getFastTime(i));
490                 name += to_string(int((fasttime - fasttime % 60) / 60));
491                 name += ":";
492                 if (fasttime % 60 < 10) {
493                     name += "0";
494                 }
495                 name += to_string(fasttime % 60);
496
497                 addButton(i, name, 10, 400 - i * 25, i > Account::active().getProgress() ? 0.5 : 1, 0, 0);
498             }
499
500             addButton(-1, "             High Score      Best Time", 10, 440);
501             addButton(numchallengelevels, "Back", 10, 10);
502             break;
503         case 10: {
504             addLabel(0, "Congratulations!", 220, 330);
505             addLabel(1, "You have avenged your family and", 140, 300);
506             addLabel(2, "restored peace to the island of Lugaru.", 110, 270);
507             addButton(3, "Back", 10, 10);
508             addLabel(4, string("Your score:         ") + to_string((int)Account::active().getCampaignScore()), 190, 200);
509             addLabel(5, string("Highest score:      ") + to_string((int)Account::active().getCampaignHighScore()), 190, 180);
510         } break;
511         case 18:
512             addButton(0, "", 70, 400);
513             addButton(1, "", 10, 360);
514             addButton(2, "", 40, 320);
515             addButton(3, "Back", 10, 10);
516             updateStereoConfigMenu();
517             break;
518     }
519 }
520
521 void Menu::Tick()
522 {
523     //escape key pressed
524     if (Input::isKeyPressed(SDL_SCANCODE_ESCAPE) &&
525         (mainmenu >= 3) && (mainmenu != 8) && !((mainmenu == 7) && entername)) {
526         selected = -1;
527         //finished with settings menu
528         if (mainmenu == 3) {
529             SaveSettings();
530         }
531         //effects
532         if (mainmenu >= 3 && mainmenu != 8) {
533             fireSound();
534             flash();
535         }
536         //go back
537         switch (mainmenu) {
538             case 3:
539             case 5:
540                 mainmenu = gameon ? 2 : 1;
541                 break;
542             case 4:
543             case 18:
544                 mainmenu = 3;
545                 break;
546             case 6:
547             case 7:
548             case 9:
549             case 10:
550                 mainmenu = 5;
551                 break;
552         }
553     }
554
555     //menu buttons
556     selected = getSelected(mousecoordh * 640 / screenwidth, 480 - mousecoordv * 480 / screenheight);
557
558     // some specific case where we do something even if the left mouse button is not pressed.
559     if ((mainmenu == 5) && (endgame == 2)) {
560         Account::active().endGame();
561         endgame = 0;
562     }
563     if (mainmenu == 10) {
564         endgame = 2;
565     }
566     if (mainmenu == 18 && Input::isKeyPressed(MOUSEBUTTON_RIGHT) && selected == 1) {
567         stereoseparation -= 0.001;
568         updateStereoConfigMenu();
569     }
570
571     static int oldmainmenu = mainmenu;
572
573     if (Input::MouseClicked() && (selected >= 0)) { // handling of the left mouse clic in menus
574         set<pair<int, int>>::iterator newscreenresolution;
575         switch (mainmenu) {
576             case 1:
577             case 2:
578                 switch (selected) {
579                     case 1:
580                         if (gameon) { //resume
581                             mainmenu = 0;
582                             pause_sound(stream_menutheme);
583                             resume_stream(leveltheme);
584                         } else { //new game
585                             fireSound(firestartsound);
586                             flash();
587                             mainmenu = (Account::hasActive() ? 5 : 7);
588                             selected = -1;
589                         }
590                         break;
591                     case 2: //options
592                         fireSound();
593                         flash();
594                         mainmenu = 3;
595                         if (newdetail > 2) {
596                             newdetail = detail;
597                         }
598                         if (newdetail < 0) {
599                             newdetail = detail;
600                         }
601                         if (newscreenwidth > 3000) {
602                             newscreenwidth = screenwidth;
603                         }
604                         if (newscreenwidth < 0) {
605                             newscreenwidth = screenwidth;
606                         }
607                         if (newscreenheight > 3000) {
608                             newscreenheight = screenheight;
609                         }
610                         if (newscreenheight < 0) {
611                             newscreenheight = screenheight;
612                         }
613                         break;
614                     case 3:
615                         fireSound();
616                         flash();
617                         if (gameon) { //end game
618                             gameon = 0;
619                             mainmenu = 1;
620                         } else { //quit
621                             tryquit = 1;
622                             pause_sound(stream_menutheme);
623                         }
624                         break;
625                 }
626                 break;
627             case 3:
628                 fireSound();
629                 switch (selected) {
630                     case 0:
631                         newscreenresolution = resolutions.find(make_pair(newscreenwidth, newscreenheight));
632                         /* Next one (end() + 1 is also end() so the ++ is safe even if it was not found) */
633                         newscreenresolution++;
634                         if (newscreenresolution == resolutions.end()) {
635                             /* It was the last one (or not found), go back to the beginning */
636                             newscreenresolution = resolutions.begin();
637                         }
638                         newscreenwidth = newscreenresolution->first;
639                         newscreenheight = newscreenresolution->second;
640                         break;
641                     case 1:
642                         newdetail++;
643                         if (newdetail > 2) {
644                             newdetail = 0;
645                         }
646                         break;
647                     case 2:
648                         bloodtoggle++;
649                         if (bloodtoggle > 2) {
650                             bloodtoggle = 0;
651                         }
652                         break;
653                     case 4:
654                         ismotionblur = !ismotionblur;
655                         break;
656                     case 5:
657                         decalstoggle = !decalstoggle;
658                         break;
659                     case 6:
660                         musictoggle = !musictoggle;
661                         if (musictoggle) {
662                             emit_stream_np(stream_menutheme);
663                         } else {
664                             pause_sound(leveltheme);
665                             pause_sound(stream_fighttheme);
666                             pause_sound(stream_menutheme);
667
668                             for (int i = 0; i < 4; i++) {
669                                 oldmusicvolume[i] = 0;
670                                 musicvolume[i] = 0;
671                             }
672                         }
673                         break;
674                     case 7: // controls
675                         flash();
676                         mainmenu = 4;
677                         selected = -1;
678                         keyselect = -1;
679                         break;
680                     case 8:
681                         flash();
682                         SaveSettings();
683                         mainmenu = gameon ? 2 : 1;
684                         break;
685                     case 9:
686                         invertmouse = !invertmouse;
687                         break;
688                     case 10:
689                         usermousesensitivity += .2;
690                         if (usermousesensitivity > 2) {
691                             usermousesensitivity = .2;
692                         }
693                         break;
694                     case 11:
695                         volume += .1f;
696                         if (volume > 1.0001f) {
697                             volume = 0;
698                         }
699                         OPENAL_SetSFXMasterVolume((int)(volume * 255));
700                         break;
701                     case 12:
702                         flash();
703                         newstereomode = stereomode;
704                         mainmenu = 18;
705                         keyselect = -1;
706                         break;
707                     case 13:
708                         showdamagebar = !showdamagebar;
709                         break;
710                     case 14:
711                         toggleFullscreen();
712                         break;
713                 }
714                 updateSettingsMenu();
715                 break;
716             case 4:
717                 if (!waiting) {
718                     fireSound();
719                     if (selected < (devtools ? 10 : 9) && keyselect == -1) {
720                         keyselect = selected;
721                     }
722                     if (keyselect != -1) {
723                         setKeySelected();
724                     }
725                     if (selected == (devtools ? 10 : 9)) {
726                         flash();
727                         mainmenu = 3;
728                     }
729                 }
730                 updateControlsMenu();
731                 break;
732             case 5:
733                 fireSound();
734                 flash();
735                 if ((selected - NB_CAMPAIGN_MENU_ITEM >= Account::active().getCampaignChoicesMade())) {
736                     startbonustotal = 0;
737
738                     loading = 2;
739                     loadtime = 0;
740                     targetlevel = 7;
741                     if (firstLoadDone) {
742                         TickOnceAfter();
743                     } else {
744                         LoadStuff();
745                     }
746                     whichchoice = selected - NB_CAMPAIGN_MENU_ITEM - Account::active().getCampaignChoicesMade();
747                     actuallevel = (Account::active().getCampaignChoicesMade() > 0 ? campaignlevels[Account::active().getCampaignChoicesMade() - 1].nextlevel[whichchoice] : 0);
748                     visibleloading = true;
749                     stillloading = 1;
750                     LoadLevel(campaignlevels[actuallevel].mapname.c_str());
751                     campaign = 1;
752                     mainmenu = 0;
753                     gameon = 1;
754                     pause_sound(stream_menutheme);
755                 }
756                 switch (selected) {
757                     case 1:
758                         startbonustotal = 0;
759
760                         loading = 2;
761                         loadtime = 0;
762                         targetlevel = -1;
763                         if (firstLoadDone) {
764                             TickOnceAfter();
765                         } else {
766                             LoadStuff();
767                         }
768                         LoadLevel(-1);
769
770                         mainmenu = 0;
771                         gameon = 1;
772                         pause_sound(stream_menutheme);
773                         break;
774                     case 2:
775                         mainmenu = 9;
776                         break;
777                     case 3:
778                         mainmenu = 6;
779                         break;
780                     case 4:
781                         mainmenu = (gameon ? 2 : 1);
782                         break;
783                     case 5:
784                         mainmenu = 7;
785                         break;
786                     case 6:
787                         vector<string> campaigns = ListCampaigns();
788                         vector<string>::iterator c;
789                         if ((c = find(campaigns.begin(), campaigns.end(), Account::active().getCurrentCampaign())) == campaigns.end()) {
790                             if (!campaigns.empty()) {
791                                 Account::active().setCurrentCampaign(campaigns.front());
792                             }
793                         } else {
794                             c++;
795                             if (c == campaigns.end()) {
796                                 c = campaigns.begin();
797                             }
798                             Account::active().setCurrentCampaign(*c);
799                         }
800                         Load();
801                         break;
802                 }
803                 break;
804             case 6:
805                 fireSound();
806                 if (selected == 1) {
807                     flash();
808                     Account::destroyActive();
809                     mainmenu = 7;
810                 } else if (selected == 2) {
811                     flash();
812                     mainmenu = 5;
813                 }
814                 break;
815             case 7:
816                 fireSound();
817                 if (selected == 0 && Account::getNbAccounts() < 8) {
818                     entername = 1;
819                 } else if (selected < Account::getNbAccounts() + 1) {
820                     flash();
821                     mainmenu = 5;
822                     Account::setActive(selected - 1);
823                 } else if (selected == Account::getNbAccounts() + 1) {
824                     flash();
825                     if (Account::hasActive()) {
826                         mainmenu = 5;
827                     } else {
828                         mainmenu = 1;
829                     }
830                     newusername.clear();
831                     newuserselected = 0;
832                     entername = 0;
833                 }
834                 break;
835             case 8:
836                 fireSound();
837                 flash();
838                 if (selected <= 2) {
839                     Account::active().setDifficulty(selected);
840                 }
841                 mainmenu = 5;
842                 break;
843             case 9:
844                 if (selected < numchallengelevels && selected <= Account::active().getProgress()) {
845                     fireSound();
846                     flash();
847
848                     startbonustotal = 0;
849
850                     loading = 2;
851                     loadtime = 0;
852                     targetlevel = selected;
853                     if (firstLoadDone) {
854                         TickOnceAfter();
855                     } else {
856                         LoadStuff();
857                     }
858                     LoadLevel(selected);
859                     campaign = 0;
860
861                     mainmenu = 0;
862                     gameon = 1;
863                     pause_sound(stream_menutheme);
864                 }
865                 if (selected == numchallengelevels) {
866                     fireSound();
867                     flash();
868                     mainmenu = 5;
869                 }
870                 break;
871             case 10:
872                 if (selected == 3) {
873                     fireSound();
874                     flash();
875                     mainmenu = 5;
876                 }
877                 break;
878             case 18:
879                 if (selected == 1) {
880                     stereoseparation += 0.001;
881                 } else {
882                     fireSound();
883                     if (selected == 0) {
884                         newstereomode = (StereoMode)(newstereomode + 1);
885                         while (!CanInitStereo(newstereomode)) {
886                             printf("Failed to initialize mode %s (%i)\n", StereoModeName(newstereomode).c_str(), newstereomode);
887                             newstereomode = (StereoMode)(newstereomode + 1);
888                             if (newstereomode >= stereoCount) {
889                                 newstereomode = stereoNone;
890                             }
891                         }
892                     } else if (selected == 2) {
893                         stereoreverse = !stereoreverse;
894                     } else if (selected == 3) {
895                         flash();
896                         mainmenu = 3;
897
898                         stereomode = newstereomode;
899                         InitStereo(stereomode);
900                     }
901                 }
902                 updateStereoConfigMenu();
903                 break;
904         }
905     }
906
907     OPENAL_SetFrequency(channels[stream_menutheme]);
908
909     if (entername) {
910         inputText(newusername, &newuserselected);
911         if (!waiting) {                 // the input as finished
912             if (!newusername.empty()) { // with enter
913                 Account::add(string(newusername));
914
915                 mainmenu = 8;
916
917                 flash();
918
919                 fireSound(firestartsound);
920
921                 newusername.clear();
922
923                 newuserselected = 0;
924             }
925             entername = 0;
926             Load();
927         }
928
929         newuserblinkdelay -= multiplier;
930         if (newuserblinkdelay <= 0) {
931             newuserblinkdelay = .3;
932             newuserblink = !newuserblink;
933         }
934     }
935
936     if (entername) {
937         setText(0, newusername, 20, 400, -1, -1);
938         setText(-2, newuserblink ? "_" : "", 20 + newuserselected * 10, 400, -1, -1);
939     }
940
941     if (oldmainmenu != mainmenu) {
942         Load();
943     }
944     oldmainmenu = mainmenu;
945 }
946
947 int setKeySelected_thread(void*)
948 {
949     using namespace Game;
950     int scancode = -1;
951     SDL_Event evenement;
952     while (scancode == -1) {
953         SDL_WaitEvent(&evenement);
954         switch (evenement.type) {
955             case SDL_KEYDOWN:
956                 scancode = evenement.key.keysym.scancode;
957                 break;
958             case SDL_MOUSEBUTTONDOWN:
959                 scancode = SDL_NUM_SCANCODES + evenement.button.button;
960                 break;
961             default:
962                 break;
963         }
964     }
965     if (scancode != SDL_SCANCODE_ESCAPE) {
966         fireSound();
967         switch (keyselect) {
968             case 0:
969                 forwardkey = scancode;
970                 break;
971             case 1:
972                 backkey = scancode;
973                 break;
974             case 2:
975                 leftkey = scancode;
976                 break;
977             case 3:
978                 rightkey = scancode;
979                 break;
980             case 4:
981                 crouchkey = scancode;
982                 break;
983             case 5:
984                 jumpkey = scancode;
985                 break;
986             case 6:
987                 drawkey = scancode;
988                 break;
989             case 7:
990                 throwkey = scancode;
991                 break;
992             case 8:
993                 attackkey = scancode;
994                 break;
995             case 9:
996                 consolekey = scancode;
997                 break;
998             default:
999                 break;
1000         }
1001     }
1002     keyselect = -1;
1003     waiting = false;
1004     Menu::Load();
1005     return 0;
1006 }
1007
1008 void Menu::setKeySelected()
1009 {
1010     waiting = true;
1011     printf("launch thread\n");
1012     SDL_Thread* thread = SDL_CreateThread(setKeySelected_thread, NULL, NULL);
1013     if (thread == NULL) {
1014         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
1015         waiting = false;
1016         return;
1017     }
1018 }