Changeset 6648 in orxonox.OLD for trunk/src/util/loading
- Timestamp:
- Jan 21, 2006, 7:09:02 PM (19 years ago)
- Location:
- trunk/src/util/loading
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/util/loading/resource_manager.cc
r6647 r6648 18 18 #include "resource_manager.h" 19 19 20 #include "substring.h" 20 21 #include "debug.h" 21 22 … … 235 236 */ 236 237 BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, 237 238 const MultiType& param0, const MultiType& param1, const MultiType& param2) 238 239 { 239 240 if (fileName == NULL) … … 342 343 // otherwise load it 343 344 if (tmpResource == NULL) 345 { 344 346 tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2); 347 } 345 348 // return cached pointer. 346 349 if (tmpResource != NULL) // if the resource was loaded before. … … 349 352 if(tmpResource->prio < prio) 350 353 tmpResource->prio = prio; 354 351 355 return tmpResource->pointer; 352 356 } … … 384 388 { 385 389 #ifndef NO_MODEL 386 case OBJ:387 if (param0.getType() != MT_NULL)388 tmpResource->param[0] = param0;389 else390 tmpResource->param[0] = 1.0f;391 392 if(ResourceManager::isFile(fullName))393 tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());394 else395 {396 PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir);397 tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());398 }399 break;400 case PRIM:401 if (param0 != MT_NULL)402 tmpResource->param[0] = param0;403 else404 tmpResource->param[0] = 1.0f;405 406 if (!strcmp(tmpResource->name, "cube"))407 tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());408 else if (!strcmp(tmpResource->name, "sphere"))409 tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());410 else if (!strcmp(tmpResource->name, "plane"))411 tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());412 else if (!strcmp(tmpResource->name, "cylinder"))413 tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());414 else if (!strcmp(tmpResource->name, "cone"))415 tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());416 break;417 case MD2:418 if(ResourceManager::isFile(fullName))419 {420 tmpResource->param[0] = param0;421 tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getString());422 // tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName);423 424 }425 break;390 case OBJ: 391 if (param0.getType() != MT_NULL) 392 tmpResource->param[0] = param0; 393 else 394 tmpResource->param[0] = 1.0f; 395 396 if(ResourceManager::isFile(fullName)) 397 tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat()); 398 else 399 { 400 PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir); 401 tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat()); 402 } 403 break; 404 case PRIM: 405 if (param0 != MT_NULL) 406 tmpResource->param[0] = param0; 407 else 408 tmpResource->param[0] = 1.0f; 409 410 if (!strcmp(tmpResource->name, "cube")) 411 tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat()); 412 else if (!strcmp(tmpResource->name, "sphere")) 413 tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat()); 414 else if (!strcmp(tmpResource->name, "plane")) 415 tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat()); 416 else if (!strcmp(tmpResource->name, "cylinder")) 417 tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat()); 418 else if (!strcmp(tmpResource->name, "cone")) 419 tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat()); 420 break; 421 case MD2: 422 if(ResourceManager::isFile(fullName)) 423 { 424 tmpResource->param[0] = param0; 425 tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getString()); 426 // tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName); 427 428 } 429 break; 426 430 #endif /* NO_MODEL */ 427 431 #ifndef NO_TEXT 428 case TTF:429 if (param0 != MT_NULL)430 {431 assert(param0.getInt() >= 0);432 tmpResource->param[0] = param0;433 }434 else435 tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;436 437 if(isFile(fullName))438 tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());439 else440 PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir);441 break;432 case TTF: 433 if (param0 != MT_NULL) 434 { 435 assert(param0.getInt() >= 0); 436 tmpResource->param[0] = param0; 437 } 438 else 439 tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE; 440 441 if(isFile(fullName)) 442 tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt()); 443 else 444 PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir); 445 break; 442 446 #endif /* NO_TEXT */ 443 447 #ifndef NO_AUDIO 444 case WAV:445 if(isFile(fullName))446 tmpResource->pointer = new SoundBuffer(fullName);447 break;448 case OGG:449 if (isFile(fullName))450 tmpResource->pointer = new OggPlayer(fullName);451 break;448 case WAV: 449 if(isFile(fullName)) 450 tmpResource->pointer = new SoundBuffer(fullName); 451 break; 452 case OGG: 453 if (isFile(fullName)) 454 tmpResource->pointer = new OggPlayer(fullName); 455 break; 452 456 #endif /* NO_AUDIO */ 453 457 #ifndef NO_TEXTURES 454 case IMAGE: 455 if (param0 != MT_NULL) 456 tmpResource->param[0] = param0; 457 else 458 tmpResource->param[0] = GL_TEXTURE_2D; 459 if(isFile(fullName)) 460 { 461 PRINTF(4)("Image %s resides to %s\n", fileName, fullName); 462 tmpResource->pointer = new Texture(fullName); 463 } 464 else 465 { 466 std::vector<char*>::iterator imageDir; 467 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 458 case IMAGE: 459 if (param0 != MT_NULL) 460 tmpResource->param[0] = param0; 461 else 462 tmpResource->param[0] = GL_TEXTURE_2D; 463 if(isFile(fullName)) 468 464 { 469 char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1]; 470 sprintf(imgName, "%s%s", *imageDir, fileName); 471 if(isFile(imgName)) 472 { 473 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); 474 tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt()); 475 delete[] imgName; 476 break; 477 } 478 delete[] imgName; 479 } 480 } 481 if(!tmpResource) 482 PRINTF(2)("!!Image %s not Found!!\n", fileName); 483 break; 484 #endif /* NO_TEXTURES */ 485 #ifndef NO_SHADERS 486 case SHADER: 487 if(ResourceManager::isFile(fullName)) 488 { 489 if (param0 != MT_NULL) 490 { 491 MultiType param = param0; /// HACK 492 char* secFullName = ResourceManager::getFullName(param.getString()); 493 if (ResourceManager::isFile(secFullName)) 494 { 495 tmpResource->param[0] = secFullName; 496 tmpResource->pointer = new Shader(fullName, secFullName); 497 } 498 delete[] secFullName; 465 PRINTF(4)("Image %s resides to %s\n", fileName, fullName); 466 tmpResource->pointer = new Texture(fullName); 499 467 } 500 468 else 501 469 { 502 tmpResource->param[0] = param0; 503 tmpResource->pointer = new Shader(fullName, NULL); 470 std::vector<char*>::iterator imageDir; 471 for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) 472 { 473 char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1]; 474 sprintf(imgName, "%s%s", *imageDir, fileName); 475 if(isFile(imgName)) 476 { 477 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); 478 tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt()); 479 delete[] imgName; 480 break; 481 } 482 delete[] imgName; 483 } 504 484 } 505 } 506 break; 485 if(!tmpResource) 486 PRINTF(2)("!!Image %s not Found!!\n", fileName); 487 break; 488 #endif /* NO_TEXTURES */ 489 #ifndef NO_SHADERS 490 case SHADER: 491 if(ResourceManager::isFile(fullName)) 492 { 493 if (param0 != MT_NULL) 494 { 495 MultiType param = param0; /// HACK 496 char* secFullName = ResourceManager::getFullName(param.getString()); 497 if (ResourceManager::isFile(secFullName)) 498 { 499 tmpResource->param[0] = secFullName; 500 tmpResource->pointer = new Shader(fullName, secFullName); 501 } 502 delete[] secFullName; 503 } 504 else 505 { 506 tmpResource->param[0] = param0; 507 tmpResource->pointer = new Shader(fullName, NULL); 508 } 509 } 510 break; 507 511 #endif /* NO_SHADERS */ 508 default:509 tmpResource->pointer = NULL;510 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);511 break;512 default: 513 tmpResource->pointer = NULL; 514 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); 515 break; 512 516 } 513 517 if (tmpResource->pointer != NULL) … … 569 573 { 570 574 #ifndef NO_MODEL 571 case OBJ:572 case PRIM:573 delete (Model*)resource->pointer;574 break;575 case MD2:576 delete (MD2Data*)resource->pointer;577 break;575 case OBJ: 576 case PRIM: 577 delete (Model*)resource->pointer; 578 break; 579 case MD2: 580 delete (MD2Data*)resource->pointer; 581 break; 578 582 #endif /* NO_MODEL */ 579 583 #ifndef NO_AUDIO 580 case WAV:581 delete (SoundBuffer*)resource->pointer;582 break;583 case OGG:584 delete (OggPlayer*)resource->pointer;585 break;584 case WAV: 585 delete (SoundBuffer*)resource->pointer; 586 break; 587 case OGG: 588 delete (OggPlayer*)resource->pointer; 589 break; 586 590 #endif /* NO_AUDIO */ 587 591 #ifndef NO_TEXT 588 case TTF:589 delete (Font*)resource->pointer;590 break;592 case TTF: 593 delete (Font*)resource->pointer; 594 break; 591 595 #endif /* NO_TEXT */ 592 596 #ifndef NO_TEXTURES 593 case IMAGE:594 delete (Texture*)resource->pointer;595 break;597 case IMAGE: 598 delete (Texture*)resource->pointer; 599 break; 596 600 #endif /* NO_TEXTURES */ 597 601 #ifndef NO_SHADERS 598 case SHADER:599 delete (Shader*)resource->pointer;600 break;602 case SHADER: 603 delete (Shader*)resource->pointer; 604 break; 601 605 #endif /* NO_SHADERS */ 602 default:603 PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n");604 return false;605 break;606 default: 607 PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); 608 return false; 609 break; 606 610 } 607 611 // deleting the List Entry: … … 662 666 */ 663 667 Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, 664 668 const MultiType& param0, const MultiType& param1, const MultiType& param2) const 665 669 { 666 670 std::vector<Resource*>::const_iterator resource; … … 673 677 { 674 678 #ifndef NO_MODEL 675 case PRIM: 676 case OBJ: 677 if (param0 == MT_NULL) 678 { 679 if ((*resource)->param[0] == 1.0f) 679 case PRIM: 680 case OBJ: 681 if (param0 == MT_NULL) 682 { 683 if ((*resource)->param[0] == 1.0f) 684 match = true; 685 } 686 else if ((*resource)->param[0] == param0.getFloat()) 680 687 match = true; 681 }682 else if ((*resource)->param[0] == param0.getFloat())683 match = true;684 break;685 case MD2:686 if (param0 == MT_NULL)687 {688 if ((*resource)->param[0] == "")688 break; 689 case MD2: 690 if (param0 == MT_NULL) 691 { 692 if ((*resource)->param[0] == "") 693 match = true; 694 } 695 else if ((*resource)->param[0] == ((MultiType)param0).getString()) 689 696 match = true; 690 } 691 else if ((*resource)->param[0] == ((MultiType)param0).getString()) 692 match = true; 693 break; 697 break; 694 698 #endif /* NO_MODEL */ 695 699 #ifndef NO_TEXT 696 case TTF: 697 if (param0 == MT_NULL) 698 { 699 if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE) 700 case TTF: 701 if (param0 == MT_NULL) 702 { 703 if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE) 704 match = true; 705 } 706 else if ((*resource)->param[0] == param0.getInt()) 700 707 match = true; 701 } 702 else if ((*resource)->param[0] == param0.getInt()) 703 match = true; 704 break; 708 break; 705 709 #endif /* NO_TEXT */ 706 710 #ifndef NO_SHADERS 707 case SHADER: 708 if (param0 == MT_NULL) 709 { 710 if ((*resource)->param[0] == "") 711 case SHADER: 712 if (param0 == MT_NULL) 713 { 714 if ((*resource)->param[0] == "") 715 match = true; 716 } 717 else if ((*resource)->param[0] == ((MultiType)param0).getString()) 711 718 match = true; 712 }713 else if ((*resource)->param[0] == ((MultiType)param0).getString())714 match = true;715 719 #endif /* NO_SHADERS */ 716 720 #ifndef NO_TEXTURES 717 case IMAGE: 718 if (param0 == MT_NULL) 719 { 720 if ((*resource)->param[0] == GL_TEXTURE_2D) 721 case IMAGE: 722 if (param0 == MT_NULL) 723 { 724 if ((*resource)->param[0] == GL_TEXTURE_2D) 725 match = true; 726 } 727 else if ((*resource)->param[0] == param0.getInt()) 721 728 match = true; 722 } 723 else if ((*resource)->param[0] == param0.getInt())729 #endif /* NO_TEXTURES */ 730 default: 724 731 match = true; 725 #endif /* NO_TEXTURES */ 726 default: 727 match = true; 728 break; 732 break; 729 733 } 730 734 if (match) … … 751 755 return NULL; 752 756 } 757 758 char* ResourceManager::toResourcableString(unsigned int i) 759 { 760 int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); 761 len += strlen(this->resourceList[i]->name); 762 if (this->resourceList[i]->param[0].getString()) len += strlen(this->resourceList[i]->param[0].getString()) +1; 763 if (this->resourceList[i]->param[1].getString()) len += strlen(this->resourceList[i]->param[1].getString()) +1; 764 if (this->resourceList[i]->param[2].getString()) len += strlen(this->resourceList[i]->param[2].getString()) +1; 765 len += 10; 766 char* tmp = new char[len]; 767 tmp[0] = '\0'; 768 strcat( tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); 769 strcat(tmp,","); 770 strcat (tmp, this->resourceList[i]->name); 771 if (this->resourceList[i]->param[0].getString() && this->resourceList[i]->param[0].getString() != '\0') 772 { 773 strcat(tmp,","); 774 strcat( tmp, this->resourceList[i]->param[0].getString()); 775 } 776 if (this->resourceList[i]->param[1].getString() && this->resourceList[i]->param[1].getString() != '\0') 777 { 778 strcat(tmp,","); 779 strcat( tmp, this->resourceList[i]->param[1].getString()); 780 } 781 if (this->resourceList[i]->param[2].getString() && this->resourceList[i]->param[2].getString() != '\0') 782 { 783 strcat(tmp,","); 784 strcat( tmp, this->resourceList[i]->param[2].getString()); 785 } 786 return tmp; 787 } 788 789 /** 790 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function 791 * @param resourceableString the String to cache the resource from. 792 */ 793 bool ResourceManager::fromResourceableString(const char* resourceableString) 794 { 795 SubString splits(resourceableString, ','); 796 splits.debug(); 797 if (splits.getCount() == 2) 798 this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 799 RP_LEVEL); 800 else if (splits.getCount() == 3) 801 this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 802 RP_LEVEL, splits[2]); 803 else if (splits.getCount() == 4) 804 this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 805 RP_LEVEL, splits[2], splits[3]); 806 else if (splits.getCount() == 5) 807 this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), 808 RP_LEVEL, splits[2], splits[3], splits[4]); 809 } 810 753 811 754 812 /** … … 977 1035 switch((*resource)->prio) 978 1036 { 979 default:980 case RP_NO:981 PRINT(0)("first posibility (0)\n");982 break;983 case RP_LEVEL:984 PRINT(0)("the end of the Level (1)\n");985 break;986 case RP_CAMPAIGN:987 PRINT(0)("the end of the campaign (2)\n");988 break;989 case RP_GAME:990 PRINT(0)("when leaving the game (3)\n");991 break;1037 default: 1038 case RP_NO: 1039 PRINT(0)("first posibility (0)\n"); 1040 break; 1041 case RP_LEVEL: 1042 PRINT(0)("the end of the Level (1)\n"); 1043 break; 1044 case RP_CAMPAIGN: 1045 PRINT(0)("the end of the campaign (2)\n"); 1046 break; 1047 case RP_GAME: 1048 PRINT(0)("when leaving the game (3)\n"); 1049 break; 992 1050 } 993 1051 } … … 1027 1085 */ 1028 1086 const char* ResourceManager::resourceNames[] = 1029 {1087 { 1030 1088 #ifndef NO_MODEL 1031 "ObjectModel", 1032 "PrimitiveModel", 1033 "MD2-Data", 1089 "ObjectModel", 1090 "PrimitiveModel", 1091 "MD2-Data", 1092 #endif 1093 #ifndef NO_TEXT 1094 "Font", 1095 #endif 1096 #ifndef NO_AUDIO 1097 "Wav", 1098 "mp3", 1099 "ogg", 1034 1100 #endif 1035 1101 #ifndef NO_TEXTURES 1036 "Texture", 1037 #endif 1038 #ifndef NO_AUDIO 1039 "Sound", 1040 "Music", 1041 #endif 1042 #ifndef NO_TEXT 1043 "Font", 1102 "Texture", 1044 1103 #endif 1045 1104 #ifndef NO_SHADERS 1046 "Shader",1105 "Shader", 1047 1106 #endif 1048 }; 1107 1108 }; -
trunk/src/util/loading/resource_manager.h
r6646 r6648 120 120 Resource* locateResourceByPointer(const void* pointer) const; 121 121 122 char* toResourcableString(unsigned int i); 123 bool fromResourceableString(const char* resourceableString); 124 122 125 void debug() const; 123 126
Note: See TracChangeset
for help on using the changeset viewer.