Kamera2D.cpp 3.7 KB

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