1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program 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 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: Patrick Boenzli |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
17 | |
---|
18 | #include "resource_manager.h" |
---|
19 | |
---|
20 | // different resource Types |
---|
21 | #ifndef NO_MODEL |
---|
22 | #include "objModel.h" |
---|
23 | #include "primitive_model.h" |
---|
24 | #include "md2Model.h" |
---|
25 | #endif /* NO_MODEL */ |
---|
26 | #ifndef NO_TEXTURES |
---|
27 | #include "texture.h" |
---|
28 | #endif /* NO_TEXTURES */ |
---|
29 | #ifndef NO_TEXT |
---|
30 | #include "text_engine.h" |
---|
31 | #endif /* NO_TEXT */ |
---|
32 | #ifndef NO_AUDIO |
---|
33 | #include "sound_engine.h" |
---|
34 | #endif /* NO_AUDIO */ |
---|
35 | |
---|
36 | #include "list.h" |
---|
37 | #include "sdlincl.h" |
---|
38 | |
---|
39 | // File Handling Includes |
---|
40 | #include <sys/types.h> |
---|
41 | #include <sys/stat.h> |
---|
42 | #include <unistd.h> |
---|
43 | |
---|
44 | using namespace std; |
---|
45 | |
---|
46 | /** |
---|
47 | \brief standard constructor |
---|
48 | */ |
---|
49 | ResourceManager::ResourceManager () |
---|
50 | { |
---|
51 | this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager"); |
---|
52 | this->setName("ResourceManager"); |
---|
53 | |
---|
54 | this->dataDir = NULL; |
---|
55 | this->setDataDir("./"); |
---|
56 | this->imageDirs = new tList<char>(); |
---|
57 | this->resourceList = new tList<Resource>(); |
---|
58 | } |
---|
59 | |
---|
60 | //! Singleton Reference to the ResourceManager |
---|
61 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
62 | |
---|
63 | /** |
---|
64 | \brief standard destructor |
---|
65 | */ |
---|
66 | ResourceManager::~ResourceManager (void) |
---|
67 | { |
---|
68 | // deleting the Resources-List |
---|
69 | this->unloadAllByPriority(RP_GAME); |
---|
70 | delete this->resourceList; |
---|
71 | // deleting the Directorie Lists |
---|
72 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
73 | char* tmpDir = tmpIt->nextElement(); |
---|
74 | while(tmpDir) |
---|
75 | { |
---|
76 | delete []tmpDir; |
---|
77 | tmpDir = tmpIt->nextElement(); |
---|
78 | } |
---|
79 | delete tmpIt; |
---|
80 | |
---|
81 | delete this->imageDirs; |
---|
82 | |
---|
83 | ResourceManager::singletonRef = NULL; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | \brief sets the data main directory |
---|
88 | \param dataDir the DataDirectory. |
---|
89 | */ |
---|
90 | bool ResourceManager::setDataDir(const char* dataDir) |
---|
91 | { |
---|
92 | char* realDir = ResourceManager::homeDirCheck(dataDir); |
---|
93 | if (isDir(realDir)) |
---|
94 | { |
---|
95 | delete this->dataDir; |
---|
96 | this->dataDir = new char[strlen(realDir)+1]; |
---|
97 | strcpy(this->dataDir, realDir); |
---|
98 | delete realDir; |
---|
99 | return true; |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir, this->dataDir); |
---|
104 | delete realDir; |
---|
105 | return false; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | \brief checks for the DataDirectory, by looking if |
---|
111 | \param fileInside is inisde?? |
---|
112 | */ |
---|
113 | bool ResourceManager::checkDataDir(const char* fileInside) |
---|
114 | { |
---|
115 | bool retVal; |
---|
116 | if (!isDir(this->dataDir)) |
---|
117 | { |
---|
118 | PRINTF(1)("%s is not a directory\n", this->dataDir); |
---|
119 | return false; |
---|
120 | } |
---|
121 | |
---|
122 | char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1]; |
---|
123 | sprintf(testFile, "%s%s", this->dataDir, fileInside); |
---|
124 | retVal = isFile(testFile); |
---|
125 | delete testFile; |
---|
126 | return retVal; |
---|
127 | } |
---|
128 | |
---|
129 | #ifndef NO_TEXTURE |
---|
130 | /** |
---|
131 | \brief adds a new Path for Images |
---|
132 | \param imageDir The path to insert |
---|
133 | \returns true, if the Path was well and injected (or already existent within the list) |
---|
134 | false otherwise |
---|
135 | */ |
---|
136 | bool ResourceManager::addImageDir(const char* imageDir) |
---|
137 | { |
---|
138 | // check if the param is a Directory |
---|
139 | if (isDir(imageDir)) |
---|
140 | { |
---|
141 | // check if the Directory has been added before |
---|
142 | tIterator<char>* tmpImageDirs = imageDirs->getIterator(); |
---|
143 | char* tmpDir = tmpImageDirs->nextElement(); |
---|
144 | while(tmpDir) |
---|
145 | { |
---|
146 | if (!strcmp(tmpDir, imageDir)) |
---|
147 | { |
---|
148 | PRINTF(4)("Path %s already loaded\n", imageDir); |
---|
149 | delete tmpImageDirs; |
---|
150 | return true; |
---|
151 | } |
---|
152 | tmpDir = tmpImageDirs->nextElement(); |
---|
153 | } |
---|
154 | delete tmpImageDirs; |
---|
155 | |
---|
156 | // adding the directory to the List |
---|
157 | tmpDir = new char[strlen(imageDir)+1]; |
---|
158 | strcpy(tmpDir, imageDir); |
---|
159 | this->imageDirs->add(tmpDir); |
---|
160 | return true; |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", dataDir); |
---|
165 | return false; |
---|
166 | } |
---|
167 | } |
---|
168 | #endif /* NO_TEXTURES */ |
---|
169 | |
---|
170 | /** |
---|
171 | \brief loads resources |
---|
172 | \param fileName: The fileName of the resource to load |
---|
173 | \param prio: The ResourcePriority of this resource (will only be increased) |
---|
174 | \param param1: an additional option to parse (see the constuctors for more help) |
---|
175 | \param param2: an additional option to parse (see the constuctors for more help) |
---|
176 | \param param3: an additional option to parse (see the constuctors for more help) |
---|
177 | \returns a pointer to a desired Resource. |
---|
178 | */ |
---|
179 | void* ResourceManager::load(const char* fileName, ResourcePriority prio, void* param1, void* param2, void* param3) |
---|
180 | { |
---|
181 | ResourceType tmpType; |
---|
182 | #ifndef NO_MODEL |
---|
183 | if (!strncmp(fileName+(strlen(fileName)-4), ".obj", 4)) |
---|
184 | tmpType = OBJ; |
---|
185 | else if (!strncmp(fileName+(strlen(fileName)-4), ".md2", 4)) |
---|
186 | tmpType = MD2; |
---|
187 | else if (!strcmp(fileName, "cube") || |
---|
188 | !strcmp(fileName, "sphere") || |
---|
189 | !strcmp(fileName, "plane") || |
---|
190 | !strcmp(fileName, "cylinder") || |
---|
191 | !strcmp(fileName, "cone")) |
---|
192 | tmpType = PRIM; |
---|
193 | #endif /* NO_MODEL */ |
---|
194 | #ifndef NO_AUDIO |
---|
195 | else if (!strncmp(fileName+(strlen(fileName)-4), ".wav", 4)) |
---|
196 | tmpType = WAV; |
---|
197 | else if (!strncmp(fileName+(strlen(fileName)-4), ".mp3", 4)) |
---|
198 | tmpType = MP3; |
---|
199 | else if (!strncmp(fileName+(strlen(fileName)-4), ".ogg", 4)) |
---|
200 | tmpType = OGG; |
---|
201 | #endif /* NO_AUDIO */ |
---|
202 | #ifndef NO_TEXT |
---|
203 | else if (!strncmp(fileName+(strlen(fileName)-4), ".ttf", 4)) |
---|
204 | tmpType = TTF; |
---|
205 | #endif /* NO_TEXT */ |
---|
206 | #ifndef NO_TEXTURES |
---|
207 | else |
---|
208 | tmpType = IMAGE; |
---|
209 | #endif /* NO_TEXTURES */ |
---|
210 | |
---|
211 | return this->load(fileName, tmpType, prio, param1, param2, param3); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | \brief loads resources |
---|
216 | \param fileName: The fileName of the resource to load |
---|
217 | \param type: The Type of Resource to load (\see ResourceType) |
---|
218 | \param prio: The ResourcePriority of this resource (will only be increased) |
---|
219 | \param param1: an additional option to parse (see the constuctors for more help) |
---|
220 | \param param2: an additional option to parse (see the constuctors for more help) |
---|
221 | \param param3: an additional option to parse (see the constuctors for more help) |
---|
222 | \returns a pointer to a desired Resource. |
---|
223 | */ |
---|
224 | void* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio, |
---|
225 | void* param1, void* param2, void* param3) |
---|
226 | { |
---|
227 | // searching if the resource was loaded before. |
---|
228 | Resource* tmpResource = this->locateResourceByInfo(fileName, type, param1, param2,param3); |
---|
229 | if (tmpResource) // if the resource was loaded before. |
---|
230 | { |
---|
231 | PRINTF(4)("not loading cached resource %s\n", tmpResource->name); |
---|
232 | tmpResource->count++; |
---|
233 | if(tmpResource->prio < prio) |
---|
234 | tmpResource->prio = prio; |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | char* tmpDir; |
---|
239 | // Setting up the new Resource |
---|
240 | tmpResource = new Resource; |
---|
241 | tmpResource->count = 1; |
---|
242 | tmpResource->type = type; |
---|
243 | tmpResource->prio = prio; |
---|
244 | tmpResource->pointer = NULL; |
---|
245 | tmpResource->name = new char[strlen(fileName)+1]; |
---|
246 | strcpy(tmpResource->name, fileName); |
---|
247 | |
---|
248 | // creating the full name. (directoryName + FileName) |
---|
249 | char* fullName = ResourceManager::getFullName(fileName); |
---|
250 | // Checking for the type of resource \see ResourceType |
---|
251 | switch(type) |
---|
252 | { |
---|
253 | #ifndef NO_MODEL |
---|
254 | case OBJ: |
---|
255 | if (param1) |
---|
256 | tmpResource->modelSize = *(float*)param1; |
---|
257 | else |
---|
258 | tmpResource->modelSize = 1.0; |
---|
259 | |
---|
260 | if(ResourceManager::isFile(fullName)) |
---|
261 | tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize); |
---|
262 | else |
---|
263 | { |
---|
264 | PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", fullName); |
---|
265 | tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize); |
---|
266 | } |
---|
267 | break; |
---|
268 | case PRIM: |
---|
269 | if (param1) |
---|
270 | tmpResource->modelSize = *(float*)param1; |
---|
271 | else |
---|
272 | tmpResource->modelSize = 1.0; |
---|
273 | |
---|
274 | if (!strcmp(tmpResource->name, "cube")) |
---|
275 | tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->modelSize); |
---|
276 | else if (!strcmp(tmpResource->name, "sphere")) |
---|
277 | tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->modelSize); |
---|
278 | else if (!strcmp(tmpResource->name, "plane")) |
---|
279 | tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->modelSize); |
---|
280 | else if (!strcmp(tmpResource->name, "cylinder")) |
---|
281 | tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->modelSize); |
---|
282 | else if (!strcmp(tmpResource->name, "cone")) |
---|
283 | tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->modelSize); |
---|
284 | break; |
---|
285 | case MD2: |
---|
286 | if(ResourceManager::isFile(fullName)) |
---|
287 | { |
---|
288 | if (param1) |
---|
289 | { |
---|
290 | tmpResource->skinFileName = new char[strlen((const char*)param1)+1]; |
---|
291 | strcpy(tmpResource->skinFileName, (const char*) param1); |
---|
292 | } |
---|
293 | else |
---|
294 | tmpResource->skinFileName = NULL; |
---|
295 | tmpResource->pointer = new MD2Data(fullName, tmpResource->skinFileName); |
---|
296 | } |
---|
297 | break; |
---|
298 | #endif /* NO_MODEL */ |
---|
299 | #ifndef NO_TEXT |
---|
300 | case TTF: |
---|
301 | if (param1) |
---|
302 | tmpResource->ttfSize = *(int*)param1; |
---|
303 | else |
---|
304 | tmpResource->ttfSize = FONT_DEFAULT_SIZE; |
---|
305 | if (param2) |
---|
306 | { |
---|
307 | Vector* tmpVec = (Vector*)param2; |
---|
308 | tmpResource->ttfColorR = (int)tmpVec->x; |
---|
309 | tmpResource->ttfColorG = (int)tmpVec->y; |
---|
310 | tmpResource->ttfColorB = (int)tmpVec->z; |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | tmpResource->ttfColorR = FONT_DEFAULT_COLOR_R; |
---|
315 | tmpResource->ttfColorG = FONT_DEFAULT_COLOR_G; |
---|
316 | tmpResource->ttfColorB = FONT_DEFAULT_COLOR_B; |
---|
317 | } |
---|
318 | |
---|
319 | if(isFile(fullName)) |
---|
320 | tmpResource->pointer = new Font(fullName, |
---|
321 | tmpResource->ttfSize, |
---|
322 | tmpResource->ttfColorR, |
---|
323 | tmpResource->ttfColorG, |
---|
324 | tmpResource->ttfColorB); |
---|
325 | else |
---|
326 | PRINTF(2)("Sorry, %s does not exist. Not loading Font\n", fullName); |
---|
327 | break; |
---|
328 | #endif /* NO_TEXT */ |
---|
329 | #ifndef NO_AUDIO |
---|
330 | case WAV: |
---|
331 | if(isFile(fullName)) |
---|
332 | tmpResource->pointer = new SoundBuffer(fullName); |
---|
333 | break; |
---|
334 | #endif /* NO_AUDIO */ |
---|
335 | #ifndef NO_TEXTURES |
---|
336 | case IMAGE: |
---|
337 | if(isFile(fullName)) |
---|
338 | { |
---|
339 | PRINTF(4)("Image %s resides to %s\n", fileName, fullName); |
---|
340 | tmpResource->pointer = new Texture(fullName); |
---|
341 | } |
---|
342 | else |
---|
343 | { |
---|
344 | tIterator<char>* iterator = imageDirs->getIterator(); |
---|
345 | tmpDir = iterator->nextElement(); |
---|
346 | //tmpDir = imageDirs->enumerate(); |
---|
347 | while(tmpDir) |
---|
348 | { |
---|
349 | char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; |
---|
350 | sprintf(imgName, "%s%s", tmpDir, fileName); |
---|
351 | if(isFile(imgName)) |
---|
352 | { |
---|
353 | PRINTF(4)("Image %s resides to %s\n", fileName, imgName); |
---|
354 | tmpResource->pointer = new Texture(imgName); |
---|
355 | delete []imgName; |
---|
356 | break; |
---|
357 | } |
---|
358 | delete []imgName; |
---|
359 | tmpDir = iterator->nextElement(); |
---|
360 | } |
---|
361 | delete iterator; |
---|
362 | } |
---|
363 | if(!tmpResource) |
---|
364 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
365 | break; |
---|
366 | #endif /* NO_TEXTURES */ |
---|
367 | default: |
---|
368 | tmpResource->pointer = NULL; |
---|
369 | PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet.!!\n", tmpResource->name); |
---|
370 | break; |
---|
371 | } |
---|
372 | if (tmpResource->pointer) |
---|
373 | this->resourceList->add(tmpResource); |
---|
374 | delete []fullName; |
---|
375 | } |
---|
376 | if (tmpResource->pointer) |
---|
377 | return tmpResource->pointer; |
---|
378 | else |
---|
379 | { |
---|
380 | PRINTF(2)("Resource %s could not be loaded\n", fileName); |
---|
381 | delete tmpResource; |
---|
382 | return NULL; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | /** |
---|
387 | \brief unloads a Resource |
---|
388 | \param pointer: The pointer to free |
---|
389 | \param prio: the PriorityLevel to unload this resource |
---|
390 | \returns true if successful (pointer found, and deleted), false otherwise |
---|
391 | */ |
---|
392 | bool ResourceManager::unload(void* pointer, ResourcePriority prio) |
---|
393 | { |
---|
394 | // if pointer is existent. and only one resource of this type exists. |
---|
395 | Resource* tmpResource = this->locateResourceByPointer(pointer); |
---|
396 | if (!tmpResource) |
---|
397 | { |
---|
398 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
399 | return false; |
---|
400 | } |
---|
401 | else |
---|
402 | unload(tmpResource, prio); |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | \brief unloads a Resource |
---|
407 | \param resource: The resource to unloade |
---|
408 | \param prio the PriorityLevel to unload this resource |
---|
409 | */ |
---|
410 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
411 | { |
---|
412 | if (resource->count > 0) |
---|
413 | resource->count--; |
---|
414 | if (resource->prio <= prio) |
---|
415 | { |
---|
416 | if (resource->count <= 0) |
---|
417 | { |
---|
418 | // deleting the Resource |
---|
419 | switch(resource->type) |
---|
420 | { |
---|
421 | #ifndef NO_MODEL |
---|
422 | case OBJ: |
---|
423 | case PRIM: |
---|
424 | delete (Model*)resource->pointer; |
---|
425 | break; |
---|
426 | case MD2: |
---|
427 | delete (MD2Data*)resource->pointer; |
---|
428 | break; |
---|
429 | #endif /* NO_MODEL */ |
---|
430 | #ifndef NO_AUDIO |
---|
431 | case WAV: |
---|
432 | delete (SoundBuffer*)resource->pointer; |
---|
433 | break; |
---|
434 | #endif /* NO_AUDIO */ |
---|
435 | #ifndef NO_TEXT |
---|
436 | case TTF: |
---|
437 | delete (Font*)resource->pointer; |
---|
438 | break; |
---|
439 | #endif /* NO_TEXT */ |
---|
440 | #ifndef NO_TEXTURES |
---|
441 | case IMAGE: |
---|
442 | delete (Texture*)resource->pointer; |
---|
443 | break; |
---|
444 | #endif /* NO_TEXTURES */ |
---|
445 | default: |
---|
446 | PRINTF(1)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
447 | return false; |
---|
448 | break; |
---|
449 | } |
---|
450 | // deleting the List Entry: |
---|
451 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
452 | delete []resource->name; |
---|
453 | this->resourceList->remove(resource); |
---|
454 | } |
---|
455 | else |
---|
456 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
457 | } |
---|
458 | else |
---|
459 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
460 | return true; |
---|
461 | } |
---|
462 | |
---|
463 | |
---|
464 | /** |
---|
465 | \brief unloads all alocated Memory of Resources with a pririty lower than prio |
---|
466 | \param prio The priority to delete |
---|
467 | */ |
---|
468 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
469 | { |
---|
470 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
471 | Resource* enumRes = iterator->nextElement(); |
---|
472 | while (enumRes) |
---|
473 | { |
---|
474 | if (enumRes->prio <= prio) |
---|
475 | if (enumRes->count == 0) |
---|
476 | unload(enumRes, prio); |
---|
477 | else |
---|
478 | PRINTF(2)("unable to unload %s because there are still %d references to it\n", |
---|
479 | enumRes->name, enumRes->count); |
---|
480 | //enumRes = resourceList->nextElement(); |
---|
481 | enumRes = iterator->nextElement(); |
---|
482 | } |
---|
483 | delete iterator; |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | \brief Searches for a Resource by some information |
---|
488 | \param fileName: The name to look for |
---|
489 | \param type the Type of resource to locate. |
---|
490 | \param param1: an additional option to parse (see the constuctors for more help) |
---|
491 | \param param2: an additional option to parse (see the constuctors for more help) |
---|
492 | \param param3: an additional option to parse (see the constuctors for more help) |
---|
493 | \returns a Pointer to the Resource if found, NULL otherwise. |
---|
494 | */ |
---|
495 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, |
---|
496 | void* param1, void* param2, void* param3) |
---|
497 | { |
---|
498 | // Resource* enumRes = resourceList->enumerate(); |
---|
499 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
500 | Resource* enumRes = iterator->nextElement(); |
---|
501 | while (enumRes) |
---|
502 | { |
---|
503 | if (enumRes->type == type && !strcmp(fileName, enumRes->name)) |
---|
504 | { |
---|
505 | bool match = false; |
---|
506 | bool subMatch = false; |
---|
507 | |
---|
508 | switch (type) |
---|
509 | { |
---|
510 | #ifndef NO_MODEL |
---|
511 | case PRIM: |
---|
512 | case OBJ: |
---|
513 | if (!param1) |
---|
514 | { |
---|
515 | if (enumRes->modelSize == 1.0) |
---|
516 | match = true; |
---|
517 | } |
---|
518 | else if (enumRes->modelSize == *(float*)param1) |
---|
519 | match = true; |
---|
520 | break; |
---|
521 | case MD2: |
---|
522 | if (!param1) |
---|
523 | { |
---|
524 | if (enumRes->skinFileName == NULL) |
---|
525 | match = true; |
---|
526 | } |
---|
527 | else if (!strcmp(enumRes->skinFileName, (const char*) param1)) |
---|
528 | match = true; |
---|
529 | break; |
---|
530 | #endif /* NO_MODEL */ |
---|
531 | #ifndef NO_TEXT |
---|
532 | case TTF: |
---|
533 | if (!param1) |
---|
534 | { |
---|
535 | if (enumRes->ttfSize == FONT_DEFAULT_SIZE) |
---|
536 | subMatch = true; |
---|
537 | } |
---|
538 | else if (enumRes->modelSize =- *(int*)param1) |
---|
539 | subMatch = true; |
---|
540 | if(subMatch) |
---|
541 | { |
---|
542 | Vector* tmpVec = (Vector*)param2; |
---|
543 | if (!param2) |
---|
544 | { |
---|
545 | if(enumRes->ttfColorR == FONT_DEFAULT_COLOR_R && |
---|
546 | enumRes->ttfColorG == FONT_DEFAULT_COLOR_G && |
---|
547 | enumRes->ttfColorB == FONT_DEFAULT_COLOR_B ) |
---|
548 | match = true; |
---|
549 | } |
---|
550 | else if (enumRes->ttfColorR == (int)tmpVec->x && |
---|
551 | enumRes->ttfColorG == (int)tmpVec->y && |
---|
552 | enumRes->ttfColorB == (int)tmpVec->z ) |
---|
553 | match = true; |
---|
554 | } |
---|
555 | break; |
---|
556 | #endif /* NO_TEXT */ |
---|
557 | default: |
---|
558 | match = true; |
---|
559 | break; |
---|
560 | } |
---|
561 | if (match) |
---|
562 | { |
---|
563 | delete iterator; |
---|
564 | return enumRes; |
---|
565 | } |
---|
566 | } |
---|
567 | enumRes = iterator->nextElement(); |
---|
568 | } |
---|
569 | delete iterator; |
---|
570 | return NULL; |
---|
571 | } |
---|
572 | |
---|
573 | /** |
---|
574 | \brief Searches for a Resource by Pointer |
---|
575 | \param pointer the Pointer to search for |
---|
576 | \returns a Pointer to the Resource if found, NULL otherwise. |
---|
577 | */ |
---|
578 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) |
---|
579 | { |
---|
580 | // Resource* enumRes = resourceList->enumerate(); |
---|
581 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
582 | Resource* enumRes = iterator->nextElement(); |
---|
583 | while (enumRes) |
---|
584 | { |
---|
585 | if (pointer == enumRes->pointer) |
---|
586 | { |
---|
587 | delete iterator; |
---|
588 | return enumRes; |
---|
589 | } |
---|
590 | enumRes = iterator->nextElement(); |
---|
591 | } |
---|
592 | delete iterator; |
---|
593 | return NULL; |
---|
594 | } |
---|
595 | |
---|
596 | /** |
---|
597 | \brief Checks if it is a Directory |
---|
598 | \param directoryName the Directory to check for |
---|
599 | \returns true if it is a directory/symlink false otherwise |
---|
600 | */ |
---|
601 | bool ResourceManager::isDir(const char* directoryName) |
---|
602 | { |
---|
603 | if (directoryName == NULL) |
---|
604 | return false; |
---|
605 | |
---|
606 | char* tmpDirName = NULL; |
---|
607 | struct stat status; |
---|
608 | |
---|
609 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
610 | if (directoryName[strlen(directoryName)-1] == '/') |
---|
611 | { |
---|
612 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
613 | strncpy(tmpDirName, directoryName, strlen(directoryName)-1); |
---|
614 | tmpDirName[strlen(directoryName)-1] = '\0'; |
---|
615 | } |
---|
616 | else |
---|
617 | { |
---|
618 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
619 | strcpy(tmpDirName, directoryName); |
---|
620 | } |
---|
621 | |
---|
622 | if(!stat(tmpDirName, &status)) |
---|
623 | { |
---|
624 | if (status.st_mode & (S_IFDIR |
---|
625 | #ifndef __WIN32__ |
---|
626 | | S_IFLNK |
---|
627 | #endif |
---|
628 | )) |
---|
629 | { |
---|
630 | delete tmpDirName; |
---|
631 | return true; |
---|
632 | } |
---|
633 | else |
---|
634 | { |
---|
635 | delete tmpDirName; |
---|
636 | return false; |
---|
637 | } |
---|
638 | } |
---|
639 | else |
---|
640 | return false; |
---|
641 | } |
---|
642 | |
---|
643 | /** |
---|
644 | \brief Checks if the file is either a Regular file or a Symlink |
---|
645 | \param fileName the File to check for |
---|
646 | \returns true if it is a regular file/symlink, false otherwise |
---|
647 | */ |
---|
648 | bool ResourceManager::isFile(const char* fileName) |
---|
649 | { |
---|
650 | if (fileName == NULL) |
---|
651 | return false; |
---|
652 | char* tmpFileName = ResourceManager::homeDirCheck(fileName); |
---|
653 | // actually checks the File |
---|
654 | struct stat status; |
---|
655 | if (!stat(tmpFileName, &status)) |
---|
656 | { |
---|
657 | if (status.st_mode & (S_IFREG |
---|
658 | #ifndef __WIN32__ |
---|
659 | | S_IFLNK |
---|
660 | #endif |
---|
661 | )) |
---|
662 | { |
---|
663 | delete tmpFileName; |
---|
664 | return true; |
---|
665 | } |
---|
666 | else |
---|
667 | { |
---|
668 | delete tmpFileName; |
---|
669 | return false; |
---|
670 | } |
---|
671 | } |
---|
672 | else |
---|
673 | { |
---|
674 | delete tmpFileName; |
---|
675 | return false; |
---|
676 | } |
---|
677 | } |
---|
678 | |
---|
679 | /** |
---|
680 | \brief touches a File on the disk (thereby creating it) |
---|
681 | \param fileName The file to touch |
---|
682 | */ |
---|
683 | bool ResourceManager::touchFile(const char* fileName) |
---|
684 | { |
---|
685 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
686 | if (tmpName == NULL) |
---|
687 | return false; |
---|
688 | FILE* stream; |
---|
689 | if( (stream = fopen (tmpName, "w")) == NULL) |
---|
690 | { |
---|
691 | PRINTF(1)("could not open %s fro writing\n", fileName); |
---|
692 | return false; |
---|
693 | } |
---|
694 | fclose(stream); |
---|
695 | |
---|
696 | delete tmpName; |
---|
697 | } |
---|
698 | |
---|
699 | /** |
---|
700 | \brief deletes a File from disk |
---|
701 | \param fileName the File to delete |
---|
702 | */ |
---|
703 | bool ResourceManager::deleteFile(const char* fileName) |
---|
704 | { |
---|
705 | if (fileName == NULL) |
---|
706 | return false; |
---|
707 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
708 | unlink(tmpName); |
---|
709 | delete tmpName; |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | \param name the Name of the file to check |
---|
714 | \returns The name of the file, including the HomeDir |
---|
715 | IMPORTANT: this has to be deleted from the outside |
---|
716 | */ |
---|
717 | char* ResourceManager::homeDirCheck(const char* name) |
---|
718 | { |
---|
719 | if (name == NULL) |
---|
720 | return NULL; |
---|
721 | char* retName; |
---|
722 | if (!strncmp(name, "~/", 2)) |
---|
723 | { |
---|
724 | char tmpFileName[500]; |
---|
725 | #ifdef __WIN32__ |
---|
726 | strcpy(tmpFileName, getenv("USERPROFILE")); |
---|
727 | #else |
---|
728 | strcpy(tmpFileName, getenv("HOME")); |
---|
729 | #endif |
---|
730 | retName = new char[strlen(tmpFileName)+strlen(name)]; |
---|
731 | sprintf(retName, "%s%s", tmpFileName, name+1); |
---|
732 | } |
---|
733 | else |
---|
734 | { |
---|
735 | retName = new char[strlen(name)+1]; |
---|
736 | strcpy(retName, name); |
---|
737 | } |
---|
738 | return retName; |
---|
739 | } |
---|
740 | |
---|
741 | /** |
---|
742 | \param fileName the Name of the File to check |
---|
743 | \returns The full name of the file, including the DataDir, and NULL if the file does not exist |
---|
744 | IMPORTANT: this has to be deleted from the outside |
---|
745 | */ |
---|
746 | char* ResourceManager::getFullName(const char* fileName) |
---|
747 | { |
---|
748 | if (fileName == NULL) |
---|
749 | return NULL; |
---|
750 | |
---|
751 | char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
752 | + strlen(fileName) + 1]; |
---|
753 | sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
754 | if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) |
---|
755 | return retName; |
---|
756 | else |
---|
757 | { |
---|
758 | delete retName; |
---|
759 | return NULL; |
---|
760 | } |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | /** |
---|
765 | \brief outputs debug information about the ResourceManager |
---|
766 | */ |
---|
767 | void ResourceManager::debug(void) |
---|
768 | { |
---|
769 | PRINT(0)("=RM===================================\n"); |
---|
770 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
771 | PRINT(0)("======================================\n"); |
---|
772 | // if it is not initialized |
---|
773 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
774 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
775 | PRINT(0)(" List of Image-Directories: "); |
---|
776 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
777 | char* tmpDir = tmpIt->nextElement(); |
---|
778 | while(tmpDir) |
---|
779 | { |
---|
780 | PRINT(0)("%s ",tmpDir); |
---|
781 | tmpDir = tmpIt->nextElement(); |
---|
782 | } |
---|
783 | delete tmpIt; |
---|
784 | PRINT(0)("\n"); |
---|
785 | |
---|
786 | PRINT(0)("List of all stored Resources:\n"); |
---|
787 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
788 | Resource* enumRes = iterator->nextElement(); |
---|
789 | while (enumRes) |
---|
790 | { |
---|
791 | PRINT(0)("-----------------------------------------\n"); |
---|
792 | PRINT(0)("Name: %s; References: %d; Type:", enumRes->name, enumRes->count); |
---|
793 | switch (enumRes->type) |
---|
794 | { |
---|
795 | case OBJ: |
---|
796 | PRINT(0)("ObjectModel\n"); |
---|
797 | break; |
---|
798 | case PRIM: |
---|
799 | PRINT(0)("PrimitiveModel\n"); |
---|
800 | break; |
---|
801 | case IMAGE: |
---|
802 | PRINT(0)("ImageFile (Texture)\n"); |
---|
803 | break; |
---|
804 | default: |
---|
805 | PRINT(0)("SoundFile\n"); |
---|
806 | break; |
---|
807 | } |
---|
808 | PRINT(0)("gets deleted at "); |
---|
809 | switch(enumRes->prio) |
---|
810 | { |
---|
811 | default: |
---|
812 | case RP_NO: |
---|
813 | PRINT(0)("first posibility (0)\n"); |
---|
814 | break; |
---|
815 | case RP_LEVEL: |
---|
816 | PRINT(0)("the end of the Level (1)\n"); |
---|
817 | break; |
---|
818 | case RP_CAMPAIGN: |
---|
819 | PRINT(0)("the end of the campaign (2)\n"); |
---|
820 | break; |
---|
821 | case RP_GAME: |
---|
822 | PRINT(0)("when leaving the game (3)\n"); |
---|
823 | break; |
---|
824 | } |
---|
825 | enumRes = iterator->nextElement(); |
---|
826 | } |
---|
827 | delete iterator; |
---|
828 | |
---|
829 | |
---|
830 | |
---|
831 | PRINT(0)("==================================RM==\n"); |
---|
832 | } |
---|