Repository navigation
Expand file tree
/
Copy pathResourcePack.cpp
More file actions
260 lines (244 loc) · 6.36 KB
/
Copy pathResourcePack.cpp
File metadata and controls
260 lines (244 loc) · 6.36 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright 2010 Daniel Albano
*
* 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 "ResourcePack.h"
#include <sys/stat.h>
#include "../Engine/Palette.h"
#include "../Engine/Font.h"
#include "../Engine/Surface.h"
#include "../Engine/SurfaceSet.h"
#include "../Engine/Language.h"
#include "../Engine/Music.h"
#include "../Geoscape/Globe.h"
#include "../Geoscape/Polygon.h"
#include "../Geoscape/Polyline.h"
#include "../Engine/SoundSet.h"
/**
* Initializes a blank resource set pointing to a folder.
* @param folder Subfolder to load resources from.
*/
ResourcePack::ResourcePack(string folder) : _folder(folder), _currentLang(0), _langName(""), _palettes(), _fonts(), _languages(), _surfaces(), _sets(), _polygons(), _musics()
{
}
/**
* Deletes all the loaded resources.
*/
ResourcePack::~ResourcePack()
{
for (map<string, Language*>::iterator i = _languages.begin(); i != _languages.end(); i++)
{
delete i->second;
}
for (map<string, Font*>::iterator i = _fonts.begin(); i != _fonts.end(); i++)
{
delete i->second;
}
for (map<string, Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); i++)
{
delete i->second;
}
for (map<string, SurfaceSet*>::iterator i = _sets.begin(); i != _sets.end(); i++)
{
delete i->second;
}
for (vector<Polygon*>::iterator i = _polygons.begin(); i != _polygons.end(); i++)
{
delete *i;
}
for (vector<Polyline*>::iterator i = _polylines.begin(); i != _polylines.end(); i++)
{
delete *i;
}
for (map<string, Palette*>::iterator i = _palettes.begin(); i != _palettes.end(); i++)
{
delete i->second;
}
for (map<string, Music*>::iterator i = _musics.begin(); i != _musics.end(); i++)
{
delete i->second;
}
for (map<string, SoundSet*>::iterator i = _sounds.begin(); i != _sounds.end(); i++)
{
delete i->second;
}
}
/**
* Takes a filename and tries to figure out the existing
* case-insensitive filename for it, for file operations.
* @param filename Original filename.
* @return Correctly-cased filename or "" if it doesn't exist.
* @note There's no actual method for figuring out the correct
* filename on case-sensitive systems, this is just a workaround.
*/
string ResourcePack::insensitive(string filename)
{
// Ignore DATA folder
size_t i = filename.find("/DATA/");
if (i != string::npos)
i += 6;
else
i = 0;
// Try lowercase and uppercase names
struct stat info;
if (stat(filename.c_str(), &info) == 0)
{
return filename;
}
else
{
for (string::iterator c = filename.begin() + i; c != filename.end(); c++)
*c = toupper(*c);
if (stat(filename.c_str(), &info) == 0)
{
return filename;
}
else
{
for (string::iterator c = filename.begin() + i; c != filename.end(); c++)
*c = tolower(*c);
if (stat(filename.c_str(), &info) == 0)
{
return filename;
}
else
{
return "";
}
}
}
}
/**
* Returns the currently active language.
* Ensures localizable strings always use the
* active language without needing to know it.
* @return Pointer to the language.
*/
Language *ResourcePack::getLanguage()
{
return _currentLang;
}
/**
* Returns the name of the currently active language.
* Necessary for logic specific to certain languages.
* @return Name of the language.
*/
string ResourcePack::getLanguageName()
{
return _langName;
}
/**
* Changes the currently active language to another
* one in the resource pack.
* @param lang Name of the language.
*/
void ResourcePack::setLanguage(string lang)
{
_currentLang = _languages[lang];
_langName = lang;
}
/**
* Returns a specific font from the resource set.
* @param name Name of the font.
* @return Pointer to the font.
*/
Font *ResourcePack::getFont(string name)
{
return _fonts[name];
}
/**
* Returns a specific surface from the resource set.
* @param name Name of the surface.
* @return Pointer to the surface.
*/
Surface *ResourcePack::getSurface(string name)
{
return _surfaces[name];
}
/**
* Returns a specific surface set from the resource set.
* @param name Name of the surface set.
* @return Pointer to the surface set.
*/
SurfaceSet *ResourcePack::getSurfaceSet(string name)
{
return _sets[name];
}
/**
* Returns the list of polygons in the resource set.
* @return Pointer to the list of polygons.
*/
vector<Polygon*> *ResourcePack::getPolygons()
{
return &_polygons;
}
/**
* Returns the list of polylines in the resource set.
* @return Pointer to the list of polylines.
*/
vector<Polyline*> *ResourcePack::getPolylines()
{
return &_polylines;
}
/**
* Returns a specific music from the resource set.
* @param name Name of the music.
* @return Pointer to the music.
*/
Music *ResourcePack::getMusic(string name)
{
return _musics[name];
}
/**
* Returns a specific sound set from the resource set.
* @param name Name of the sound set.
* @return Pointer to the sound set.
*/
SoundSet *ResourcePack::getSoundSet(string name)
{
return _sounds[name];
}
/**
* Returns a specific palette from the resource set.
* @param name Name of the palette.
* @return Pointer to the palette.
*/
Palette *ResourcePack::getPalette(string name)
{
return _palettes[name];
}
/**
* Changes the palette of all the graphics in the resource set.
* @param colors Pointer to the set of colors.
* @param firstcolor Offset of the first color to replace.
* @param ncolors Amount of colors to replace.
*/
void ResourcePack::setPalette(SDL_Color *colors, int firstcolor, int ncolors)
{
for (map<string, Font*>::iterator i = _fonts.begin(); i != _fonts.end(); i++)
{
i->second->getSurface()->setPalette(colors, firstcolor, ncolors);
}
for (map<string, Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); i++)
{
i->second->setPalette(colors, firstcolor, ncolors);
}
for (map<string, SurfaceSet*>::iterator i = _sets.begin(); i != _sets.end(); i++)
{
i->second->setPalette(colors, firstcolor, ncolors);
}
}