1
0

Terrain.cpp 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "Terrain.h"
  2. Terrain::Terrain( const unsigned int size )
  3. : m_size( size ), m_nextAdd( 0 ), m_plot( 0x0 )
  4. {
  5. m_plot = new b2Vec2[ size ];
  6. }
  7. Terrain::~Terrain()
  8. {
  9. delete[] m_plot ;
  10. m_plot = 0x0 ;
  11. }
  12. void Terrain::addPlot( float x, float y )
  13. {
  14. if ( m_nextAdd == m_size )
  15. return ;
  16. m_plot[ m_nextAdd ].Set( x, y );
  17. m_nextAdd ++;
  18. }
  19. void Terrain::build( b2World &world )
  20. {
  21. b2BodyDef areaDef;
  22. areaDef.position.Set(0.0f, 0.0f);
  23. b2Body* areaBody = world.CreateBody(&areaDef);
  24. b2ChainShape chain;
  25. chain.CreateChain( m_plot, m_nextAdd, m_plot[0], m_plot[m_size - 1] );
  26. areaBody->CreateFixture( &chain, 0.0f );
  27. }
  28. void Terrain::draw( SDL_Surface* screen, b2Vec2 origin )
  29. {
  30. int x1, y1, x2, y2 ;
  31. for ( unsigned int i(0); i < m_nextAdd - 1 ; i++ )
  32. {
  33. x1 = ( m_plot[i].x - origin.x ) * MULTI ;
  34. y1 = ( m_plot[i].y - origin.y ) * MULTI ;
  35. x2 = ( m_plot[i + 1].x - origin.x ) * MULTI ;
  36. y2 = ( m_plot[i + 1].y - origin.y ) * MULTI ;
  37. lineRGBA (screen, x1, y1, x2, y2, 255, 0, 0, 255);
  38. }
  39. }