Kamera2D.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "Kamera2D.h"
  2. #include "Welt2D.h"
  3. #include "Bild.h"
  4. #include "Globals.h"
  5. using namespace Framework;
  6. Kamera2D::Kamera2D()
  7. : ZeichnungHintergrund()
  8. {
  9. welt = 0;
  10. rotation = 0;
  11. matrix = Mat3< float >::identity();
  12. zoom = 1;
  13. ref = 1;
  14. }
  15. Kamera2D::~Kamera2D()
  16. {
  17. if( welt )
  18. welt->release();
  19. }
  20. void Kamera2D::setWelt( Welt2D *welt )
  21. {
  22. if( this->welt )
  23. this->welt->release();
  24. this->welt = welt;
  25. }
  26. bool Kamera2D::tick( double time )
  27. {
  28. bool ret = rend;
  29. rend = 0;
  30. if( getTastenStand( 'q' ) )
  31. {
  32. rotation += (float)( time * 0.5 );
  33. ret = 1;
  34. }
  35. if( getTastenStand( 'e' ) )
  36. {
  37. rotation -= (float)( time * 0.5 );
  38. ret = 1;
  39. }
  40. Vertex move;
  41. if( getTastenStand( 'w' ) )
  42. move = Vertex( 0, -100 * (float)time );
  43. if( getTastenStand( 'a' ) )
  44. move = Vertex( -100 * (float)time, 0 );
  45. if( getTastenStand( 'd' ) )
  46. move = Vertex( 100 * (float)time, 0 );
  47. if( getTastenStand( 's' ) )
  48. move = Vertex( 0, +100 * (float)time );
  49. if( move != Vertex( 0, 0 ) )
  50. {
  51. wPos += move.rotation( -rotation ) * ( 1 / zoom );
  52. ret = 1;
  53. }
  54. if( getTastenStand( '+' ) )
  55. {
  56. zoom += ( zoom ) * (float)time;
  57. ret = 1;
  58. }
  59. if( getTastenStand( '-' ) )
  60. {
  61. zoom -= ( zoom ) * (float)time;
  62. if( zoom <= 0 )
  63. zoom = 1;
  64. ret = 1;
  65. }
  66. if( welt )
  67. ret |= welt->tick( time );
  68. return ret | ZeichnungHintergrund::tick( time );
  69. }
  70. void Kamera2D::render( Bild &zRObj )
  71. {
  72. if( hatStyleNicht( Style::Sichtbar ) )
  73. return;
  74. ZeichnungHintergrund::render( zRObj );
  75. if( !welt )
  76. return;
  77. lockZeichnung();
  78. if( !zRObj.setDrawOptions( innenPosition, innenSize ) )
  79. {
  80. unlockZeichnung();
  81. return;
  82. }
  83. matrix = Mat3< float >::translation( (Vertex)gr / 2 ) * Mat3< float >::rotation( rotation ) * Mat3< float >::scaling( zoom ) * Mat3< float >::translation( -wPos );
  84. welt->render( matrix, gr, zRObj );
  85. zRObj.releaseDrawOptions();
  86. unlockZeichnung();
  87. }
  88. Vertex Kamera2D::getWorldCoordinates( Punkt screenPos )
  89. {
  90. return ( Mat3< float >::translation( wPos ) * Mat3< float >::scaling( 1 / zoom ) * Mat3< float >::rotation( -rotation ) * Mat3< float >::translation( (Vertex)gr / -2 ) ) * (Vertex)screenPos;
  91. }
  92. Kamera2D *Kamera2D::getThis()
  93. {
  94. ref++;
  95. return this;
  96. }
  97. Kamera2D *Kamera2D::release()
  98. {
  99. if( !--ref )
  100. delete this;
  101. return 0;
  102. }