minor cleanups

This commit is contained in:
silverweed 2024-07-16 17:58:01 +02:00
parent 65e6ccb880
commit 4970a30ea9
2 changed files with 8 additions and 41 deletions

View file

@ -27,14 +27,13 @@ GLFWwindow *init_glfw(i32 desired_win_width, i32 desired_win_height)
); );
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
//glfwSetFramebufferSizeCallback(window, resize_keep_ratio);
return window; return window;
} }
internal internal
void monitor_key(GLFWwindow *window, u16 *key_state, i32 glfw_key, Input_Key haru_key) { void monitor_key(GLFWwindow *window, u8 *key_state, i32 glfw_key, Input_Key key) {
u16& state = key_state[haru_key]; u8 &state = key_state[key];
if (glfwGetKey(window, glfw_key) == GLFW_PRESS) { if (glfwGetKey(window, glfw_key) == GLFW_PRESS) {
if (!(state & KEY_STATE_IS_DOWN)) state |= KEY_STATE_JUST_PRESSED; if (!(state & KEY_STATE_IS_DOWN)) state |= KEY_STATE_JUST_PRESSED;
state |= KEY_STATE_IS_DOWN; 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 internal
void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app) 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); app.inspected_file.size = file_size(app.inspected_file.stream);
} }
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window)) {
glfwWindowShouldClose(window))
{
running = false; running = false;
break; break;
} }
@ -108,7 +93,7 @@ void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app)
b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED); b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED);
if (focused) { if (focused) {
// Update keyboard // 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_Q, KEY_Q);
monitor_key(window, key_state, GLFW_KEY_UP, KEY_UP); monitor_key(window, key_state, GLFW_KEY_UP, KEY_UP);
monitor_key(window, key_state, GLFW_KEY_DOWN, KEY_DOWN); 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; f64 mposx, mposy;
glfwGetCursorPos(window, &mposx, &mposy); glfwGetCursorPos(window, &mposx, &mposy);
app.user_input.mouse_pos.x = (i32)mposx; app.user_input.mouse_pos.x = static_cast<i32>(mposx);
app.user_input.mouse_pos.y = (i32)mposy; app.user_input.mouse_pos.y = static_cast<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);
} }
update_and_render(arena, app, delta_time_ms); update_and_render(arena, app, delta_time_ms);

View file

@ -10,29 +10,15 @@ enum Input_Key {
KEY_COUNT KEY_COUNT
}; };
enum Mouse_Button { enum Key_State : u8 {
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 {
KEY_STATE_IS_DOWN = 0x1, KEY_STATE_IS_DOWN = 0x1,
KEY_STATE_JUST_PRESSED = 0x2, KEY_STATE_JUST_PRESSED = 0x2,
KEY_STATE_JUST_RELEASED = 0x4, KEY_STATE_JUST_RELEASED = 0x4,
}; };
struct User_Input { 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; struct { i32 x; i32 y; } mouse_pos;
}; };