Kamera2D.cpp 5.1 KB

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