home *** CD-ROM | disk | FTP | other *** search
- #include "Sequences.h"
- #include "Scene2D.h"
- #include "AutoPtr.h"
- #include "LogPtr.h"
- #include "Color.h"
- #include "PictureFormatManagerPtr.h"
- #include "HardwarePicture2D.h"
- #include <algorithm>
- #include "safe_new.h"
-
- void Sequences::Sequence::render_frame(Scene2D * scene, unsigned fr, int alpha)const
- {
- if( fr < frames.size() )
- scene->blit( frames[fr].ha, alpha );
- }
- unsigned Sequences::Sequence::get_width(unsigned fr)const
- {
- return fr >= frames.size() ? 0 : frames[fr].width;
- }
- unsigned Sequences::Sequence::get_height(unsigned fr)const
- {
- return fr >= frames.size() ? 0 : frames[fr].height;
- }
- unsigned Sequences::Sequence::get_duration(unsigned fr)const
- {
- return fr >= frames.size() ? 0 : frames[fr].duration;
- }
- unsigned Sequences::Sequence::get_frame_count()const
- {
- return frames.size();
- }
-
- std::list<Sequences *> * Sequences::sequences;
- int Sequences::counter = 0;
-
- class SequncesEqPred
- {
- const String & name;
- public:
- SequncesEqPred(const String & name)
- : name( name )
- {}
- bool operator()(const Sequences * seq)
- {
- return seq->get_name() == name;
- }
- };
-
- unsigned Sequences::cached_count()
- {
- return sequences ? sequences->size() : 0;
- }
-
- void Sequences::unload(const Sequences * seq)
- {
- if( !sequences )
- {
- LogPtr()->error( String("in Sequences::unload() sequences table is already destroyed") );
- return;
- }
- std::list<Sequences *>::iterator it = std::find( sequences->begin(), sequences->end(), seq );
- if( it == sequences->end() )
- {
- LogPtr()->warning( String("in Sequences::unload() <") + seq->get_name() + String("> not found") );
- return;
- }
- if( --(*it)->ref_count == 0 )
- {
- delete *it; *it = 0;
- it = sequences->erase( it );
- }
- if( --counter == 0 )
- {
- delete sequences; sequences = 0;
- }
- }
-
- const Sequences * Sequences::load(const String & name, Scene2D * scene)
- {
- if( ++counter == 1 )
- {
- if( sequences )
- {
- LogPtr()->error( String("in Sequences::load() sequences table is already created") );
- }
- else
- {
- sequences = new std::list<Sequences *>;
- }
- }
- std::list<Sequences *>::iterator it = std::find_if( sequences->begin(), sequences->end(), SequncesEqPred( name ) );
- if( it != sequences->end() )
- return *it;
- it = sequences->insert( sequences->end(), new Sequences( name, scene ) );
- return *it;
- }
-
- Sequences::Sequences(const String & name, Scene2D * scene)
- : name( name ),
- scene( scene ),
- ref_count( 1 )
- {
- AutoPtr<PictureFormat> pic = PictureFormatManagerPtr()->create_format( name );
- if( !pic )
- return;
-
- const Color * separator = pic->colors() + pic->get_shift( pic->width()-1, 0 );
-
- unsigned start_y = 0;
- unsigned end_y;
- // unsigned curr_seq = 0;
- // Let's find the sequences now
- do
- {
- const Color * cc = pic->colors() + pic->get_shift(0, start_y );
- while( start_y < pic->height() && *cc == *separator )
- {
- ++start_y;
- cc += pic->width();
- }
- end_y = start_y;
- while( end_y < pic->height() && *cc != *separator )
- {
- ++end_y;
- cc += pic->width();
- }
- if( end_y != start_y )
- {
- // Found a sequence - let's find the frames now
- // ++curr_seq;
- data.push_back( Sequence() );
- unsigned start_x = 0;
- unsigned end_x;
- // int curr_frame = 0;
- do
- {
- const Color * cc = pic->colors() + pic->get_shift( start_x, start_y );
- while( start_x < pic->width() && *cc == *separator )
- {
- ++start_x;
- ++cc;
- }
- end_x = start_x;
- while( end_x < pic->width() && *cc != *separator )
- {
- ++end_x;
- ++cc;
- }
- if( end_x != start_x )
- {
- // Found a frame
- const Color * address = pic->colors() + pic->get_shift( start_x, start_y );
- unsigned sx = end_x - start_x;
- unsigned sy = end_y - start_y;
- HardwarePicture2D * ha = scene->load_picture(
- address, sx, sy, pic->width() );
- data.back().frames.push_back( Frame( 200, sx, sy, ha ) );
- }
- start_x = end_x;
- }while( start_x < pic->width() );
- }
- start_y = end_y;
- }while( start_y < pic->height() );
-
- if( data.empty() )
- {
- Color dummy[4] = { Color(255,0,0), Color(0,255,0), Color(0,255,0), Color(255,0,0) };
- data.push_back( Sequence() );
- HardwarePicture2D * ha = scene->load_picture(
- dummy, 2, 2, 2 );
- data[0].frames.push_back( Frame( 500, 2, 2, ha ) );
- }
- }
-
- Sequences::~Sequences()
- {
- for( unsigned s = 0; s < data.size(); ++s )
- for( unsigned f = 0; f < data[s].frames.size(); ++f )
- {
- delete data[s].frames[f].ha; data[s].frames[f].ha = 0;
- }
- }
-
- unsigned Sequences::get_frame_count(unsigned seq)const
- {
- return seq >= data.size() ? 0 : data[seq].get_frame_count();
- }
- unsigned Sequences::get_height(unsigned seq, unsigned fr)const
- {
- return seq >= data.size() ? 0 : data[seq].get_height( fr );
- }
- unsigned Sequences::get_sequence_count()const
- {
- return data.size();
- }
- unsigned Sequences::get_width(unsigned seq, unsigned fr )const
- {
- return seq >= data.size() ? 0 : data[seq].get_width( fr );
- }
- void Sequences::render_frame(unsigned seq, unsigned fr, unsigned alpha)const
- {
- if( seq < data.size() )
- data[seq].render_frame( scene, fr, alpha );
- }
- unsigned Sequences::get_duration(unsigned seq, unsigned fr)const
- {
- return seq >= data.size() ? 0 : data[seq].get_duration( fr );
- }
-