Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
InputSystem.hpp
Go to the documentation of this file.
1#pragma once
2#include <glm/glm.hpp>
3#include <GLFW/glfw3.h>
4#include "ecs/System.hpp"
5#include "ecs/Registry.hpp"
10
15class InputSystem : public System {
16public:
26
31 void update(float dt) override {
32
33 double x, y;
34 static double lastX = 0, lastY = 0;
35 glfwGetCursorPos(renderer.getWindow(), &x, &y);
36
37 if (!editorMode.isPlaying) {
38 switchMode(lastY, lastX);
39 } else {
40 // Play Mode: allow Escape key to toggle cursor lock
41 static bool prevEscDown = false;
42 GLFWwindow* window = renderer.getWindow();
43 bool escDown = glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS;
44 bool escPressed = escDown && !prevEscDown;
45 prevEscDown = escDown;
46
47 if (escPressed) {
48 editorMode.flyMode = !editorMode.flyMode;
49 if (editorMode.flyMode) {
50 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
51 } else {
52 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
53 }
54 // Update coordinate cache to prevent viewport jumps on recapture
55 double cx, cy;
56 glfwGetCursorPos(window, &cx, &cy);
57 lastX = cx;
58 lastY = cy;
59 }
60 }
61
62 double dx = x - lastX;
63 double dy = lastY - y; // invert Y
64 lastX = x;
65 lastY = y;
66
67 if (!editorMode.isPlaying) {
68 // Editor Mode: inputs routed to editor camera if fly mode active
69 if (!editorMode.flyMode) {
70 for (auto [e, input] : registry.view<InputComponent>()) {
71 input.look = glm::vec2(0.0f);
72 input.movement = glm::vec3(0.0f);
73 }
74 return;
75 }
76
77 for (auto [e, input] : registry.view<InputComponent>()) {
78 if (registry.has<EditorCamera>(e)) {
79 input.look = glm::vec2(dx, dy);
80
81 GLFWwindow* window = renderer.getWindow();
82 input.movement = glm::vec3(
83 (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) - (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS),
84 (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) - (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS),
85 (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) - (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
86 );
87 } else {
88 input.look = glm::vec2(0.0f);
89 input.movement = glm::vec3(0.0f);
90 }
91 }
92 } else {
93 // Play Mode: inputs routed to all standard gameplay entities, zeroing out editor camera
94 if (!editorMode.flyMode) {
95 for (auto [e, input] : registry.view<InputComponent>()) {
96 input.look = glm::vec2(0.0f);
97 input.movement = glm::vec3(0.0f);
98 }
99 return;
100 }
101
102 for (auto [e, input] : registry.view<InputComponent>()) {
103 if (!registry.has<EditorCamera>(e)) {
104 input.look = glm::vec2(dx, dy);
105
106 GLFWwindow* window = renderer.getWindow();
107 input.movement = glm::vec3(
108 (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) - (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS),
109 (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) - (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS),
110 (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) - (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
111 );
112 } else {
113 input.look = glm::vec2(0.0f);
114 input.movement = glm::vec3(0.0f);
115 }
116 }
117 }
118 }
119
125 void switchMode(double& lastY, double& lastX) {
126 static bool prevTabDown = false;
127
128 GLFWwindow* window = renderer.getWindow();
129 bool tabDown = glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS;
130
131 bool tabPressed = tabDown && !prevTabDown;
132 prevTabDown = tabDown;
133
134 if (tabPressed) {
135 editorMode.flyMode = !editorMode.flyMode;
136
138
139 double x, y;
140 glfwGetCursorPos(window, &x, &y);
141 lastX = x;
142 lastY = y;
143 }
144 }
145
146
151 GLFWwindow* window = renderer.getWindow();
152
153 if (editorMode.flyMode) {
154 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
155 }
156 else {
157 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
158 }
159 }
160
161
162private:
169};
void switchMode(double &lastY, double &lastX)
Toggles between editor/fly mode and handles cursor state cache.
Definition InputSystem.hpp:125
VulkanRenderer & renderer
Reference to the renderer.
Definition InputSystem.hpp:166
void applyCursorMode()
Configures GLFW cursor modes based on current fly/editor state.
Definition InputSystem.hpp:150
void update(float dt) override
Updates inputs for entities and handles cursor mode switches.
Definition InputSystem.hpp:31
InputSystem(Registry &reg, VulkanRenderer &renderer, EditorModeState &editorMode)
Construct a new Input System object.
Definition InputSystem.hpp:23
Registry & registry
Reference to the entity registry.
Definition InputSystem.hpp:164
EditorModeState & editorMode
Reference to the editor mode state.
Definition InputSystem.hpp:168
Coordinates entities and components, providing creation, removal, lookup, and views.
Definition Registry.hpp:30
Base class for ECS systems that process sets of entities.
Definition System.hpp:12
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
Tag component identifying the persistent editor-only camera entity.
Definition EditorCamera.hpp:9
Struct keeping track of active editor states (e.g. fly mode vs editor UI interaction).
Definition EditorModeState.hpp:7
Component representing input state for an entity (e.g., player or camera).
Definition inputComponent.hpp:9