gamepad
gamepad
¶
Gamepads.
Controllers are opened automatically when they connect (the event loop calls
openGamepad), and any already connected when the game starts are opened up
front, so they are reported from the first frame. The gamepadpressed,
gamepadreleased and gamepadaxis callbacks report input. You can also poll
with isGamepadDown and gamepadAxis. Built on the SDL3 gamepad API, so
anything SDL recognizes as a gamepad works.
toGamepadButton ¶
proc toGamepadButton(b: SDL_GamepadButton): GamepadButton
The nim2d GamepadButton for an SDL gamepad button.
Parameters
-
b(SDL_GamepadButton)
Returns
GamepadButton
toGamepadAxis ¶
proc toGamepadAxis(a: SDL_GamepadAxis): GamepadAxis
The nim2d GamepadAxis for an SDL gamepad axis.
Parameters
-
a(SDL_GamepadAxis)
Returns
GamepadAxis
openGamepad ¶
proc openGamepad(id: GamepadId)
Open a controller. The event loop calls this when one connects, so games
normally never do.
Parameters
-
id(GamepadId)
closeGamepad ¶
proc closeGamepad(id: GamepadId)
Close a controller. The event loop calls this when one disconnects.
Parameters
-
id(GamepadId)
openConnectedGamepads ¶
proc openConnectedGamepads()
Open every controller the system currently reports. newNim2d calls this at
startup and connectedGamepads calls it before reading the list, so a
controller plugged in before the game launched is opened and reported from
the first frame rather than waiting for SDL's connect event, which can lag
past the opening frames. Opening is idempotent, so repeated calls are cheap.
connectedGamepads ¶
proc connectedGamepads(): seq[GamepadId]
The ids of every connected controller. Controllers connected before the game
launched are included from the first frame, not only once SDL reports them.
Returns
seq[GamepadId]
isGamepadDown ¶
proc isGamepadDown(id: GamepadId; button: GamepadButton): bool
Whether a controller button is currently held.
Parameters
-
id(GamepadId) -
button(GamepadButton)
Returns
bool
gamepadAxis ¶
proc gamepadAxis(id: GamepadId; axis: GamepadAxis): float
Axis value from -1 to 1 (triggers run 0 to 1).
Parameters
-
id(GamepadId) -
axis(GamepadAxis)
Returns
float