]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/options.cpp
Prevent a segmentation fault when using the -map option without specifying a map.
[quix0rs-blobwars.git] / src / options.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "options.h"
23
24 void showCheatConfig()
25 {
26         SDL_FillRect(graphics.screen, NULL, graphics.black);
27         graphics.delay(500);
28
29         SDL_Surface *header = graphics.getSprite("cheatHeader", true)->image[0];
30         SDL_Surface *optionsBackground = graphics.getSprite("optionsBackground", true)->image[0];
31         SDL_SetColorKey(optionsBackground, 0, SDL_MapRGB(optionsBackground->format, 0, 0, 0));
32
33         if (!engine.loadWidgets(_("data/cheatWidgets")))
34         {
35                 graphics.showErrorAndExit(ERR_FILE, _("data/cheatWidgets"));
36         }
37                 
38         int done = 0;
39
40         engine.setWidgetVariable("health", &engine.cheatHealth);
41         engine.setWidgetVariable("extras", &engine.cheatExtras);
42         engine.setWidgetVariable("fuel", &engine.cheatFuel);
43         engine.setWidgetVariable("rate", &engine.cheatReload);
44         engine.setWidgetVariable("blood", &engine.cheatBlood);
45         engine.setWidgetVariable("invulnerable", &engine.cheatInvulnerable);
46         engine.setWidgetVariable("speed", &engine.cheatSpeed);
47         engine.setWidgetVariable("levels", &engine.cheatLevels);
48         engine.setWidgetVariable("skip", &engine.cheatSkipLevel);
49         engine.setWidgetVariable("confirm", &done);
50         
51         graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
52         graphics.blit(header, 320, 25, graphics.screen, true);
53         drawWidgets();
54
55         engine.flushInput();
56         engine.clearInput();
57         
58         int menuSound = -1;
59
60         while (!done)
61         {
62                 graphics.updateScreen();
63                 
64                 if (menuSound)
65                         audio.playMenuSound(menuSound);
66
67                 engine.getInput();
68                 config.populate();
69
70                 menuSound = engine.processWidgets();
71
72                 if (menuSound)
73                 {
74                         graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
75                         graphics.blit(header, 320, 25, graphics.screen, true);
76                         drawWidgets();
77                 }
78
79                 if (engine.keyState[SDL_SCANCODE_ESCAPE])
80                 {
81                         engine.clearInput();
82                         engine.flushInput();
83                         done = 1;
84                 }
85
86                 SDL_Delay(16);
87         }
88         
89         audio.playMenuSound(2);
90
91         SDL_FillRect(graphics.screen, NULL, graphics.black);
92         graphics.delay(500);
93
94         if (!engine.loadWidgets(_("data/optionWidgets")))
95         {
96                 graphics.showErrorAndExit(ERR_FILE, _("data/optionWidgets"));
97         }
98         
99         engine.highlightWidget("cheats");
100 }
101
102 void showKeyConfig()
103 {
104         SDL_FillRect(graphics.screen, NULL, graphics.black);
105         graphics.delay(500);
106
107         if (!engine.loadWidgets(_("data/keyboardWidgets")))
108         {
109                 graphics.showErrorAndExit(ERR_FILE, _("data/keyboardWidgets"));
110         }
111                 
112         SDL_Surface *header = graphics.getSprite("keyHeader", true)->image[0];
113         SDL_Surface *optionsBackground = graphics.getSprite("optionsBackground", true)->image[0];
114         SDL_SetColorKey(optionsBackground, 0, SDL_MapRGB(optionsBackground->format, 0, 0, 0));
115
116         int done = 0;
117         int defaults = 0;
118
119         engine.setWidgetVariable("left", &config.keyboard.control[CONTROL::LEFT]);
120         engine.setWidgetVariable("right", &config.keyboard.control[CONTROL::RIGHT]);
121         engine.setWidgetVariable("down", &config.keyboard.control[CONTROL::DOWN]);
122         engine.setWidgetVariable("fire", &config.keyboard.control[CONTROL::FIRE]);
123         engine.setWidgetVariable("jump", &config.keyboard.control[CONTROL::JUMP]);
124         engine.setWidgetVariable("pause", &config.keyboard.control[CONTROL::PAUSE]);
125
126         engine.setWidgetVariable("jetpack", &config.keyboard.control[CONTROL::JETPACK]);
127         engine.setWidgetVariable("map", &config.keyboard.control[CONTROL::MAP]);
128
129         engine.setWidgetVariable("defaults", &defaults);
130         engine.setWidgetVariable("confirm", &done);
131         
132         graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
133         graphics.blit(header, 320, 25, graphics.screen, true);
134         drawWidgets();
135
136         engine.flushInput();
137         engine.clearInput();
138         
139         int menuSound = -1;
140         
141         engine.allowJoypad = false;
142
143         while (!done)
144         {
145                 graphics.updateScreen();
146                 
147                 if (menuSound)
148                 {
149                         audio.playMenuSound(menuSound);
150                 }
151
152                 engine.getInput();
153                 config.populate();
154
155                 menuSound = engine.processWidgets();
156
157                 graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
158                 graphics.blit(header, 320, 25, graphics.screen, true);
159                 drawWidgets();
160                 
161                 if (defaults)
162                 {
163                         config.restoreKeyDefaults();
164                         defaults = 0;
165                 }
166
167                 if (engine.keyState[SDL_SCANCODE_ESCAPE])
168                 {
169                         engine.clearInput();
170                         engine.flushInput();
171                         done = 1;
172                 }
173
174                 SDL_Delay(16);
175         }
176         
177         engine.allowJoypad = true;
178         
179         config.saveKeyConfig();
180
181         audio.playMenuSound(2);
182
183         SDL_FillRect(graphics.screen, NULL, graphics.black);
184         graphics.delay(500);
185
186         if (!engine.loadWidgets(_("data/optionWidgets")))
187         {
188                 graphics.showErrorAndExit(ERR_FILE, _("data/optionWidgets"));
189         }
190         
191         engine.highlightWidget("keys");
192 }
193
194 void showJoystickConfig()
195 {
196         SDL_FillRect(graphics.screen, NULL, graphics.black);
197         graphics.delay(500);
198
199         if (!engine.loadWidgets(_("data/joystickWidgets")))
200                 graphics.showErrorAndExit(ERR_FILE, _("data/joystickWidgets"));
201                 
202         SDL_Surface *header = graphics.getSprite("joystickHeader", true)->image[0];
203         SDL_Surface *optionsBackground = graphics.getSprite("optionsBackground", true)->image[0];
204         SDL_SetColorKey(optionsBackground, 0, SDL_MapRGB(optionsBackground->format, 0, 0, 0));
205
206         int done = 0;
207         int sensitivity = (config.joystick.sensitivity / 100);
208
209         engine.setWidgetVariable("left", &config.joystick.control[CONTROL::LEFT]);
210         engine.setWidgetVariable("right", &config.joystick.control[CONTROL::RIGHT]);
211         engine.setWidgetVariable("up", &config.joystick.control[CONTROL::UP]);
212         engine.setWidgetVariable("down", &config.joystick.control[CONTROL::DOWN]);
213         engine.setWidgetVariable("fire", &config.joystick.control[CONTROL::FIRE]);
214         engine.setWidgetVariable("jump", &config.joystick.control[CONTROL::JUMP]);
215         engine.setWidgetVariable("pause", &config.joystick.control[CONTROL::PAUSE]);
216
217         engine.setWidgetVariable("jetpack", &config.joystick.control[CONTROL::JETPACK]);
218         engine.setWidgetVariable("map", &config.joystick.control[CONTROL::MAP]);
219         
220         engine.setWidgetVariable("sensitivity", &sensitivity);
221
222         engine.setWidgetVariable("confirm", &done);
223         
224         graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
225         graphics.blit(header, 320, 25, graphics.screen, true);
226         drawWidgets();
227
228         engine.flushInput();
229         engine.clearInput();
230         
231         int menuSound = -1;
232         
233         engine.allowJoypad = false;
234
235         while (!done)
236         {
237                 graphics.updateScreen();
238                 
239                 if (menuSound)
240                         audio.playMenuSound(menuSound);
241
242                 engine.getInput();
243                 config.populate();
244
245                 menuSound = engine.processWidgets();
246
247                 graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
248                 graphics.blit(header, 320, 25, graphics.screen, true);
249                 drawWidgets();
250
251                 if (engine.keyState[SDL_SCANCODE_ESCAPE])
252                 {
253                         engine.clearInput();
254                         engine.flushInput();
255                         done = 1;
256                 }
257
258                 SDL_Delay(16);
259         }
260         
261         config.joystick.sensitivity = (sensitivity * 100);
262         
263         engine.allowJoypad = true;
264         
265         config.saveJoystickConfig();
266
267         audio.playMenuSound(2);
268
269         SDL_FillRect(graphics.screen, NULL, graphics.black);
270         graphics.delay(500);
271
272         if (!engine.loadWidgets(_("data/optionWidgets")))
273         {
274                 graphics.showErrorAndExit(ERR_FILE, _("data/optionWidgets"));
275         }
276         
277         engine.highlightWidget("joysticks");
278 }
279
280 void showOptions()
281 {
282         float brightness;
283
284         SDL_FillRect(graphics.screen, NULL, graphics.black);
285         graphics.delay(500);
286
287         if (!engine.loadWidgets(_("data/optionWidgets")))
288         {
289                 graphics.showErrorAndExit(ERR_FILE, _("data/optionWidgets"));
290         }
291                 
292         SDL_Surface *header = graphics.getSprite("optionsHeader", true)->image[0];
293         SDL_Surface *optionsBackground = graphics.getSprite("optionsBackground", true)->image[0];
294         SDL_SetColorKey(optionsBackground, 0, SDL_MapRGB(optionsBackground->format, 0, 0, 0));
295
296         int done = 0;
297         int joysticks = 0;
298         int cheats = 0;
299         int keys = 0;
300
301         engine.setWidgetVariable("fullscreen", &engine.fullScreen);
302         engine.setWidgetVariable("soundvol", &game.soundVol);
303         engine.setWidgetVariable("musicvol", &game.musicVol);
304         engine.setWidgetVariable("output", &game.output);
305         engine.setWidgetVariable("autosave", &game.autoSave);
306         engine.setWidgetVariable("gamma", &game.brightness);
307         engine.setWidgetVariable("gore", &game.gore);
308         engine.setWidgetVariable("keys", &keys);
309         engine.setWidgetVariable("joysticks", &joysticks);
310         engine.setWidgetVariable("cheats", &cheats);
311         engine.setWidgetVariable("confirm", &done);
312
313         if (!engine.useAudio)
314         {
315                 engine.enableWidget("soundvol", false);
316                 engine.enableWidget("musicvol", false);
317                 engine.enableWidget("output", false);
318         }
319
320         if (SDL_NumJoysticks() == 0)
321         {
322                 engine.enableWidget("joysticks", false);
323         }
324
325         engine.showWidget("cheats", engine.cheats);
326
327         graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
328         graphics.blit(header, 320, 25, graphics.screen, true);
329         drawWidgets();
330
331         engine.flushInput();
332         engine.clearInput();
333         
334         int menuSound = -1;
335
336         while (!done)
337         {
338                 graphics.updateScreen();
339                 
340                 if (menuSound)
341                         audio.playMenuSound(menuSound);
342
343                 engine.getInput();
344                 config.populate();
345
346                 if (engine.compareLastKeyInputs())
347                 {
348                         if (engine.cheats)
349                         {
350                                 audio.playSound(SND_CHEAT, CH_ANY);
351                                 engine.clearCheatVars();
352                         }
353
354                         engine.showWidget("cheats", engine.cheats);
355                         drawWidgets();
356                 }
357                 
358                 menuSound = engine.processWidgets();
359
360                 if (menuSound)
361                 {
362                         if (engine.widgetChanged("soundvol"))
363                                 audio.setSoundVolume(game.soundVol);
364
365                         if (engine.widgetChanged("musicvol"))
366                                 audio.setMusicVolume(game.musicVol);
367
368                         if (engine.widgetChanged("fullscreen"))
369                                 SDL_SetWindowFullscreen(graphics.window, engine.fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
370
371                         if (engine.widgetChanged("gamma"))
372                         {
373                                 brightness = game.brightness;
374                                 if (brightness > 0) {
375                                         brightness /= 10;
376                                         uint16_t ramp[256];
377                                         SDL_CalculateGammaRamp(brightness, ramp);
378                                         SDL_SetWindowGammaRamp(graphics.window, ramp, ramp, ramp);
379                                 }
380                         }
381                         
382                         if ((joysticks) || (cheats) || (keys))
383                         {
384                                 audio.playMenuSound(2);
385                                 menuSound = 0;
386
387                                 if (joysticks)
388                                 {
389                                         showJoystickConfig();
390                                 }
391                                 else if (cheats)
392                                 {
393                                         showCheatConfig();
394                                 }
395                                 else if (keys)
396                                 {
397                                         showKeyConfig();
398                                 }
399                                 
400                                 joysticks = keys = cheats = 0;
401
402                                 engine.setWidgetVariable("fullscreen", &engine.fullScreen);
403                                 engine.setWidgetVariable("soundvol", &game.soundVol);
404                                 engine.setWidgetVariable("musicvol", &game.musicVol);
405                                 engine.setWidgetVariable("output", &game.output);
406                                 engine.setWidgetVariable("autosave", &game.autoSave);
407                                 engine.setWidgetVariable("gamma", &game.brightness);
408                                 engine.setWidgetVariable("gore", &game.gore);
409                                 engine.setWidgetVariable("keys", &keys);
410                                 engine.setWidgetVariable("joysticks", &joysticks);
411                                 engine.setWidgetVariable("cheats", &cheats);
412                                 engine.setWidgetVariable("confirm", &done);
413
414                                 if (!engine.useAudio)
415                                 {
416                                         engine.enableWidget("soundvol", false);
417                                         engine.enableWidget("musicvol", false);
418                                         engine.enableWidget("output", false);
419                                 }
420                                 
421                                 if (SDL_NumJoysticks() == 0)
422                                 {
423                                         engine.enableWidget("joysticks", false);
424                                 }
425                                 
426                                 engine.showWidget("cheats", engine.cheats);
427                         }
428
429                         graphics.blit(optionsBackground, 0, 0, graphics.screen, false);
430                         graphics.blit(header, 320, 25, graphics.screen, true);
431                         drawWidgets();
432                 }
433
434                 if (engine.keyState[SDL_SCANCODE_ESCAPE])
435                 {
436                         engine.clearInput();
437                         engine.flushInput();
438                         done = 1;
439                 }
440
441                 SDL_Delay(16);
442         }
443         
444         
445         if (audio.output  != game.output)
446         {
447                 audio.output = game.output;
448                 
449                 if (!audio.output)
450                 {
451                         audio.stopMusic();
452                         audio.stopAmbiance();
453                 }
454                 else
455                 {
456                         audio.playMusic();
457                         audio.playAmbiance();
458                 }
459         }
460
461         audio.playMenuSound(2);
462
463         SDL_FillRect(graphics.screen, NULL, graphics.black);
464         graphics.delay(500);
465 }