Added controller support.
Added SDL3 controller mappings and loaded with raylib. Updated input manager to get active controller, check for disconnect etc and then use that within the poll input proc.
This commit is contained in:
+61
-14
@@ -20,30 +20,77 @@ Input_Event :: struct {
|
||||
pause_pressed: bool,
|
||||
}
|
||||
|
||||
// Currently active controller
|
||||
active_gamepad_id: i32 = -1
|
||||
|
||||
update_active_gamepad :: proc() {
|
||||
// is there currently a controller, if so check if its still connected.
|
||||
if active_gamepad_id != -1 {
|
||||
if rl.IsGamepadAvailable(active_gamepad_id) {
|
||||
// do nothing still connected
|
||||
return
|
||||
}
|
||||
// else we lost connection
|
||||
active_gamepad_id = -1
|
||||
rl.TraceLog(.WARNING, "Controller disconnected!!")
|
||||
}
|
||||
|
||||
for i: i32 = 0; i < 4; i += 1 {
|
||||
if rl.IsGamepadAvailable(i) {
|
||||
active_gamepad_id = i
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
poll_input :: proc() -> Input_Event {
|
||||
input_event := Input_Event{}
|
||||
input_dir := rl.Vector2{0, 0}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Continuous Input ie movement code
|
||||
// --------------------------------------------------------------------------
|
||||
if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { input_dir.y -= 1 }
|
||||
if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { input_dir.y += 1 }
|
||||
update_active_gamepad()
|
||||
gamepad_active := active_gamepad_id != -1
|
||||
gid := active_gamepad_id
|
||||
|
||||
if input_dir.x != 0 || input_dir.y != 0 {
|
||||
input_dir = rl.Vector2Normalize(input_dir)
|
||||
// --------------------------------------------------------------------------
|
||||
// Continuous Input (keyboard + gamepad d-pad)
|
||||
// --------------------------------------------------------------------------
|
||||
if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) || (gamepad_active && rl.IsGamepadButtonDown(gid, .LEFT_FACE_UP)) { input_dir.y -= 1 }
|
||||
if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) || (gamepad_active && rl.IsGamepadButtonDown(gid, .LEFT_FACE_DOWN)) { input_dir.y += 1 }
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Analog stick input gamepads
|
||||
// --------------------------------------------------------------------------
|
||||
if gamepad_active {
|
||||
DEADZONE :: 0.25
|
||||
axis_y := rl.GetGamepadAxisMovement(gid, .LEFT_Y)
|
||||
|
||||
// apply deadzone and combine with input if moved
|
||||
if abs(axis_y) > DEADZONE { input_dir.y = axis_y }
|
||||
}
|
||||
input_event.move_dir = input_dir
|
||||
|
||||
// Normalize so diagonal movement isn't faster than cardinal directions
|
||||
if input_dir.x != 0 || input_dir.y != 0 do input_event.move_dir = rl.Vector2Normalize(input_dir)
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// actions single press ie menu navigation etc
|
||||
// --------------------------------------------------------------------------
|
||||
input_event.up_pressed = rl.IsKeyPressed(.W) || rl.IsKeyPressed(.UP)
|
||||
input_event.down_pressed = rl.IsKeyPressed(.S) || rl.IsKeyPressed(.DOWN)
|
||||
input_event.left_pressed = rl.IsKeyPressed(.A) || rl.IsKeyPressed(.LEFT)
|
||||
input_event.right_pressed = rl.IsKeyPressed(.D) || rl.IsKeyPressed(.RIGHT)
|
||||
input_event.confirm_pressed = rl.IsKeyPressed(.ENTER) || rl.IsKeyPressed(.SPACE)
|
||||
input_event.pause_pressed = rl.IsKeyPressed(.P)
|
||||
input_event.up_pressed = rl.IsKeyPressed(.W) || rl.IsKeyPressed(.UP) ||
|
||||
(gamepad_active && rl.IsGamepadButtonPressed(gid, .LEFT_FACE_UP))
|
||||
|
||||
input_event.down_pressed = rl.IsKeyPressed(.S) || rl.IsKeyPressed(.DOWN) ||
|
||||
(gamepad_active && rl.IsGamepadButtonPressed(gid, .LEFT_FACE_DOWN))
|
||||
|
||||
input_event.left_pressed = rl.IsKeyPressed(.A) || rl.IsKeyPressed(.LEFT) ||
|
||||
(gamepad_active && rl.IsGamepadButtonPressed(gid, .LEFT_FACE_LEFT))
|
||||
|
||||
input_event.right_pressed = rl.IsKeyPressed(.D) || rl.IsKeyPressed(.RIGHT) ||
|
||||
(gamepad_active && rl.IsGamepadButtonPressed(gid, .LEFT_FACE_RIGHT))
|
||||
|
||||
input_event.confirm_pressed = rl.IsKeyPressed(.ENTER) || rl.IsKeyPressed(.SPACE) ||
|
||||
(gamepad_active && rl.IsGamepadButtonPressed(gid, .RIGHT_FACE_DOWN))
|
||||
|
||||
input_event.pause_pressed = rl.IsKeyPressed(.SPACE) ||
|
||||
(gamepad_active && rl.IsGamepadButtonPressed(gid, .MIDDLE_RIGHT))
|
||||
|
||||
return input_event
|
||||
}
|
||||
Reference in New Issue
Block a user