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