Kamera2D.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "Kamera2D.h"
  2. #include "Bild.h"
  3. #include "Globals.h"
  4. #include "Welt2D.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) welt->release();
  20. name->release();
  21. }
  22. void Kamera2D::setName(const char* name)
  23. {
  24. this->name->setText(name);
  25. }
  26. void Kamera2D::setName(Text* name)
  27. {
  28. this->name->setText(name);
  29. }
  30. void Kamera2D::lookAtWorldPos(float x, float y)
  31. {
  32. rend |= wPos != Vertex(x, y);
  33. wPos.x = x;
  34. wPos.y = y;
  35. if (welt && welt->getWorldInfo().hasSize && welt->getWorldInfo().circular)
  36. {
  37. if (wPos.x < 0) wPos.x += (float)welt->getWorldInfo().size.x;
  38. if (wPos.y < 0) wPos.y += (float)welt->getWorldInfo().size.y;
  39. if (wPos.x > (float)welt->getWorldInfo().size.x)
  40. wPos.x -= (float)welt->getWorldInfo().size.x;
  41. if (wPos.y > (float)welt->getWorldInfo().size.y)
  42. wPos.y -= (float)welt->getWorldInfo().size.y;
  43. }
  44. }
  45. void Kamera2D::lookAtWorldPos(Vertex pos)
  46. {
  47. lookAtWorldPos(pos.x, pos.y);
  48. }
  49. void Kamera2D::lookAtWorldArea(float width, float height)
  50. {
  51. zoom = (float)getBreite() / width;
  52. // TODO have two scaling factors
  53. }
  54. void Kamera2D::setDrehung(float rotation)
  55. {
  56. rend |= this->rotation != rotation;
  57. this->rotation = rotation;
  58. }
  59. void Kamera2D::setZoom(float zoom)
  60. {
  61. rend |= this->zoom != zoom;
  62. this->zoom = zoom;
  63. }
  64. void Kamera2D::setWelt(Welt2D* welt, bool tick)
  65. {
  66. if (this->welt) this->welt->release();
  67. this->welt = welt;
  68. tickWelt = tick;
  69. rend = 1;
  70. }
  71. bool Kamera2D::tick(double time)
  72. {
  73. if (welt && tickWelt) rend |= welt->tick(time);
  74. return ZeichnungHintergrund::tick(time);
  75. }
  76. void Kamera2D::render(Bild& zRObj)
  77. {
  78. if (hatStyleNicht(Style::Sichtbar)) return;
  79. ZeichnungHintergrund::render(zRObj);
  80. if (!welt) return;
  81. lockZeichnung();
  82. if (!zRObj.setDrawOptions(innenPosition, innenSize))
  83. {
  84. unlockZeichnung();
  85. return;
  86. }
  87. matrix = Mat3<float>::translation((Vertex)gr / 2)
  88. * Mat3<float>::rotation(rotation) * Mat3<float>::scaling(zoom)
  89. * Mat3<float>::translation(-wPos);
  90. welt->render(matrix, gr, zRObj, *name);
  91. zRObj.releaseDrawOptions();
  92. unlockZeichnung();
  93. }
  94. Vertex Kamera2D::getWorldCoordinates(Punkt screenPos)
  95. {
  96. Vertex wKoord
  97. = (Mat3<float>::translation(wPos) * Mat3<float>::scaling(1 / zoom)
  98. * Mat3<float>::rotation(-rotation)
  99. * Mat3<float>::translation((Vertex)gr / -2))
  100. * (Vertex)screenPos;
  101. if (welt->getWorldInfo().circular && welt->getWorldInfo().hasSize
  102. && 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')) move = Vertex(0, -100 * (float)time);
  159. if (getTastenStand('a')) move = Vertex(-100 * (float)time, 0);
  160. if (getTastenStand('d')) move = Vertex(100 * (float)time, 0);
  161. if (getTastenStand('s')) move = Vertex(0, +100 * (float)time);
  162. if (move != Vertex(0, 0))
  163. {
  164. wPos += move.rotation(-rotation) * (1 / zoom);
  165. ret = 1;
  166. }
  167. if (getTastenStand('+'))
  168. {
  169. zoom += (zoom) * (float)time;
  170. ret = 1;
  171. }
  172. if (getTastenStand('-'))
  173. {
  174. zoom -= (zoom) * (float)time;
  175. if (zoom <= 0) zoom = 1;
  176. ret = 1;
  177. }
  178. return ret;
  179. }