aboutsummaryrefslogtreecommitdiff
path: root/src/input.odin
diff options
context:
space:
mode:
authorXander Swan <email>2025-12-03 09:52:13 -0500
committerXander Swan <email>2025-12-03 09:52:13 -0500
commitb6cf1d104da53ec9c30dc45c35d9de48812f0afc (patch)
tree49421206d8d866e82a8a743cba5ced494cbfc785 /src/input.odin
Initial commit
Diffstat (limited to 'src/input.odin')
-rw-r--r--src/input.odin60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/input.odin b/src/input.odin
new file mode 100644
index 0000000..0dad408
--- /dev/null
+++ b/src/input.odin
@@ -0,0 +1,60 @@
+package demonchime
+
+import sapp "shared:sokol/app"
+
+KeybindInput :: union {
+ sapp.Keycode,
+ sapp.Mousebutton,
+}
+
+Keybind :: struct {
+ input: KeybindInput,
+ pressed: bool,
+ just_pressed: bool,
+}
+
+Input :: struct {
+ move_up: Keybind,
+ move_left: Keybind,
+ move_down: Keybind,
+ move_right: Keybind,
+
+ key_down: [sapp.MAX_KEYCODES]bool,
+ key_just_down: [sapp.MAX_KEYCODES]bool,
+ mouse_down: [sapp.MAX_MOUSEBUTTONS]bool,
+ mouse_just_down: [sapp.MAX_MOUSEBUTTONS]bool,
+}
+
+init_keybinds :: proc(input: ^Input) {
+ input.move_up.input = sapp.Keycode.W
+ input.move_left.input = sapp.Keycode.A
+ input.move_down.input = sapp.Keycode.S
+ input.move_right.input = sapp.Keycode.D
+}
+
+input_event :: proc(event: ^sapp.Event, input: ^Input) {
+ #partial switch event.type {
+ case .KEY_DOWN:
+ input.key_down[event.key_code] = true
+ input.key_just_down[event.key_code] = true
+ case .KEY_UP:
+ input.key_down[event.key_code] = false
+ case .MOUSE_DOWN:
+ input.mouse_down[event.key_code] = true
+ input.mouse_just_down[event.key_code] = true
+ case .MOUSE_UP:
+ input.mouse_down[event.key_code] = false
+ }
+}
+
+is_keybind_down :: proc(input: Input, keybind: Keybind) -> bool {
+ switch val in keybind.input {
+ case sapp.Keycode:
+ return input.key_down[val]
+ case sapp.Mousebutton:
+ return input.mouse_down[val]
+ }
+
+ assert(false)
+ return false
+}