Kamera2D.cpp 4.8 KB

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