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