Harbor

branch main
showing the latest snapshot on main
main.odin 2.9 KB · Plain text
gpu/examples/input_actions/main.odin 0644 Raw
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)
	}
}