CameraController.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "CameraController.h"
  2. #include <algorithm>
  3. #include "Input/InputState.h"
  4. #include "ComponentsManager.h"
  5. #include "../framework.h"
  6. #include "../Input/Mouse/Mouse.h"
  7. #include "../Input/Keyboard/Keyboard.h"
  8. #include "../Input/Bindings/KeyboardBinding.h"
  9. #define GLUT_CURSOR_RIGHT_ARROW 0x0000
  10. #define GLUT_CURSOR_NONE 0x0065
  11. using namespace TLAC::Input;
  12. using namespace TLAC::Utilities;
  13. namespace TLAC::Components
  14. {
  15. CameraController::CameraController()
  16. {
  17. }
  18. CameraController::~CameraController()
  19. {
  20. delete ToggleBinding;
  21. delete ForwardBinding;
  22. delete BackwardBinding;
  23. delete LeftBinding;
  24. delete RightBinding;
  25. delete UpBinding;
  26. delete DownBinding;
  27. delete ClockwiseBinding;
  28. delete CounterClockwiseBinding;
  29. delete ZoomInBinding;
  30. delete ZoomOutBinding;
  31. delete FastBinding;
  32. delete SlowBinding;
  33. }
  34. const char* CameraController::GetDisplayName()
  35. {
  36. return "camera_controller";
  37. }
  38. void CameraController::Initialize(ComponentsManager* manager)
  39. {
  40. componentsManager = manager;
  41. printf("[TLAC] CameraController::Initialize(): Initialized\n");
  42. for (int i = 0; i < sizeof(cameraSetterAddresses) / sizeof(void*); i++)
  43. {
  44. DWORD oldProtect;
  45. VirtualProtect((void*)cameraSetterAddresses[i], sizeof(uint8_t), PAGE_EXECUTE_READWRITE, &oldProtect);
  46. originalSetterBytes[i] = *(uint8_t*)cameraSetterAddresses[i];
  47. }
  48. ToggleBinding = new Binding();
  49. ToggleBinding->AddBinding(new KeyboardBinding(VK_F3));
  50. ForwardBinding = new Binding();
  51. ForwardBinding->AddBinding(new KeyboardBinding('W'));
  52. BackwardBinding = new Binding();
  53. BackwardBinding->AddBinding(new KeyboardBinding('S'));
  54. LeftBinding = new Binding();
  55. LeftBinding->AddBinding(new KeyboardBinding('A'));
  56. RightBinding = new Binding();
  57. RightBinding->AddBinding(new KeyboardBinding('D'));
  58. UpBinding = new Binding();
  59. UpBinding->AddBinding(new KeyboardBinding(VK_SPACE));
  60. DownBinding = new Binding();
  61. DownBinding->AddBinding(new KeyboardBinding(VK_CONTROL));
  62. ClockwiseBinding = new Binding();
  63. ClockwiseBinding->AddBinding(new KeyboardBinding('E'));
  64. CounterClockwiseBinding = new Binding();
  65. CounterClockwiseBinding->AddBinding(new KeyboardBinding('Q'));
  66. ZoomInBinding = new Binding();
  67. ZoomInBinding->AddBinding(new KeyboardBinding('R'));
  68. ZoomOutBinding = new Binding();
  69. ZoomOutBinding->AddBinding(new KeyboardBinding('F'));
  70. FastBinding = new Binding();
  71. FastBinding->AddBinding(new KeyboardBinding(VK_SHIFT));
  72. SlowBinding = new Binding();
  73. SlowBinding->AddBinding(new KeyboardBinding(VK_MENU));
  74. camera = (Camera*)CAMERA_ADDRESS;
  75. }
  76. void CameraController::Update()
  77. {
  78. return;
  79. }
  80. void CameraController::UpdateInput()
  81. {
  82. if (ToggleBinding->AnyTapped())
  83. {
  84. SetControls(!GetIsEnabled());
  85. return;
  86. }
  87. if (!GetIsEnabled())
  88. return;
  89. auto keyboard = Keyboard::GetInstance();
  90. auto mouse = Mouse::GetInstance();
  91. bool forward = ForwardBinding->AnyDown();
  92. bool backward = BackwardBinding->AnyDown();
  93. bool left = LeftBinding->AnyDown();
  94. bool right = RightBinding->AnyDown();
  95. bool up = UpBinding->AnyDown();
  96. bool down = DownBinding->AnyDown();
  97. bool fast = FastBinding->AnyDown();
  98. bool slow = SlowBinding->AnyDown();
  99. bool clockwise = ClockwiseBinding->AnyDown();
  100. bool counterclockwise = CounterClockwiseBinding->AnyDown();
  101. bool zoomin = ZoomInBinding->AnyDown();
  102. bool zoomout = ZoomOutBinding->AnyDown();
  103. float speed = GetElapsedTime() * (fast ? fastSpeed : slow ? slowSpeed : normalSpeed);
  104. if (forward ^ backward)
  105. camera->Position += PointFromAngle(verticalRotation + (forward ? +0.0f : -180.0f), speed);
  106. if (left ^ right)
  107. camera->Position += PointFromAngle(verticalRotation + (right ? +90.0f : -90.0f), speed);
  108. if (up ^ down)
  109. camera->Position.Y += speed * (up ? +0.25f : -0.25f);
  110. if (clockwise ^ counterclockwise)
  111. camera->Rotation += speed * (clockwise ? -1.0f : +1.0f);
  112. if (zoomin ^ zoomout)
  113. {
  114. camera->HorizontalFov += speed * (zoomin ? -1.0f : +1.0f);
  115. camera->HorizontalFov = std::clamp(camera->HorizontalFov, +1.0f, +170.0f);
  116. }
  117. if (mouse->HasMoved())
  118. {
  119. SetMouseWindowCenter();
  120. auto delta = mouse->GetDeltaPosition();
  121. verticalRotation += delta.x * sensitivity;
  122. horizontalRotation -= delta.y * (sensitivity / 5.0f);
  123. horizontalRotation = std::clamp(horizontalRotation, -75.0f, +75.0f);
  124. }
  125. ((InputState*)*(uint64_t*)INPUT_STATE_PTR_ADDRESS)->HideCursor();
  126. Vec2 focus = PointFromAngle(verticalRotation, 1.0f);
  127. camera->Focus.X = camera->Position.X + focus.X;
  128. camera->Focus.Z = camera->Position.Z + focus.Y;
  129. camera->Focus.Y = camera->Position.Y + PointFromAngle(horizontalRotation, 5.0f).X;
  130. }
  131. void CameraController::SetControls(bool value)
  132. {
  133. if (GetIsEnabled() == value)
  134. return;
  135. SetIsEnabled(value);
  136. componentsManager->SetUpdateGameInput(!value);
  137. printf("[TLAC] CameraController::SetControls(): enabled = %s\n", GetIsEnabled() ? "true" : "false");
  138. typedef void __stdcall _glutSetCursor(int);
  139. auto glutSetCursor = (_glutSetCursor*)GLUT_SET_CURSOR_ADDRESS;
  140. // hide cursor
  141. glutSetCursor(value ? GLUT_CURSOR_NONE : GLUT_CURSOR_RIGHT_ARROW);
  142. if (value)
  143. {
  144. // disable camera setters
  145. for (int i = 0; i < sizeof(cameraSetterAddresses) / sizeof(void*); i++)
  146. *(uint8_t*)cameraSetterAddresses[i] = RET_OPCODE;
  147. // set initial camera angle
  148. Vec2 camXz = Vec2(camera->Position.X, camera->Position.Z);
  149. Vec2 focusXz = Vec2(camera->Focus.X, camera->Focus.Z);
  150. verticalRotation = AngleFromPoints(camXz, focusXz);
  151. horizontalRotation = 0;
  152. camera->Rotation = defaultRotation;
  153. camera->HorizontalFov = defaultFov;
  154. }
  155. else
  156. {
  157. // restore camera setters
  158. for (int i = 0; i < sizeof(cameraSetterAddresses) / sizeof(void*); i++)
  159. *(uint8_t*)cameraSetterAddresses[i] = originalSetterBytes[i];
  160. }
  161. }
  162. void CameraController::SetMouseWindowCenter()
  163. {
  164. RECT windowRect = framework::GetWindowBounds();
  165. int centerX = windowRect.left + (windowRect.right - windowRect.left) / 2;
  166. int centerY = windowRect.top + (windowRect.bottom - windowRect.top) / 2;
  167. Mouse::GetInstance()->SetPosition(centerX, centerY);
  168. }
  169. bool CameraController::GetIsEnabled()
  170. {
  171. return isEnabled;
  172. }
  173. void CameraController::SetIsEnabled(bool value)
  174. {
  175. isEnabled = value;
  176. }
  177. }