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