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