-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceManager.cpp
More file actions
337 lines (270 loc) · 8.49 KB
/
ResourceManager.cpp
File metadata and controls
337 lines (270 loc) · 8.49 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// system includes
#include <iostream>
#include <fstream>
#include <stdlib.h>
// game engine includes
#include "LogManager.h"
#include "ResourceManager.h"
// constructor private
df::ResourceManager::ResourceManager(){
// Private (a singleton).
setType("ResourceManager");
sprite_count = 0;
}
// copying constructor
df::ResourceManager::ResourceManager(ResourceManager const&){
// Don't allow copy.
setType("ResourceManager");
sprite_count = 0;
}
// destructor
df::ResourceManager::~ResourceManager(){
}
// Get the one and only instance of the ResourceManager.
df::ResourceManager &df::ResourceManager::getInstance(){
static ResourceManager resource_manager;
return resource_manager;
}
// Get manager ready for resources.
int df::ResourceManager::startUp(){
return 0;
}
// Shut down manager, freeing up any allocated Sprites.
void df::ResourceManager::shutDown(){
}
// Read single line `tag number', return number
int readLineInt(std::ifstream *p_file, int *p_line_number, const char *tag){
string line;
// get line from file
getline(*p_file, line);
// check if tag is in line
if (line.find(tag) == std::string::npos){
df::LogManager::getInstance().writeLog("LogManger::readLineInt(): Could not find tag \"%s\" on line %d\n", tag, *p_line_number);
return -1;
}
// get everything but the tag
string sub = tag;
sub.append(" ");
int number = atoi(line.substr(sub.length()).c_str());
// increment the line number
(*p_line_number)++;
// return the number
return number;
}
// Read single line `tag string', return string.
std::string readLineStr(std::ifstream *p_file, int *p_line_number, const char *tag){
string line;
// get a line from the file
getline(*p_file, line);
// make sure tag is in the line
if (line.compare(tag) == std::string::npos){
df::LogManager::getInstance().writeLog("ResourceManager::readLineStr(): Error line %d. Could not find tag \"%s\".\n", *p_line_number, tag);
return "-1";
}
// get everything but the tag
string sub = tag;
sub.append(" ");
string result = line.substr(sub.length()).c_str();
// increment line number
(*p_line_number)++;
// return string
return result;
}
// Read frame until `eof', return Frame.
df::Frame readFrame(std::ifstream *p_file, int *p_line_number, int width, int height){
string line, frame_str;
// loop accross the height of the frame
for (int i = 1; i <= height; i++){
// get a line from the file
getline(*p_file, line); // Error check.
// check to see if the line length is the same as the width of the frame
if (line.length() > width){
df::LogManager::getInstance().writeLog("ResourceManager::readFrame(): Error line %d. Line width is %d, expected %d\n", *p_line_number, line.length(), width);
return df::Frame();
}
// add the line to the frame
frame_str += line;
// increment line number
(*p_line_number)++;
}
// get the next line from the file
getline(*p_file, line); // Error check.
// if the line is not end then write to log
if (line.compare(df::END_FRAME_TOKEN) == std::string::npos){
df::LogManager::getInstance().writeLog("ResourceManager::readFrame(): Error line %d. end expected but not found\n", *p_line_number);
return df::Frame();
}
// increment line number
(*p_line_number)++;
// create frame from file input taken and return it
df::Frame frame = df::Frame(width, height, frame_str);
return frame;
}
// change a color string into an integer
df::Color stringToColor(std::string color){
if (color.compare("black") == 0)
return df::BLACK;
if (color.compare("red") == 0)
return df::RED;
if (color.compare("green") == 0)
return df::GREEN;
if (color.compare("yellow") == 0)
return df::YELLOW;
if (color.compare("blue") == 0)
return df::BLUE;
if (color.compare("magenta") == 0)
return df::MAGENTA;
if (color.compare("cyan") == 0)
return df::CYAN;
else
return df::WHITE;
}
// Load Sprite from file.
// Assign indicated label to sprite.
// Return 0 if ok, else -1.
int df::ResourceManager::loadSprite(std::string filename, std::string label){
string line;
std::ifstream myfile(filename);
if (myfile.is_open()){
int lineNumber = 1;
// write to log
df::LogManager::getInstance().writeLog("ResourceManager::loadSprite(): starting loading sprite \"%s\" with label: %s\n",
filename.c_str(), label.c_str());
// get header of file
int frames = readLineInt(&myfile, &lineNumber, df::FRAMES_TOKEN.c_str());
int width = readLineInt(&myfile, &lineNumber, df::WIDTH_TOKEN.c_str());
int height = readLineInt(&myfile, &lineNumber, df::HEIGHT_TOKEN.c_str());
string color = readLineStr(&myfile, &lineNumber, df::COLOR_TOKEN.c_str());
// create a sprite with header
df::Sprite *sprite = new df::Sprite(frames);
sprite->setWidth(width);
sprite->setHeight(height);
sprite->setColor(stringToColor(color));
sprite->setLabel(label);
// get the frames from the file
while (myfile.good()) {
df::Frame frame = readFrame(&myfile, &lineNumber, width, height);
sprite->addFrame(frame);
}
// add sprite into list
p_sprite[sprite_count] = sprite;
sprite_count++;
// Close file when done.
myfile.close();
df::LogManager::getInstance().writeLog("ResourceManager::loadSprite(): done loading sprite \"%s\" with label: %s\n",
filename.c_str(), label.c_str());
}
else{
df::LogManager::getInstance().writeLog("ResourceManager::loadSprite(): Error. could not open file \"%s\"\n", filename.c_str());
return -1;
}
}
// Unload Sprite with indicated label.
// Return 0 if ok, else -1.
int df::ResourceManager::unloadSprite(std::string label){
for (int i = 0; i < sprite_count; i++){
// delete sprite if the labels are the same
if (label.compare(p_sprite[i]->getLabel()) == 0){
delete p_sprite[i];
// scoot over remaining sprites
for (int j = i; j < sprite_count - 1; j++){
p_sprite[j] = p_sprite[j + 1];
}
sprite_count--;
return 0;
}
}
return -1; // sprite not found
}
// Find Sprite with indicated label.
// Return pointer to it if found, else NULL.
df::Sprite *df::ResourceManager::getSprite(std::string label) const{
for (int i = 0; i < sprite_count; i++){
if (p_sprite[i]->getLabel().compare(label) == 0)
return p_sprite[i];
}
// return NULL if not found
return NULL;
}
// load sound from file
// return 0 if ok, else -1
int df::ResourceManager::loadSound(std::string filename, std::string label){
if (sound_count == MAX_SOUNDS){
df::LogManager::getInstance().writeLog("ResourceManager::loadSound(): sound array full\n");
return -1; // error
}
if (sound[sound_count].loadSound(filename) == -1){
df::LogManager::getInstance().writeLog("ResourceManager::loadSound(): unable to load from file\n");
return -1; // error
}
// all set
sound[sound_count].setLabel(label);
sound_count++;
return 0;
}
// remove sound with indicated label.
// return 0 if ok, else -1
int df::ResourceManager::unloadSound(std::string label){
for (int i = 0; i < sound_count; i++){
if (label == sound[i].getLabel()){
// scoot over remaining sounds
for (int j = i; j < sound_count; j++){
sound[j] = sound[j + 1];
}
sound_count--;
return 0;
}
}
return -1; // sound not found
}
// find sound with indicated label.
// return pointer to it if found, else NULL.
df::Sound *df::ResourceManager::getSound(std::string label){
for(int i = 0; i < sound_count; i++){
if (label == sound[i].getLabel()){
return &sound[i];
}
}
return NULL; // sound not found
}
// Associate file with music
// return 0 if ok, else -1
int df::ResourceManager::loadMusic(std::string filename, std::string label){
if (music_count == MAX_MUSICS){
df::LogManager::getInstance().writeLog("ResourceManager::loadMusic(): music array full\n");
return -1; // error
}
if (music[music_count].loadMusic(filename) == -1){
df::LogManager::getInstance().writeLog("ResourceManager::loadMusic(): unable to load from file\n");
return -1; // error
}
// all set
music[music_count].setLabel(label);
music_count++;
return 0;
}
// remove label for Music with indicated label.
// return 0 if ok, else -1
int df::ResourceManager::unloadMusic(std::string label){
for (int i = 0; i < music_count; i++){
if (label == music[i].getLabel()){
// scoot over remaining sounds
for (int j = i; j < music_count; j++){
//music[j] = music[j + 1];
}
music_count--;
return 0;
}
}
return -1; // music not found
}
// find music with indicated label.
// return pointer to it if found, else NULL
df::Music *df::ResourceManager::getMusic(std::string label){
for (int i = 0; i < music_count; i++){
if (label == music[i].getLabel()){
return &music[i];
}
}
return NULL; // music not found
}