factored input library
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 30 Jan 2022 18:54:43 +0000 (12:54 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 30 Jan 2022 18:54:43 +0000 (12:54 -0600)
src/main.onyx
src/utils/input.onyx

index b91e144882d8c36036432aa0dca4f9bf9baf174b..e4b272356bf8ffabf6b43f775c7d3a3f8440bf2b 100644 (file)
@@ -19,6 +19,7 @@ entity_manager: Entity_Manager;
 
 init :: () {
     create_window();
+    input_bind_glfw_events(window);
     glInit(glfwGetLoadProcAddress());
 
     glEnable(GL_TEXTURE);
@@ -61,6 +62,11 @@ update :: (dt: f32) {
     input_update();
     defer input_post_update();
 
+    if is_key_down(GLFW_KEY_ESCAPE) {
+        glfwSetWindowShouldClose(window, true);
+        return;
+    }
+
     entity_manager->update(dt);
 }
 
@@ -141,8 +147,6 @@ create_window :: () {
     glfwMakeContextCurrent(window);
     glfwSwapInterval(1);
     glfwSetWindowSizeCallback(window, "on_resize");
-    glfwSetKeyCallback(window, "on_key");
-    glfwSetMouseButtonCallback(window, "on_mouse_button");
 }
 
 #export "on_resize" (window: GLFWwindow_p, width, height: u32) {
@@ -152,19 +156,6 @@ create_window :: () {
     update_view_matrix();
 }
 
-#export "on_key" (window: GLFWwindow_p, key, scancode, action, mod: u32) {
-    if key == GLFW_KEY_ESCAPE && action == GLFW_PRESS {
-        glfwSetWindowShouldClose(window, true);
-    }
-
-    handle_key_event(key, scancode, action, mod);
-}
-
-#export "on_mouse_button" (window: GLFWwindow_p, button, action, mod: u32) {
-    handle_button_event(button, action, mod);
-}
-
-
 main :: (args) => {
     if !glfwInit() {
         println("Failed to initialize GLFW");
index 02a7dc61a77c438da247918850ef0340ac341587..c8d7523c9f3951e424885a8b6f27c660ddd39733 100644 (file)
@@ -23,30 +23,10 @@ input_post_update :: () {
        last_mouse_y = mouse_y;
 }
 
-handle_key_event :: (key, scancode, action, mod: u32) {
-       if action == GLFW_PRESS {
-               keys_this_frame << key;
-       }
-
-       if action == GLFW_RELEASE {
-               array.remove(^keys_this_frame, key);
-       }
-}
-
 is_key_down      :: (key) => array.contains(keys_this_frame, key);
 is_key_just_down :: (key) => array.contains(keys_this_frame, key) && !array.contains(keys_last_frame, key);
 is_key_just_up   :: (key) => !array.contains(keys_this_frame, key) && array.contains(keys_last_frame, key);
 
-handle_button_event :: (button, action, mod: u32) {
-       if action == GLFW_PRESS {
-               buttons_this_frame[button] = true;
-       }
-
-       if action == GLFW_RELEASE {
-               buttons_this_frame[button] = false;
-       }
-}
-
 is_button_down      :: (button) => buttons_this_frame[button];
 is_button_just_down :: (button) => buttons_this_frame[button] && !buttons_last_frame[button];
 is_button_just_up   :: (button) => !buttons_this_frame[button] && buttons_last_frame[button];
@@ -65,4 +45,35 @@ mouse_get_delta :: () -> (f64, f64) {
 
 mouse_get_position :: () -> (f64, f64) {
        return mouse_x, mouse_y;
-}
\ No newline at end of file
+}
+
+
+input_bind_glfw_events :: (window: GLFWwindow_p) {
+       glfwSetKeyCallback(window, INPUT_KEY_EVENT);
+       glfwSetMouseButtonCallback(window, INPUT_BUTTON_EVENT);
+}
+
+#local {
+       INPUT_BUTTON_EVENT :: "__input_button_event"
+       INPUT_KEY_EVENT    :: "__input_key_event"
+}
+
+#export INPUT_BUTTON_EVENT (window: GLFWwindow_p, button, action, mod: u32) {
+       if action == GLFW_PRESS {
+               buttons_this_frame[button] = true;
+       }
+
+       if action == GLFW_RELEASE {
+               buttons_this_frame[button] = false;
+       }
+}
+
+#export INPUT_KEY_EVENT (window: GLFWwindow_p, key, scancode, action, mod: u32) {
+       if action == GLFW_PRESS {
+               keys_this_frame << key;
+       }
+
+       if action == GLFW_RELEASE {
+               array.remove(^keys_this_frame, key);
+       }
+}