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