/******************************************************************************* * * W�ms of Prey - game data loader class * * $Id: loader.cpp,v 1.13 2005/12/14 21:38:03 chfreund Exp $ * *******************************************************************************/ #include "loader.hpp" #include "global.hpp" #include "video.hpp" #include "wopsettings.hpp" /*******************************************************************************/ //! init loader, pass argv[0] from commandline to init PhysFs Loader::Loader(String argv0) { #ifdef USE_PHYSFS // init physfs if(!PHYSFS_init( argv0 )) { LOG( 1 ) INFO( "Loader::Loader: PhysFs init failed!\n" ); return; } // add default search paths const char *dirSep = PHYSFS_getDirSeparator(); const char *baseDir = PHYSFS_getBaseDir(); char physfsPath[256]; bool addSucceeded; // add "../data" to the search path snprintf(physfsPath,256, "%s..%sdata", baseDir, dirSep ); addSucceeded = PHYSFS_addToSearchPath( physfsPath, 0); if(addSucceeded) { LOG( 2 ) INFO( "Loader::Loader: search path '%s' added\n", physfsPath ); } else { LOG( 2 ) INFO( "Loader::Loader: search path '%s' skipped\n", physfsPath ); } // add "./data.zip" to the search path snprintf(physfsPath,256, "%sdata.zip", baseDir ); PHYSFS_addToSearchPath( physfsPath, 0); if(addSucceeded) { LOG( 2 ) INFO( "Loader::Loader: search path '%s' added\n", physfsPath ); } else { LOG( 2 ) INFO( "Loader::Loader: search path '%s' skipped\n", physfsPath ); } // TODO add path from config file #endif } /*******************************************************************************/ //! destruct loader, deinit PhysFs Loader::~Loader() { #ifdef USE_PHYSFS LOG( 2 ) INFO( "main: Shutting down PhysFs\n" ); PHYSFS_deinit(); #endif } #ifdef USE_PHYSFS /*******************************************************************************/ //! open a physfs file SDL_RWops *Loader::getRWops(String filename, bool abortOnError) { SDL_RWops *rw = PHYSFSRWOPS_openRead( filename ); if(abortOnError) { ASSERT( rw!=NULL, "Loader::getRWops: Couldn't open file '%s'\n", filename.getString() ); } return rw; } #endif /*******************************************************************************/ // shared loading routines /*******************************************************************************/ //! load a font TTF file TTF_Font *Loader::getFont(String filename, int fontSize, bool abortOnError) { TTF_Font *font = NULL; // check if already loaded String mapid = "TTF::"; mapid += filename; mapid += "::"; mapid += fontSize; if(m_map[mapid]) { return static_cast(m_map[mapid]); } font = getUniqueFont(filename,fontSize, abortOnError); m_map[mapid] = static_cast(font); return font; } /*******************************************************************************/ //! load a font an image file into a SDL surface SDL_Surface *Loader::getImage(String filename, bool alpha, bool abortOnError) { SDL_Surface *surf = NULL; // check if already loaded String mapid = "IMG::"; mapid += filename; if(m_map[mapid]) { return static_cast(m_map[mapid]); } surf = getUniqueImage(filename, alpha, abortOnError); m_map[mapid] = static_cast(surf); //LOG( 3 ) INFO( " DEBUG '%s' %d %d \n", mapid.getString(), (int)(m_map[mapid]), (int)surf ); return surf; } /*******************************************************************************/ //! load a sound file Mix_Chunk* Loader::getSound(String filename, bool abortOnError) { Mix_Chunk *chunk = NULL; // check if already loaded String mapid = "MIX::"; mapid += filename; if(m_map[mapid]) { return static_cast(m_map[mapid]); } chunk = getUniqueSound(filename, abortOnError); m_map[mapid] = static_cast(chunk); //LOG( 3 ) INFO( " DEB '%s' %d \n", mapid.getString(), (int)(m_map[mapid]) ); return chunk; } /*******************************************************************************/ //! load a music file Mix_Music* Loader::getMusic(String filename, bool abortOnError) { Mix_Music *music = NULL; // check if already loaded String mapid = "MIX::"; mapid += filename; if(m_map[mapid]) { return static_cast(m_map[mapid]); } music = getUniqueMusic(filename, abortOnError); m_map[mapid] = static_cast(music); //LOG( 3 ) INFO( " DEB '%s' %d \n", mapid.getString(), (int)(m_map[mapid]) ); return music; } /*******************************************************************************/ // unique loading routines // convert unix path to absolute, if necessary String Loader::getAbsolutePath(String filename) { String pathToFile; if(filename[0] == '/' ) { // abs. path pathToFile = filename; } else { pathToFile = WopSettings::getInstance()->getData(); pathToFile += "/"; pathToFile += filename; } return pathToFile; } /*******************************************************************************/ //! load a unique font TTF file TTF_Font *Loader::getUniqueFont(String filename, int fontSize, bool abortOnError) { TTF_Font *font = NULL; #ifdef USE_PHYSFS SDL_RWops *fontrw = getRWops(filename, abortOnError); if(fontrw) font = TTF_OpenFontRW( fontrw,true, fontSize ); #else font = TTF_OpenFont( getAbsolutePath(filename), fontSize ); #endif if(abortOnError) { DBG( 1 ) { ASSERT( NULL != font, "Loader::getFont: could not load font from file '%s'\n", filename.getString() ); } } LOG( 3 ) INFO( "Loader::getUniqueFont: loaded '%s' \n", filename.getString() ); return font; } /*******************************************************************************/ //! load a unique font an image file into a SDL surface SDL_Surface *Loader::getUniqueImage(String filename, bool alpha, bool abortOnError) { SDL_Surface *surf = NULL; // dont load graphics, if screen is not initialized if(!m_video) return NULL; if(!m_video->getScreen()) return NULL; #ifdef USE_PHYSFS SDL_Surface *rawsurf = NULL; SDL_RWops *surfrw = getRWops(filename, abortOnError); if(surfrw) rawsurf = IMG_Load_RW( surfrw, true ); if(abortOnError) { DBG( 1 ) { ASSERT( NULL != rawsurf, "Loader::getImage: could not load image from file '%s'\n", filename.getString() ); } } if(!rawsurf) { return NULL; } surf = m_video->convertSurface( rawsurf, alpha ); SDL_FreeSurface( rawsurf ); #else surf = m_video->loadImage( getAbsolutePath(filename), alpha ); #endif if(abortOnError) { DBG( 1 ) { ASSERT( NULL != surf, "Loader::getImage: could not get surface for file '%s'\n", filename.getString() ); } } LOG( 5 ) INFO( "Loader::getUniqueImage: loaded '%s' \n", filename.getString() ); return surf; } /*******************************************************************************/ //! load a unique sound file // warning: even loading a sound will fail, if the sound system is not properly // initialized // TODO: dont even load sounds then? Mix_Chunk* Loader::getUniqueSound(String filename, bool abortOnError) { Mix_Chunk *chunk = NULL; #ifdef USE_PHYSFS SDL_RWops *chunkrw = getRWops(filename, abortOnError); if(chunkrw) chunk = Mix_LoadWAV_RW( chunkrw, true ); #else chunk = Mix_LoadWAV( getAbsolutePath(filename) ); #endif if(abortOnError) { DBG( 1 ) { ASSERT( (chunk!=NULL), "Loader::getSound: could not load sound from file '%s'\n", filename.getString() ); } } LOG( 3 ) INFO( "Loader::getUniqueSound: loaded '%s' \n", filename.getString() ); return chunk; } /*******************************************************************************/ //! load a unique music file // warning: even loading a music will fail, if the sound system is not properly // initialized // TODO: dont even load music then? Mix_Music* Loader::getUniqueMusic(String filename, bool abortOnError) { Mix_Music *music = NULL; #ifdef USE_PHYSFS not implemented /* SDL_RWops *musicrw = getRWops( filename, abortOnError ); if ( musicrw ) music = Mix_LoadMUS_RW( musicrw );*/ #else music = Mix_LoadMUS( getAbsolutePath(filename) ); #endif if(abortOnError) { DBG( 1 ) { ASSERT( (music!=NULL), "Loader::getSound: could not load sound from file '%s'\n", filename.getString() ); } } LOG( 3 ) INFO( "Loader::getUniqueSound: loaded '%s' \n", filename.getString() ); return music; } /*******************************************************************************/ // singleton stuff // singleton instance Loader* Loader::m_singleton = NULL; // base path for the loader String Loader::m_wopPath( 0 ); // no video by default Video *Loader::m_video = NULL; //! singleton interface, returns loade object, creates if it doenst yet exist Loader *Loader::getInstance() { if(!m_singleton) { m_singleton = new Loader(m_wopPath); } return m_singleton; } void Loader::deleteInstance() { delete m_singleton; m_singleton = NULL; }