home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 May / Game.EXE_05_2002.iso / Alawar / Lib / 2D / Scene2D.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-03  |  2.1 KB  |  87 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() >= hidden_layer() )
  36.             break;
  37.         push_shift_add( (*it)->get_x(), (*it)->get_y() );
  38.         (*it)->render();
  39.         pop_shift();
  40.     }
  41. //    std::for_each( object_list.begin(), object_list.end(), std::mem_fun( Object2D::render ) );
  42.  
  43.     hardware->flip();
  44.     return true;
  45. }
  46.  
  47. void Scene2D::push_shift_add(int shx, int shy)
  48. {
  49.     shift.push_back( shift[ shift.size() - 1 ] + Shift( shx, shy ) );
  50. }
  51.  
  52. void Scene2D::pop_shift()
  53. {
  54.     if( shift.size() > 1 )
  55.     {
  56.         shift.pop_back();
  57.         return;
  58.     }
  59.     LogPtr()->error("in Scene2D::pop_shift() shift stack undeflow");
  60. }
  61.  
  62. HardwarePicture2D * Scene2D::load_picture(const Color * colors, int width, int height, int stride)
  63. {
  64.     if( !hardware )
  65.         return 0;
  66.     return hardware->load_picture( colors, width, height, stride );
  67. }
  68.  
  69. void Scene2D::blit(HardwarePicture2D * pic, int alpha)
  70. {
  71.     pic->blit( shift.back().x, shift.back().y, alpha );
  72. }
  73.  
  74. void Scene2D::add_object2d(Object2D * obj)
  75. {
  76.     std::list<Object2D *>::iterator it = object_list.begin();
  77.     for( ;it != object_list.end(); ++it )
  78.         if( (*it)->get_layer() > obj->get_layer() )
  79.             break;
  80.     object_list.insert( it, obj );
  81. }
  82.  
  83. void Scene2D::remove_object2d(Object2D * obj)
  84. {
  85.     object_list.remove( obj );
  86. }
  87.