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