home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 May / Game.EXE_05_2002.iso / Alawar / Lib / 2D / FontObject2D.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-04  |  1.7 KB  |  76 lines

  1. #include "FontObject2D.h"
  2. #include "Sequences.h"
  3. #include "Scene2D.h"
  4. #include <AutoPtr.h>
  5. #include <Resource/ResourceManager.h>
  6.  
  7. FontObject2D::FontObject2D(Scene2D * scene, int layer, const String & name  )
  8. :    Object2D( scene, layer ),
  9.     sequences( Sequences::load( name, scene ) ),
  10.     alpha( 255 )
  11. {
  12.     AutoPtr<Resource> res = ResourceManager::create_resource( name + ".font" );
  13.     if( !res )
  14.         return;
  15.     int table_size = res->get_size() / 9;
  16.  
  17.     for( int n = 0; n < table_size; ++n )
  18.     {
  19.         unsigned char font_desc[9] = { 0,0,0,0,0,0,0,0 };
  20.         res->read( font_desc, 9 );
  21.         unsigned char sym = font_desc[0];
  22.         int seq = (font_desc[2] - '0')*10 + (font_desc[3] - '0');
  23.         int fr = (font_desc[5] - '0')*10 + (font_desc[6] - '0');
  24.         font_table[ sym ] = SeqFr( seq, fr );
  25.     }
  26. }
  27.  
  28. FontObject2D::~FontObject2D()
  29. {
  30.     Sequences::unload( sequences );
  31. }
  32.  
  33. void FontObject2D::render()
  34. {
  35.     int pos_x = 0;
  36.     for( int i = 0; i < text.size(); ++i )
  37.     {
  38.         FontTable::const_iterator it = font_table.find( text[i] );
  39.         if( it == font_table.end() )
  40.         {
  41.             pos_x += sequences->get_width( 0, 0 );
  42.             continue;
  43.         }
  44.         int seq = it->second.seq;
  45.         int fr = it->second.fr;
  46.  
  47.         scene->push_shift_add( pos_x, 0 );
  48.         {
  49.             sequences->render_frame( seq, fr, alpha );
  50.         }
  51.         scene->pop_shift();
  52.  
  53.         pos_x += sequences->get_width( seq, fr );
  54.     }
  55. }
  56.  
  57. void FontObject2D::set_transparency(int alpha)
  58. {
  59.     if( alpha < 0   ) alpha = 0;
  60.     if( alpha > 255 ) alpha = 255;
  61.     this->alpha = alpha;
  62. }
  63.  
  64. String FontObject2D::change_text(const String & text)
  65. {
  66.     this->text = "";
  67.     for( int i = 0; i < text.size(); ++i )
  68.     {
  69.         FontTable::const_iterator it = font_table.find( text[i] );
  70.         if( it == font_table.end() )
  71.             continue;
  72.         this->text += text[i];
  73.     }
  74.     return this->text;
  75. }
  76.