summaryrefslogtreecommitdiff
path: root/src/systems/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/systems/input.c')
-rw-r--r--src/systems/input.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/systems/input.c b/src/systems/input.c
new file mode 100644
index 0000000..3b7ab9e
--- /dev/null
+++ b/src/systems/input.c
@@ -0,0 +1,77 @@
+#include "input.h"
+
+#include <GLFW/glfw3.h>
+
+#include "log.h"
+
+bool input_system_init(input_state *input, GLFWwindow *window) {
+ INFO("Input init");
+ input->window = window;
+ // Set everything to false. Could just set memory to zero but where's the fun in that
+ for (int i = 0; i < KEYCODE_MAX; i++) {
+ input->depressed_keys[i] = false;
+ input->just_pressed_keys[i] = false;
+ input->just_released_keys[i] = false;
+ }
+
+ return true;
+}
+
+void input_update(input_state *input) {
+ // --- update keyboard input
+
+ // if we go from un-pressed -> pressed, set as "just pressed"
+ // if we go from pressed -> un-pressed, set as "just released"
+ for (int i = 0; i < KEYCODE_MAX; i++) {
+ bool new_state = false;
+ if (glfwGetKey(input->window, i) == GLFW_PRESS) {
+ new_state = true;
+ } else {
+ new_state = false;
+ }
+ if (!input->depressed_keys[i] == false && new_state) {
+ input->just_pressed_keys[i] = true;
+ } else {
+ input->just_pressed_keys[i] = false;
+ }
+
+ if (input->depressed_keys[i] && !new_state) {
+ input->just_released_keys[i] = true;
+ } else {
+ input->just_released_keys[i] = false;
+ }
+
+ input->depressed_keys[i] = new_state;
+ }
+
+ // --- update mouse input
+
+ // cursor position
+ f64 current_x, current_y;
+ glfwGetCursorPos(input->window, &current_x, &current_y);
+ i32 quantised_cur_x = (i32)current_x;
+ i32 quantised_cur_y = (i32)current_y;
+
+ mouse_state new_mouse_state = { 0 };
+ new_mouse_state.x = quantised_cur_x;
+ new_mouse_state.y = quantised_cur_y;
+ new_mouse_state.x_delta = quantised_cur_x - input->mouse.x;
+ new_mouse_state.y_delta = quantised_cur_y - input->mouse.y;
+
+ // buttons
+ int left_state = glfwGetMouseButton(input->window, GLFW_MOUSE_BUTTON_LEFT);
+ // int right_state = glfwGetMouseButton(input->window, GLFW_MOUSE_BUTTON_RIGHT);
+
+ new_mouse_state.prev_left_btn_pressed = input->mouse.left_btn_pressed;
+ if (left_state == GLFW_PRESS) {
+ new_mouse_state.left_btn_pressed = true;
+ } else {
+ new_mouse_state.left_btn_pressed = false;
+ }
+
+ // this was dumb! need to also check button state changes lol
+ // if (new_mouse_state.x != input->mouse.x || new_mouse_state.y != input->mouse.y)
+ // TRACE("Mouse (x,y) = (%d,%d)", input->mouse.x, input->mouse.y);
+
+ input->mouse = new_mouse_state;
+}