00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "OSDLResourceManager.h"
00028
00029 #include "OSDLMusic.h"
00030 #include "OSDLSound.h"
00031 #include "OSDLGLTexture.h"
00032
00033
00034 using namespace Ceylan ;
00035 using namespace Ceylan::Log ;
00036
00037
00038 using namespace OSDL ;
00039 using namespace OSDL::Data ;
00040 using namespace OSDL::Video::TwoDimensional ;
00041
00042 using std::string ;
00043 using std::list ;
00044 using std::map ;
00045
00046
00047
00048 #ifdef OSDL_USES_CONFIG_H
00049 #include <OSDLConfig.h>
00050 #endif // OSDL_USES_CONFIG_H
00051
00052
00053
00054 #if OSDL_DEBUG_RESOURCE_MANAGER
00055
00056 #define OSDL_DATA_LOG(message) send( message )
00057
00058 #else // OSDL_DEBUG_RESOURCE_MANAGER
00059
00060 #define OSDL_DATA_LOG(message)
00061
00062 #endif // OSDL_DEBUG_RESOURCE_MANAGER
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 Data::ResourceManagerException::ResourceManagerException(
00091 const string & reason ) :
00092 DataException( reason )
00093 {
00094
00095 }
00096
00097
00098 Data::ResourceManagerException::~ResourceManagerException() throw()
00099 {
00100
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 typedef std::list<Ceylan::XML::XMLParser::XMLTree * > XMLSubtreeList ;
00114
00115
00116
00117 Data::ResourceManager::ResourceManager( const string & resourceMapFilename ):
00118 _maxID( 0 )
00119 {
00120
00121 send( "Creating a ResourceManager based on the resource map in file '"
00122 + resourceMapFilename + "'." ) ;
00123
00124 Ceylan::XML::XMLParser * resourceParser =
00125 new Ceylan::XML::XMLParser( resourceMapFilename ) ;
00126
00127
00128
00129 resourceParser->loadFromFile() ;
00130
00131
00132
00133 Ceylan::XML::XMLParser::XMLTree & root = resourceParser->getXMLTree() ;
00134
00135
00136
00137
00138 const XMLSubtreeList & sons = root.getSons() ;
00139
00140 for ( XMLSubtreeList::const_iterator it = sons.begin();
00141 it != sons.end(); it++ )
00142 registerResource( *(*it) ) ;
00143
00144 send( "All resources inserted, initial state is: " + toString() ) ;
00145
00146 delete resourceParser ;
00147
00148 dropIdentifier() ;
00149
00150 }
00151
00152
00153
00154 Data::ResourceManager::~ResourceManager() throw()
00155 {
00156
00157
00158
00159
00160
00161
00162
00163
00164 send( "Deleting ResourceManager, whose final state was: "
00165 + toString() ) ;
00166
00167 }
00168
00169
00170
00171 Audio::MusicCountedPtr Data::ResourceManager::getMusic( Ceylan::ResourceID id )
00172 {
00173
00174 map<Ceylan::ResourceID, Audio::MusicCountedPtr>::iterator it =
00175 _musicMap.find( id ) ;
00176
00177 if ( it == _musicMap.end() )
00178 throw ResourceManagerException( "ResourceManager::getMusic: ID #"
00179 + Ceylan::toString( id ) + " could not be found." ) ;
00180
00181 Audio::MusicCountedPtr res = (*it).second ;
00182
00183
00184 res->load() ;
00185
00186 return res ;
00187
00188 }
00189
00190
00191
00192 Audio::MusicCountedPtr Data::ResourceManager::getMusic(
00193 const string & musicPath )
00194 {
00195
00196 map<Ceylan::ResourceID, Audio::MusicCountedPtr>::iterator it =
00197 _musicMap.find( getIDForPath(musicPath) ) ;
00198
00199 if ( it == _musicMap.end() )
00200 throw ResourceManagerException(
00201 "ResourceManager::getMusic: music path '" + musicPath
00202 + "' could not be found." ) ;
00203
00204 Audio::MusicCountedPtr res = (*it).second ;
00205
00206
00207 res->load() ;
00208
00209 return res ;
00210
00211 }
00212
00213
00214
00215
00216 Audio::SoundCountedPtr Data::ResourceManager::getSound( Ceylan::ResourceID id )
00217 {
00218
00219
00220 map<Ceylan::ResourceID, Audio::SoundCountedPtr>::iterator it =
00221 _soundMap.find( id ) ;
00222
00223 if ( it == _soundMap.end() )
00224 throw ResourceManagerException( "ResourceManager::getSound: ID #"
00225 + Ceylan::toString( id ) + " could not be found." ) ;
00226
00227 Audio::SoundCountedPtr res = (*it).second ;
00228
00229
00230 res->load() ;
00231
00232 return res ;
00233
00234 }
00235
00236
00237
00238 Audio::SoundCountedPtr Data::ResourceManager::getSound(
00239 const string & soundPath )
00240 {
00241
00242 map<Ceylan::ResourceID, Audio::SoundCountedPtr>::iterator it =
00243 _soundMap.find( getIDForPath(soundPath) ) ;
00244
00245 if ( it == _soundMap.end() )
00246 throw ResourceManagerException(
00247 "ResourceManager::getSound: sound path '" + soundPath
00248 + "' could not be found." ) ;
00249
00250 Audio::SoundCountedPtr res = (*it).second ;
00251
00252
00253 res->load() ;
00254
00255 return res ;
00256
00257 }
00258
00259
00260
00261
00262 Video::TwoDimensional::ImageCountedPtr Data::ResourceManager::getImage(
00263 Ceylan::ResourceID id )
00264 {
00265
00266 map<Ceylan::ResourceID,
00267 Video::TwoDimensional::ImageCountedPtr>::iterator it =
00268 _imageMap.find( id ) ;
00269
00270 if ( it == _imageMap.end() )
00271 throw ResourceManagerException( "ResourceManager::getImage: ID #"
00272 + Ceylan::toString( id ) + " could not be found." ) ;
00273
00274 Video::TwoDimensional::ImageCountedPtr res = (*it).second ;
00275
00276
00277 res->load() ;
00278
00279 return res ;
00280
00281 }
00282
00283
00284
00285 Video::TwoDimensional::ImageCountedPtr Data::ResourceManager::getImage(
00286 const string & imagePath )
00287 {
00288
00289 map<Ceylan::ResourceID,
00290 Video::TwoDimensional::ImageCountedPtr>::iterator it =
00291 _imageMap.find( getIDForPath(imagePath) ) ;
00292
00293 if ( it == _imageMap.end() )
00294 throw ResourceManagerException( "ResourceManager::getImage: image '"
00295 + imagePath + "' could not be found." ) ;
00296
00297 Video::TwoDimensional::ImageCountedPtr res = (*it).second ;
00298
00299
00300 res->load() ;
00301
00302 return res ;
00303
00304 }
00305
00306
00307
00308 Video::OpenGL::TextureCountedPtr Data::ResourceManager::getTexture(
00309 Ceylan::ResourceID id, bool uploadWanted )
00310 {
00311
00312 map<Ceylan::ResourceID,Video::OpenGL::TextureCountedPtr>::iterator it =
00313 _textureMap.find( id ) ;
00314
00315 if ( it == _textureMap.end() )
00316 {
00317
00318 LogPlug::fatal( "Data::ResourceManager::getTexture for ID failed." ) ;
00319
00320 throw ResourceManagerException( "ResourceManager::getTexture: ID #"
00321 + Ceylan::toString( id ) + " could not be found." ) ;
00322
00323 }
00324
00325
00326 Video::OpenGL::TextureCountedPtr res = (*it).second ;
00327
00328
00329 res->load() ;
00330
00331
00332 if ( uploadWanted && ( ! res->wasUploaded() ) )
00333 res->upload() ;
00334
00335 return res ;
00336
00337 }
00338
00339
00340
00341 Video::OpenGL::TextureCountedPtr Data::ResourceManager::getTexture(
00342 const string & texturePath, bool uploadWanted )
00343 {
00344
00345 map<Ceylan::ResourceID,Video::OpenGL::TextureCountedPtr>::iterator it =
00346 _textureMap.find( getIDForPath(texturePath) ) ;
00347
00348 if ( it == _textureMap.end() )
00349 {
00350
00351 LogPlug::fatal( "Data::ResourceManager::getTexture for file '"
00352 + texturePath + "' failed." ) ;
00353
00354 throw ResourceManagerException( "ResourceManager::getTexture: texture '"
00355 + texturePath + "' could not be found." ) ;
00356
00357 }
00358
00359
00360 Video::OpenGL::TextureCountedPtr res = (*it).second ;
00361
00362
00363 res->load() ;
00364
00365
00366 if ( uploadWanted && ( ! res->wasUploaded() ) )
00367 res->upload() ;
00368
00369 return res ;
00370
00371 }
00372
00373
00374
00375 std::pair<Video::OpenGL::TextureCountedPtr,Ceylan::ResourceID>
00376 Data::ResourceManager::getTextureFrom(
00377 Video::Surface & sourceSurface,
00378 Video::OpenGL::GLTexture::TextureFlavour flavour,
00379 bool uploadWanted )
00380 {
00381
00382 Video::OpenGL::TextureCountedPtr resPtr = new Video::OpenGL::GLTexture(
00383 sourceSurface, flavour ) ;
00384
00385
00386 if ( uploadWanted && ( ! resPtr->wasUploaded() ) )
00387 resPtr->upload() ;
00388
00389 _maxID++ ;
00390
00391 Ceylan::ResourceID resID = _maxID ;
00392
00393 _textureMap.insert( std::pair<Ceylan::ResourceID,
00394 Video::OpenGL::TextureCountedPtr>( resID, resPtr ) ) ;
00395
00396 return std::pair<Video::OpenGL::TextureCountedPtr,Ceylan::ResourceID>(
00397 resPtr, resID ) ;
00398
00399 }
00400
00401
00402
00403 Video::TwoDimensional::Text::TrueTypeFontCountedPtr
00404 Data::ResourceManager::getTrueTypeFont( Ceylan::ResourceID id,
00405 Text::PointSize pointSize )
00406 {
00407
00408 map<ResourceID,Text::TrueTypeFontCountedPtr>::iterator it =
00409 _truetypeFontMap.find( id ) ;
00410
00411 if ( it == _truetypeFontMap.end() )
00412 throw ResourceManagerException( "ResourceManager::getTrueTypeFont: ID #"
00413 + Ceylan::toString( id ) + " could not be found." ) ;
00414
00415 Video::TwoDimensional::Text::TrueTypeFontCountedPtr res = (*it).second ;
00416
00417
00418 res->load( pointSize ) ;
00419
00420 return res ;
00421
00422 }
00423
00424
00425
00426 Video::TwoDimensional::Text::TrueTypeFontCountedPtr
00427 Data::ResourceManager::getTrueTypeFont( const string & fontPath,
00428 Text::PointSize pointSize )
00429 {
00430
00431 map<ResourceID,Text::TrueTypeFontCountedPtr>::iterator it =
00432 _truetypeFontMap.find( getIDForPath(fontPath) ) ;
00433
00434 if ( it == _truetypeFontMap.end() )
00435 throw ResourceManagerException( "ResourceManager::getTrueTypeFont: "
00436 "font '" + fontPath + "' could not be found." ) ;
00437
00438 Text::TrueTypeFontCountedPtr res = (*it).second ;
00439
00440
00441 res->load( pointSize ) ;
00442
00443 return res ;
00444
00445 }
00446
00447
00448
00449 void Data::ResourceManager::discardTexture( Ceylan::ResourceID textureId )
00450 {
00451
00452 map<Ceylan::ResourceID,Video::OpenGL::TextureCountedPtr>::iterator it =
00453 _textureMap.find( textureId ) ;
00454
00455 if ( it == _textureMap.end() )
00456 throw ResourceManagerException( "ResourceManager::discardTexture: "
00457 "no texture with ID #" + Ceylan::toString( textureId )
00458 + " found." ) ;
00459
00460
00461 _textureMap.erase( it ) ;
00462
00463 }
00464
00465
00466
00467 void Data::ResourceManager::purge()
00468 {
00469
00470 for ( map<Ceylan::ResourceID, Audio::MusicCountedPtr>::iterator it =
00471 _musicMap.begin(); it != _musicMap.end(); it++ )
00472 {
00473
00474 if ( ((*it).second).getReferenceCount() == 1 )
00475 ((*it).second)->unload() ;
00476
00477 }
00478
00479
00480 for ( map<Ceylan::ResourceID, Audio::SoundCountedPtr>::iterator it =
00481 _soundMap.begin(); it != _soundMap.end(); it++ )
00482 {
00483
00484 if ( ((*it).second).getReferenceCount() == 1 )
00485 ((*it).second)->unload() ;
00486
00487 }
00488
00489
00490 for ( map<Ceylan::ResourceID,
00491 Video::TwoDimensional::ImageCountedPtr>::iterator it =
00492 _imageMap.begin(); it != _imageMap.end(); it++ )
00493 {
00494
00495 if ( ((*it).second).getReferenceCount() == 1 )
00496 ((*it).second)->unload() ;
00497
00498 }
00499
00500
00501 for ( map<Ceylan::ResourceID,
00502 Video::OpenGL::TextureCountedPtr>::iterator it =
00503 _textureMap.begin(); it != _textureMap.end(); it++ )
00504 {
00505
00506 if ( ((*it).second).getReferenceCount() == 1 )
00507 ((*it).second)->unload() ;
00508
00509 }
00510
00511
00512 for ( map<Ceylan::ResourceID,
00513 Video::TwoDimensional::Text::TrueTypeFontCountedPtr>::iterator it =
00514 _truetypeFontMap.begin(); it != _truetypeFontMap.end(); it++ )
00515 {
00516
00517 if ( ((*it).second).getReferenceCount() == 1 )
00518 ((*it).second)->unload() ;
00519
00520 }
00521
00522
00523
00524
00525
00526
00527
00528 }
00529
00530
00531
00532 void Data::ResourceManager::displayLoadedResources()
00533 {
00534
00535 Ceylan::VerbosityLevels level = Ceylan::high ;
00536
00537 list<string> loadedList ;
00538
00539 for ( map<Ceylan::ResourceID,
00540 Audio::MusicCountedPtr>::const_iterator it = _musicMap.begin();
00541 it != _musicMap.end(); it++ )
00542 {
00543
00544 if ( ((*it).second)->hasContent() )
00545 loadedList.push_back( "ID #" + Ceylan::toString( (*it).first )
00546 + ": " + (*it).second->toString(level)
00547 + " and whose reference count is "
00548 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00549
00550 }
00551
00552
00553 for ( map<Ceylan::ResourceID,
00554 Audio::SoundCountedPtr>::const_iterator it = _soundMap.begin();
00555 it != _soundMap.end(); it++ )
00556 {
00557
00558 if ( ((*it).second)->hasContent() )
00559 loadedList.push_back( "ID #" + Ceylan::toString( (*it).first )
00560 + ": " + (*it).second->toString(level)
00561 + " and whose reference count is "
00562 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00563
00564 }
00565
00566
00567 for ( map<Ceylan::ResourceID,
00568 Video::TwoDimensional::ImageCountedPtr>::const_iterator it =
00569 _imageMap.begin(); it != _imageMap.end(); it++ )
00570 {
00571
00572 if ( ((*it).second)->hasContent() )
00573 loadedList.push_back( "ID #" + Ceylan::toString( (*it).first )
00574 + ": " + (*it).second->toString(level)
00575 + " and whose reference count is "
00576 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00577
00578 }
00579
00580
00581 for ( map<Ceylan::ResourceID,
00582 Video::OpenGL::TextureCountedPtr>::const_iterator it =
00583 _textureMap.begin(); it != _textureMap.end(); it++ )
00584 {
00585
00586 if ( ((*it).second)->hasContent() )
00587 loadedList.push_back( "ID #" + Ceylan::toString( (*it).first )
00588 + ": " + (*it).second->toString(level)
00589 + " and whose reference count is "
00590 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00591
00592 }
00593
00594
00595 for ( map<Ceylan::ResourceID,
00596 Text::TrueTypeFontCountedPtr>::const_iterator it =
00597 _truetypeFontMap.begin(); it != _truetypeFontMap.end(); it++ )
00598 {
00599
00600 if ( ((*it).second)->hasContent() )
00601 loadedList.push_back( "ID #" + Ceylan::toString( (*it).first )
00602 + ": " + (*it).second->toString(level)
00603 + " and whose reference count is "
00604 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00605
00606 }
00607
00608 if ( loadedList.empty() )
00609 {
00610
00611 Ceylan::checkpoint( "Resource manager: no resource loaded." ) ;
00612
00613 }
00614 else
00615 {
00616
00617 Ceylan::checkpoint( "Resource manager: "
00618 + Ceylan::formatStringList( loadedList,
00619 TextDisplayable::rawText ) ) ;
00620
00621 }
00622
00623 }
00624
00625
00626
00627 const string Data::ResourceManager::toString(
00628 Ceylan::VerbosityLevels level ) const
00629 {
00630
00631 list<string> maps ;
00632
00633 Ceylan::Uint32 size = _musicMap.size() ;
00634
00635 string temp ;
00636
00637 if ( size != 0 )
00638 {
00639
00640 temp = "Number of musics managed: " + Ceylan::toString( size ) ;
00641
00642 if ( level != Ceylan::low )
00643 {
00644
00645 list<string> musics ;
00646
00647 for ( map<Ceylan::ResourceID,
00648 Audio::MusicCountedPtr>::const_iterator it = _musicMap.begin();
00649 it != _musicMap.end(); it++ )
00650 {
00651
00652 musics.push_back( (*it).second->toString(level)
00653 + " (ID #" + Ceylan::toString( (*it).first )
00654 + " and whose reference count is "
00655 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00656
00657 }
00658
00659 temp += Ceylan::formatStringList( musics,
00660 false, 2 ) ;
00661
00662 }
00663
00664
00665 maps.push_back( temp ) ;
00666
00667 }
00668 else
00669 {
00670
00671 maps.push_back( "No music is managed" ) ;
00672
00673 }
00674
00675
00676 size = _soundMap.size() ;
00677
00678 if ( size != 0 )
00679 {
00680
00681 temp = "Number of sounds managed: " + Ceylan::toString( size ) ;
00682
00683 if ( level != Ceylan::low )
00684 {
00685
00686 list<string> sounds ;
00687
00688 for ( map<Ceylan::ResourceID,
00689 Audio::SoundCountedPtr>::const_iterator it = _soundMap.begin();
00690 it != _soundMap.end(); it++ )
00691 {
00692
00693 sounds.push_back( (*it).second->toString(level)
00694 + " (ID #" + Ceylan::toString( (*it).first )
00695 + " and whose reference count is "
00696 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00697
00698 }
00699
00700 temp += Ceylan::formatStringList( sounds,
00701 false, 2 ) ;
00702
00703 }
00704
00705
00706 maps.push_back( temp ) ;
00707
00708 }
00709 else
00710 {
00711
00712 maps.push_back( "No sound is managed" ) ;
00713
00714 }
00715
00716
00717 size = _imageMap.size() ;
00718
00719 if ( size != 0 )
00720 {
00721
00722 temp = "Number of images managed: " + Ceylan::toString( size ) ;
00723
00724 if ( level != Ceylan::low )
00725 {
00726
00727 list<string> images ;
00728
00729 for ( map<Ceylan::ResourceID,
00730 Video::TwoDimensional::ImageCountedPtr>::const_iterator it =
00731 _imageMap.begin(); it != _imageMap.end(); it++ )
00732 {
00733
00734 images.push_back( (*it).second->toString(level)
00735 + " (ID #" + Ceylan::toString( (*it).first )
00736 + " and whose reference count is "
00737 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00738
00739 }
00740
00741 temp += Ceylan::formatStringList( images,
00742 false, 2 ) ;
00743
00744 }
00745
00746
00747 maps.push_back( temp ) ;
00748
00749 }
00750 else
00751 {
00752
00753 maps.push_back( "No image is managed" ) ;
00754
00755 }
00756
00757
00758 size = _textureMap.size() ;
00759
00760 if ( size != 0 )
00761 {
00762
00763 temp = "Number of textures managed: " + Ceylan::toString( size ) ;
00764
00765 if ( level != Ceylan::low )
00766 {
00767
00768 list<string> textures ;
00769
00770 for ( map<Ceylan::ResourceID,
00771 Video::OpenGL::TextureCountedPtr>::const_iterator it =
00772 _textureMap.begin(); it != _textureMap.end(); it++ )
00773 {
00774
00775 textures.push_back( (*it).second->toString(level)
00776 + " (ID #" + Ceylan::toString( (*it).first )
00777 + " and whose reference count is "
00778 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00779
00780 }
00781
00782 temp += Ceylan::formatStringList( textures,
00783 false, 2 ) ;
00784
00785 }
00786
00787
00788 maps.push_back( temp ) ;
00789
00790 }
00791 else
00792 {
00793
00794 maps.push_back( "No texture is managed" ) ;
00795
00796 }
00797
00798
00799 size = _truetypeFontMap.size() ;
00800
00801 if ( size != 0 )
00802 {
00803
00804 temp = "Number of TrueType font managed: " + Ceylan::toString( size ) ;
00805
00806 if ( level != Ceylan::low )
00807 {
00808
00809 list<string> trueTypeFonts ;
00810
00811 for ( map<Ceylan::ResourceID,
00812 Text::TrueTypeFontCountedPtr>::const_iterator it =
00813 _truetypeFontMap.begin(); it != _truetypeFontMap.end();
00814 it++ )
00815 {
00816
00817 trueTypeFonts.push_back( (*it).second->toString(level)
00818 + " (ID #" + Ceylan::toString( (*it).first )
00819 + " and whose reference count is "
00820 + Ceylan::toString( (*it).second.getReferenceCount() ) ) ;
00821
00822 }
00823
00824 temp += Ceylan::formatStringList( trueTypeFonts,
00825 false, 2 ) ;
00826
00827 }
00828
00829
00830 maps.push_back( temp ) ;
00831
00832 }
00833 else
00834 {
00835
00836 maps.push_back( "No texture is managed" ) ;
00837
00838 }
00839
00840
00841 size = _reverseMap.size() ;
00842
00843 if ( size != 0 )
00844 {
00845
00846 temp = "Number of entries in reverse map: " + Ceylan::toString( size ) ;
00847
00848 if ( level != Ceylan::low )
00849 {
00850
00851 list<string> entries ;
00852
00853 for ( map<std::string,Ceylan::ResourceID>::const_iterator it =
00854 _reverseMap.begin(); it != _reverseMap.end(); it++ )
00855 {
00856
00857 entries.push_back( "Resource path '" + (*it).first
00858 + "' corresponds to ID #"
00859 + Ceylan::toString( (*it).second ) ) ;
00860
00861 }
00862
00863 temp += Ceylan::formatStringList( entries,
00864 false, 2 ) ;
00865
00866 }
00867
00868
00869 maps.push_back( temp ) ;
00870
00871 }
00872 else
00873 {
00874
00875 maps.push_back( "No entry in reverse resource map" ) ;
00876
00877 }
00878
00879 maps.push_back( "Maximum resource ID currently allocated: "
00880 + Ceylan::toString( _maxID ) ) ;
00881
00882 return "ResourceManager state: " + Ceylan::formatStringList( maps ) ;
00883
00884 }
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900 void Data::ResourceManager::registerResource(
00901 const Ceylan::XML::XMLParser::XMLTree & resourceXMLEntry )
00902 {
00903
00904 const Ceylan::XML::XMLMarkup * const resourceMarkup =
00905 dynamic_cast<const Ceylan::XML::XMLMarkup*>(
00906 & resourceXMLEntry.getContentAsConst() ) ;
00907
00908 if ( resourceMarkup == 0 )
00909 throw ResourceManagerException( "ResourceManager::registerResource: "
00910 "expected resource mark-up not found." ) ;
00911
00912 Ceylan::ResourceID id = static_cast<Ceylan::ResourceID>(
00913 Ceylan::stringToUnsignedLong(
00914 resourceMarkup->getExistingAttribute( "id" ) ) ) ;
00915
00916
00917
00918 if ( id > _maxID )
00919 _maxID = id ;
00920
00921 const XMLSubtreeList & resourceSettingEntries = resourceXMLEntry.getSons() ;
00922
00923 string resourcePath, resourceStringifiedType ;
00924
00925 const Ceylan::XML::XMLMarkup * currentMarkup ;
00926
00927
00928 for ( XMLSubtreeList::const_iterator it = resourceSettingEntries.begin();
00929 it != resourceSettingEntries.end(); it++ )
00930 {
00931
00932 currentMarkup = dynamic_cast<const Ceylan::XML::XMLMarkup*>(
00933 & (*it)->getContentAsConst() ) ;
00934
00935 if ( currentMarkup == 0 )
00936 throw ResourceManagerException(
00937 "ResourceManager::registerResource: "
00938 "expected setting mark-up not found." ) ;
00939
00940 if ( currentMarkup->getMarkupName() == "resource_path" )
00941 {
00942
00943 const Ceylan::XML::XMLText * resourceText =
00944 dynamic_cast<const Ceylan::XML::XMLText*>(
00945 & (*it)->getSons().front()->getContent() ) ;
00946
00947 if ( resourceText == 0 )
00948 throw ResourceManagerException(
00949 "ResourceManager::registerResource: "
00950 "expected resource path not found." ) ;
00951
00952 resourcePath = resourceText->toString() ;
00953
00954 }
00955 else if ( currentMarkup->getMarkupName() == "content_type" )
00956 {
00957
00958 const Ceylan::XML::XMLText * resourceContentType =
00959 dynamic_cast<const Ceylan::XML::XMLText*>(
00960 & (*it)->getSons().front()->getContent() ) ;
00961
00962 if ( resourceContentType == 0 )
00963 throw ResourceManagerException(
00964 "ResourceManager::registerResource: "
00965 "expected resource content type not found." ) ;
00966
00967 resourceStringifiedType = resourceContentType->toString() ;
00968
00969 }
00970 else
00971 {
00972
00973 LogPlug::warning( "ResourceManager::registerResource: "
00974 "mark-up setting '" + currentMarkup->getMarkupName()
00975 + "' ignored." ) ;
00976 }
00977
00978
00979 }
00980
00981 if ( resourcePath.empty() )
00982 throw ResourceManagerException(
00983 "ResourceManager::registerResource: "
00984 "no resource path could be found." ) ;
00985
00986 if ( resourceStringifiedType.empty() )
00987 throw ResourceManagerException(
00988 "ResourceManager::registerResource: "
00989 "no resource content type could be found." ) ;
00990
00991
00992
00993 _reverseMap.insert( std::pair<std::string,Ceylan::ResourceID>(
00994 resourcePath, id ) ) ;
00995
00996
00997 ContentType resourceType = GetContentType( resourceStringifiedType,
00998 false ) ;
00999
01000 if ( resourceType == Data::unknown )
01001 throw ResourceManagerException(
01002 "ResourceManager::registerResource: the resource whose path is '"
01003 + resourcePath + "' is not of a known content type. "
01004 "Forgot to post-process it, or to remove this file?" ) ;
01005
01006 if ( resourceType == Data::music )
01007 {
01008
01009 Audio::MusicCountedPtr newMusicPtr =
01010 new Audio::Music( resourcePath, false ) ;
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028 _musicMap.insert(
01029 std::pair<Ceylan::ResourceID,Audio::MusicCountedPtr>(
01030 id, newMusicPtr ) ) ;
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040 }
01041 else if ( resourceType == Data::sound )
01042 {
01043
01044 Audio::SoundCountedPtr newSoundPtr =
01045 new Audio::Sound( resourcePath, false ) ;
01046
01047 _soundMap.insert(
01048 std::pair<Ceylan::ResourceID,Audio::SoundCountedPtr>(
01049 id, newSoundPtr ) ) ;
01050
01051 }
01052 else if ( resourceType == Data::image )
01053 {
01054
01055
01056 bool convertToDisplayFormat = false ;
01057 bool convertWithAlpha = true ;
01058
01059
01060
01061 Video::TwoDimensional::ImageCountedPtr newImagePtr =
01062 new Video::TwoDimensional::Image( resourcePath,
01063 false,
01064 convertToDisplayFormat,
01065 convertWithAlpha ) ;
01066
01067 _imageMap.insert( std::pair<Ceylan::ResourceID,
01068 Video::TwoDimensional::ImageCountedPtr>( id, newImagePtr ) ) ;
01069
01070
01071 }
01072 else if ( resourceType == Data::texture_2D )
01073 {
01074
01075 Video::OpenGL::TextureCountedPtr newTexturePtr =
01076 new Video::OpenGL::GLTexture( resourcePath,
01077 Video::OpenGL::GLTexture::For2D,
01078 false ) ;
01079
01080 _textureMap.insert( std::pair<Ceylan::ResourceID,
01081 Video::OpenGL::TextureCountedPtr>( id, newTexturePtr ) ) ;
01082
01083 }
01084 else if ( resourceType == Data::texture_3D )
01085 {
01086
01087 Video::OpenGL::TextureCountedPtr newTexturePtr =
01088 new Video::OpenGL::GLTexture( resourcePath,
01089 Video::OpenGL::GLTexture::For3D,
01090 false ) ;
01091
01092 _textureMap.insert( std::pair<Ceylan::ResourceID,
01093 Video::OpenGL::TextureCountedPtr>( id, newTexturePtr ) ) ;
01094
01095 }
01096 else if ( resourceType == Data::ttf_font )
01097 {
01098
01099
01100
01101
01102
01103
01104 Text::TrueTypeFontCountedPtr newTrueTypeFontPtr =
01105 new Video::TwoDimensional::Text::TrueTypeFont(
01106 resourcePath,
01107 0,
01108 false,
01109 Text::Font::GlyphCached,
01110 false ) ;
01111
01112 _truetypeFontMap.insert( std::pair<Ceylan::ResourceID,
01113 Video::TwoDimensional::Text::TrueTypeFontCountedPtr>( id,
01114 newTrueTypeFontPtr ) ) ;
01115
01116 }
01117 else
01118 {
01119
01120 string message = "Resource in '" + resourcePath + "' has for type '"
01121 + resourceStringifiedType + "', which is not managed." ;
01122
01123 send( message ) ;
01124
01125 LogPlug::warning( message ) ;
01126
01127 return ;
01128
01129 }
01130
01131
01132
01133
01134
01135
01136 }
01137
01138
01139
01140 Ceylan::ResourceID Data::ResourceManager::getIDForPath(
01141 const string & resourcePath, bool emergencyStopInNotFound ) const
01142 {
01143
01144 map<string,Ceylan::ResourceID>::const_iterator it =
01145 _reverseMap.find( resourcePath ) ;
01146
01147 if ( it == _reverseMap.end() )
01148 {
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162 string message = "getIDForPath: failed to look-up resource file '"
01163 + resourcePath + "'." ;
01164
01165 LogPlug::error( message ) ;
01166
01167 if ( emergencyStopInNotFound )
01168 Ceylan::emergencyShutdown( message ) ;
01169 else
01170 throw ResourceManagerException( "ResourceManager::getIDForPath: "
01171 "resource path '" + resourcePath
01172 + "' could not be found in archive." ) ;
01173
01174 }
01175
01176 return (*it).second ;
01177
01178 }
01179
01180
01181
01182 ContentType Data::ResourceManager::GetContentType(
01183 const std::string & stringifiedType, bool throwIfNotMatched )
01184 {
01185
01186 if ( stringifiedType == "text" )
01187 return Data::text ;
01188 else if ( stringifiedType == "data" )
01189 return Data::data ;
01190 else if ( stringifiedType == "sound" )
01191 return Data::sound ;
01192 else if ( stringifiedType == "music" )
01193 return Data::music ;
01194 else if ( stringifiedType == "image" )
01195 return Data::image ;
01196 else if ( stringifiedType == "texture_2D" )
01197 return Data::texture_2D ;
01198 else if ( stringifiedType == "texture_3D" )
01199 return Data::texture_3D ;
01200 else if ( stringifiedType == "ttf_font" )
01201 return Data::ttf_font ;
01202 else
01203 {
01204
01205
01206 if ( throwIfNotMatched )
01207 throw ResourceManagerException( "ResourceManager::GetContentType: "
01208 "the string '" + stringifiedType
01209 + "' could not be interpreted as a content type." ) ;
01210 else
01211 return Data::unknown ;
01212
01213 }
01214
01215 }
01216
01217