]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/title.cpp
1.17 Medal Support
[quix0rs-blobwars.git] / src / title.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "title.h"
22
23 /**
24 * Displays the skill level widgets and hide the others
25 */
26 void showSkillLevels()
27 {
28         engine.showWidgetGroup("skill", true);
29         engine.showWidgetGroup("mainMenu", false);
30         engine.showWidgetGroup("gameSlots", false);
31
32         engine.showWidget("back", true);
33
34         engine.highlightWidget("normal");
35                   
36         engine.enableWidget("extreme", engine.extremeAvailable);
37 }
38
39 /**
40 * Displays the save game widgets and hide the others
41 */
42 void showGameSlots()
43 {
44         engine.showWidgetGroup("gameSlots", true);
45         engine.showWidgetGroup("mainMenu", false);
46
47         engine.showWidget("back", true);
48
49         engine.highlightWidget("slot1");
50 }
51
52 void showSaves()
53 {
54         engine.showWidgetGroup("saveGames", true);
55         engine.showWidgetGroup("mainMenu", false);
56
57         engine.showWidget("back", true);
58
59         engine.highlightWidget("save1");
60 }
61
62 /**
63 * Displays the manual widgets and hide the others
64 */
65 void showManualHelp()
66 {
67         #ifdef FRAMEWORK_SDL
68         openHelpURL();
69         #else
70         engine.showWidgetGroup("help", true);
71         engine.showWidgetGroup("mainMenu", false);
72         
73         engine.showWidget("back", true);
74
75         engine.highlightWidget("back");
76         #endif
77 }
78
79 /**
80 * Displays the title widgets and hide the others.
81 * Also disables Continue and Load Game if games are not
82 * available to be loaded.
83 */
84 void showTitleWidgets()
85 {
86         engine.showWidgetGroup("skill", false);
87         engine.showWidgetGroup("saveGames", false);
88         engine.showWidgetGroup("gameSlots", false);
89         engine.showWidgetGroup("help", false);
90         engine.showWidget("back", false);
91         
92         engine.showWidgetGroup("mainMenu", true);
93         
94         engine.highlightWidget("newGame");
95
96         if (!engine.continueSaveSlot)
97         {
98                 engine.enableWidget("loadGame", false);
99                 engine.enableWidget("continue", false);
100         }
101         else
102         {
103                 engine.highlightWidget("continue");
104         }
105 }
106
107 /**
108 * Creates the data labels for the save game slots
109 * according to the save game data available
110 */
111 void setupSaveWidgets()
112 {
113         char widgetName[10];
114         strcpy(widgetName, "");
115         
116         for (int i = 0 ; i < 5 ; i++)
117         {
118                 sprintf(widgetName, "save%d", i + 1);
119                 strcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i]);
120                 
121                 if ((strstr(engine.saveSlot[i], _("Empty"))) || (strstr(engine.saveSlot[i], _("Corrupt"))))
122                 {
123                         engine.enableWidget(widgetName, false);
124                 }
125                 
126                 sprintf(widgetName, "slot%d", i + 1);
127                 strcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i]);
128         }
129 }
130
131 /**
132 * Loads the title widgets
133 */
134 void loadTitleWidgets()
135 {
136         if (!engine.loadWidgets(_("data/titleWidgets")))
137         {
138                 graphics.showErrorAndExit(ERR_FILE, _("data/titleWidgets"));
139         }
140
141         initSaveSlots();
142
143         setupSaveWidgets();
144         
145         Widget *widget = engine.getWidgetByName("labelManual");
146         strcpy(widget->label, GAMEPLAYMANUAL);
147
148         showTitleWidgets();
149 }
150
151 /**
152 * Does the main game title sequence
153 * @return The section to proceed to (see main.cpp)
154 */
155 int title()
156 {
157         audio.free();
158         graphics.free();
159
160         SDL_FillRect(graphics.screen, NULL, graphics.black);
161         graphics.delay(1000);
162
163         newGame();
164
165         loadTitleWidgets();
166
167         float backAlpha = 0;
168         float titleAlpha = 0;
169         bool allFadedOn = false;
170
171         graphics.quickSprite("cheatHeader", graphics.loadImage("gfx/main/cheats.png"));
172         graphics.quickSprite("optionsHeader", graphics.loadImage("gfx/main/options.png"));
173         graphics.quickSprite("keyHeader", graphics.loadImage("gfx/main/keyConfig.png"));
174         graphics.quickSprite("joystickHeader", graphics.loadImage("gfx/main/joystickConfig.png"));
175         graphics.quickSprite("optionsBackground", graphics.loadImage("gfx/main/optionsBackground.png"));
176
177         SDL_Surface *prlogo = graphics.quickSprite("PRLogo", graphics.loadImage("gfx/main/prlogo.gif"));
178         SDL_Surface *title = graphics.quickSprite("Title", graphics.loadImage("gfx/main/title.png"));
179         
180         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
181
182         graphics.setFontSize(3);
183         SDL_Surface *presents = graphics.quickSprite("Presents", graphics.getString(_("Presents"), true));
184         SDL_Surface *sdl = graphics.quickSprite("SDL", graphics.getString(_("An SDL Game"), true));
185
186         graphics.setFontSize(2);
187         SDL_Surface *subTitle = graphics.quickSprite("SubTitle", graphics.getString(_("Blob Wars : Episode I"), true));
188
189         graphics.setFontSize(0);
190         SDL_Surface *copyright = graphics.quickSprite("Copyright", graphics.getString(_("Copyright (C) 2004, 2005 Parallel Realities"), true));
191
192         char v[50];
193         #define STRINGIFY_VALUE(x) STRINGIFY(x)
194         #define STRINGIFY(x) #x
195         sprintf(v, _("Version %s"), STRINGIFY_VALUE(VERSION));
196         SDL_Surface *version = graphics.quickSprite("Version", graphics.getString(v, true));
197
198         SDL_SetAlpha(title, SDL_SRCALPHA|SDL_RLEACCEL, 0);
199
200         audio.loadMusic("music/helmet-shake.mod");
201
202         graphics.loadBackground("gfx/main/CircuitBoard.jpg");
203         SDL_SetAlpha(graphics.background, SDL_SRCALPHA|SDL_RLEACCEL, 0);
204
205         int startNewGame, options, quit, help, easy, normal, hard, extreme, back, practice;
206         int load, cont, save[5], slot[6];
207         int autoSaveSlot;
208
209         startNewGame = options = quit = easy = normal = hard = extreme = back = help = practice = 0;
210         load = cont = save[0] = save[1] = save[2] = save[3] = save[4] = 0;
211         slot[0] = slot[1] = slot[2] = slot[3] = slot[4] = slot[5] = 0;
212         autoSaveSlot = 0;
213
214         engine.setWidgetVariable("newGame", &startNewGame);
215         engine.setWidgetVariable("loadGame", &load);
216         engine.setWidgetVariable("continue", &cont);
217         engine.setWidgetVariable("options", &options);
218         engine.setWidgetVariable("help", &help);
219         engine.setWidgetVariable("quit", &quit);
220
221         float offX = 0;
222
223         engine.flushInput();
224         engine.clearInput();
225
226         audio.playMusic();
227
228         Uint32 now = SDL_GetTicks();
229
230         unsigned int frameLimit = SDL_GetTicks() + 16;
231
232         while (true)
233         {
234                 graphics.updateScreen();
235
236                 engine.getInput();
237                 config.populate();
238
239                 for (int x = (int)offX ; x < 640 ; x += graphics.background->w)
240                 {
241                         for (int y = 0 ; y < 480 ; y += graphics.background->h)
242                         {
243                                 graphics.blit(graphics.background, x, y, graphics.screen, false);
244                         }
245                 }
246
247                 if (!allFadedOn)
248                 {
249                         if ((SDL_GetTicks() >= now + 2000) && (SDL_GetTicks() <= now + 10000))
250                         {
251                                 graphics.blit(prlogo, 320, 240, graphics.screen, true);
252                         }
253                         else if ((SDL_GetTicks() >= now + 13000) && (SDL_GetTicks() <= now + 19000))
254                         {
255                                 graphics.blit(presents, 320, 240, graphics.screen, true);
256                         }
257                         else if ((SDL_GetTicks() >= now + 22000) && (SDL_GetTicks() <= now + 27000))
258                         {
259                                 graphics.blit(sdl, 320, 240, graphics.screen, true);
260                         }
261
262                         if (engine.userAccepts())
263                         {
264                                 allFadedOn = true;
265                                 engine.flushInput();
266                                 engine.clearInput();
267                                 backAlpha = 255;
268                                 titleAlpha = 255;
269                                 SDL_SetAlpha(graphics.background, SDL_SRCALPHA|SDL_RLEACCEL, (int)backAlpha);
270                                 SDL_SetAlpha(title, SDL_SRCALPHA|SDL_RLEACCEL, (int)titleAlpha);
271                         }
272                 }
273
274                 if ((SDL_GetTicks() >= now + 4000) || (allFadedOn))
275                 {
276                         if (backAlpha < 255)
277                         {
278                                 backAlpha++;
279                                 SDL_SetAlpha(graphics.background, SDL_SRCALPHA|SDL_RLEACCEL, (int)backAlpha);
280                         }
281                 }
282
283                 if ((SDL_GetTicks() >= now + 29000) || (allFadedOn))
284                 {
285                         if (titleAlpha < 255)
286                         {
287                                 titleAlpha++;
288                                 SDL_SetAlpha(title, SDL_SRCALPHA|SDL_RLEACCEL, (int)titleAlpha);
289                         }
290                         else
291                         {
292                                 graphics.blit(subTitle, 320, 180, graphics.screen, true);
293                                 graphics.blit(copyright, 10, 460, graphics.screen, false);
294                                 graphics.blit(version, (630 - version->w), 460, graphics.screen, false);
295                                 allFadedOn = true;
296                         }
297                 }
298
299                 Math::wrapFloat(&(offX -= 0.25), -graphics.background->w, 0);
300
301                 if (backAlpha > 0)
302                 {
303                         graphics.blit(title, 320, 100, graphics.screen, true);
304                 }
305
306                 if (allFadedOn)
307                 {
308                         drawWidgets();
309                         audio.playMenuSound(engine.processWidgets());
310                 }
311
312                 if (options)
313                 {
314                         showOptions();
315                         loadTitleWidgets();
316                         engine.setWidgetVariable("newGame", &startNewGame);
317                         engine.setWidgetVariable("loadGame", &load);
318                         engine.setWidgetVariable("continue", &cont);
319                         engine.setWidgetVariable("options", &options);
320                         engine.setWidgetVariable("help", &help);
321                         engine.setWidgetVariable("quit", &quit);
322                         options = 0;
323                         engine.flushInput();
324                 }
325
326                 if (help)
327                 {
328                         showManualHelp();
329                         engine.setWidgetVariable("back", &back);
330                         help = 0;
331                 }
332
333                 if (startNewGame)
334                 {
335                         showGameSlots();
336                         engine.setWidgetVariable("slot1", &slot[0]);
337                         engine.setWidgetVariable("slot2", &slot[1]);
338                         engine.setWidgetVariable("slot3", &slot[2]);
339                         engine.setWidgetVariable("slot4", &slot[3]);
340                         engine.setWidgetVariable("slot5", &slot[4]);
341                         engine.setWidgetVariable("slot6", &slot[5]);
342                         engine.setWidgetVariable("back", &back);
343                         startNewGame = 0;
344                 }
345
346                 for (int i = 0 ; i < 6 ; i++)
347                 {
348                         if (slot[i])
349                         {
350                                 showSkillLevels();
351                                 engine.setWidgetVariable("practice", &practice);
352                                 engine.setWidgetVariable("easy", &easy);
353                                 engine.setWidgetVariable("normal", &normal);
354                                 engine.setWidgetVariable("hard", &hard);
355                                 engine.setWidgetVariable("extreme", &extreme);
356                                 engine.setWidgetVariable("back", &back);
357                                 autoSaveSlot = i;
358                                 slot[i] = 0;
359                         }
360                 }
361
362                 if (load)
363                 {
364                         showSaves();
365                         engine.setWidgetVariable("save1", &save[0]);
366                         engine.setWidgetVariable("save2", &save[1]);
367                         engine.setWidgetVariable("save3", &save[2]);
368                         engine.setWidgetVariable("save4", &save[3]);
369                         engine.setWidgetVariable("save5", &save[4]);
370                         engine.setWidgetVariable("back", &back);
371                         load = 0;
372                 }
373
374                 if (easy) {engine.skill = 0; break;}
375                 if (normal) {engine.skill = 1; break;}
376                 if (hard) {engine.skill = 2; break;}
377                 if (extreme) {engine.skill = 3; break;}
378                 if (practice) {engine.skill = -1; break;}
379
380                 if (cont) {load = engine.continueSaveSlot; break;}
381
382                 if (save[0]) {load = 1; break;}
383                 if (save[1]) {load = 2; break;}
384                 if (save[2]) {load = 3; break;}
385                 if (save[3]) {load = 4; break;}
386                 if (save[4]) {load = 5; break;}
387
388                 if (engine.keyState[SDLK_ESCAPE])
389                 {
390                         audio.playMenuSound(2);
391                         back = 1;
392                         engine.clearInput();
393                         engine.flushInput();
394                 }
395
396                 if (back)
397                 {
398                         showTitleWidgets();
399                         back = 0;
400                 }
401
402                 if (quit)
403                 {
404                         break;
405                 }
406
407                 engine.delay(frameLimit);
408                 frameLimit = SDL_GetTicks()  + 16;
409         }
410
411         engine.deleteWidgets();
412
413         SDL_FillRect(graphics.screen, NULL, graphics.black);
414         graphics.updateScreen();
415         audio.stopMusic();
416         
417         if (quit)
418         {
419                 if (engine.useAudio)
420                 {
421                         SDL_Delay(1000);
422                 }
423                 exit(0);
424         }
425         
426         newGame();
427         
428         (autoSaveSlot < 5) ? game.autoSaveSlot = autoSaveSlot : game.autoSave = false;
429
430         if (load)
431         {
432                 loadGame(load - 1);
433                 
434                 debug(("Map Name = %s", game.mapName))
435         }
436         else if (engine.skill == -1)
437         {
438                 game.setMapName("data/practice");
439                 game.setStageName("Training Mission");
440                 engine.practice = true;
441                 game.skill = 1;
442                 return SECTION_GAME;
443         }
444         else if (engine.skill == 3)
445         {
446                 game.skill = 3;
447                 game.hasAquaLung = game.hasJetPack = true;
448         }
449         
450         if (strcmp(game.mapName, "data/spaceStation") == 0)
451         {
452                 game.setMapName("data/finalBattle");
453                 game.setStageName("Final Battle");
454                 return SECTION_GAME;
455         }
456
457         return SECTION_HUB;
458 }
459
460 /**
461 * Loads and shows the credits at the end of the game.
462 */
463 void doCredits()
464 {
465         map.clear();
466
467         char *line;
468         char string[100];
469         int i = 0;
470         int numberOfCredits = 0;
471         int pos1 = 0, pos2 = 0, size = 0;
472         float *y;
473
474         SDL_Surface *backdrop = graphics.quickSprite("CreditsBackGround", graphics.loadImage("gfx/main/creditsBack.png"));
475
476         engine.loadData(_("data/credits"));
477
478         line = strtok((char*)engine.dataBuffer, "\n");
479
480         sscanf(line, "%d", &numberOfCredits);
481
482         y = new float[numberOfCredits];
483          SDL_Surface **credit = new SDL_Surface*[numberOfCredits];
484
485         line = strtok(NULL, "\n");
486
487         pos1 = 520;
488
489         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
490
491         while (line)
492         {
493                 sscanf(line, "%d %d %[^\n\r]", &pos2, &size, string);
494
495                 pos1 += pos2;
496
497                 y[i] = pos1;
498
499                 graphics.setFontSize(size);
500                 credit[i] = graphics.quickSprite("credit", graphics.getString(string, true));
501
502                 i++;
503
504                 if (i == numberOfCredits)
505                 {
506                         break;
507                 }
508
509                 line = strtok(NULL, "\n");
510         }
511         
512         SDL_Surface *device = graphics.quickSprite("credit", graphics.loadImage("gfx/main/creditsDevice.png"));
513         float deviceY = y[numberOfCredits - 7] - 50;
514
515         audio.loadMusic("music/autumnsDawning.s3m");
516         audio.playMusic();
517
518         engine.resetTimeDifference();
519
520         while (y[numberOfCredits - 1] > 350)
521         {
522                 graphics.updateScreen();
523                 engine.getInput();
524                 config.populate();
525
526                 engine.doTimeDifference();
527
528                 SDL_FillRect(graphics.screen, NULL, graphics.black);
529                 graphics.blit(backdrop, 0, 365, graphics.screen, false);
530                 
531                 deviceY -= (0.25 * engine.getTimeDifference());
532                 
533                 if ((deviceY > 10) && (deviceY < 470))
534                 {
535                         graphics.blit(device, 320, (int)deviceY, graphics.screen, true);
536                 }
537
538                 for (i = 0 ; i < numberOfCredits ; i++)
539                 {
540                         y[i] -= (0.25 * engine.getTimeDifference());
541                         
542                         if ((y[i] > 10) && (y[i] < 470))
543                         {
544                                 graphics.blit(credit[i], 320, (int)y[i], graphics.screen, true);
545                         }
546
547                         graphics.drawRect(0, 450, 640, 30, graphics.black, graphics.screen);
548                         graphics.drawRect(0, 0, 640, 30, graphics.black, graphics.screen);
549                 }
550
551                 SDL_Delay(16);
552         }
553
554         graphics.delay(12000);
555         audio.fadeMusic();
556         graphics.fadeToBlack();
557
558         delete[] y;
559         delete[] credit;
560 }