minor cleanups
This commit is contained in:
parent
65e6ccb880
commit
4970a30ea9
2 changed files with 8 additions and 41 deletions
|
@ -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<i32>(mposx);
|
||||
app.user_input.mouse_pos.y = static_cast<i32>(mposy);
|
||||
}
|
||||
|
||||
update_and_render(arena, app, delta_time_ms);
|
||||
|
|
18
src/window.h
18
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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue