Repository navigation
Expand file tree
/
Copy pathLoadGameState.cpp
More file actions
226 lines (196 loc) · 6.32 KB
/
Copy pathLoadGameState.cpp
File metadata and controls
226 lines (196 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright 2010-2012 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LoadGameState.h"
#include <iostream>
#include <yaml-cpp/yaml.h>
#include "../Savegame/SavedGame.h"
#include "../Savegame/SavedBattleGame.h"
#include "../Engine/Game.h"
#include "../Engine/Exception.h"
#include "../Engine/Options.h"
#include "../Resource/ResourcePack.h"
#include "../Ruleset/XcomRuleset.h"
#include "../Engine/Language.h"
#include "../Engine/Font.h"
#include "../Engine/Palette.h"
#include "../Interface/TextButton.h"
#include "../Interface/Window.h"
#include "../Interface/Text.h"
#include "../Interface/TextList.h"
#include "../Geoscape/GeoscapeState.h"
#include "ErrorMessageState.h"
#include "../Battlescape/BattlescapeState.h"
namespace OpenXcom
{
/**
* Initializes all the elements in the Load Game screen.
* @param game Pointer to the core game.
* @param geo True to use Geoscape palette, false to use Battlescape palette.
*/
LoadGameState::LoadGameState(Game *game, bool geo) : State(game), _geo(geo)
{
// Create objects
WindowPopup p = POPUP_BOTH;
if (!geo)
p = POPUP_NONE;
_window = new Window(this, 320, 200, 0, 0, p);
_btnCancel = new TextButton(80, 16, 120, 172);
_txtTitle = new Text(310, 16, 5, 8);
_txtName = new Text(150, 9, 16, 24);
_txtTime = new Text(30, 9, 184, 24);
_txtDate = new Text(30, 9, 214, 24);
_lstSaves = new TextList(288, 128, 8, 32);
// Set palette
if (_geo)
{
_game->setPalette(_game->getResourcePack()->getPalette("BACKPALS.DAT")->getColors(Palette::blockOffset(6)), Palette::backPos, 16);
}
add(_window);
add(_btnCancel);
add(_txtTitle);
add(_txtName);
add(_txtTime);
add(_txtDate);
add(_lstSaves);
// Set up objects
if (_geo)
{
_window->setColor(Palette::blockOffset(8)+5);
_window->setBackground(game->getResourcePack()->getSurface("BACK01.SCR"));
_btnCancel->setColor(Palette::blockOffset(8)+5);
_txtTitle->setColor(Palette::blockOffset(15)-1);
_txtName->setColor(Palette::blockOffset(15)-1);
_txtTime->setColor(Palette::blockOffset(15)-1);
_txtDate->setColor(Palette::blockOffset(15)-1);
_lstSaves->setColor(Palette::blockOffset(8)+10);
}
else
{
_window->setColor(Palette::blockOffset(0));
_window->setHighContrast(true);
_window->setBackground(_game->getResourcePack()->getSurface("TAC00.SCR"));
_btnCancel->setColor(Palette::blockOffset(0));
_btnCancel->setHighContrast(true);
_txtTitle->setColor(Palette::blockOffset(0));
_txtTitle->setHighContrast(true);
_txtName->setColor(Palette::blockOffset(0));
_txtName->setHighContrast(true);
_txtTime->setColor(Palette::blockOffset(0));
_txtTime->setHighContrast(true);
_txtDate->setColor(Palette::blockOffset(0));
_txtDate->setHighContrast(true);
_lstSaves->setColor(Palette::blockOffset(0));
_lstSaves->setHighContrast(true);
}
_btnCancel->setText(_game->getLanguage()->getString("STR_CANCEL_UC"));
_btnCancel->onMouseClick((ActionHandler)&LoadGameState::btnCancelClick);
_txtTitle->setBig();
_txtTitle->setAlign(ALIGN_CENTER);
_txtTitle->setText(_game->getLanguage()->getString("STR_SELECT_GAME_TO_LOAD"));
_txtName->setText(_game->getLanguage()->getString("STR_NAME"));
_txtTime->setText(_game->getLanguage()->getString("STR_TIME"));
_txtDate->setText(_game->getLanguage()->getString("STR_DATE"));
_lstSaves->setColumns(5, 168, 30, 30, 30, 30);
_lstSaves->setSelectable(true);
_lstSaves->setBackground(_window);
_lstSaves->setMargin(8);
_lstSaves->onMouseClick((ActionHandler)&LoadGameState::lstSavesClick);
}
/**
*
*/
LoadGameState::~LoadGameState()
{
}
/**
* Resets the palette and refreshes saves.
*/
void LoadGameState::init()
{
if (_geo)
{
_game->setPalette(_game->getResourcePack()->getPalette("BACKPALS.DAT")->getColors(Palette::blockOffset(6)), Palette::backPos, 16);
}
_lstSaves->clearList();
try
{
SavedGame::getList(_lstSaves, _game->getLanguage());
}
catch (Exception &e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
std::cerr << Options::getUserFolder() << std::endl;
}
}
/**
* Returns to the previous screen.
* @param action Pointer to an action.
*/
void LoadGameState::btnCancelClick(Action *action)
{
_game->popState();
}
/**
* Loads the selected save.
* @param action Pointer to an action.
*/
void LoadGameState::lstSavesClick(Action *action)
{
//Ruleset *r = new XcomRuleset();
SavedGame *s = new SavedGame();
try
{
//_game->setRuleset(r);
s->load(Language::wstrToUtf8(_lstSaves->getCellText(_lstSaves->getSelectedRow(), 0)), _game->getRuleset());
_game->setSavedGame(s);
_game->setState(new GeoscapeState(_game));
if (_game->getSavedGame()->getBattleGame() != 0)
{
_game->getSavedGame()->getBattleGame()->loadMapResources(_game->getResourcePack());
_game->pushState(new BattlescapeState(_game));
}
}
catch (Exception &e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
std::wstring error = _game->getLanguage()->getString("STR_LOAD_UNSUCCESSFUL") + L'\x02' + Language::utf8ToWstr(e.what());
if (_geo)
_game->pushState(new ErrorMessageState(_game, error, Palette::blockOffset(8)+10, "BACK01.SCR", 6));
else
_game->pushState(new ErrorMessageState(_game, error, Palette::blockOffset(0), "TAC00.SCR", -1));
//delete r;
delete s;
//_game->setRuleset(0);
_game->setSavedGame(0);
}
catch (YAML::Exception &e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
std::wstring error = _game->getLanguage()->getString("STR_LOAD_UNSUCCESSFUL") + L'\x02' + Language::utf8ToWstr(e.what());
if (_geo)
_game->pushState(new ErrorMessageState(_game, error, Palette::blockOffset(8)+10, "BACK01.SCR", 6));
else
_game->pushState(new ErrorMessageState(_game, error, Palette::blockOffset(0), "TAC00.SCR", -1));
//delete r;
delete s;
//_game->setRuleset(0);
_game->setSavedGame(0);
}
}
}