[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3655] | 12 | main-programmer: Benjamin Grauer |
---|
[3672] | 13 | co-programmer: Patrick Boenzli |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3655] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
[1853] | 17 | |
---|
[3655] | 18 | #include "resource_manager.h" |
---|
[1853] | 19 | |
---|
[3655] | 20 | // different resource Types |
---|
| 21 | #include "objModel.h" |
---|
[3657] | 22 | #include "primitive_model.h" |
---|
[3655] | 23 | #include "texture.h" |
---|
[3790] | 24 | #include "text_engine.h" |
---|
[1853] | 25 | |
---|
[3658] | 26 | #include "list.h" |
---|
| 27 | |
---|
[3655] | 28 | // File Handling Includes |
---|
| 29 | #include <sys/types.h> |
---|
| 30 | #include <sys/stat.h> |
---|
| 31 | #include <unistd.h> |
---|
| 32 | |
---|
[1856] | 33 | using namespace std; |
---|
[1853] | 34 | |
---|
[3245] | 35 | /** |
---|
| 36 | \brief standard constructor |
---|
| 37 | */ |
---|
[3655] | 38 | ResourceManager::ResourceManager () |
---|
[3365] | 39 | { |
---|
[3655] | 40 | this->setClassName ("ResourceManager"); |
---|
[3672] | 41 | this->dataDir = "./"; |
---|
| 42 | this->imageDirs = new tList<char>(); |
---|
| 43 | this->resourceList = new tList<Resource>(); |
---|
[3365] | 44 | } |
---|
[1853] | 45 | |
---|
[3660] | 46 | /** |
---|
| 47 | \returns the Instance to this ResourceManager |
---|
| 48 | */ |
---|
[3655] | 49 | ResourceManager* ResourceManager::getInstance(void) |
---|
| 50 | { |
---|
| 51 | if (!ResourceManager::singletonRef) |
---|
| 52 | ResourceManager::singletonRef = new ResourceManager(); |
---|
| 53 | return ResourceManager::singletonRef; |
---|
| 54 | } |
---|
[1853] | 55 | |
---|
[3658] | 56 | //! Singleton Reference to the ResourceManager |
---|
[3655] | 57 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
| 58 | |
---|
[3245] | 59 | /** |
---|
[3660] | 60 | \brief standard destructor |
---|
[3655] | 61 | */ |
---|
| 62 | ResourceManager::~ResourceManager (void) |
---|
| 63 | { |
---|
[3660] | 64 | // deleting the Resources-List |
---|
[3672] | 65 | this->unloadAllByPriority(RP_GAME); |
---|
| 66 | delete this->resourceList; |
---|
[3660] | 67 | // deleting the Directorie Lists |
---|
[3672] | 68 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
| 69 | char* tmpDir = tmpIt->nextElement(); |
---|
[3660] | 70 | while(tmpDir) |
---|
| 71 | { |
---|
| 72 | delete []tmpDir; |
---|
[3672] | 73 | tmpDir = tmpIt->nextElement(); |
---|
[3660] | 74 | } |
---|
[3672] | 75 | delete tmpIt; |
---|
[3660] | 76 | |
---|
[3672] | 77 | delete this->imageDirs; |
---|
| 78 | |
---|
[3655] | 79 | ResourceManager::singletonRef = NULL; |
---|
| 80 | } |
---|
[1853] | 81 | |
---|
[3655] | 82 | /** |
---|
| 83 | \brief sets the data main directory |
---|
| 84 | \param dataDir the DataDirectory. |
---|
[3245] | 85 | */ |
---|
[3655] | 86 | bool ResourceManager::setDataDir(char* dataDir) |
---|
[3543] | 87 | { |
---|
[3655] | 88 | if (isDir(dataDir)) |
---|
| 89 | { |
---|
[3672] | 90 | this->dataDir = new char[strlen(dataDir)+1]; |
---|
| 91 | strcpy(this->dataDir, dataDir); |
---|
[3655] | 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | PRINTF(1)("%s is not a Directory, and can not be the Data Directory\n", dataDir); |
---|
| 96 | } |
---|
[3543] | 97 | } |
---|
[1853] | 98 | |
---|
[3660] | 99 | /** |
---|
| 100 | \brief adds a new Path for Images |
---|
| 101 | \param imageDir The path to insert |
---|
| 102 | \returns true, if the Path was well and injected (or already existent within the list) |
---|
| 103 | false otherwise |
---|
| 104 | */ |
---|
[3658] | 105 | bool ResourceManager::addImageDir(char* imageDir) |
---|
| 106 | { |
---|
[3660] | 107 | // check if the param is a Directory |
---|
[3658] | 108 | if (isDir(imageDir)) |
---|
| 109 | { |
---|
[3660] | 110 | // check if the Directory has been added before |
---|
[3672] | 111 | tIterator<char>* tmpImageDirs = imageDirs->getIterator(); |
---|
| 112 | char* tmpDir = tmpImageDirs->nextElement(); |
---|
[3660] | 113 | while(tmpDir) |
---|
| 114 | { |
---|
| 115 | if (!strcmp(tmpDir, imageDir)) |
---|
| 116 | { |
---|
| 117 | PRINTF(4)("Path %s already loaded\n", imageDir); |
---|
[3672] | 118 | delete tmpImageDirs; |
---|
[3660] | 119 | return true; |
---|
| 120 | } |
---|
[3672] | 121 | tmpDir = tmpImageDirs->nextElement(); |
---|
[3660] | 122 | } |
---|
[3672] | 123 | delete tmpImageDirs; |
---|
| 124 | |
---|
[3660] | 125 | // adding the directory to the List |
---|
| 126 | tmpDir = new char[strlen(imageDir)+1]; |
---|
[3658] | 127 | strcpy(tmpDir, imageDir); |
---|
[3672] | 128 | this->imageDirs->add(tmpDir); |
---|
[3660] | 129 | return true; |
---|
[3658] | 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", dataDir); |
---|
[3660] | 134 | return false; |
---|
[3658] | 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[3245] | 138 | /** |
---|
[3655] | 139 | \brief loads resources |
---|
| 140 | \param fileName The fileName of the resource to load |
---|
[3660] | 141 | \param prio The ResourcePriority of this resource (will only be increased) |
---|
[3655] | 142 | \returns a pointer to a desired Resource. |
---|
| 143 | */ |
---|
[3790] | 144 | void* ResourceManager::load(const char* fileName, ResourcePriority prio, void* param1, void* param2, void* param3) |
---|
[3655] | 145 | { |
---|
| 146 | ResourceType tmpType; |
---|
| 147 | if (!strncmp(fileName+(strlen(fileName)-4), ".obj", 4)) |
---|
| 148 | tmpType = OBJ; |
---|
[3658] | 149 | else if (!strncmp(fileName+(strlen(fileName)-4), ".wav", 4)) |
---|
| 150 | tmpType = WAV; |
---|
| 151 | else if (!strncmp(fileName+(strlen(fileName)-4), ".mp3", 4)) |
---|
| 152 | tmpType = MP3; |
---|
| 153 | else if (!strncmp(fileName+(strlen(fileName)-4), ".ogg", 4)) |
---|
| 154 | tmpType = OGG; |
---|
| 155 | else if (!strcmp(fileName, "cube") || |
---|
| 156 | !strcmp(fileName, "sphere") || |
---|
| 157 | !strcmp(fileName, "plane") || |
---|
| 158 | !strcmp(fileName, "cylinder") || |
---|
| 159 | !strcmp(fileName, "cone")) |
---|
| 160 | tmpType = PRIM; |
---|
[3790] | 161 | else if (!strncmp(fileName+(strlen(fileName)-4), ".ttf", 4)) |
---|
| 162 | tmpType = TTF; |
---|
[3658] | 163 | else |
---|
| 164 | tmpType = IMAGE; |
---|
[3245] | 165 | |
---|
[3790] | 166 | return this->load(fileName, tmpType, prio, param1, param2, param3); |
---|
[3655] | 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | \brief loads resources |
---|
| 171 | \param fileName The fileName of the resource to load |
---|
| 172 | \param type The Type of Resource to load (\see ResourceType) |
---|
[3660] | 173 | \param prio The ResourcePriority of this resource (will only be increased) |
---|
[3655] | 174 | \returns a pointer to a desired Resource. |
---|
[3245] | 175 | */ |
---|
[3790] | 176 | void* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio, void* param1, void* param2, void* param3) |
---|
[3655] | 177 | { |
---|
[3658] | 178 | // searching if the resource was loaded before. |
---|
[3790] | 179 | Resource* tmpResource = this->locateResourceByInfo(fileName, type, param1, param2,param3); |
---|
[3677] | 180 | if (tmpResource) // if the resource was not loaded before. |
---|
[3655] | 181 | { |
---|
[3677] | 182 | PRINTF(4)("not loading cached resource %s\n", tmpResource->name); |
---|
| 183 | tmpResource->count++; |
---|
| 184 | if(tmpResource->prio < prio) |
---|
| 185 | tmpResource->prio = prio; |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
[3660] | 189 | char* tmpDir; |
---|
[3658] | 190 | // Setting up the new Resource |
---|
| 191 | tmpResource = new Resource; |
---|
| 192 | tmpResource->count = 1; |
---|
| 193 | tmpResource->type = type; |
---|
[3660] | 194 | tmpResource->prio = prio; |
---|
[3658] | 195 | tmpResource->name = new char[strlen(fileName)+1]; |
---|
| 196 | strcpy(tmpResource->name, fileName); |
---|
| 197 | |
---|
| 198 | // creating the full name. (directoryName + FileName) |
---|
| 199 | char* fullName = new char[strlen(dataDir)+strlen(fileName)+1]; |
---|
[3672] | 200 | sprintf(fullName, "%s%s", this->dataDir, fileName); |
---|
[3658] | 201 | |
---|
| 202 | // Checking for the type of resource \see ResourceType |
---|
| 203 | switch(type) |
---|
[3655] | 204 | { |
---|
[3658] | 205 | case OBJ: |
---|
[3790] | 206 | if (param1) |
---|
| 207 | tmpResource->modelSize = *(float*)param1; |
---|
| 208 | else |
---|
| 209 | tmpResource->modelSize = 1.0; |
---|
| 210 | |
---|
[3658] | 211 | if(isFile(fullName)) |
---|
[3790] | 212 | tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize); |
---|
[3658] | 213 | else |
---|
| 214 | { |
---|
| 215 | PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", fullName); |
---|
[3790] | 216 | tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize); |
---|
[3658] | 217 | } |
---|
| 218 | break; |
---|
| 219 | case PRIM: |
---|
[3790] | 220 | if (param1) |
---|
| 221 | tmpResource->modelSize = *(float*)param1; |
---|
| 222 | else |
---|
| 223 | tmpResource->modelSize = 1.0; |
---|
| 224 | |
---|
[3658] | 225 | if (!strcmp(tmpResource->name, "cube")) |
---|
[3790] | 226 | tmpResource->pointer = new PrimitiveModel(CUBE, tmpResource->modelSize); |
---|
[3658] | 227 | else if (!strcmp(tmpResource->name, "sphere")) |
---|
[3790] | 228 | tmpResource->pointer = new PrimitiveModel(SPHERE, tmpResource->modelSize); |
---|
[3658] | 229 | else if (!strcmp(tmpResource->name, "plane")) |
---|
[3790] | 230 | tmpResource->pointer = new PrimitiveModel(PLANE, tmpResource->modelSize); |
---|
[3658] | 231 | else if (!strcmp(tmpResource->name, "cylinder")) |
---|
[3790] | 232 | tmpResource->pointer = new PrimitiveModel(CYLINDER, tmpResource->modelSize); |
---|
[3658] | 233 | else if (!strcmp(tmpResource->name, "cone")) |
---|
[3790] | 234 | tmpResource->pointer = new PrimitiveModel(CONE, tmpResource->modelSize); |
---|
[3658] | 235 | break; |
---|
[3790] | 236 | case TTF: |
---|
| 237 | if (param1) |
---|
| 238 | tmpResource->ttfSize = *(int*)param1; |
---|
| 239 | else |
---|
| 240 | tmpResource->ttfSize = FONT_DEFAULT_SIZE; |
---|
| 241 | if (param2) |
---|
| 242 | { |
---|
| 243 | Vector* tmpVec = (Vector*)param2; |
---|
| 244 | tmpResource->ttfColorR = (int)tmpVec->x; |
---|
| 245 | tmpResource->ttfColorG = (int)tmpVec->y; |
---|
| 246 | tmpResource->ttfColorB = (int)tmpVec->z; |
---|
| 247 | } |
---|
| 248 | else |
---|
| 249 | { |
---|
| 250 | tmpResource->ttfColorR = FONT_DEFAULT_COLOR_R; |
---|
| 251 | tmpResource->ttfColorG = FONT_DEFAULT_COLOR_G; |
---|
| 252 | tmpResource->ttfColorB = FONT_DEFAULT_COLOR_B; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | if(isFile(fullName)) |
---|
| 256 | tmpResource->pointer = new Font(fullName, |
---|
| 257 | tmpResource->ttfSize, |
---|
| 258 | tmpResource->ttfColorR, |
---|
| 259 | tmpResource->ttfColorG, |
---|
| 260 | tmpResource->ttfColorB); |
---|
| 261 | else |
---|
| 262 | PRINTF(2)("Sorry, %s does not exist. Not loading Font\n", fullName); |
---|
| 263 | break; |
---|
[3658] | 264 | case IMAGE: |
---|
| 265 | if(isFile(fullName)) |
---|
| 266 | { |
---|
[3676] | 267 | PRINTF(4)("Image %s resides to %s\n", fileName, fullName); |
---|
[3658] | 268 | tmpResource->pointer = new Texture(fullName); |
---|
| 269 | } |
---|
| 270 | else |
---|
| 271 | { |
---|
[3667] | 272 | tIterator<char>* iterator = imageDirs->getIterator(); |
---|
| 273 | tmpDir = iterator->nextElement(); |
---|
| 274 | //tmpDir = imageDirs->enumerate(); |
---|
[3658] | 275 | while(tmpDir) |
---|
| 276 | { |
---|
| 277 | char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; |
---|
| 278 | sprintf(imgName, "%s%s", tmpDir, fileName); |
---|
| 279 | if(isFile(imgName)) |
---|
[3676] | 280 | { |
---|
| 281 | PRINTF(4)("Image %s resides to %s\n", fileName, imgName); |
---|
| 282 | tmpResource->pointer = new Texture(imgName); |
---|
| 283 | delete []imgName; |
---|
| 284 | break; |
---|
| 285 | } |
---|
[3658] | 286 | delete []imgName; |
---|
[3667] | 287 | tmpDir = iterator->nextElement(); |
---|
[3658] | 288 | } |
---|
[3667] | 289 | delete iterator; |
---|
[3658] | 290 | } |
---|
| 291 | if(!tmpResource) |
---|
| 292 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
| 293 | break; |
---|
| 294 | default: |
---|
| 295 | tmpResource->pointer = NULL; |
---|
| 296 | PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet.!!\n", tmpResource->name); |
---|
| 297 | break; |
---|
[3655] | 298 | } |
---|
[3672] | 299 | this->resourceList->add(tmpResource); |
---|
[3658] | 300 | delete []fullName; |
---|
[3655] | 301 | } |
---|
[3790] | 302 | if (tmpResource->pointer) |
---|
| 303 | return tmpResource->pointer; |
---|
| 304 | else |
---|
| 305 | { |
---|
| 306 | PRINTF(2)("Resource %s could not be loaded\n", fileName); |
---|
| 307 | delete tmpResource; |
---|
| 308 | return NULL; |
---|
| 309 | } |
---|
[3658] | 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | \brief unloads a Resource |
---|
| 314 | \param pointer The pointer to free |
---|
| 315 | \returns true if successful (pointer found, and deleted), false otherwise |
---|
| 316 | |
---|
| 317 | */ |
---|
[3660] | 318 | bool ResourceManager::unload(void* pointer, ResourcePriority prio) |
---|
[3658] | 319 | { |
---|
| 320 | // if pointer is existent. and only one resource of this type exists. |
---|
[3672] | 321 | Resource* tmpResource = this->locateResourceByPointer(pointer); |
---|
[3658] | 322 | if (!tmpResource) |
---|
[3655] | 323 | { |
---|
[3658] | 324 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
| 325 | return false; |
---|
[3655] | 326 | } |
---|
[3660] | 327 | else |
---|
| 328 | unload(tmpResource, prio); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
| 332 | { |
---|
[3665] | 333 | if (resource->count > 0) |
---|
| 334 | resource->count--; |
---|
[3660] | 335 | if (resource->prio <= prio) |
---|
[3658] | 336 | { |
---|
[3660] | 337 | if (resource->count <= 0) |
---|
[3658] | 338 | { |
---|
[3660] | 339 | // deleting the Resource |
---|
| 340 | switch(resource->type) |
---|
| 341 | { |
---|
| 342 | case OBJ: |
---|
| 343 | case PRIM: |
---|
| 344 | delete (Model*)resource->pointer; |
---|
| 345 | break; |
---|
| 346 | case IMAGE: |
---|
| 347 | delete (Texture*)resource->pointer; |
---|
| 348 | break; |
---|
[3790] | 349 | case TTF: |
---|
| 350 | delete (Font*)resource->pointer; |
---|
| 351 | break; |
---|
[3660] | 352 | default: |
---|
| 353 | PRINTF(1)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
| 354 | return false; |
---|
| 355 | break; |
---|
| 356 | } |
---|
| 357 | // deleting the List Entry: |
---|
| 358 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
| 359 | delete []resource->name; |
---|
[3672] | 360 | this->resourceList->remove(resource); |
---|
[3658] | 361 | } |
---|
[3660] | 362 | else |
---|
| 363 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
[3658] | 364 | } |
---|
| 365 | else |
---|
[3660] | 366 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
[3658] | 367 | return true; |
---|
[3655] | 368 | } |
---|
| 369 | |
---|
[3660] | 370 | |
---|
[3655] | 371 | /** |
---|
[3660] | 372 | \brief unloads all alocated Memory of Resources with a pririty lower than prio |
---|
| 373 | \param prio The priority to delete |
---|
| 374 | */ |
---|
| 375 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
| 376 | { |
---|
[3666] | 377 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
| 378 | Resource* enumRes = iterator->nextElement(); |
---|
[3660] | 379 | while (enumRes) |
---|
| 380 | { |
---|
| 381 | if (enumRes->prio <= prio) |
---|
[3677] | 382 | if (enumRes->count == 0) |
---|
| 383 | unload(enumRes, prio); |
---|
| 384 | else |
---|
| 385 | PRINTF(2)("unable to unload %s because there are still %d references to it\n", |
---|
| 386 | enumRes->name, enumRes->count); |
---|
[3666] | 387 | //enumRes = resourceList->nextElement(); |
---|
| 388 | enumRes = iterator->nextElement(); |
---|
[3660] | 389 | } |
---|
[3666] | 390 | delete iterator; |
---|
[3660] | 391 | } |
---|
| 392 | |
---|
| 393 | /** |
---|
[3790] | 394 | \brief Searches for a Resource by some information |
---|
[3658] | 395 | \param fileName The name to look for |
---|
| 396 | \returns a Pointer to the Resource if found, NULL otherwise. |
---|
| 397 | */ |
---|
[3790] | 398 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3) |
---|
[3658] | 399 | { |
---|
[3667] | 400 | // Resource* enumRes = resourceList->enumerate(); |
---|
| 401 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
| 402 | Resource* enumRes = iterator->nextElement(); |
---|
[3658] | 403 | while (enumRes) |
---|
| 404 | { |
---|
[3790] | 405 | if (enumRes->type == type && !strcmp(fileName, enumRes->name)) |
---|
[3667] | 406 | { |
---|
[3790] | 407 | bool match = false; |
---|
| 408 | bool subMatch = false; |
---|
| 409 | switch (type) |
---|
| 410 | { |
---|
| 411 | case PRIM: |
---|
| 412 | case OBJ: |
---|
| 413 | if (!param1) |
---|
| 414 | { |
---|
| 415 | if (enumRes->modelSize == 1.0) |
---|
| 416 | match = true; |
---|
| 417 | } |
---|
| 418 | else if (enumRes->modelSize == *(float*)param1) |
---|
| 419 | match = true; |
---|
| 420 | break; |
---|
| 421 | case TTF: |
---|
| 422 | if (!param1) |
---|
| 423 | { |
---|
| 424 | if (enumRes->ttfSize == FONT_DEFAULT_SIZE) |
---|
| 425 | subMatch = true; |
---|
| 426 | } |
---|
| 427 | else if (enumRes->modelSize =- *(int*)param1) |
---|
| 428 | subMatch = true; |
---|
| 429 | if(subMatch) |
---|
| 430 | { |
---|
| 431 | Vector* tmpVec = (Vector*)param2; |
---|
| 432 | if (!param2) |
---|
| 433 | { |
---|
| 434 | if(enumRes->ttfColorR == FONT_DEFAULT_COLOR_R && |
---|
| 435 | enumRes->ttfColorG == FONT_DEFAULT_COLOR_G && |
---|
| 436 | enumRes->ttfColorB == FONT_DEFAULT_COLOR_B ) |
---|
| 437 | match = true; |
---|
| 438 | } |
---|
| 439 | else if (enumRes->ttfColorR == (int)tmpVec->x && |
---|
| 440 | enumRes->ttfColorG == (int)tmpVec->y && |
---|
| 441 | enumRes->ttfColorB == (int)tmpVec->z ) |
---|
| 442 | match = true; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | break; |
---|
| 446 | default: |
---|
| 447 | match = true; |
---|
| 448 | break; |
---|
| 449 | } |
---|
| 450 | if (match) |
---|
| 451 | { |
---|
| 452 | delete iterator; |
---|
| 453 | return enumRes; |
---|
| 454 | } |
---|
[3667] | 455 | } |
---|
| 456 | enumRes = iterator->nextElement(); |
---|
[3658] | 457 | } |
---|
[3667] | 458 | delete iterator; |
---|
[3658] | 459 | return NULL; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | /** |
---|
| 463 | \brief Searches for a Resource by Pointer |
---|
| 464 | \param pointer the Pointer to search for |
---|
| 465 | \returns a Pointer to the Resource if found, NULL otherwise. |
---|
| 466 | */ |
---|
| 467 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) |
---|
| 468 | { |
---|
[3667] | 469 | // Resource* enumRes = resourceList->enumerate(); |
---|
| 470 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
| 471 | Resource* enumRes = iterator->nextElement(); |
---|
[3658] | 472 | while (enumRes) |
---|
| 473 | { |
---|
[3665] | 474 | if (pointer == enumRes->pointer) |
---|
[3667] | 475 | { |
---|
| 476 | delete iterator; |
---|
| 477 | return enumRes; |
---|
| 478 | } |
---|
| 479 | enumRes = iterator->nextElement(); |
---|
[3658] | 480 | } |
---|
[3667] | 481 | delete iterator; |
---|
[3658] | 482 | return NULL; |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | /** |
---|
[3655] | 486 | \brief Checks if it is a Directory |
---|
| 487 | \param directoryName the Directory to check for |
---|
| 488 | \returns true if it is a directory/symlink false otherwise |
---|
| 489 | */ |
---|
| 490 | bool ResourceManager::isDir(const char* directoryName) |
---|
| 491 | { |
---|
| 492 | struct stat status; |
---|
| 493 | stat(directoryName, &status); |
---|
[3790] | 494 | if (status.st_mode & (S_IFDIR |
---|
| 495 | #ifndef __WIN32__ |
---|
| 496 | | S_IFLNK |
---|
| 497 | #endif |
---|
| 498 | )) |
---|
[3655] | 499 | return true; |
---|
[3658] | 500 | else |
---|
[3655] | 501 | return false; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | /** |
---|
| 505 | \brief Checks if the file is either a Regular file or a Symlink |
---|
| 506 | \param fileName the File to check for |
---|
| 507 | \returns true if it is a regular file/symlink, false otherwise |
---|
| 508 | */ |
---|
| 509 | bool ResourceManager::isFile(const char* fileName) |
---|
| 510 | { |
---|
| 511 | struct stat status; |
---|
| 512 | stat(fileName, &status); |
---|
[3790] | 513 | if (status.st_mode & (S_IFREG |
---|
| 514 | #ifndef __WIN32__ |
---|
| 515 | | S_IFLNK |
---|
| 516 | #endif |
---|
| 517 | )) |
---|
[3655] | 518 | return true; |
---|
| 519 | else |
---|
| 520 | return false; |
---|
| 521 | } |
---|
[3676] | 522 | |
---|
| 523 | /** |
---|
| 524 | \brief outputs debug information about the ResourceManager |
---|
| 525 | */ |
---|
| 526 | void ResourceManager::debug(void) |
---|
| 527 | { |
---|
| 528 | PRINT(0)("=RM===================================\n"); |
---|
| 529 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
| 530 | PRINT(0)("======================================\n"); |
---|
| 531 | // if it is not initialized |
---|
| 532 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
| 533 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
| 534 | PRINT(0)(" List of Image-Directories: "); |
---|
| 535 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
| 536 | char* tmpDir = tmpIt->nextElement(); |
---|
| 537 | while(tmpDir) |
---|
| 538 | { |
---|
| 539 | PRINT(0)("%s ",tmpDir); |
---|
| 540 | tmpDir = tmpIt->nextElement(); |
---|
| 541 | } |
---|
| 542 | delete tmpIt; |
---|
| 543 | PRINT(0)("\n"); |
---|
| 544 | |
---|
| 545 | PRINT(0)("List of all stored Resources:\n"); |
---|
| 546 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
| 547 | Resource* enumRes = iterator->nextElement(); |
---|
| 548 | while (enumRes) |
---|
| 549 | { |
---|
| 550 | PRINT(0)("-----------------------------------------\n"); |
---|
| 551 | PRINT(0)("Name: %s; References: %d; Type:", enumRes->name, enumRes->count); |
---|
| 552 | switch (enumRes->type) |
---|
| 553 | { |
---|
| 554 | case OBJ: |
---|
| 555 | PRINT(0)("ObjectModel\n"); |
---|
| 556 | break; |
---|
| 557 | case PRIM: |
---|
| 558 | PRINT(0)("PrimitiveModel\n"); |
---|
| 559 | break; |
---|
| 560 | case IMAGE: |
---|
| 561 | PRINT(0)("ImageFile (Texture)\n"); |
---|
| 562 | break; |
---|
| 563 | default: |
---|
| 564 | PRINT(0)("SoundFile\n"); |
---|
| 565 | break; |
---|
| 566 | } |
---|
| 567 | PRINT(0)("gets deleted at "); |
---|
| 568 | switch(enumRes->prio) |
---|
| 569 | { |
---|
| 570 | default: |
---|
| 571 | case RP_NO: |
---|
| 572 | PRINT(0)("first posibility (0)\n"); |
---|
| 573 | break; |
---|
| 574 | case RP_LEVEL: |
---|
| 575 | PRINT(0)("the end of the Level (1)\n"); |
---|
| 576 | break; |
---|
| 577 | case RP_CAMPAIGN: |
---|
| 578 | PRINT(0)("the end of the campaign (2)\n"); |
---|
| 579 | break; |
---|
| 580 | case RP_GAME: |
---|
| 581 | PRINT(0)("when leaving the game (3)\n"); |
---|
| 582 | break; |
---|
| 583 | } |
---|
| 584 | enumRes = iterator->nextElement(); |
---|
| 585 | } |
---|
| 586 | delete iterator; |
---|
| 587 | |
---|
| 588 | |
---|
| 589 | |
---|
| 590 | PRINT(0)("==================================RM==\n"); |
---|
| 591 | } |
---|