]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/title.cpp
Use strlcat() and strlcpy(), #define wrappers when these functions are not available.
[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 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, 2005 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                 if (engine.useAudio)
424                 {
425                         SDL_Delay(1000);
426                 }
427                 exit(0);
428         }
429         
430         newGame();
431         
432         (autoSaveSlot < 5) ? game.autoSaveSlot = autoSaveSlot : game.autoSave = false;
433
434         if (load)
435         {
436                 loadGame(load - 1);
437                 
438                 debug(("Map Name = %s", game.mapName))
439         }
440         else if (engine.skill == -1)
441         {
442                 game.setMapName("data/practice");
443                 game.setStageName("Training Mission");
444                 engine.practice = true;
445                 game.skill = 1;
446                 return SECTION_GAME;
447         }
448         else if (engine.skill == 3)
449         {
450                 game.skill = 3;
451                 game.hasAquaLung = game.hasJetPack = true;
452         }
453         
454         if (strcmp(game.mapName, "data/spaceStation") == 0)
455         {
456                 game.setMapName("data/finalBattle");
457                 game.setStageName("Final Battle");
458                 return SECTION_GAME;
459         }
460
461         return SECTION_HUB;
462 }
463
464 /**
465 * Loads and shows the credits at the end of the game.
466 */
467 void doCredits()
468 {
469         map.clear();
470
471         char *line;
472         char string[100];
473         int i = 0;
474         int numberOfCredits = 0;
475         int pos1 = 0, pos2 = 0, size = 0;
476         float *y;
477
478         SDL_Surface *backdrop = graphics.quickSprite("CreditsBackGround", graphics.loadImage("gfx/main/creditsBack.png"));
479
480         engine.loadData(_("data/credits"));
481
482         line = strtok((char*)engine.dataBuffer, "\n");
483
484         sscanf(line, "%d", &numberOfCredits);
485
486         y = new float[numberOfCredits];
487          SDL_Surface **credit = new SDL_Surface*[numberOfCredits];
488
489         line = strtok(NULL, "\n");
490
491         pos1 = 520;
492
493         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
494
495         while (line)
496         {
497                 sscanf(line, "%d %d %[^\n\r]", &pos2, &size, string);
498
499                 pos1 += pos2;
500
501                 y[i] = pos1;
502
503                 graphics.setFontSize(size);
504                 credit[i] = graphics.quickSprite("credit", graphics.getString(string, true));
505
506                 i++;
507
508                 if (i == numberOfCredits)
509                 {
510                         break;
511                 }
512
513                 line = strtok(NULL, "\n");
514         }
515         
516         SDL_Surface *device = graphics.quickSprite("credit", graphics.loadImage("gfx/main/creditsDevice.png"));
517         float deviceY = y[numberOfCredits - 7] - 50;
518
519         audio.loadMusic("music/credits");
520         audio.playMusic();
521
522         engine.resetTimeDifference();
523         Uint32 now = SDL_GetTicks();
524
525         while (y[numberOfCredits - 1] > 350)
526         {
527                 graphics.updateScreen();
528                 engine.getInput();
529                 config.populate();
530
531                 engine.doTimeDifference();
532
533                 SDL_FillRect(graphics.screen, NULL, graphics.black);
534                 graphics.blit(backdrop, 0, 365, graphics.screen, false);
535                 
536                 deviceY -= (0.25 * engine.getTimeDifference());
537                 
538                 if ((deviceY > 10) && (deviceY < 470))
539                 {
540                         graphics.blit(device, 320, (int)deviceY, graphics.screen, true);
541                 }
542
543                 for (i = 0 ; i < numberOfCredits ; i++)
544                 {
545                         y[i] -= (0.25 * engine.getTimeDifference());
546                         
547                         if ((y[i] > 10) && (y[i] < 470))
548                         {
549                                 graphics.blit(credit[i], 320, (int)y[i], graphics.screen, true);
550                         }
551
552                         graphics.drawRect(0, 450, 640, 30, graphics.black, graphics.screen);
553                         graphics.drawRect(0, 0, 640, 30, graphics.black, graphics.screen);
554                 }
555
556                 doMusicInfo(SDL_GetTicks() - (now + 10000));
557
558                 SDL_Delay(16);
559         }
560
561         graphics.delay(12000);
562         audio.fadeMusic();
563         graphics.fadeToBlack();
564
565         delete[] y;
566         delete[] credit;
567 }