Kamera2D.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. tickWelt = 0;
  14. ref = 1;
  15. }
  16. Kamera2D::~Kamera2D()
  17. {
  18. if( welt )
  19. welt->release();
  20. }
  21. void Kamera2D::lookAtWorldPos( int x, int y )
  22. {
  23. wPos.x = (float)x;
  24. wPos.y = (float)y;
  25. }
  26. void Kamera2D::lookAtWorldPos( Vertex pos )
  27. {
  28. wPos = pos;
  29. }
  30. void Kamera2D::lookAtWorldArea( int width, int height )
  31. {
  32. zoom = (float)getBreite() / (float)width;
  33. // TODO have two scaling factors
  34. }
  35. void Kamera2D::setDrehung( float rotation )
  36. {
  37. this->rotation = rotation;
  38. }
  39. void Kamera2D::setZoom( float zoom )
  40. {
  41. this->zoom = zoom;
  42. }
  43. void Kamera2D::setWelt( Welt2D *welt, bool tick )
  44. {
  45. if( this->welt )
  46. this->welt->release();
  47. this->welt = welt;
  48. tickWelt = tick;
  49. }
  50. bool Kamera2D::tick( double time )
  51. {
  52. bool ret = rend;
  53. rend = 0;
  54. if( welt && tickWelt )
  55. ret |= welt->tick( time );
  56. return ret | ZeichnungHintergrund::tick( time );
  57. }
  58. void Kamera2D::render( Bild &zRObj )
  59. {
  60. if( hatStyleNicht( Style::Sichtbar ) )
  61. return;
  62. ZeichnungHintergrund::render( zRObj );
  63. if( !welt )
  64. return;
  65. lockZeichnung();
  66. if( !zRObj.setDrawOptions( innenPosition, innenSize ) )
  67. {
  68. unlockZeichnung();
  69. return;
  70. }
  71. matrix = Mat3< float >::translation( (Vertex)gr / 2 ) * Mat3< float >::rotation( rotation ) * Mat3< float >::scaling( zoom ) * Mat3< float >::translation( -wPos );
  72. welt->render( matrix, gr, zRObj );
  73. zRObj.releaseDrawOptions();
  74. unlockZeichnung();
  75. }
  76. Vertex Kamera2D::getWorldCoordinates( Punkt screenPos )
  77. {
  78. Vertex wKoord = ( Mat3< float >::translation( wPos ) * Mat3< float >::scaling( 1 / zoom ) * Mat3< float >::rotation( -rotation ) * Mat3< float >::translation( (Vertex)gr / -2 ) ) * (Vertex)screenPos;
  79. if( welt->getWorldInfo().circular && welt->getWorldInfo().hasSize && welt->getWorldInfo().size.x && welt->getWorldInfo().size.y )
  80. {
  81. while( wKoord.x < 0 )
  82. wKoord.x += (float)welt->getWorldInfo().size.x;
  83. while( wKoord.x > welt->getWorldInfo().size.x )
  84. wKoord.x -= (float)welt->getWorldInfo().size.x;
  85. while( wKoord.y < 0 )
  86. wKoord.y += (float)welt->getWorldInfo().size.y;
  87. while( wKoord.y > welt->getWorldInfo().size.y )
  88. wKoord.y -= (float)welt->getWorldInfo().size.y;
  89. }
  90. return wKoord;
  91. }
  92. Vertex Kamera2D::getWorldDirection( Vertex dir )
  93. {
  94. return Vertex( dir.x, dir.y ).rotation( -rotation ) * ( 1 / zoom );
  95. }
  96. Vertex Kamera2D::getWorldPosition()
  97. {
  98. return wPos;
  99. }
  100. float Kamera2D::getRotation()
  101. {
  102. return rotation;
  103. }
  104. float Kamera2D::getZoom()
  105. {
  106. return zoom;
  107. }
  108. Kamera2D *Kamera2D::getThis()
  109. {
  110. ref++;
  111. return this;
  112. }
  113. Kamera2D *Kamera2D::release()
  114. {
  115. if( !--ref )
  116. delete this;
  117. return 0;
  118. }
  119. bool TestKamera2D::tick( double time )
  120. {
  121. bool ret = rend;
  122. rend = Kamera2D::tick( time );
  123. if( getTastenStand( 'q' ) )
  124. {
  125. rotation += (float)( time * 0.5 );
  126. ret = 1;
  127. }
  128. if( getTastenStand( 'e' ) )
  129. {
  130. rotation -= (float)( time * 0.5 );
  131. ret = 1;
  132. }
  133. Vertex move;
  134. if( getTastenStand( 'w' ) )
  135. move = Vertex( 0, -100 * (float)time );
  136. if( getTastenStand( 'a' ) )
  137. move = Vertex( -100 * (float)time, 0 );
  138. if( getTastenStand( 'd' ) )
  139. move = Vertex( 100 * (float)time, 0 );
  140. if( getTastenStand( 's' ) )
  141. move = Vertex( 0, +100 * (float)time );
  142. if( move != Vertex( 0, 0 ) )
  143. {
  144. wPos += move.rotation( -rotation ) * ( 1 / zoom );
  145. ret = 1;
  146. }
  147. if( getTastenStand( '+' ) )
  148. {
  149. zoom += ( zoom ) * (float)time;
  150. ret = 1;
  151. }
  152. if( getTastenStand( '-' ) )
  153. {
  154. zoom -= ( zoom ) * (float)time;
  155. if( zoom <= 0 )
  156. zoom = 1;
  157. ret = 1;
  158. }
  159. return ret;
  160. }