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