home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 June / Game.EXE_06_2002.iso / Alawar / Lib / 2D / Scene2D.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-24  |  2.3 KB  |  97 lines

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