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

  1. #include "Scene2D.h"
  2. #include "Object2D.h"
  3. #include "LogPtr.h"
  4. #include "String.h"
  5. #include "Hardware2D.h"
  6.  
  7. Scene2D::Scene2D(Hardware2D * hardware)
  8. :    hardware( hardware ),
  9.     shift( 1 )
  10. {}
  11.  
  12. Scene2D::~Scene2D()
  13. {
  14.     if( !object_list.empty() )
  15.         LogPtr()->error( "in Scene2D::~Scene2D() there remained objects in list" );
  16. }
  17.  
  18. bool Scene2D::render(int camera_x, int camera_y, const Color & back_color)
  19. {
  20.     if( shift.size() != 1 )
  21.     {
  22.         LogPtr()->warning( String("in Scene2D::render() after all pop_shift called") + String::convert(shift.size()) + "shifts left" );
  23.     }
  24.     shift.resize( 1 );
  25.     shift[0] = Shift( camera_x, camera_y );
  26.  
  27.     if( !hardware )
  28.         return false;
  29.     hardware->fill( 0, 0, hardware->get_width(), hardware->get_height(), back_color );
  30.     
  31.     std::list<Object2D *>::iterator it = object_list.begin();
  32.     for( ;it != object_list.end(); ++it )
  33.     {
  34.         if( (*it)->get_layer() >= hidden_layer() )
  35.             break;
  36.         push_shift_add( (*it)->get_x(), (*it)->get_y() );
  37.         (*it)->render();
  38.         pop_shift();
  39.     }
  40. //    std::for_each( object_list.begin(), object_list.end(), std::mem_fun( Object2D::render ) );
  41.  
  42.     hardware->flip();
  43.     return true;
  44. }
  45.  
  46. void Scene2D::push_shift_add(int shx, int shy)
  47. {
  48.     shift.push_back( shift[ shift.size() - 1 ] + Shift( shx, shy ) );
  49. }
  50.  
  51. void Scene2D::pop_shift()
  52. {
  53.     if( shift.size() > 1 )
  54.     {
  55.         shift.pop_back();
  56.         return;
  57.     }
  58.     LogPtr()->error("in Scene2D::pop_shift() shift stack undeflow");
  59. }
  60.  
  61. HardwarePicture2D * Scene2D::load_picture(const Color * colors, unsigned width, unsigned height, unsigned stride)
  62. {
  63.     if( !hardware )
  64.         return 0;
  65.     return hardware->load_picture( colors, width, height, stride );
  66. }
  67.  
  68. void Scene2D::blit(HardwarePicture2D * pic, int alpha)
  69. {
  70.     pic->blit( shift.back().x, shift.back().y, alpha );
  71. }
  72.  
  73. void Scene2D::add_object2d(Object2D * obj)
  74. {
  75.     std::list<Object2D *>::iterator it = object_list.begin();
  76.     for( ;it != object_list.end(); ++it )
  77.         if( (*it)->get_layer() > obj->get_layer() )
  78.             break;
  79.     object_list.insert( it, obj );
  80. }
  81.  
  82. void Scene2D::remove_object2d(Object2D * obj)
  83. {
  84.     object_list.remove( obj );
  85. }
  86.