Changeset 6693 in orxonox.OLD for branches/network/src/util/loading
- Timestamp:
- Jan 25, 2006, 2:19:46 PM (19 years ago)
- Location:
- branches/network/src/util/loading
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/util/loading/resource_manager.cc
r6467 r6693 18 18 #include "resource_manager.h" 19 19 20 #include "substring.h" 20 21 #include "debug.h" 21 22 22 23 #include <algorithm> 24 #include <assert.h> 23 25 24 26 // different resource Types … … 50 52 51 53 /** 52 * standard constructor54 * @brief standard constructor 53 55 */ 54 56 ResourceManager::ResourceManager () … … 66 68 67 69 /** 68 * standard destructor70 * @brief standard destructor 69 71 */ 70 72 ResourceManager::~ResourceManager () … … 78 80 // deleting the Directorie Lists 79 81 while (!this->imageDirs.empty()) 80 81 delete[] this->imageDirs.front();82 this->imageDirs.pop_front();83 82 { 83 delete[] this->imageDirs.back(); 84 this->imageDirs.pop_back(); 85 } 84 86 85 87 delete[] this->dataDir; … … 89 91 90 92 /** 91 * sets the data main directory93 * @brief sets the data main directory 92 94 * @param dataDir the DataDirectory. 93 95 */ … … 122 124 123 125 /** 124 * sets the data main directory126 * @brief sets the data main directory 125 127 * @param dataDir the DataDirectory. 126 128 * … … 153 155 154 156 155 156 /** 157 * checks for the DataDirectory, by looking if 157 /** 158 * @brief checks for the DataDirectory, by looking if 158 159 * @param fileInside is iniside of the given directory. 159 160 */ … … 162 163 bool retVal; 163 164 if (!isDir(this->dataDir)) 164 165 166 167 165 { 166 PRINTF(1)("%s is not a directory\n", this->dataDir); 167 return false; 168 } 168 169 169 170 char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1]; … … 176 177 #ifndef NO_TEXTURES 177 178 /** 178 * adds a new Path for Images179 * @brief adds a new Path for Images 179 180 * @param imageDir The path to insert 180 181 * @returns true, if the Path was well and injected (or already existent within the list) … … 201 202 // check if the param is a Directory 202 203 if (isDir(newDir)) 203 { 204 // check if the Directory has been added before 205 std::list<char*>::const_iterator imageDir; 206 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 204 { 205 // check if the Directory has been added before 206 std::vector<char*>::const_iterator imageDir; 207 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 208 { 209 if (!strcmp(*imageDir, newDir)) 207 210 { 208 if (!strcmp(*imageDir, newDir)) 209 { 210 PRINTF(3)("Path %s already loaded\n", newDir); 211 delete[] newDir; 212 return true; 213 } 211 PRINTF(3)("Path %s already loaded\n", newDir); 212 delete[] newDir; 213 return true; 214 214 } 215 // adding the directory to the List 216 this->imageDirs.push_back(newDir); 217 return true; 218 } 219 else 220 { 221 PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir); 222 delete[] newDir; 223 return false; 224 } 215 } 216 // adding the directory to the List 217 this->imageDirs.push_back(newDir); 218 return true; 219 } 220 else 221 { 222 PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir); 223 delete[] newDir; 224 return false; 225 } 225 226 } 226 227 #endif /* NO_TEXTURES */ 227 228 228 229 /** 229 * loads resources230 * @brief loads resources 230 231 * @param fileName: The fileName of the resource to load 231 232 * @param prio: The ResourcePriority of this resource (will only be increased) 233 * @param param0: an additional option to parse (see the constuctors for more help) 232 234 * @param param1: an additional option to parse (see the constuctors for more help) 233 235 * @param param2: an additional option to parse (see the constuctors for more help) 234 * @param param3: an additional option to parse (see the constuctors for more help)235 236 * @returns a pointer to a desired Resource. 236 237 */ 237 BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, void* param1, void* param2, void* param3) 238 BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, 239 const MultiType& param0, const MultiType& param1, const MultiType& param2) 238 240 { 239 241 if (fileName == NULL) … … 258 260 #endif 259 261 #define __IF_OK 260 if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4))261 tmpType = WAV;262 else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4))263 tmpType = MP3;264 else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4))265 tmpType = OGG;262 if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4)) 263 tmpType = WAV; 264 else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4)) 265 tmpType = MP3; 266 else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4)) 267 tmpType = OGG; 266 268 #endif /* NO_AUDIO */ 267 269 #ifndef NO_TEXT 268 270 #ifdef __IF_OK 269 else271 else 270 272 #endif 271 273 #define __IF_OK 272 if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4))273 tmpType = TTF;274 if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4)) 275 tmpType = TTF; 274 276 #endif /* NO_TEXT */ 275 277 #ifndef NO_SHADERS 276 278 #ifdef __IF_OK 277 else279 else 278 280 #endif 279 281 #define __IF_OK 280 if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5))281 tmpType = SHADER;282 if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5)) 283 tmpType = SHADER; 282 284 #endif /* NO_SHADERS */ 283 285 #ifndef NO_TEXTURES 284 286 #ifdef __IF_OK 285 else287 else 286 288 #else 287 289 if 288 290 #endif 289 tmpType = IMAGE;291 tmpType = IMAGE; 290 292 #endif /* NO_TEXTURES */ 291 293 #undef __IF_OK 292 return this->load(fileName, tmpType, prio, param1, param2, param3); 293 } 294 295 /** 296 * loads resources 294 return this->load(fileName, tmpType, prio, param0, param1, param2); 295 } 296 297 /** 298 * @brief caches a Resource 299 * 300 * @see load; 301 * 302 * @brief returns true if ok, false otherwise. 303 * This function loads a Resource without applying it to an Object. 304 * This is for loading purposes, e.g, when the user is loading a Resource 305 * during the initialisation instead of at Runtime. 306 */ 307 bool ResourceManager::cache(const char* fileName, ResourceType type, ResourcePriority prio, 308 const MultiType& param0, const MultiType& param1, const MultiType& param2) 309 { 310 assert(fileName != NULL); 311 312 // searching if the resource was loaded before. 313 Resource* tmpResource; 314 // check if we already loaded this Resource 315 tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2); 316 // otherwise load it 317 if (tmpResource == NULL) 318 tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2); 319 // return cached pointer. 320 if (tmpResource != NULL) // if the resource was loaded before. 321 { 322 if(tmpResource->prio < prio) 323 tmpResource->prio = prio; 324 return true; 325 } 326 else 327 return false; 328 } 329 330 /** 331 * tells the ResourceManager to generate a Copy of the Resource. 332 * @brief resourcePointer: The Pointer to the resource to copy 333 * @returns the Resource pointed to resourcePointer. 334 */ 335 BaseObject* ResourceManager::copy(BaseObject* resourcePointer) 336 { 337 Resource* tmp = locateResourceByPointer(resourcePointer); 338 if (tmp!=NULL) 339 { 340 tmp->count++; 341 return tmp->pointer; 342 } 343 else 344 return NULL; 345 } 346 347 348 /** 349 * @brief loads resources 297 350 * @param fileName: The fileName of the resource to load 298 351 * @param type: The Type of Resource to load. 299 352 * @param prio: The ResourcePriority of this resource (will only be increased) 353 * @param param0: an additional option to parse (see the constuctors for more help) 300 354 * @param param1: an additional option to parse (see the constuctors for more help) 301 355 * @param param2: an additional option to parse (see the constuctors for more help) 302 * @param param3: an additional option to parse (see the constuctors for more help)303 356 * @returns a pointer to a desired Resource. 304 357 */ 305 358 BaseObject* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio, 306 void* param1, void* param2, void* param3) 307 { 308 if (fileName == NULL) 359 const MultiType& param0, const MultiType& param1, const MultiType& param2) 360 { 361 assert(fileName != NULL); 362 363 // searching if the resource was loaded before. 364 Resource* tmpResource; 365 // check if we already loaded this Resource 366 tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2); 367 // otherwise load it 368 if (tmpResource == NULL) 369 { 370 tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2); 371 } 372 // return cached pointer. 373 if (tmpResource != NULL) // if the resource was loaded before. 374 { 375 tmpResource->count++; 376 if(tmpResource->prio < prio) 377 tmpResource->prio = prio; 378 379 return tmpResource->pointer; 380 } 381 else 309 382 return NULL; 310 311 // searching if the resource was loaded before. 312 Resource* tmpResource = this->locateResourceByInfo(fileName, type, param1, param2, param3); 313 if (tmpResource != NULL) // if the resource was loaded before. 314 { 315 PRINTF(4)("not loading cached resource %s\n", tmpResource->name); 316 tmpResource->count++; 317 if(tmpResource->prio < prio) 318 tmpResource->prio = prio; 319 } 320 else 321 { 322 // Setting up the new Resource 323 tmpResource = new Resource; 324 tmpResource->count = 1; 325 tmpResource->type = type; 326 tmpResource->prio = prio; 327 tmpResource->pointer = NULL; 328 tmpResource->name = new char[strlen(fileName)+1]; 329 strcpy(tmpResource->name, fileName); 330 331 // creating the full name. (directoryName + FileName) 332 char* fullName = ResourceManager::getFullName(fileName); 333 // Checking for the type of resource \see ResourceType 334 switch(type) 335 { 383 } 384 385 386 /** 387 * @brief loads resources for internal purposes 388 * @param fileName: The fileName of the resource to load 389 * @param type: The Type of Resource to load. 390 * @param prio: The ResourcePriority of this resource (will only be increased) 391 * @param param0: an additional option to parse (see the constuctors for more help) 392 * @param param1: an additional option to parse (see the constuctors for more help) 393 * @param param2: an additional option to parse (see the constuctors for more help) 394 * @returns a pointer to a desired Resource. 395 */ 396 Resource* ResourceManager::loadResource(const char* fileName, ResourceType type, ResourcePriority prio, 397 const MultiType& param0, const MultiType& param1, const MultiType& param2) 398 { 399 // Setting up the new Resource 400 Resource* tmpResource = new Resource; 401 tmpResource->count = 0; 402 tmpResource->type = type; 403 tmpResource->prio = prio; 404 tmpResource->pointer = NULL; 405 tmpResource->name = new char[strlen(fileName)+1]; 406 strcpy(tmpResource->name, fileName); 407 408 // creating the full name. (directoryName + FileName) 409 char* fullName = ResourceManager::getFullName(fileName); 410 // Checking for the type of resource \see ResourceType 411 switch(type) 412 { 336 413 #ifndef NO_MODEL 337 case OBJ: 338 if (param1) 339 tmpResource->modelSize = *(float*)param1; 340 else 341 tmpResource->modelSize = 1.0; 342 343 if(ResourceManager::isFile(fullName)) 344 tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize); 345 else 346 { 347 PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir); 348 tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize); 349 } 350 break; 351 case PRIM: 352 if (param1) 353 tmpResource->modelSize = *(float*)param1; 354 else 355 tmpResource->modelSize = 1.0; 356 357 if (!strcmp(tmpResource->name, "cube")) 358 tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->modelSize); 359 else if (!strcmp(tmpResource->name, "sphere")) 360 tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->modelSize); 361 else if (!strcmp(tmpResource->name, "plane")) 362 tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->modelSize); 363 else if (!strcmp(tmpResource->name, "cylinder")) 364 tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->modelSize); 365 else if (!strcmp(tmpResource->name, "cone")) 366 tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->modelSize); 367 break; 368 case MD2: 369 if(ResourceManager::isFile(fullName)) 370 { 371 if (param1 != NULL) 372 { 373 tmpResource->secFileName = new char[strlen((const char*)param1)+1]; 374 strcpy(tmpResource->secFileName, (const char*) param1); 375 } 376 else 377 tmpResource->secFileName = NULL; 378 tmpResource->pointer = new MD2Data(fullName, tmpResource->secFileName); 379 // tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName); 380 381 } 382 break; 414 case OBJ: 415 if (param0.getType() != MT_NULL) 416 tmpResource->param[0] = param0; 417 else 418 tmpResource->param[0] = 1.0f; 419 420 if(ResourceManager::isFile(fullName)) 421 tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat()); 422 else 423 { 424 PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir); 425 tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat()); 426 } 427 break; 428 case PRIM: 429 if (param0 != MT_NULL) 430 tmpResource->param[0] = param0; 431 else 432 tmpResource->param[0] = 1.0f; 433 434 if (!strcmp(tmpResource->name, "cube")) 435 tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat()); 436 else if (!strcmp(tmpResource->name, "sphere")) 437 tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat()); 438 else if (!strcmp(tmpResource->name, "plane")) 439 tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat()); 440 else if (!strcmp(tmpResource->name, "cylinder")) 441 tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat()); 442 else if (!strcmp(tmpResource->name, "cone")) 443 tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat()); 444 break; 445 case MD2: 446 if(ResourceManager::isFile(fullName)) 447 { 448 tmpResource->param[0] = param0; 449 tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getString()); 450 // tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName); 451 452 } 453 break; 383 454 #endif /* NO_MODEL */ 384 455 #ifndef NO_TEXT 385 case TTF: 386 if (param1 != NULL) 387 tmpResource->ttfSize = *(unsigned int*)param1; 388 else 389 tmpResource->ttfSize = FONT_DEFAULT_RENDER_SIZE; 390 391 if(isFile(fullName)) 392 tmpResource->pointer = new Font(fullName, tmpResource->ttfSize); 393 else 394 PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir); 395 break; 456 case TTF: 457 if (param0 != MT_NULL) 458 { 459 assert(param0.getInt() >= 0); 460 tmpResource->param[0] = param0; 461 } 462 else 463 tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE; 464 465 if(isFile(fullName)) 466 tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt()); 467 else 468 PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir); 469 break; 396 470 #endif /* NO_TEXT */ 397 471 #ifndef NO_AUDIO 398 399 400 401 402 403 404 405 472 case WAV: 473 if(isFile(fullName)) 474 tmpResource->pointer = new SoundBuffer(fullName); 475 break; 476 case OGG: 477 if (isFile(fullName)) 478 tmpResource->pointer = new OggPlayer(fullName); 479 break; 406 480 #endif /* NO_AUDIO */ 407 481 #ifndef NO_TEXTURES 408 409 if (param1 !=NULL)410 tmpResource->texTarget = *(GLenum*)param1;411 412 tmpResource->texTarget= GL_TEXTURE_2D;413 414 415 416 417 418 419 420 std::list<char*>::iterator imageDir;421 422 423 424 425 426 427 428 tmpResource->pointer = new Texture(imgName, tmpResource->texTarget);429 430 431 432 433 434 435 436 437 482 case IMAGE: 483 if (param0 != MT_NULL) 484 tmpResource->param[0] = param0; 485 else 486 tmpResource->param[0] = GL_TEXTURE_2D; 487 if(isFile(fullName)) 488 { 489 PRINTF(4)("Image %s resides to %s\n", fileName, fullName); 490 tmpResource->pointer = new Texture(fullName); 491 } 492 else 493 { 494 std::vector<char*>::iterator imageDir; 495 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 496 { 497 char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1]; 498 sprintf(imgName, "%s%s", *imageDir, fileName); 499 if(isFile(imgName)) 500 { 501 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); 502 tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt()); 503 delete[] imgName; 504 break; 505 } 506 delete[] imgName; 507 } 508 } 509 if(!tmpResource) 510 PRINTF(2)("!!Image %s not Found!!\n", fileName); 511 break; 438 512 #endif /* NO_TEXTURES */ 439 513 #ifndef NO_SHADERS 440 441 442 443 if (param1 !=NULL)444 445 char* secFullName = ResourceManager::getFullName((const char*)param1);446 if (ResourceManager::isFile(secFullName))447 {448 tmpResource->secFileName = new char[strlen((const char*)param1)+1];449 strcpy(tmpResource->secFileName, (const char*) param1);450 451 452 453 454 455 456 tmpResource->secFileName = NULL;457 458 459 460 514 case SHADER: 515 if(ResourceManager::isFile(fullName)) 516 { 517 if (param0 != MT_NULL) 518 { 519 MultiType param = param0; /// HACK 520 char* secFullName = ResourceManager::getFullName(param.getString()); 521 if (ResourceManager::isFile(secFullName)) 522 { 523 tmpResource->param[0] = secFullName; 524 tmpResource->pointer = new Shader(fullName, secFullName); 525 } 526 delete[] secFullName; 527 } 528 else 529 { 530 tmpResource->param[0] = param0; 531 tmpResource->pointer = new Shader(fullName, NULL); 532 } 533 } 534 break; 461 535 #endif /* NO_SHADERS */ 462 default: 463 tmpResource->pointer = NULL; 464 PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name); 465 break; 466 } 467 if (tmpResource->pointer != NULL) 468 this->resourceList.push_back(tmpResource); 469 delete[] fullName; 470 } 536 default: 537 tmpResource->pointer = NULL; 538 PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name); 539 break; 540 } 471 541 if (tmpResource->pointer != NULL) 472 return tmpResource->pointer; 473 else 474 { 475 PRINTF(2)("Resource %s could not be loaded\n", fileName); 476 delete[] tmpResource->name; 477 delete tmpResource; 478 return NULL; 479 } 480 } 481 482 /** 483 * unloads a Resource 542 this->resourceList.push_back(tmpResource); 543 delete[] fullName; 544 545 546 if (tmpResource->pointer != NULL) 547 return tmpResource; 548 else 549 { 550 PRINTF(2)("Resource %s could not be loaded\n", fileName); 551 delete[] tmpResource->name; 552 delete tmpResource; 553 return NULL; 554 } 555 } 556 557 /** 558 * @brief unloads a Resource 484 559 * @param pointer: The pointer to free 485 560 * @param prio: the PriorityLevel to unload this resource 486 561 * @returns true if successful (pointer found, and deleted), false otherwise 487 562 */ 488 bool ResourceManager::unload( void* pointer, ResourcePriority prio)563 bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio) 489 564 { 490 565 if (pointer == NULL) … … 502 577 503 578 /** 504 * unloads a Resource579 * @brief unloads a Resource 505 580 * @param resource: The resource to unloade 506 581 * @param prio the PriorityLevel to unload this resource … … 515 590 516 591 if (resource->prio <= prio) 517 518 519 520 521 522 592 { 593 if (resource->count == 0) 594 { 595 // deleting the Resource 596 switch(resource->type) 597 { 523 598 #ifndef NO_MODEL 524 525 526 527 528 529 530 599 case OBJ: 600 case PRIM: 601 delete (Model*)resource->pointer; 602 break; 603 case MD2: 604 delete (MD2Data*)resource->pointer; 605 break; 531 606 #endif /* NO_MODEL */ 532 607 #ifndef NO_AUDIO 533 534 535 536 537 538 608 case WAV: 609 delete (SoundBuffer*)resource->pointer; 610 break; 611 case OGG: 612 delete (OggPlayer*)resource->pointer; 613 break; 539 614 #endif /* NO_AUDIO */ 540 615 #ifndef NO_TEXT 541 542 543 616 case TTF: 617 delete (Font*)resource->pointer; 618 break; 544 619 #endif /* NO_TEXT */ 545 620 #ifndef NO_TEXTURES 546 547 548 621 case IMAGE: 622 delete (Texture*)resource->pointer; 623 break; 549 624 #endif /* NO_TEXTURES */ 550 625 #ifndef NO_SHADERS 551 552 553 626 case SHADER: 627 delete (Shader*)resource->pointer; 628 break; 554 629 #endif /* NO_SHADERS */ 555 556 557 558 559 560 561 562 563 std::list<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);564 565 566 567 568 569 630 default: 631 PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); 632 return false; 633 break; 634 } 635 // deleting the List Entry: 636 PRINTF(4)("Resource %s safely removed.\n", resource->name); 637 delete[] resource->name; 638 std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource); 639 this->resourceList.erase(resourceIT); 640 delete resource; 641 } 642 else 643 PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); 644 } 570 645 else 571 646 PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); … … 575 650 576 651 /** 577 * unloads all alocated Memory of Resources with a pririty lower than prio652 * @brief unloads all alocated Memory of Resources with a pririty lower than prio 578 653 * @param prio The priority to delete 579 654 */ 580 655 bool ResourceManager::unloadAllByPriority(ResourcePriority prio) 581 656 { 582 std::list<Resource*>::iterator resource, pre;583 pre = --this->resourceList.end();584 657 unsigned int removeCount; 585 658 for (unsigned int round = 0; round < 3; round++) 586 659 { 660 int index = this->resourceList.size() - 1; 587 661 removeCount = 0; 588 while (pre != resourceList.end()) 589 { 590 resource = pre; 591 pre--; 592 if ((*resource)->prio <= prio) 662 while (index >= 0) 663 { 664 if (this->resourceList[index]->prio <= prio) 665 { 666 if (this->resourceList[index]->count == 0) 667 unload(this->resourceList[index], prio); 668 else 593 669 { 594 if ((*resource)->count == 0) 595 unload((*resource), prio); 596 else 597 { 670 if (round == 3) 598 671 PRINTF(2)("unable to unload %s because there are still %d references to it\n", 599 (*resource)->name, (*resource)->count); 600 removeCount++; 601 } 672 this->resourceList[index]->name, this->resourceList[index]->count); 673 removeCount++; 602 674 } 603 } 604 if (removeCount == 0) break; 605 } 606 } 607 608 609 /** 610 * Searches for a Resource by some information 675 } 676 index--; 677 } 678 if (removeCount == 0) break; 679 } 680 } 681 682 683 /** 684 * @brief Searches for a Resource by some information 611 685 * @param fileName: The name to look for 612 686 * @param type the Type of resource to locate. 687 * @param param0: an additional option to parse (see the constuctors for more help) 613 688 * @param param1: an additional option to parse (see the constuctors for more help) 614 689 * @param param2: an additional option to parse (see the constuctors for more help) 615 * @param param3: an additional option to parse (see the constuctors for more help)616 690 * @returns a Pointer to the Resource if found, NULL otherwise. 617 691 */ 618 692 Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, 619 void* param1, void* param2, void* param3) const620 { 621 std:: list<Resource*>::const_iterator resource;693 const MultiType& param0, const MultiType& param1, const MultiType& param2) const 694 { 695 std::vector<Resource*>::const_iterator resource; 622 696 for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) 623 697 { 624 698 if ((*resource)->type == type && !strcmp(fileName, (*resource)->name)) 625 { 626 bool match = false; 627 628 switch (type) 629 { 699 { 700 bool match = false; 701 switch (type) 702 { 630 703 #ifndef NO_MODEL 631 632 633 if (!param1)634 635 if ((*resource)->modelSize == 1.0)636 637 638 else if ((*resource)->modelSize == *(float*)param1)639 640 641 642 if (!param1)643 644 if ((*resource)->secFileName == NULL)645 646 647 else if (!strcmp((*resource)->secFileName, (const char*)param1))648 649 704 case PRIM: 705 case OBJ: 706 if (param0 == MT_NULL) 707 { 708 if ((*resource)->param[0] == 1.0f) 709 match = true; 710 } 711 else if ((*resource)->param[0] == param0.getFloat()) 712 match = true; 713 break; 714 case MD2: 715 if (param0 == MT_NULL) 716 { 717 if ((*resource)->param[0] == "") 718 match = true; 719 } 720 else if ((*resource)->param[0] == ((MultiType)param0).getString()) 721 match = true; 722 break; 650 723 #endif /* NO_MODEL */ 651 724 #ifndef NO_TEXT 652 653 if (param1 ==NULL)654 655 if ((*resource)->ttfSize== FONT_DEFAULT_RENDER_SIZE)656 657 658 else if ((*resource)->ttfSize == *(unsigned int*)param1)659 660 725 case TTF: 726 if (param0 == MT_NULL) 727 { 728 if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE) 729 match = true; 730 } 731 else if ((*resource)->param[0] == param0.getInt()) 732 match = true; 733 break; 661 734 #endif /* NO_TEXT */ 662 735 #ifndef NO_SHADERS 663 664 if (!param1)665 666 if ((*resource)->secFileName == NULL)667 668 669 else if (!strcmp((*resource)->secFileName, (const char*)param1))670 736 case SHADER: 737 if (param0 == MT_NULL) 738 { 739 if ((*resource)->param[0] == "") 740 match = true; 741 } 742 else if ((*resource)->param[0] == ((MultiType)param0).getString()) 743 match = true; 671 744 #endif /* NO_SHADERS */ 672 745 #ifndef NO_TEXTURES 673 674 if (!param1)675 676 if ((*resource)->texTarget== GL_TEXTURE_2D)677 678 679 else if ((*resource)->texTarget == *(GLenum*)param1)680 746 case IMAGE: 747 if (param0 == MT_NULL) 748 { 749 if ((*resource)->param[0] == GL_TEXTURE_2D) 750 match = true; 751 } 752 else if ((*resource)->param[0] == param0.getInt()) 753 match = true; 681 754 #endif /* NO_TEXTURES */ 682 683 684 685 686 687 688 689 690 691 755 default: 756 match = true; 757 break; 758 } 759 if (match) 760 { 761 return (*resource); 762 } 763 } 764 } 692 765 return NULL; 693 766 } 694 767 695 768 /** 696 * Searches for a Resource by Pointer769 * @brief Searches for a Resource by Pointer 697 770 * @param pointer the Pointer to search for 698 771 * @returns a Pointer to the Resource if found, NULL otherwise. 699 */772 */ 700 773 Resource* ResourceManager::locateResourceByPointer(const void* pointer) const 701 774 { 702 775 // Resource* enumRes = resourceList->enumerate(); 703 std:: list<Resource*>::const_iterator resource;776 std::vector<Resource*>::const_iterator resource; 704 777 for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) 705 778 if (pointer == (*resource)->pointer) 706 779 return (*resource); 707 780 return NULL; 708 781 } 709 782 710 /** 711 * Checks if it is a Directory 783 char* ResourceManager::toResourcableString(unsigned int i) 784 { 785 int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); 786 len += strlen(this->resourceList[i]->name); 787 if (this->resourceList[i]->param[0].getString()) len += strlen(this->resourceList[i]->param[0].getString()) +1; 788 if (this->resourceList[i]->param[1].getString()) len += strlen(this->resourceList[i]->param[1].getString()) +1; 789 if (this->resourceList[i]->param[2].getString()) len += strlen(this->resourceList[i]->param[2].getString()) +1; 790 len += 10; 791 char* tmp = new char[len]; 792 tmp[0] = '\0'; 793 strcat( tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); 794 strcat(tmp,","); 795 strcat (tmp, this->resourceList[i]->name); 796 if (this->resourceList[i]->param[0].getString() && this->resourceList[i]->param[0].getString() != '\0') 797 { 798 strcat(tmp,","); 799 strcat( tmp, this->resourceList[i]->param[0].getString()); 800 } 801 if (this->resourceList[i]->param[1].getString() && this->resourceList[i]->param[1].getString() != '\0') 802 { 803 strcat(tmp,","); 804 strcat( tmp, this->resourceList[i]->param[1].getString()); 805 } 806 if (this->resourceList[i]->param[2].getString() && this->resourceList[i]->param[2].getString() != '\0') 807 { 808 strcat(tmp,","); 809 strcat( tmp, this->resourceList[i]->param[2].getString()); 810 } 811 return tmp; 812 } 813 814 /** 815 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function 816 * @param resourceableString the String to cache the resource from. 817 */ 818 bool ResourceManager::fromResourceableString(const char* resourceableString) 819 { 820 SubString splits(resourceableString, ','); 821 splits.debug(); 822 if (splits.getCount() == 2) 823 this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 824 RP_LEVEL); 825 else if (splits.getCount() == 3) 826 return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 827 RP_LEVEL, splits[2]); 828 else if (splits.getCount() == 4) 829 return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 830 RP_LEVEL, splits[2], splits[3]); 831 else if (splits.getCount() == 5) 832 return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 833 RP_LEVEL, splits[2], splits[3], splits[4]); 834 } 835 836 837 /** 838 * @brief Checks if it is a Directory 712 839 * @param directoryName the Directory to check for 713 840 * @returns true if it is a directory/symlink false otherwise … … 724 851 if (directoryName[strlen(directoryName)-1] == '/' || 725 852 directoryName[strlen(directoryName)-1] == '\\') 726 727 728 729 730 731 else 732 733 734 735 853 { 854 tmpDirName = new char[strlen(directoryName)]; 855 strncpy(tmpDirName, directoryName, strlen(directoryName)-1); 856 tmpDirName[strlen(directoryName)-1] = '\0'; 857 } 858 else 859 { 860 tmpDirName = new char[strlen(directoryName)+1]; 861 strcpy(tmpDirName, directoryName); 862 } 736 863 737 864 if(!stat(tmpDirName, &status)) 738 739 865 { 866 if (status.st_mode & (S_IFDIR 740 867 #ifndef __WIN32__ 741 868 | S_IFLNK 742 869 #endif 743 744 745 746 747 748 749 750 751 752 753 870 )) 871 { 872 delete[] tmpDirName; 873 return true; 874 } 875 else 876 { 877 delete[] tmpDirName; 878 return false; 879 } 880 } 754 881 else 755 882 { … … 760 887 761 888 /** 762 * Checks if the file is either a Regular file or a Symlink889 * @brief Checks if the file is either a Regular file or a Symlink 763 890 * @param fileName the File to check for 764 891 * @returns true if it is a regular file/symlink, false otherwise … … 772 899 struct stat status; 773 900 if (!stat(tmpFileName, &status)) 774 775 901 { 902 if (status.st_mode & (S_IFREG 776 903 #ifndef __WIN32__ 777 904 | S_IFLNK 778 905 #endif 779 )) 780 { 781 delete[] tmpFileName; 782 return true; 783 } 784 else 785 { 786 delete[] tmpFileName; 787 return false; 788 } 789 } 790 else 906 )) 907 { 908 delete[] tmpFileName; 909 return true; 910 } 911 else 791 912 { 792 913 delete[] tmpFileName; 793 914 return false; 794 915 } 795 } 796 797 /** 798 * touches a File on the disk (thereby creating it) 916 } 917 else 918 { 919 delete[] tmpFileName; 920 return false; 921 } 922 } 923 924 /** 925 * @brief touches a File on the disk (thereby creating it) 799 926 * @param fileName The file to touch 800 927 */ … … 806 933 FILE* stream; 807 934 if( (stream = fopen (tmpName, "w")) == NULL) 808 809 810 811 812 935 { 936 PRINTF(1)("could not open %s fro writing\n", fileName); 937 delete[] tmpName; 938 return false; 939 } 813 940 fclose(stream); 814 941 … … 817 944 818 945 /** 819 * deletes a File from disk946 * @brief deletes a File from disk 820 947 * @param fileName the File to delete 821 948 */ … … 840 967 char* retName; 841 968 if (!strncmp(name, "~/", 2)) 842 843 969 { 970 char tmpFileName[500]; 844 971 #ifdef __WIN32__ 845 972 strcpy(tmpFileName, getenv("USERPROFILE")); 846 973 #else 847 974 strcpy(tmpFileName, getenv("HOME")); 848 975 #endif 849 850 851 852 else 853 854 855 856 976 retName = new char[strlen(tmpFileName)+strlen(name)]; 977 sprintf(retName, "%s%s", tmpFileName, name+1); 978 } 979 else 980 { 981 retName = new char[strlen(name)+1]; 982 strcpy(retName, name); 983 } 857 984 return retName; 858 985 } … … 874 1001 return retName; 875 1002 else 876 877 878 879 880 } 881 882 883 /** 884 * checks wether a file is in the DataDir.1003 { 1004 delete[] retName; 1005 return NULL; 1006 } 1007 } 1008 1009 1010 /** 1011 * @brief checks wether a file is in the DataDir. 885 1012 * @param fileName the File to check if it is in the Data-Dir structure. 886 1013 * @returns true if the file exists, false otherwise … … 893 1020 bool retVal = false; 894 1021 char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir()) 895 + strlen(fileName) + 1];1022 + strlen(fileName) + 1]; 896 1023 sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); 897 1024 … … 906 1033 907 1034 /** 908 * outputs debug information about the ResourceManager909 */1035 * @brief outputs debug information about the ResourceManager 1036 */ 910 1037 void ResourceManager::debug() const 911 1038 { … … 917 1044 PRINT(0)(" Data-Directory is: %s\n", this->dataDir); 918 1045 PRINT(0)(" List of Image-Directories: "); 919 std:: list<char*>::const_iterator imageDir;1046 std::vector<char*>::const_iterator imageDir; 920 1047 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 921 1048 PRINT(0)("%s ", (*imageDir)); 922 1049 PRINT(0)("\n"); 923 1050 924 1051 PRINT(0)("List of all stored Resources:\n"); 925 std:: list<Resource*>::const_iterator resource;1052 std::vector<Resource*>::const_iterator resource; 926 1053 for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) 927 1054 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 1055 { 1056 PRINT(0)("-----------------------------------------\n"); 1057 PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type)); 1058 1059 PRINT(0)("gets deleted at "); 1060 switch((*resource)->prio) 1061 { 1062 default: 1063 case RP_NO: 1064 PRINT(0)("first posibility (0)\n"); 1065 break; 1066 case RP_LEVEL: 1067 PRINT(0)("the end of the Level (1)\n"); 1068 break; 1069 case RP_CAMPAIGN: 1070 PRINT(0)("the end of the campaign (2)\n"); 1071 break; 1072 case RP_GAME: 1073 PRINT(0)("when leaving the game (3)\n"); 1074 break; 1075 } 1076 } 950 1077 951 1078 … … 956 1083 957 1084 /** 958 * converts a ResourceType into the corresponding String1085 * @brief converts a ResourceType into the corresponding String 959 1086 * @param type the ResourceType to translate 960 1087 * @returns the converted String. … … 962 1089 const char* ResourceManager::ResourceTypeToChar(ResourceType type) 963 1090 { 964 switch (type) 1091 return ResourceManager::resourceNames[type]; 1092 } 1093 1094 /** 1095 * @brief converts a String into a ResourceType (good for loading) 1096 * @param resourceType the name of the Type 1097 * @returns the Number of the Type, or 0 (defautl) if not found. 1098 */ 1099 ResourceType ResourceManager::stringToResourceType(const char* resourceType) 1100 { 1101 assert(resourceType != NULL); 1102 for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++) 1103 if (!strcmp(resourceType, ResourceManager::resourceNames[i])) 1104 return (ResourceType)i; 1105 return (ResourceType)0; 1106 } 1107 1108 /** 1109 * The Names of the ResourceTypes 1110 */ 1111 const char* ResourceManager::resourceNames[] = 965 1112 { 966 1113 #ifndef NO_MODEL 967 case OBJ: 968 return "ObjectModel"; 969 break; 970 case PRIM: 971 return "PrimitiveModel"; 972 break; 973 case MD2: 974 return "MD2-Data"; 975 break; 1114 "ObjectModel", 1115 "PrimitiveModel", 1116 "MD2-Data", 1117 #endif 1118 #ifndef NO_TEXT 1119 "Font", 1120 #endif 1121 #ifndef NO_AUDIO 1122 "Wav", 1123 "mp3", 1124 "ogg", 976 1125 #endif 977 1126 #ifndef NO_TEXTURES 978 case IMAGE: 979 return "ImageFile (Texture)"; 980 break; 981 #endif 982 #ifndef NO_AUDIO 983 case WAV: 984 return "SoundFile"; 985 break; 986 case OGG: 987 return "MusicFile"; 988 break; 989 #endif 990 #ifndef NO_TEXT 991 case TTF: 992 return "Font (TTF)"; 993 break; 1127 "Texture", 994 1128 #endif 995 1129 #ifndef NO_SHADERS 996 case SHADER: 997 return "Shader"; 998 break; 1130 "Shader", 999 1131 #endif 1000 default: 1001 return "unknown Format"; 1002 break; 1003 } 1004 } 1132 1133 }; -
branches/network/src/util/loading/resource_manager.h
r6633 r6693 14 14 (NO_TEXT) 15 15 (NO_TEXTURES) 16 (NO_SHADERS) 16 17 */ 17 18 … … 20 21 21 22 #include "base_object.h" 23 #include "multi_type.h" 22 24 23 24 #include <list> 25 #include <vector> 25 26 26 27 //! An eumerator for different fileTypes the resourceManager supports … … 46 47 SHADER, //!< openGL-shader program 47 48 #endif /* NO_SHADERS */ 49 RESOURCE_TYPE_SIZE 48 50 }; 49 51 … … 73 75 ResourcePriority prio; //!< The Priority of this resource. (This will only be increased) 74 76 75 // more specific 76 union { 77 #ifndef NO_MODEL 78 float modelSize; //!< the size of the model (OBJ/PRIM) 79 char* secFileName; //!< a seconf fileName 80 #endif /* NO_MODEL */ 81 #ifndef NO_TEXT 82 unsigned int ttfSize; //!< the size of the ttf-font (TTF) 83 #endif /* NO_TEXT */ 84 #ifndef NO_TEXTURES 85 GLenum texTarget; 86 #endif /* NO_TEXTURES */ 87 }; 77 MultiType param[3]; //!< The Parameters given to this Resource. 88 78 }; 89 79 … … 114 104 bool verifyDataDir(const char* fileInside); 115 105 bool addImageDir(const char* imageDir); 106 107 bool cache(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, 108 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); 109 BaseObject* copy(BaseObject* resourcePointer); 110 116 111 BaseObject* load(const char* fileName, ResourcePriority prio = RP_NO, 117 void* param1 = NULL, void* param2 = NULL, void* param3 = NULL);112 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); 118 113 BaseObject* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO, 119 void* param1 = NULL, void* param2 = NULL, void* param3 = NULL);120 bool unload( void* pointer, ResourcePriority prio = RP_NO);114 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()); 115 bool unload(BaseObject* pointer, ResourcePriority prio = RP_NO); 121 116 bool unload(Resource* resource, ResourcePriority = RP_NO); 122 117 bool unloadAllByPriority(ResourcePriority prio); 123 118 124 Resource* locateResourceByInfo(const char* fileName, ResourceType type, void* param1, void* param2, void* param3) const; 119 Resource* locateResourceByInfo(const char* fileName, ResourceType type, 120 const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()) const; 125 121 Resource* locateResourceByPointer(const void* pointer) const; 122 123 char* toResourcableString(unsigned int i); 124 bool fromResourceableString(const char* resourceableString); 125 /** @returns the Count of Resources the ResourceManager handles */ 126 unsigned int resourceCount() const { return this->resourceList.size(); } 126 127 127 128 void debug() const; … … 138 139 139 140 static const char* ResourceTypeToChar(ResourceType type); 140 141 static ResourceType stringToResourceType(const char* resourceType); 141 142 142 143 private: 143 144 ResourceManager(); 145 Resource* loadResource(const char* fileName, ResourceType type, ResourcePriority prio, 146 const MultiType& param0, const MultiType& param1, const MultiType& param2); 144 147 145 148 private: … … 147 150 148 151 char* dataDir; //!< The Data Directory, where all relevant Data is stored. 149 std::list<Resource*> resourceList; //!< The List of Resources, that has already been loaded. 150 std::list<char*> imageDirs; //!< A list of directories in which images are stored. 152 std::vector<char*> imageDirs; //!< A list of directories in which images are stored. 151 153 154 std::vector<Resource*> resourceList; //!< The List of Resources, that has already been loaded. 155 156 static const char* resourceNames[RESOURCE_TYPE_SIZE]; 152 157 }; 153 158
Note: See TracChangeset
for help on using the changeset viewer.