summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-30 14:14:01 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-04 19:26:55 +0000
commit2eeaeafb40a6b04b811714c793fb97ce4de41254 (patch)
tree55cea15af898afe6f0b81a3e6e2e7debe618a8b6 /gfx
parentBasic support for loading a heightmap from an image (diff)
downloadilt-2eeaeafb40a6b04b811714c793fb97ce4de41254.tar.bz2
ilt-2eeaeafb40a6b04b811714c793fb97ce4de41254.tar.xz
ilt-2eeaeafb40a6b04b811714c793fb97ce4de41254.zip
Split into main app and library the rest for testing
Diffstat (limited to 'gfx')
-rw-r--r--gfx/inputHandler.h18
-rw-r--r--gfx/manualCameraController.cpp65
-rw-r--r--gfx/manualCameraController.h25
3 files changed, 108 insertions, 0 deletions
diff --git a/gfx/inputHandler.h b/gfx/inputHandler.h
new file mode 100644
index 0000000..5b426c7
--- /dev/null
+++ b/gfx/inputHandler.h
@@ -0,0 +1,18 @@
+#ifndef INPUT_HANDLER_H
+#define INPUT_HANDLER_H
+
+#include <special_members.hpp>
+
+union SDL_Event;
+
+class InputHandler {
+public:
+ InputHandler() = default;
+ virtual ~InputHandler() = default;
+
+ DEFAULT_MOVE_COPY(InputHandler);
+
+ virtual bool handleInput(SDL_Event &) = 0;
+};
+
+#endif
diff --git a/gfx/manualCameraController.cpp b/gfx/manualCameraController.cpp
new file mode 100644
index 0000000..36f3312
--- /dev/null
+++ b/gfx/manualCameraController.cpp
@@ -0,0 +1,65 @@
+#include "manualCameraController.h"
+#include <gfx/gl/camera.h>
+#include <gfx/gl/shader.h>
+
+bool
+ManualCameraController::handleInput(SDL_Event & e)
+{
+ switch (e.type) {
+ case SDL_KEYDOWN:
+ switch (e.key.keysym.sym) {
+ case SDLK_LCTRL:
+ case SDLK_RCTRL:
+ ctrl = true;
+ return true;
+ }
+ break;
+ case SDL_KEYUP:
+ switch (e.key.keysym.sym) {
+ case SDLK_LCTRL:
+ case SDLK_RCTRL:
+ ctrl = false;
+ return true;
+ }
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ switch (e.button.button) {
+ case SDL_BUTTON_RIGHT:
+ SDL_SetRelativeMouseMode(SDL_TRUE);
+ mrb = true;
+ return true;
+ }
+ break;
+ case SDL_MOUSEBUTTONUP:
+ switch (e.button.button) {
+ case SDL_BUTTON_RIGHT:
+ SDL_SetRelativeMouseMode(SDL_FALSE);
+ mrb = false;
+ return true;
+ }
+ break;
+ case SDL_MOUSEMOTION:
+ if (mrb) {
+ motion = e.motion;
+ }
+ return true;
+ }
+ return false;
+}
+
+void
+ManualCameraController::updateCamera(Camera * camera, Shader * shader) const
+{
+ if (motion) {
+ if (ctrl) {
+ camera->RotateY(-0.01F * motion->xrel);
+ camera->Pitch(-0.01F * motion->yrel);
+ }
+ else {
+ camera->MoveRight(motion->xrel);
+ camera->SlideForward(motion->yrel);
+ }
+ shader->setView(camera->GetViewProjection());
+ motion.reset();
+ }
+}
diff --git a/gfx/manualCameraController.h b/gfx/manualCameraController.h
new file mode 100644
index 0000000..112ec5c
--- /dev/null
+++ b/gfx/manualCameraController.h
@@ -0,0 +1,25 @@
+#ifndef MANUAL_CAMERA_CONTROLLER_H
+#define MANUAL_CAMERA_CONTROLLER_H
+
+#include "game/worldobject.h"
+#include "inputHandler.h"
+#include <SDL2/SDL.h>
+#include <gfx/camera_controller.h>
+#include <optional>
+
+class Camera;
+class Shader;
+
+class ManualCameraController : public CameraController, public InputHandler {
+public:
+ bool handleInput(SDL_Event & e) override;
+
+ void tick(TickDuration) override { }
+
+ void updateCamera(Camera * camera, Shader * shader) const override;
+
+private:
+ bool ctrl {false}, mrb {false};
+ mutable std::optional<SDL_MouseMotionEvent> motion;
+};
+#endif