Removed that logo from this bramch aswell..
[dbw.git] / src / title.cpp
bloba87527750fbaee2eb90cd5c4a53adb8b95f7c3e1
1 /*
2 Copyright © 2004 Parallel Realities
3 Copyright © 2007-2008 Kővágó Zoltán <DirtY.iCE.hu@gmail.com>
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.
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.
14 See the GNU General Public License for more details.
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.
22 #include <list>
23 #include <stdexcept>
25 #include "CAudio.h"
26 #include "CEngine.h"
27 #include "CGame.h"
28 #include "CGraphics.h"
29 #include "CMap.h"
30 #include "CMath.h"
31 #include "config.h"
32 #include "console/console.hpp"
33 #include "gettext.hpp"
34 #include "lisp/lisp.hpp"
35 #include "lisp/parser.hpp"
36 #include "lisp/list_iterator.hpp"
37 #include "title.h"
40 /**
41 * Displays the skill level widgets and hide the others
43 void showSkillLevels()
45 engine.showWidgetGroup("skill", true);
46 engine.showWidgetGroup("mainMenu", false);
47 engine.showWidgetGroup("gameSlots", false);
49 engine.showWidget("back", true);
51 engine.highlightWidget("normal");
53 engine.enableWidget("extreme", engine.extremeAvailable);
56 /**
57 * Displays the save game widgets and hide the others
59 void showGameSlots()
61 engine.showWidgetGroup("gameSlots", true);
62 engine.showWidgetGroup("mainMenu", false);
64 engine.showWidget("back", true);
66 engine.highlightWidget("slot1");
69 void showSaves()
71 engine.showWidgetGroup("saveGames", true);
72 engine.showWidgetGroup("mainMenu", false);
74 engine.showWidget("back", true);
76 engine.highlightWidget("save1");
79 /**
80 * Displays the manual widgets and hide the others
82 void showManualHelp()
84 #ifdef FRAMEWORK_SDL
85 openHelpURL();
86 #else
87 engine.showWidgetGroup("help", true);
88 engine.showWidgetGroup("mainMenu", false);
90 engine.showWidget("back", true);
92 engine.highlightWidget("back");
93 #endif
96 /**
97 * Displays the title widgets and hide the others.
98 * Also disables Continue and Load Game if games are not
99 * available to be loaded.
101 void showTitleWidgets()
103 engine.showWidgetGroup("skill", false);
104 engine.showWidgetGroup("saveGames", false);
105 engine.showWidgetGroup("gameSlots", false);
106 engine.showWidgetGroup("help", false);
107 engine.showWidget("back", false);
109 engine.showWidgetGroup("mainMenu", true);
111 engine.highlightWidget("newGame");
113 if (!engine.continueSaveSlot)
115 engine.enableWidget("loadGame", false);
116 engine.enableWidget("continue", false);
118 else
120 engine.highlightWidget("continue");
125 * Creates the data labels for the save game slots
126 * according to the save game data available
128 void setupSaveWidgets()
130 char widgetName[10];
131 strcpy(widgetName, "");
133 for (int i = 0 ; i < 5 ; i++)
135 sprintf(widgetName, "save%d", i + 1);
136 strcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i]);
138 if ((strstr(engine.saveSlot[i], _("Empty").c_str())) || (strstr(engine.saveSlot[i], _("Corrupt").c_str())))
140 engine.enableWidget(widgetName, false);
143 sprintf(widgetName, "slot%d", i + 1);
144 strcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i]);
149 * Loads the title widgets
151 void loadTitleWidgets()
153 if (!engine.loadWidgets("data/titleWidgets"))
155 graphics.showErrorAndExit(ERR_FILE, "data/titleWidgets");
158 initSaveSlots();
160 setupSaveWidgets();
162 Widget *widget = engine.getWidgetByName("labelManual");
163 strcpy(widget->label, GAMEPLAYMANUAL);
165 showTitleWidgets();
169 * Does the main game title sequence
170 * @return The section to proceed to (see main.cpp)
172 int title()
174 audio.free();
175 graphics.free();
177 SDL_FillRect(graphics.screen, NULL, graphics.black);
178 //graphics.delay(1000);
180 newGame();
182 loadTitleWidgets();
184 float backAlpha = 0;
185 float titleAlpha = 0;
186 bool allFadedOn = false;
188 graphics.quickSprite("cheatHeader", graphics.loadImage("gfx/main/cheats.png"));
189 graphics.quickSprite("optionsHeader", graphics.loadImage("gfx/main/options.png"));
190 graphics.quickSprite("keyHeader", graphics.loadImage("gfx/main/keyConfig.png"));
191 graphics.quickSprite("joystickHeader", graphics.loadImage("gfx/main/joystickConfig.png"));
192 graphics.quickSprite("optionsBackground", graphics.loadImage("gfx/main/optionsBackground.png"));
194 SDL_Surface * par_text = graphics.quickSprite("par_text", graphics.loadImage("gfx/main/par_text.png", false));
195 SDL_Surface * ds_logo = graphics.quickSprite("ds_logo", graphics.loadImage("gfx/main/ds_logo.png", false));
196 SDL_Surface * ds = graphics.quickSprite("ds", graphics.loadImage("gfx/main/ds.png", false));
197 SDL_Surface * dbw_logo = graphics.quickSprite("dbw_logo", graphics.loadImage("gfx/main/dbw_logo.png", false));
199 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
201 graphics.setFontSize(3);
202 SDL_Surface * andsign = graphics.quickSprite("and", graphics.getString("&", true));
203 SDL_Surface * presents = graphics.quickSprite("Presents", graphics.getString(_("Presents"), true));
204 SDL_Surface * sdl = graphics.quickSprite("SDL", graphics.getString(_("An SDL Game"), true));
206 graphics.setFontSize(0);
207 SDL_Surface *copyright = graphics.quickSprite("Copyright", graphics.getString("Copyright © 2004, 2005 Parallel Realities", true));
208 SDL_Surface *copyright2 = graphics.quickSprite("Copyright2", graphics.getString(_("Copyright © 2007-2008 Kővágó Zoltán, & contributors"), true));
210 SDL_Surface *branch = graphics.quickSprite("branch", graphics.getString(_("Branch:") + " " BRANCH, true));
211 SDL_Surface *version = graphics.quickSprite("Version", graphics.getString(_("Version:") + " " VERSION, true));
213 //SDL_SetAlpha(title, SDL_SRCALPHA|SDL_RLEACCEL, 0);
215 audio.loadMusic("music/helmet-shake.mod");
217 graphics.loadBackground("gfx/main/CircuitBoard.jpg");
218 SDL_SetAlpha(graphics.background, SDL_SRCALPHA|SDL_RLEACCEL, 0);
220 int startNewGame, options, quit, help, easy, normal, hard, extreme, back, practice;
221 int load, cont, save[5], slot[6];
222 int autoSaveSlot;
224 startNewGame = options = quit = easy = normal = hard = extreme = back = help = practice = 0;
225 load = cont = save[0] = save[1] = save[2] = save[3] = save[4] = 0;
226 slot[0] = slot[1] = slot[2] = slot[3] = slot[4] = slot[5] = 0;
227 autoSaveSlot = 0;
229 engine.setWidgetVariable("newGame", &startNewGame);
230 engine.setWidgetVariable("loadGame", &load);
231 engine.setWidgetVariable("continue", &cont);
232 engine.setWidgetVariable("options", &options);
233 engine.setWidgetVariable("help", &help);
234 engine.setWidgetVariable("quit", &quit);
236 float offX = 0;
238 engine.flushInput();
239 engine.clearInput();
241 audio.playMusic();
243 Uint32 now = SDL_GetTicks();
245 unsigned int frameLimit = SDL_GetTicks() + 16;
247 while (true)
249 graphics.updateScreen();
251 engine.getInput();
253 SDL_FillRect(graphics.screen, NULL, graphics.black);
255 for (int x = (int)offX ; x < graphics.width ; x += graphics.background->w)
257 for (int y = 0 ; y < graphics.height ; y += graphics.background->h)
259 graphics.blit(graphics.background, x, y, graphics.screen, false);
263 Uint32 tick = SDL_GetTicks();
265 if (!allFadedOn)
267 if (/*(tick >= now + 2000) &&*/ (tick <= now + 10000))
269 if (tick >= now + 4000)
271 graphics.blit(par_text, graphics.width / 2 - 80, graphics.height / 2 - 220, graphics.screen, true);
272 graphics.blit(andsign, graphics.width / 2 + 300, graphics.height / 2 - 220, graphics.screen, true);
273 graphics.blit(ds, graphics.width / 2, graphics.height / 2 + 210, graphics.screen, true);
275 graphics.blit(ds_logo, graphics.width / 2, graphics.height / 2, graphics.screen, true);
277 else if ((tick >= now + 13000) && (tick <= now + 19000))
279 graphics.blit(presents, graphics.width/2, graphics.height/2, graphics.screen, true);
281 else if ((tick >= now + 22000) && (tick <= now + 27000))
283 graphics.blit(sdl, graphics.width/2, graphics.height/2, graphics.screen, true);
286 if (engine.userAccepts())
288 allFadedOn = true;
289 engine.flushInput();
290 engine.clearInput();
291 backAlpha = 255;
292 titleAlpha = 255;
293 SDL_SetAlpha(graphics.background, SDL_SRCALPHA|SDL_RLEACCEL, (int)backAlpha);
294 //SDL_SetAlpha(title, SDL_SRCALPHA|SDL_RLEACCEL, (int)titleAlpha);
298 if ((tick >= now + 4000) || (allFadedOn))
300 if (backAlpha < 255)
302 backAlpha++;
303 SDL_SetAlpha(graphics.background, SDL_SRCALPHA|SDL_RLEACCEL, (int)backAlpha);
307 if ((tick >= now + 29000) || (allFadedOn))
309 if (titleAlpha < 255)
311 titleAlpha++;
312 //SDL_SetAlpha(title, SDL_SRCALPHA|SDL_RLEACCEL, (int)titleAlpha);
314 else
316 graphics.blit(copyright, 10, graphics.height - 40, graphics.screen, false);
317 graphics.blit(copyright2,10, graphics.height - 20, graphics.screen, false);
318 graphics.blit(branch, (graphics.width - 10 - branch->w), graphics.height - 40, graphics.screen, false);
319 graphics.blit(version, (graphics.width - 10 - version->w), graphics.height - 20, graphics.screen, false);
320 allFadedOn = true;
324 Math::wrapFloat(&(offX -= 0.25), -graphics.background->w, 0);
326 if (titleAlpha > 0)
328 graphics.blit(dbw_logo, graphics.width/2, 120, graphics.screen, true);
331 if (allFadedOn)
333 drawWidgets();
334 audio.playMenuSound(engine.processWidgets());
337 if (options)
339 showOptions();
340 loadTitleWidgets();
341 engine.setWidgetVariable("newGame", &startNewGame);
342 engine.setWidgetVariable("loadGame", &load);
343 engine.setWidgetVariable("continue", &cont);
344 engine.setWidgetVariable("options", &options);
345 engine.setWidgetVariable("help", &help);
346 engine.setWidgetVariable("quit", &quit);
347 options = 0;
348 engine.flushInput();
351 if (help)
353 showManualHelp();
354 engine.setWidgetVariable("back", &back);
355 help = 0;
358 if (startNewGame)
360 showGameSlots();
361 engine.setWidgetVariable("slot1", &slot[0]);
362 engine.setWidgetVariable("slot2", &slot[1]);
363 engine.setWidgetVariable("slot3", &slot[2]);
364 engine.setWidgetVariable("slot4", &slot[3]);
365 engine.setWidgetVariable("slot5", &slot[4]);
366 engine.setWidgetVariable("slot6", &slot[5]);
367 engine.setWidgetVariable("back", &back);
368 startNewGame = 0;
371 for (int i = 0 ; i < 6 ; i++)
373 if (slot[i])
375 showSkillLevels();
376 engine.setWidgetVariable("practice", &practice);
377 engine.setWidgetVariable("easy", &easy);
378 engine.setWidgetVariable("normal", &normal);
379 engine.setWidgetVariable("hard", &hard);
380 engine.setWidgetVariable("extreme", &extreme);
381 engine.setWidgetVariable("back", &back);
382 autoSaveSlot = i;
383 slot[i] = 0;
387 if (load)
389 showSaves();
390 engine.setWidgetVariable("save1", &save[0]);
391 engine.setWidgetVariable("save2", &save[1]);
392 engine.setWidgetVariable("save3", &save[2]);
393 engine.setWidgetVariable("save4", &save[3]);
394 engine.setWidgetVariable("save5", &save[4]);
395 engine.setWidgetVariable("back", &back);
396 load = 0;
399 if (easy) {engine.skill = 0; break;}
400 if (normal) {engine.skill = 1; break;}
401 if (hard) {engine.skill = 2; break;}
402 if (extreme) {engine.skill = 3; break;}
403 if (practice) {engine.skill = -1; break;}
405 if (cont) {load = engine.continueSaveSlot; break;}
407 if (save[0]) {load = 1; break;}
408 if (save[1]) {load = 2; break;}
409 if (save[2]) {load = 3; break;}
410 if (save[3]) {load = 4; break;}
411 if (save[4]) {load = 5; break;}
413 if (engine.keyState[SDLK_ESCAPE])
415 audio.playMenuSound(2);
416 back = 1;
417 engine.clearInput();
418 engine.flushInput();
421 if (back)
423 showTitleWidgets();
424 back = 0;
427 if (quit)
429 break;
432 engine.delay(frameLimit);
433 frameLimit = SDL_GetTicks() + 16;
436 engine.deleteWidgets();
438 SDL_FillRect(graphics.screen, NULL, graphics.black);
439 graphics.updateScreen();
440 audio.stopMusic();
442 if (quit)
444 exit(0);
447 newGame();
449 (autoSaveSlot < 5) ? game.autoSaveSlot = autoSaveSlot : game.autoSave = false;
451 if (load)
453 loadGame(load - 1);
455 debug("misc", 2) << "Map Name = " << game.mapName << std::endl;
457 else if (engine.skill == -1)
459 game.setMapName("data/practice");
460 game.setStageName("Training Mission");
461 engine.practice = true;
462 game.skill = 1;
463 return SECTION_GAME;
465 else if (engine.skill == 3)
467 game.skill = 3;
468 game.hasAquaLung = game.hasJetPack = true;
471 if (strcmp(game.mapName, "data/spaceStation") == 0)
473 game.setMapName("data/finalBattle");
474 game.setStageName("Final Battle");
475 return SECTION_GAME;
478 return SECTION_HUB;
483 * A class; that stores one line in the credits screen.
485 class CreditsLine
487 public:
489 * Constructor for CreditsLine
490 * @param iter A lisp::ListIterator, to the actual Lisp item
492 CreditsLine(const lisp::ListIterator& iter) : it(iter) { surface = NULL; };
493 /// @return The SDL_Surface of the image
494 SDL_Surface * GetImage() { if (!surface) MakeSurface(); return surface; };
495 /// @return The height of the line (currently the skip)
496 int GetHeight() { if (!surface) MakeSurface(); return height; };
497 /// Destructor
498 ~CreditsLine() { SDL_FreeSurface(surface); }
499 private:
500 lisp::ListIterator it;
501 SDL_Surface * surface;
502 int height;
504 void MakeSurface();
508 * Parses the Lisp item; then generates an SDL_Surface
509 * for blitting.
511 void CreditsLine::MakeSurface()
513 if (it.item() == "line")
515 height = it.value()->get_int();
516 const lisp::Lisp * ptr = it.lisp()->get_cdr();
517 graphics.setFontSize(ptr->get_car()->get_int());
518 ptr = ptr->get_cdr();
519 surface = graphics.getString(ptr->get_car()->get_string(), true);
521 else
522 throw Exception::RuntimeError("Unknown lisp item: '" + it.item() +"'", AT);
526 * Loads and shows the credits at the end of the game.
528 void doCredits()
530 map.clear();
532 audio.loadMusic("music/autumnsDawning.s3m");
533 audio.playMusic();
535 engine.resetTimeDifference();
537 SDL_Surface *backdrop = graphics.quickSprite("credits_background", graphics.loadImage("gfx/main/creditsBack.png", false));
539 lisp::Parser parser(true);
540 const lisp::Lisp *credits_root = parser.parse("credits.dbwc");
541 const lisp::Lisp *credits = credits_root->get_lisp("dirtyblobwars-credits");
543 float y = 480;
545 std::list<CreditsLine> lines;
546 std::list<CreditsLine>::iterator it;
548 lisp::ListIterator iter(credits);
550 while (iter.next())
552 lines.push_back(CreditsLine(iter));
555 while (!lines.empty())
557 graphics.updateScreen();
559 engine.getInput();
560 engine.doTimeDifference();
562 SDL_FillRect(graphics.screen, NULL, graphics.black);
564 graphics.blit(backdrop, bx, by+365, graphics.screen, false);
566 y -= (0.3 * engine.getTimeDifference());
567 //y -= 1;
569 it = lines.begin();
570 if ((y + it->GetHeight()) < -10)
572 y += it->GetHeight();
573 lines.pop_front();
576 //if we don't get a new iterator; we'll have an iterator to a deleted memory region!
577 it = lines.begin();
579 for (float y2 = y; ((y2 += it->GetHeight()) < 490) && (it != lines.end()); it++)
581 graphics.blit(it->GetImage(), graphics.width/2, by+(int)y2, graphics.screen, true);
585 audio.fadeMusic();
586 graphics.fadeToBlack();