aboutsummaryrefslogtreecommitdiff
path: root/src/input.odin
blob: 6c02349a7967b21e829f104834e9bd715a01079b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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_left: Keybind,
  move_right: Keybind,
  jump: 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_left.input = sapp.Keycode.A
  input.move_right.input = sapp.Keycode.D
  input.jump.input = sapp.Keycode.SPACE
}

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
}