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