1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "DefeatState.h"
20 #include <sstream>
21 #include "../Engine/Game.h"
22 #include "../Resource/ResourcePack.h"
23 #include "../Engine/Language.h"
24 #include "../Engine/Palette.h"
25 #include "../Interface/Text.h"
26 #include "../Engine/InteractiveSurface.h"
27 #include "../Savegame/SavedGame.h"
28 #include "../Menu/MainMenuState.h"
29 #include "../Engine/Music.h"
30 #include "../Engine/Timer.h"
31 #include "../Engine/CrossPlatform.h"
32 #include "../Engine/Options.h"
33 #include "../Engine/Screen.h"
34 
35 namespace OpenXcom
36 {
37 
38 /**
39  * Initializes all the elements in the Defeat screen.
40  * @param game Pointer to the core game.
41  */
DefeatState(Game * game)42 DefeatState::DefeatState(Game *game) : State(game), _screen(-1)
43 {
44 	Options::baseXResolution = Screen::ORIGINAL_WIDTH;
45 	Options::baseYResolution = Screen::ORIGINAL_HEIGHT;
46 	game->getScreen()->resetDisplay(false);
47 	const char *files[] = {"PICT4.LBM", "PICT5.LBM"};
48 
49 	_timer = new Timer(30000);
50 
51 	_text[0] = new Text(190, 104, 0, 0);
52 	_text[1] = new Text(200, 34, 32, 0);
53 
54 	for (int i = 0; i < SCREENS; ++i)
55 	{
56 		Surface *screen = _game->getResourcePack()->getSurface(files[i]);
57 		_bg[i] = new InteractiveSurface(320, 200, 0, 0);
58 
59 		setPalette(screen->getPalette());
60 
61 		add(_bg[i]);
62 		add(_text[i]);
63 
64 		screen->blit(_bg[i]);
65 		_bg[i]->setVisible(false);
66 		_bg[i]->onMouseClick((ActionHandler)&DefeatState::screenClick);
67 
68 		std::ostringstream ss;
69 		ss << "STR_GAME_OVER_" << i + 1;
70 		_text[i]->setText(tr(ss.str()));
71 		_text[i]->setColor(Palette::blockOffset(15)+9);
72 		_text[i]->setWordWrap(true);
73 		_text[i]->setVisible(false);
74 	}
75 
76 	_game->getResourcePack()->playMusic("GMLOSE");
77 
78 	centerAllSurfaces();
79 
80 	_timer->onTimer((StateHandler)&DefeatState::screenClick);
81 	_timer->start();
82 
83 	screenClick(0);
84 
85 	// Ironman is over
86 	if (_game->getSavedGame()->isIronman())
87 	{
88 		std::string filename = CrossPlatform::sanitizeFilename(Language::wstrToFs(_game->getSavedGame()->getName())) + ".sav";
89 		CrossPlatform::deleteFile(Options::getUserFolder() + filename);
90 	}
91 }
92 
93 /**
94  *
95  */
~DefeatState()96 DefeatState::~DefeatState()
97 {
98 	delete _timer;
99 }
100 
101 /**
102  * Handle timers.
103  */
think()104 void DefeatState::think()
105 {
106 	_timer->think(this, 0);
107 }
108 
109 /**
110  * Shows the next screen in the slideshow
111  * or goes back to the Main Menu.
112  * @param action Pointer to an action.
113  */
screenClick(Action *)114 void DefeatState::screenClick(Action *)
115 {
116 	if (_screen >= 0)
117 	{
118 		_bg[_screen]->setVisible(false);
119 		_text[_screen]->setVisible(false);
120 	}
121 
122 	++_screen;
123 
124 	// next screen
125 	if (_screen < SCREENS)
126 	{
127 		setPalette(_bg[_screen]->getPalette());
128 		_bg[_screen]->setVisible(true);
129 		_text[_screen]->setVisible(true);
130 		init();
131 	}
132 	// quit game
133 	else
134 	{
135 		_game->popState();
136 		Screen::updateScale(Options::geoscapeScale, Options::geoscapeScale, Options::baseXGeoscape, Options::baseYGeoscape, true);
137 		_game->getScreen()->resetDisplay(false);
138 		_game->setState(new MainMenuState(_game));
139 		_game->setSavedGame(0);
140 	}
141 }
142 
143 }
144