home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 April / Game.EXE_04_2002.iso / Alawar / Sequences.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-25  |  5.2 KB  |  209 lines

  1. #include "Sequences.h"
  2. #include "Scene2D.h"
  3. #include "AutoPtr.h"
  4. #include "LogPtr.h"
  5. #include "Color.h"
  6. #include "PictureFormatManagerPtr.h"
  7. #include "HardwarePicture2D.h"
  8. #include <algorithm>
  9. #include "safe_new.h"
  10.  
  11. void Sequences::Sequence::render_frame(Scene2D * scene, unsigned fr, int alpha)const
  12. {
  13.     if( fr < frames.size() )
  14.         scene->blit( frames[fr].ha, alpha );
  15. }
  16. unsigned Sequences::Sequence::get_width(unsigned fr)const
  17. {
  18.     return fr >= frames.size() ? 0 : frames[fr].width;
  19. }
  20. unsigned Sequences::Sequence::get_height(unsigned fr)const
  21. {
  22.     return fr >= frames.size() ? 0 : frames[fr].height;
  23. }
  24. unsigned Sequences::Sequence::get_duration(unsigned fr)const
  25. {
  26.     return fr >= frames.size() ? 0 : frames[fr].duration;
  27. }
  28. unsigned Sequences::Sequence::get_frame_count()const
  29. {
  30.     return frames.size();
  31. }
  32.  
  33. std::list<Sequences *> * Sequences::sequences;
  34. int Sequences::counter = 0;
  35.  
  36. class SequncesEqPred
  37. {
  38.     const String & name;
  39. public:
  40.     SequncesEqPred(const String & name)
  41.         :    name( name )
  42.     {}
  43.     bool operator()(const Sequences * seq)
  44.     {
  45.         return seq->get_name() == name;
  46.     }
  47. };
  48.  
  49. unsigned Sequences::cached_count()
  50. {
  51.     return sequences ? sequences->size() : 0;
  52. }
  53.  
  54. void Sequences::unload(const Sequences * seq)
  55. {
  56.     if( !sequences )
  57.     {
  58.         LogPtr()->error( String("in Sequences::unload() sequences table is already destroyed") );
  59.         return;
  60.     }
  61.     std::list<Sequences *>::iterator it = std::find( sequences->begin(), sequences->end(), seq );
  62.     if( it == sequences->end() )
  63.     {
  64.         LogPtr()->warning( String("in Sequences::unload() <") + seq->get_name() + String("> not found") );
  65.         return;
  66.     }
  67.     if( --(*it)->ref_count == 0 )
  68.     {
  69.         delete *it; *it = 0;
  70.         it = sequences->erase( it );
  71.     }
  72.     if( --counter == 0 )
  73.     {
  74.         delete sequences; sequences = 0;
  75.     }
  76. }
  77.  
  78. const Sequences * Sequences::load(const String & name, Scene2D * scene)
  79. {
  80.     if( ++counter == 1 )
  81.     {
  82.         if( sequences )
  83.         {
  84.             LogPtr()->error( String("in Sequences::load() sequences table is already created") );
  85.         }
  86.         else
  87.         {
  88.             sequences = new std::list<Sequences *>;
  89.         }
  90.     }
  91.     std::list<Sequences *>::iterator it = std::find_if( sequences->begin(), sequences->end(), SequncesEqPred( name ) );
  92.     if( it != sequences->end() )
  93.         return *it;
  94.     it = sequences->insert( sequences->end(), new Sequences( name, scene ) );
  95.     return *it;
  96. }
  97.  
  98. Sequences::Sequences(const String & name, Scene2D * scene)
  99. :    name( name ),
  100.     scene( scene ),
  101.     ref_count( 1 )
  102. {
  103.     AutoPtr<PictureFormat> pic = PictureFormatManagerPtr()->create_format( name );
  104.     if( !pic )
  105.         return;
  106.  
  107.     const Color * separator = pic->colors() + pic->get_shift( pic->width()-1, 0 );
  108.  
  109.     unsigned start_y = 0;
  110.     unsigned end_y;
  111. //    unsigned curr_seq = 0;
  112.     // Let's find the sequences now
  113.     do
  114.     {
  115.         const Color * cc = pic->colors() + pic->get_shift(0, start_y );
  116.         while( start_y < pic->height() && *cc == *separator )
  117.         {
  118.             ++start_y;
  119.             cc += pic->width();
  120.         }
  121.         end_y = start_y;
  122.         while( end_y < pic->height() && *cc != *separator )
  123.         {
  124.             ++end_y;
  125.             cc += pic->width();
  126.         }
  127.         if( end_y != start_y )
  128.         {
  129.             // Found a sequence - let's find the frames now
  130. //            ++curr_seq;
  131.             data.push_back( Sequence() );
  132.             unsigned start_x = 0;
  133.             unsigned end_x;
  134. //            int curr_frame = 0;
  135.             do
  136.             {
  137.                 const Color * cc = pic->colors() + pic->get_shift( start_x, start_y );
  138.                 while( start_x < pic->width() && *cc == *separator )
  139.                 {
  140.                     ++start_x;
  141.                     ++cc;
  142.                 }
  143.                 end_x = start_x;
  144.                 while( end_x < pic->width() && *cc != *separator )
  145.                 {
  146.                     ++end_x;
  147.                     ++cc;
  148.                 }
  149.                 if( end_x != start_x )
  150.                 {
  151.                     // Found a frame
  152.                     const Color * address = pic->colors() + pic->get_shift( start_x, start_y );
  153.                     unsigned sx = end_x - start_x;
  154.                     unsigned sy = end_y - start_y;
  155.                     HardwarePicture2D * ha = scene->load_picture(
  156.                             address, sx, sy, pic->width() );
  157.                     data.back().frames.push_back( Frame( 200, sx, sy, ha ) );
  158.                 }
  159.                 start_x = end_x;
  160.             }while( start_x < pic->width() );
  161.         }
  162.         start_y = end_y;
  163.     }while( start_y < pic->height() );
  164.  
  165.     if( data.empty() )
  166.     {
  167.         Color dummy[4] = { Color(255,0,0), Color(0,255,0), Color(0,255,0), Color(255,0,0) };
  168.         data.push_back( Sequence() );
  169.         HardwarePicture2D * ha = scene->load_picture(
  170.                             dummy, 2, 2, 2 );
  171.         data[0].frames.push_back( Frame( 500, 2, 2, ha ) );
  172.     }
  173. }
  174.  
  175. Sequences::~Sequences()
  176. {
  177.     for( unsigned s = 0; s < data.size(); ++s )
  178.         for( unsigned f = 0; f < data[s].frames.size(); ++f )
  179.         {
  180.             delete data[s].frames[f].ha; data[s].frames[f].ha = 0;
  181.         }
  182. }
  183.  
  184. unsigned Sequences::get_frame_count(unsigned seq)const
  185. {
  186.     return seq >= data.size() ? 0 : data[seq].get_frame_count();
  187. }
  188. unsigned Sequences::get_height(unsigned seq, unsigned fr)const
  189. {
  190.     return seq >= data.size() ? 0 : data[seq].get_height( fr );
  191. }
  192. unsigned Sequences::get_sequence_count()const
  193. {
  194.     return data.size();
  195. }
  196. unsigned Sequences::get_width(unsigned seq, unsigned fr )const
  197. {
  198.     return seq >= data.size() ? 0 : data[seq].get_width( fr );
  199. }
  200. void Sequences::render_frame(unsigned seq, unsigned fr, unsigned alpha)const
  201. {
  202.     if( seq < data.size() )
  203.         data[seq].render_frame( scene, fr, alpha );
  204. }
  205. unsigned Sequences::get_duration(unsigned seq, unsigned fr)const
  206. {
  207.     return seq >= data.size() ? 0 : data[seq].get_duration( fr );
  208. }
  209.