use package glfw3
#local {
- keys_this_frame : [..] u32 // Keys currently being pressed this frame
- keys_pulse_frame : [..] u32 // Keys pressed during this frame, only set once per keypress
- keys_last_frame : [..] u32 // Keys being pressed in the last frame
+ keys_this_frame : [..] u32 // Keys currently being pressed this frame
+ keys_pulse_frame : [..] u32 // Keys pressed during this frame, only set once per keypress
+ keys_last_frame : [..] u32 // Keys being pressed in the last frame
- buttons_this_frame: [8] bool // Mouse buttons being pressed this frame
- buttons_last_frame: [8] bool // Mouse buttons being pressed last frame
+ buttons_this_frame: [8] bool // Mouse buttons being pressed this frame
+ buttons_last_frame: [8] bool // Mouse buttons being pressed last frame
}
input_update :: () {
glfwGetCursorPos(window, ^mouse_x, ^mouse_y);
- array.clear(^keys_pulse_frame);
- for keys_this_frame {
- if !array.contains(keys_last_frame, it) {
- keys_pulse_frame << it;
- }
- }
+ array.clear(^keys_pulse_frame);
+ for keys_this_frame {
+ if !array.contains(keys_last_frame, it) {
+ keys_pulse_frame << it;
+ }
+ }
}
input_post_update :: () {
- array.clear(^keys_pulse_frame);
- array.clear(^keys_last_frame);
- for keys_this_frame do keys_last_frame << it;
+ array.clear(^keys_pulse_frame);
+ array.clear(^keys_last_frame);
+ for keys_this_frame do keys_last_frame << it;
- for 8 do buttons_last_frame[it] = buttons_this_frame[it];
+ for 8 do buttons_last_frame[it] = buttons_this_frame[it];
- last_mouse_x = mouse_x;
- last_mouse_y = mouse_y;
+ last_mouse_x = mouse_x;
+ last_mouse_y = mouse_y;
}
input_get_keys_this_frame :: () -> [] u32 {
- return keys_pulse_frame;
+ return keys_pulse_frame;
}
is_key_down :: (key) => array.contains(keys_this_frame, key);
is_button_just_up :: (button) => !buttons_this_frame[button] && buttons_last_frame[button];
#local {
- last_mouse_x: f64;
- last_mouse_y: f64;
+ last_mouse_x: f64;
+ last_mouse_y: f64;
- mouse_x: f64;
- mouse_y: f64;
+ mouse_x: f64;
+ mouse_y: f64;
}
mouse_get_delta :: () -> (f64, f64) {
- return mouse_x - last_mouse_x, mouse_y - last_mouse_y;
+ return mouse_x - last_mouse_x, mouse_y - last_mouse_y;
}
mouse_get_delta_vector :: () -> Vector2 {
- dmx, dmy := mouse_get_delta();
- return .{ ~~dmx, ~~dmy };
+ dmx, dmy := mouse_get_delta();
+ return .{ ~~dmx, ~~dmy };
}
mouse_get_position :: () -> (f64, f64) {
- return mouse_x, mouse_y;
+ return mouse_x, mouse_y;
}
mouse_get_position_vector :: () -> Vector2 {
- return .{ ~~mouse_x, ~~mouse_y };
+ return .{ ~~mouse_x, ~~mouse_y };
}
input_bind_glfw_events :: (window: GLFWwindow_p) {
- glfwSetKeyCallback(window, INPUT_KEY_EVENT);
- glfwSetMouseButtonCallback(window, INPUT_BUTTON_EVENT);
+ glfwSetKeyCallback(window, INPUT_KEY_EVENT);
+ glfwSetMouseButtonCallback(window, INPUT_BUTTON_EVENT);
}
#local {
- INPUT_BUTTON_EVENT :: "__input_button_event"
- INPUT_KEY_EVENT :: "__input_key_event"
+ 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_PRESS {
+ buttons_this_frame[button] = true;
+ }
- if action == GLFW_RELEASE {
- buttons_this_frame[button] = false;
- }
+ 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_PRESS {
+ keys_this_frame << key;
+ }
- if action == GLFW_RELEASE {
- array.remove(^keys_this_frame, key);
- }
+ if action == GLFW_RELEASE {
+ array.remove(^keys_this_frame, key);
+ }
}