diff options
Diffstat (limited to 'src/fw/input.odin')
| -rw-r--r-- | src/fw/input.odin | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/fw/input.odin b/src/fw/input.odin new file mode 100644 index 0000000..a3cbe25 --- /dev/null +++ b/src/fw/input.odin @@ -0,0 +1,76 @@ +package fw + +import "core:c" +import "vendor:glfw" + +Keyboard_Input :: union { + Key, + Mouse_Button, +} + +Controller_Input :: union { +} + +Keybind :: struct { + input: Keyboard_Input, + pressed: bool, + just_pressed: bool, +} + +key_down_last_frame: #sparse [Key]bool +key_just_down: #sparse [Key]bool + +mouse_down_last_frame: #sparse [Mouse_Button]bool +mouse_just_down: #sparse [Mouse_Button]bool + +@(private) +_update_input :: proc() { + for key in Key { + status := glfw.GetKey(window, c.int(key)) + key_pressed := status == glfw.PRESS + key_just_down[key] = key_pressed && !key_down_last_frame[key] + + key_down_last_frame[key] = key_pressed + } + + for mb in Mouse_Button { + status := glfw.GetMouseButton(window, c.int(mb)) + mouse_pressed := status == glfw.PRESS + mouse_just_down[mb] = mouse_pressed && !mouse_down_last_frame[mb] + + mouse_down_last_frame[mb] = mouse_pressed + } +} + +is_keybind_down :: proc(keybind: Keybind) -> bool { + switch val in keybind.input { + case Key: + return glfw.GetKey(window, c.int(val)) == glfw.PRESS + case Mouse_Button: + return glfw.GetMouseButton(window, c.int(val)) == glfw.PRESS + } + + assert(false) + return false +} + +is_keybind_just_down :: proc(keybind: Keybind) -> bool { + switch val in keybind.input { + case Key: return key_just_down[val] + case Mouse_Button: return mouse_just_down[val] + } + + assert(false) + return false +} + +get_mouse_pos :: proc() -> (mouse_pos: Vec2) { + w_width, w_height := glfw.GetWindowSize(window) + mx, my := glfw.GetCursorPos(window) + + mouse_pos = Vec2{f32(mx), f32(mx)} + mouse_pos /= Vec2{f32(w_width), f32(w_height)} + mouse_pos *= Vec2{f32(framebuf.color.size.x), f32(framebuf.color.size.y)} + // mouse_pos += state.camera.target + return +} |
