Kamera2D.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. name = new Text( "" );
  15. ref = 1;
  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. Kamera2D *Kamera2D::getThis()
  153. {
  154. ref++;
  155. return this;
  156. }
  157. Kamera2D *Kamera2D::release()
  158. {
  159. if( !--ref )
  160. delete this;
  161. return 0;
  162. }
  163. bool TestKamera2D::tick( double time )
  164. {
  165. bool ret = rend;
  166. rend = Kamera2D::tick( time );
  167. if( getTastenStand( 'q' ) )
  168. {
  169. rotation += (float)( time * 0.5 );
  170. ret = 1;
  171. }
  172. if( getTastenStand( 'e' ) )
  173. {
  174. rotation -= (float)( time * 0.5 );
  175. ret = 1;
  176. }
  177. Vertex move;
  178. if( getTastenStand( 'w' ) )
  179. move = Vertex( 0, -100 * (float)time );
  180. if( getTastenStand( 'a' ) )
  181. move = Vertex( -100 * (float)time, 0 );
  182. if( getTastenStand( 'd' ) )
  183. move = Vertex( 100 * (float)time, 0 );
  184. if( getTastenStand( 's' ) )
  185. move = Vertex( 0, +100 * (float)time );
  186. if( move != Vertex( 0, 0 ) )
  187. {
  188. wPos += move.rotation( -rotation ) * ( 1 / zoom );
  189. ret = 1;
  190. }
  191. if( getTastenStand( '+' ) )
  192. {
  193. zoom += ( zoom ) * (float)time;
  194. ret = 1;
  195. }
  196. if( getTastenStand( '-' ) )
  197. {
  198. zoom -= ( zoom ) * (float)time;
  199. if( zoom <= 0 )
  200. zoom = 1;
  201. ret = 1;
  202. }
  203. return ret;
  204. }