Kamera2D.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. const Mat3< float > &Kamera2D::getMatrix()
  109. {
  110. return matrix;
  111. }
  112. Kamera2D *Kamera2D::getThis()
  113. {
  114. ref++;
  115. return this;
  116. }
  117. Kamera2D *Kamera2D::release()
  118. {
  119. if( !--ref )
  120. delete this;
  121. return 0;
  122. }
  123. bool TestKamera2D::tick( double time )
  124. {
  125. bool ret = rend;
  126. rend = Kamera2D::tick( time );
  127. if( getTastenStand( 'q' ) )
  128. {
  129. rotation += (float)( time * 0.5 );
  130. ret = 1;
  131. }
  132. if( getTastenStand( 'e' ) )
  133. {
  134. rotation -= (float)( time * 0.5 );
  135. ret = 1;
  136. }
  137. Vertex move;
  138. if( getTastenStand( 'w' ) )
  139. move = Vertex( 0, -100 * (float)time );
  140. if( getTastenStand( 'a' ) )
  141. move = Vertex( -100 * (float)time, 0 );
  142. if( getTastenStand( 'd' ) )
  143. move = Vertex( 100 * (float)time, 0 );
  144. if( getTastenStand( 's' ) )
  145. move = Vertex( 0, +100 * (float)time );
  146. if( move != Vertex( 0, 0 ) )
  147. {
  148. wPos += move.rotation( -rotation ) * ( 1 / zoom );
  149. ret = 1;
  150. }
  151. if( getTastenStand( '+' ) )
  152. {
  153. zoom += ( zoom ) * (float)time;
  154. ret = 1;
  155. }
  156. if( getTastenStand( '-' ) )
  157. {
  158. zoom -= ( zoom ) * (float)time;
  159. if( zoom <= 0 )
  160. zoom = 1;
  161. ret = 1;
  162. }
  163. return ret;
  164. }