Kamera2D.cpp 5.3 KB

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