Changeset 4597 in orxonox.OLD for orxonox/trunk/src/util/resource_manager.cc
- Timestamp:
- Jun 11, 2005, 12:55:48 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/util/resource_manager.cc
r4534 r4597 1 /* 1 /* 2 2 orxonox - the future of 3D-vertical-scrollers 3 3 … … 47 47 \brief standard constructor 48 48 */ 49 ResourceManager::ResourceManager () 50 { 51 this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager"); 52 this->dataDir = NULL; 53 this->setDataDir("./"); 54 this->imageDirs = new tList<char>(); 55 this->resourceList = new tList<Resource>(); 49 ResourceManager::ResourceManager () 50 { 51 this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager"); 52 this->setName("ResourceManager"); 53 54 this->dataDir = NULL; 55 this->setDataDir("./"); 56 this->imageDirs = new tList<char>(); 57 this->resourceList = new tList<Resource>(); 56 58 } 57 59 … … 62 64 \brief standard destructor 63 65 */ 64 ResourceManager::~ResourceManager (void) 66 ResourceManager::~ResourceManager (void) 65 67 { 66 68 // deleting the Resources-List … … 106 108 107 109 /** 108 \brief checks for the DataDirectory, by looking if 110 \brief checks for the DataDirectory, by looking if 109 111 \param fileInside is inisde?? 110 112 */ … … 117 119 return false; 118 120 } 119 121 120 122 char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1]; 121 123 sprintf(testFile, "%s%s", this->dataDir, fileInside); … … 141 143 char* tmpDir = tmpImageDirs->nextElement(); 142 144 while(tmpDir) 143 144 145 146 147 148 149 150 151 145 { 146 if (!strcmp(tmpDir, imageDir)) 147 { 148 PRINTF(4)("Path %s already loaded\n", imageDir); 149 delete tmpImageDirs; 150 return true; 151 } 152 tmpDir = tmpImageDirs->nextElement(); 153 } 152 154 delete tmpImageDirs; 153 155 … … 185 187 else if (!strcmp(fileName, "cube") || 186 188 !strcmp(fileName, "sphere") || 187 189 !strcmp(fileName, "plane") || 188 190 !strcmp(fileName, "cylinder") || 189 191 !strcmp(fileName, "cone")) … … 203 205 #endif /* NO_TEXT */ 204 206 #ifndef NO_TEXTURES 205 else 207 else 206 208 tmpType = IMAGE; 207 209 #endif /* NO_TEXTURES */ … … 221 223 */ 222 224 void* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio, 223 225 void* param1, void* param2, void* param3) 224 226 { 225 227 // searching if the resource was loaded before. … … 230 232 tmpResource->count++; 231 233 if(tmpResource->prio < prio) 232 234 tmpResource->prio = prio; 233 235 } 234 236 else … … 248 250 // Checking for the type of resource \see ResourceType 249 251 switch(type) 250 252 { 251 253 #ifndef NO_MODEL 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 254 case OBJ: 255 if (param1) 256 tmpResource->modelSize = *(float*)param1; 257 else 258 tmpResource->modelSize = 1.0; 259 260 if(ResourceManager::isFile(fullName)) 261 tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize); 262 else 263 { 264 PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", fullName); 265 tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize); 266 } 267 break; 268 case PRIM: 269 if (param1) 270 tmpResource->modelSize = *(float*)param1; 271 else 272 tmpResource->modelSize = 1.0; 273 274 if (!strcmp(tmpResource->name, "cube")) 275 tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->modelSize); 276 else if (!strcmp(tmpResource->name, "sphere")) 277 tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->modelSize); 278 else if (!strcmp(tmpResource->name, "plane")) 279 tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->modelSize); 280 else if (!strcmp(tmpResource->name, "cylinder")) 281 tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->modelSize); 282 else if (!strcmp(tmpResource->name, "cone")) 283 tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->modelSize); 284 break; 285 case MD2: 286 if(ResourceManager::isFile(fullName)) 287 { 288 if (param1) 289 { 290 tmpResource->skinFileName = new char[strlen((const char*)param1)+1]; 291 strcpy(tmpResource->skinFileName, (const char*) param1); 292 } 293 else 294 tmpResource->skinFileName = NULL; 295 tmpResource->pointer = new MD2Data(fullName, tmpResource->skinFileName); 296 } 297 break; 296 298 #endif /* NO_MODEL */ 297 299 #ifndef NO_TEXT 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 300 case TTF: 301 if (param1) 302 tmpResource->ttfSize = *(int*)param1; 303 else 304 tmpResource->ttfSize = FONT_DEFAULT_SIZE; 305 if (param2) 306 { 307 Vector* tmpVec = (Vector*)param2; 308 tmpResource->ttfColorR = (int)tmpVec->x; 309 tmpResource->ttfColorG = (int)tmpVec->y; 310 tmpResource->ttfColorB = (int)tmpVec->z; 311 } 312 else 313 { 314 tmpResource->ttfColorR = FONT_DEFAULT_COLOR_R; 315 tmpResource->ttfColorG = FONT_DEFAULT_COLOR_G; 316 tmpResource->ttfColorB = FONT_DEFAULT_COLOR_B; 317 } 318 319 if(isFile(fullName)) 320 tmpResource->pointer = new Font(fullName, 321 tmpResource->ttfSize, 322 tmpResource->ttfColorR, 323 tmpResource->ttfColorG, 324 tmpResource->ttfColorB); 325 else 326 PRINTF(2)("Sorry, %s does not exist. Not loading Font\n", fullName); 327 break; 326 328 #endif /* NO_TEXT */ 327 329 #ifndef NO_AUDIO 328 329 330 331 330 case WAV: 331 if(isFile(fullName)) 332 tmpResource->pointer = new SoundBuffer(fullName); 333 break; 332 334 #endif /* NO_AUDIO */ 333 335 #ifndef NO_TEXTURES 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 336 case IMAGE: 337 if(isFile(fullName)) 338 { 339 PRINTF(4)("Image %s resides to %s\n", fileName, fullName); 340 tmpResource->pointer = new Texture(fullName); 341 } 342 else 343 { 344 tIterator<char>* iterator = imageDirs->getIterator(); 345 tmpDir = iterator->nextElement(); 346 //tmpDir = imageDirs->enumerate(); 347 while(tmpDir) 348 { 349 char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; 350 sprintf(imgName, "%s%s", tmpDir, fileName); 351 if(isFile(imgName)) 352 { 353 PRINTF(4)("Image %s resides to %s\n", fileName, imgName); 354 tmpResource->pointer = new Texture(imgName); 355 delete []imgName; 356 break; 357 } 358 delete []imgName; 359 tmpDir = iterator->nextElement(); 360 } 361 delete iterator; 362 } 363 if(!tmpResource) 364 PRINTF(2)("!!Image %s not Found!!\n", fileName); 365 break; 364 366 #endif /* NO_TEXTURES */ 365 366 367 368 369 367 default: 368 tmpResource->pointer = NULL; 369 PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet.!!\n", tmpResource->name); 370 break; 371 } 370 372 if (tmpResource->pointer) 371 373 this->resourceList->add(tmpResource); 372 374 delete []fullName; 373 375 } 374 376 if (tmpResource->pointer) 375 377 return tmpResource->pointer; 376 else 378 else 377 379 { 378 380 PRINTF(2)("Resource %s could not be loaded\n", fileName); … … 413 415 { 414 416 if (resource->count <= 0) 415 416 417 418 417 { 418 // deleting the Resource 419 switch(resource->type) 420 { 419 421 #ifndef NO_MODEL 420 421 422 423 424 425 426 422 case OBJ: 423 case PRIM: 424 delete (Model*)resource->pointer; 425 break; 426 case MD2: 427 delete (MD2Data*)resource->pointer; 428 break; 427 429 #endif /* NO_MODEL */ 428 430 #ifndef NO_AUDIO 429 430 431 431 case WAV: 432 delete (SoundBuffer*)resource->pointer; 433 break; 432 434 #endif /* NO_AUDIO */ 433 435 #ifndef NO_TEXT 434 435 436 436 case TTF: 437 delete (Font*)resource->pointer; 438 break; 437 439 #endif /* NO_TEXT */ 438 440 #ifndef NO_TEXTURES 439 440 441 441 case IMAGE: 442 delete (Texture*)resource->pointer; 443 break; 442 444 #endif /* NO_TEXTURES */ 443 444 445 446 447 448 449 450 451 452 445 default: 446 PRINTF(1)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); 447 return false; 448 break; 449 } 450 // deleting the List Entry: 451 PRINTF(4)("Resource %s safely removed.\n", resource->name); 452 delete []resource->name; 453 this->resourceList->remove(resource); 454 } 453 455 else 454 456 PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); 455 457 } 456 458 else … … 471 473 { 472 474 if (enumRes->prio <= prio) 473 474 475 476 477 475 if (enumRes->count == 0) 476 unload(enumRes, prio); 477 else 478 PRINTF(2)("unable to unload %s because there are still %d references to it\n", 479 enumRes->name, enumRes->count); 478 480 //enumRes = resourceList->nextElement(); 479 481 enumRes = iterator->nextElement(); … … 492 494 */ 493 495 Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, 494 496 void* param1, void* param2, void* param3) 495 497 { 496 498 // Resource* enumRes = resourceList->enumerate(); … … 500 502 { 501 503 if (enumRes->type == type && !strcmp(fileName, enumRes->name)) 502 503 504 505 506 507 504 { 505 bool match = false; 506 bool subMatch = false; 507 508 switch (type) 509 { 508 510 #ifndef NO_MODEL 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 511 case PRIM: 512 case OBJ: 513 if (!param1) 514 { 515 if (enumRes->modelSize == 1.0) 516 match = true; 517 } 518 else if (enumRes->modelSize == *(float*)param1) 519 match = true; 520 break; 521 case MD2: 522 if (!param1) 523 { 524 if (enumRes->skinFileName == NULL) 525 match = true; 526 } 527 else if (!strcmp(enumRes->skinFileName, (const char*) param1)) 528 match = true; 529 break; 528 530 #endif /* NO_MODEL */ 529 531 #ifndef NO_TEXT 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 532 case TTF: 533 if (!param1) 534 { 535 if (enumRes->ttfSize == FONT_DEFAULT_SIZE) 536 subMatch = true; 537 } 538 else if (enumRes->modelSize =- *(int*)param1) 539 subMatch = true; 540 if(subMatch) 541 { 542 Vector* tmpVec = (Vector*)param2; 543 if (!param2) 544 { 545 if(enumRes->ttfColorR == FONT_DEFAULT_COLOR_R && 546 enumRes->ttfColorG == FONT_DEFAULT_COLOR_G && 547 enumRes->ttfColorB == FONT_DEFAULT_COLOR_B ) 548 match = true; 549 } 550 else if (enumRes->ttfColorR == (int)tmpVec->x && 551 enumRes->ttfColorG == (int)tmpVec->y && 552 enumRes->ttfColorB == (int)tmpVec->z ) 553 match = true; 554 } 555 break; 554 556 #endif /* NO_TEXT */ 555 556 557 558 559 560 561 562 563 564 557 default: 558 match = true; 559 break; 560 } 561 if (match) 562 { 563 delete iterator; 564 return enumRes; 565 } 566 } 565 567 enumRes = iterator->nextElement(); 566 568 } … … 582 584 { 583 585 if (pointer == enumRes->pointer) 584 585 586 587 586 { 587 delete iterator; 588 return enumRes; 589 } 588 590 enumRes = iterator->nextElement(); 589 591 } … … 622 624 if (status.st_mode & (S_IFDIR 623 625 #ifndef __WIN32__ 624 626 | S_IFLNK 625 627 #endif 626 627 628 629 630 628 )) 629 { 630 delete tmpDirName; 631 return true; 632 } 631 633 else 632 633 634 635 634 { 635 delete tmpDirName; 636 return false; 637 } 636 638 } 637 639 else … … 653 655 if (!stat(tmpFileName, &status)) 654 656 { 655 if (status.st_mode & (S_IFREG 657 if (status.st_mode & (S_IFREG 656 658 #ifndef __WIN32__ 657 659 | S_IFLNK 658 660 #endif 659 660 661 662 663 661 )) 662 { 663 delete tmpFileName; 664 return true; 665 } 664 666 else 665 666 667 668 669 } 670 else 667 { 668 delete tmpFileName; 669 return false; 670 } 671 } 672 else 671 673 { 672 674 delete tmpFileName; … … 691 693 } 692 694 fclose(stream); 693 694 delete tmpName; 695 696 delete tmpName; 695 697 } 696 698 … … 708 710 } 709 711 710 /** 712 /** 711 713 \param name the Name of the file to check 712 714 \returns The name of the file, including the HomeDir … … 737 739 } 738 740 739 /** 741 /** 740 742 \param fileName the Name of the File to check 741 743 \returns The full name of the file, including the DataDir, and NULL if the file does not exist … … 748 750 749 751 char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) 750 752 + strlen(fileName) + 1]; 751 753 sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); 752 754 if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) … … 790 792 PRINT(0)("Name: %s; References: %d; Type:", enumRes->name, enumRes->count); 791 793 switch (enumRes->type) 792 793 794 795 796 797 798 799 800 801 802 803 804 805 794 { 795 case OBJ: 796 PRINT(0)("ObjectModel\n"); 797 break; 798 case PRIM: 799 PRINT(0)("PrimitiveModel\n"); 800 break; 801 case IMAGE: 802 PRINT(0)("ImageFile (Texture)\n"); 803 break; 804 default: 805 PRINT(0)("SoundFile\n"); 806 break; 807 } 806 808 PRINT(0)("gets deleted at "); 807 809 switch(enumRes->prio) 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 810 { 811 default: 812 case RP_NO: 813 PRINT(0)("first posibility (0)\n"); 814 break; 815 case RP_LEVEL: 816 PRINT(0)("the end of the Level (1)\n"); 817 break; 818 case RP_CAMPAIGN: 819 PRINT(0)("the end of the campaign (2)\n"); 820 break; 821 case RP_GAME: 822 PRINT(0)("when leaving the game (3)\n"); 823 break; 824 } 823 825 enumRes = iterator->nextElement(); 824 826 }
Note: See TracChangeset
for help on using the changeset viewer.