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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import gpu "../.."
import app "../../app"
main :: proc() {
state, ok := app.init_window(800, 600, "GPU - Input Actions")
if !ok {
return
}
defer app.shutdown(&state)
app.set_target_fps(&state, 60)
// Register actions with default bindings
move_up := app.register_action(&state, "move_up")
move_down := app.register_action(&state, "move_down")
move_left := app.register_action(&state, "move_left")
move_right := app.register_action(&state, "move_right")
action_a := app.register_action(&state, "action_a")
quit := app.register_action(&state, "quit")
// Default bindings -- WASD + arrows
app.bind_action_key(&state, move_up, .W)
app.bind_action_key(&state, move_up, .Arrow_Up)
app.bind_action_key(&state, move_down, .S)
app.bind_action_key(&state, move_down, .Arrow_Down)
app.bind_action_key(&state, move_left, .A)
app.bind_action_key(&state, move_left, .Arrow_Left)
app.bind_action_key(&state, move_right, .D)
app.bind_action_key(&state, move_right, .Arrow_Right)
app.bind_action_key(&state, action_a, .Space)
app.bind_action_mouse(&state, action_a, .Left)
app.bind_action_key(&state, quit, .Escape)
// Player state
px: f32 = 368
py: f32 = 268
size: f32 = 64
speed: f32 = 200
color := gpu.CYAN
// Rebind mode: press R to enter, then press a new key for move_up
rebind_mode := false
for !app.window_should_close(&state) {
dt := app.get_frame_time(&state)
// Toggle rebind mode with R
if app.is_key_pressed(&state, .R) {
rebind_mode = !rebind_mode
}
// In rebind mode, wait for a key press and rebind move_up to it
if rebind_mode {
for key in app.Key {
if key == .Unknown || key == .R || key == .Escape {
continue
}
if app.is_key_pressed(&state, key) {
app.unbind_action(&state, move_up)
app.bind_action_key(&state, move_up, key)
rebind_mode = false
break
}
}
}
// Movement via actions (screen coords: Y-down)
if app.is_action_down(&state, move_up) { py -= speed * dt }
if app.is_action_down(&state, move_down) { py += speed * dt }
if app.is_action_down(&state, move_left) { px -= speed * dt }
if app.is_action_down(&state, move_right) { px += speed * dt }
// Action button changes color
if app.is_action_pressed(&state, action_a) {
color = color == gpu.CYAN ? gpu.MAGENTA : gpu.CYAN
}
if app.is_action_pressed(&state, quit) {
break
}
// Clamp to screen
if px < 0 { px = 0 }
if py < 0 { py = 0 }
if px > 800 - size { px = 800 - size }
if py > 600 - size { py = 600 - size }
bg := rebind_mode ? gpu.DARKGRAY : gpu.CORNFLOWER_BLUE
frame := app.begin_frame(&state, bg)
_ = gpu.draw_rect(&frame, {
x = px,
y = py,
width = size,
height = size,
color = color,
})
indicator_color := gpu.GREEN
if rebind_mode {
indicator_color = gpu.RED
}
_ = gpu.draw_rect(&frame, {
x = 10,
y = 10,
width = 20,
height = 20,
color = indicator_color,
})
_ = app.submit_frame(&state, &frame)
}
}