From: Brendan Hansen Date: Sun, 30 Jan 2022 18:54:43 +0000 (-0600) Subject: factored input library X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=886c753887729b1b1df188655ea0a92f1eaddca8;p=bar-game.git factored input library --- diff --git a/src/main.onyx b/src/main.onyx index b91e144..e4b2723 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -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"); diff --git a/src/utils/input.onyx b/src/utils/input.onyx index 02a7dc6..c8d7523 100644 --- a/src/utils/input.onyx +++ b/src/utils/input.onyx @@ -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); + } +}