From 4970a30ea9d56fc938254a6d803ed4caaac509cd Mon Sep 17 00:00:00 2001 From: silverweed Date: Tue, 16 Jul 2024 17:58:01 +0200 Subject: [PATCH] minor cleanups --- src/mainloop.cpp | 31 ++++++------------------------- src/window.h | 18 ++---------------- 2 files changed, 8 insertions(+), 41 deletions(-) diff --git a/src/mainloop.cpp b/src/mainloop.cpp index 3af2f85..8f63ccb 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -27,14 +27,13 @@ GLFWwindow *init_glfw(i32 desired_win_width, i32 desired_win_height) ); glfwMakeContextCurrent(window); - //glfwSetFramebufferSizeCallback(window, resize_keep_ratio); return window; } internal -void monitor_key(GLFWwindow *window, u16 *key_state, i32 glfw_key, Input_Key haru_key) { - u16& state = key_state[haru_key]; +void monitor_key(GLFWwindow *window, u8 *key_state, i32 glfw_key, Input_Key key) { + u8 &state = key_state[key]; if (glfwGetKey(window, glfw_key) == GLFW_PRESS) { if (!(state & KEY_STATE_IS_DOWN)) state |= KEY_STATE_JUST_PRESSED; state |= KEY_STATE_IS_DOWN; @@ -44,18 +43,6 @@ void monitor_key(GLFWwindow *window, u16 *key_state, i32 glfw_key, Input_Key har } } -internal -void monitor_mouse_btn(GLFWwindow *window, u16 *mouse_btn_state, i32 glfw_btn, Mouse_Button haru_btn) { - u16& state = mouse_btn_state[haru_btn]; - if (glfwGetMouseButton(window, glfw_btn) == GLFW_PRESS) { - if (!(state & MOUSE_BTN_STATE_IS_DOWN)) state |= MOUSE_BTN_STATE_JUST_PRESSED; - state |= MOUSE_BTN_STATE_IS_DOWN; - } else if (glfwGetMouseButton(window, glfw_btn) == GLFW_RELEASE) { - if (state & MOUSE_BTN_STATE_IS_DOWN) state |= MOUSE_BTN_STATE_JUST_RELEASED; - state &= ~MOUSE_BTN_STATE_IS_DOWN; - } -} - internal void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app) { @@ -98,9 +85,7 @@ void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app) app.inspected_file.size = file_size(app.inspected_file.stream); } - if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || - glfwWindowShouldClose(window)) - { + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window)) { running = false; break; } @@ -108,7 +93,7 @@ void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app) b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED); if (focused) { // Update keyboard - u16 *key_state = app.user_input.key_state; + u8 *key_state = app.user_input.key_state; monitor_key(window, key_state, GLFW_KEY_Q, KEY_Q); monitor_key(window, key_state, GLFW_KEY_UP, KEY_UP); monitor_key(window, key_state, GLFW_KEY_DOWN, KEY_DOWN); @@ -121,12 +106,8 @@ void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app) f64 mposx, mposy; glfwGetCursorPos(window, &mposx, &mposy); - app.user_input.mouse_pos.x = (i32)mposx; - app.user_input.mouse_pos.y = (i32)mposy; - - u16 *mouse_btn_state = app.user_input.mouse_btn_state; - monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_LEFT, MOUSE_BTN_LEFT); - monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_RIGHT, MOUSE_BTN_RIGHT); + app.user_input.mouse_pos.x = static_cast(mposx); + app.user_input.mouse_pos.y = static_cast(mposy); } update_and_render(arena, app, delta_time_ms); diff --git a/src/window.h b/src/window.h index aff4a62..b334b23 100644 --- a/src/window.h +++ b/src/window.h @@ -10,29 +10,15 @@ enum Input_Key { KEY_COUNT }; -enum Mouse_Button { - MOUSE_BTN_LEFT, - MOUSE_BTN_RIGHT, - - MOUSE_BTN_COUNT -}; - -enum Mouse_Button_State : u16 { - MOUSE_BTN_STATE_IS_DOWN = 0x1, - MOUSE_BTN_STATE_JUST_PRESSED = 0x2, - MOUSE_BTN_STATE_JUST_RELEASED = 0x4, -}; - -enum Key_State : u16 { +enum Key_State : u8 { KEY_STATE_IS_DOWN = 0x1, KEY_STATE_JUST_PRESSED = 0x2, KEY_STATE_JUST_RELEASED = 0x4, }; struct User_Input { - u16 key_state[KEY_COUNT]; + u8 key_state[KEY_COUNT]; - u16 mouse_btn_state[MOUSE_BTN_COUNT]; struct { i32 x; i32 y; } mouse_pos; };