Camera2D.cpp 4.6 KB

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