Kamera2D.cpp 3.6 KB

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