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