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