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