/* $Id: spritesequence.cpp,v 1.12 2005/11/01 16:19:16 pohlt Exp $ */ #include "spritesequence.hpp" /********************************************************** * * spritesequence.hpp * Uwe Fabricius Uwe.F@bricius.de * Version 1.00.00 14.01.2004 * **********************************************************/ #ifndef BLACK_ #ifdef SUPPRESS_COLORED_OUTPUT #define RED(s) s #define BLACK_(s) s #define CYAN(s) s #else #define RED(s) "\033[31m" << s << "\033[0m" #define BLACK_(s) "\033[0;1;m" << s << "\033[0m" #define CYAN(s) "\033[36m" << s << "\033[0m" #endif // SUPPRESS_COLORING #endif // BLACK_ #ifndef SRITESEQUENCE_STRINGBUFFER_ADD #define SRITESEQUENCE_STRINGBUFFER_ADD 50 #endif /********************************************************** * **********************************************************/ template SpriteSequence::SpriteSequence( const int size ) : PointerVector(size) { } template SpriteSequence::~SpriteSequence() { } /********************************************************** * **********************************************************/ template void SpriteSequence::setHotSpot( const int x, const int y ) { for( int i = 0; i < this->getSize(); i++ ) { this->m_Data[i]->setHotSpot( x, y ); } } /********************************************************** * **********************************************************/ template bool SpriteSequence::setColorKey( const Uint8 red, const Uint8 green, const Uint8 blue ) { for( int i = 0; i < this->getSize(); i++ ) { if( false == this->m_Data[i]->setColorKey(red, green, blue) ) { #ifdef DEBUG_VERBOSITY cerr << PVEC_CALLED_FROM("SpriteSequence::setColorKey") << " -> color key RGB(" < bool SpriteSequence::setAlphaValue( const Uint8 alpha ) { for( int i = 0; i < this->getSize(); i++ ) { if( false == this->m_Data[i]->setAlphaValue(alpha) ) { #ifdef DEBUG_VERBOSITY cerr << PVEC_CALLED_FROM("SpriteSequence::setAlphaValue") << " -> alpha value " < int SpriteSequence::draw( SDL_Surface* background, int i, const int x, const int y ) const { #ifdef _DEBUG_ if( i < 0 || i >= this->getSize() ) { cerr << PVEC_FN_ERR("SpriteSequence::draw") "frame [" << i << "] does not exist\n"; return 0; } #endif this->m_Data[i]->draw( background, x, y ); return (++i >= this->getSize()) ? 0 : i; } /**********************************************************/ template int SpriteSequence::load( const char* const path, const char* const configfile, const char* const section ) { #ifdef DEBUG_VERBOSITY const char fn[] = "SpriteSequence::load(char*,char*,char*)"; #endif SettingDataBase settings; String file( path ); file += "/"; file += configfile; // first load settings if( false == readSettings(file, settings, section) ) { #ifdef DEBUG_VERBOSITY cerr << PVEC_CALLED_FROM(fn) << " -> sprite sequence not loaded, object not changed\n"; #endif } else { // load sequence if( load(path, settings) <= 0 ) { #ifdef DEBUG_VERBOSITY cerr << PVEC_CALLED_FROM(fn); #endif } } return this->getSize(); } /********************************************************** * Loads a sprite sequence from the base path "path", whereby * the settings are already loaded and present in "settings". * * "settings" must have passed SettingDataBase::finalCheck(..). **********************************************************/ template int SpriteSequence::load( const char* const path, const SettingDataBase& settings ) { const char fn[] = "SpriteSequence::load(char*,SettingDataBase&)"; String Path( path ); Path += "/"; const int pathlength = Path.getLength(); Path.lockBuffer( pathlength + strlen(settings.getSetting(SettingID(DEF_FRAMES))->getString(0)) + SRITESEQUENCE_STRINGBUFFER_ADD ); // reset the sequence this->reset(); // fix size of the sequence (as we have passed finalCheck, it // is garanted, that setting "indices" is present) Setting *setting = settings.getSetting( SettingID(DEF_INDICES) ); if( false == this->resize(setting->getInt(1) - setting->getInt(0) + 1) ) { #ifdef DEBUG_VERBOSITY cerr << PVEC_CALLED_FROM(fn) << " -> sprite sequence " << BLACK_(settings.getSetting("frames")->getString(0)) << " could not be loaded\n"; #endif return 0; } //////////////// // load frames String framename; const char* frameformat = settings.getSetting( SettingID(DEF_FRAMES) )->getString(0); for( int i = setting->getInt(0); i <= setting->getInt(1); i++ ) { // create full path name of the frame framename.format( frameformat, i ); Path.cut( pathlength ); #ifdef _DEBUG_ if( framename.getLength() + Path.getLength() > Path.getLockedLength() ) { cerr << PVEC_FN_ERR(fn) << "buffer not large enough to store full path\n" " -> increase SRITESEQUENCE_STRINGBUFFER_ADD for " "spritesequence.cpp\n current value = " << SRITESEQUENCE_STRINGBUFFER_ADD << std::endl; } #endif Path += framename; // create and load new frame T *frame = new T; if( false == frame->loadImage(Path) ) { #ifdef DEBUG_VERBOSITY cerr << PVEC_CALLED_FROM(fn) << " -> end loading of sequence at frame ["<resize( i - setting->getInt(0) ); return -this->getSize(); } else { this->set( i-setting->getInt(0), frame ); } } //////////////////////////// // set colorkey (optional) setting = settings.getSetting( SettingID(DEF_COLORKEY) ); if( setting ) { #ifdef _DEBUG_ if( false == setColorKey(setting->getInt(0), setting->getInt(1), setting->getInt(2) ) ) { cerr << PVEC_CALLED_FROM(fn); } #else setColorKey( setting->getInt(0), setting->getInt(1), setting->getInt(2) ); #endif } ////////////////////////////// // set alpha value (optional) setting = settings.getSetting( SettingID(DEF_ALPHA) ); if( setting ) { #ifdef _DEBUG_ if( false == setAlphaValue(setting->getInt(0)) ) { cerr << PVEC_CALLED_FROM(fn); } #else setAlphaValue( setting->getInt(0) ); #endif } ////////////////////////// // set hotspot (optional) setting = settings.getSetting( SettingID(DEF_HOTSPOT) ); if( setting ) setHotSpot( setting->getInt(0), setting->getInt(1) ); return this->getSize(); } /********************************************************** * **********************************************************/ template const char* SpriteSequence::SettingID( const int i ) const { return (i >= 0 && i < NUM_SETTING_DEFS) ? m_SettingDef[i].m_IDString : NULL; } /********************************************************** * **********************************************************/ template bool SpriteSequence::readSettings( const char* const configfile, SettingDataBase& settings, const char* const section ) { const char fn[] = "SpriteSequence::readSettings"; bool pickySuccess = true; // restrict reading to section, if section == NULL, // reading will not be restricted if( false == settings.restrictToSection(section) ) { cerr << PVEC_CALLED_FROM(fn); return false; } // read settings if( false == settings.readSettings(m_SettingDef, (char*)configfile, true, &pickySuccess) || false == settings.finalCheck(m_SettingDef) ) { cerr << PVEC_CALLED_FROM(fn) << " -> sprite sequence not loaded\n"; return false; } #ifdef _DEBUG_ // some checks of the contents of the settings if( settings.getSetting(SettingID(DEF_INDICES))->getInt(0) > settings.getSetting(SettingID(DEF_INDICES))->getInt(1) ) { cerr << PVEC_FN_ERR(fn) << "the range of frame indices [" << BLACK_( settings.getSetting(SettingID(DEF_INDICES))->getInt(0) << "," << settings.getSetting(SettingID(DEF_INDICES))->getInt(1) << "]" ) << "does not make sense -> no frame of sequence " << BLACK_(configfile) << " will be loaded\n"; pickySuccess = false; } #endif return pickySuccess; } /**********************************************************/