Skip to content

Gamepad support #102

@konsumer

Description

@konsumer

I really like windy's support of keyboard/mouse, but for making a game-engine I want to support gamepads, primarily.

I made a light wrapper for libstem_gamepad, which is made in C, and works on Windows, Linux, and OSX. I could make it feel a bit more like windy's state-helpers (like buttonPressed / buttonDown / buttonReleased / buttonToggle) and make a PR to include this, if there is interest. Is there a more nim-ish way to do this? Is this outside the scope of what windy is trying to do?

Usage currently looks like this:

let windowSize = ivec2(320, 240)
let window = newWindow("null0", windowSize)
makeContextCurrent(window)
loadExtensions()

proc onGamepadAttached(device: ptr Gamepad_device; context: pointer) =
  var js = device[]
  echo "attached: " & $js.deviceID

proc onGamepadRemoved(device: ptr Gamepad_device; context: pointer) =
  var js = device[]
  echo "removed: " & $js.deviceID

proc onButtonDown (device: ptr Gamepad_device; buttonID: cuint; timestamp: cdouble; context: pointer) =
  var js = device[]
  echo "buttonDown(" & $js.deviceID & "): " & $buttonID

proc onButtonUp (device: ptr Gamepad_device; buttonID: cuint; timestamp: cdouble; context: pointer) =
  var js = device[]
  echo "buttonUp(" & $js.deviceID & "): " & $buttonID

proc onAxisMoved (device: ptr Gamepad_device; axisID: cuint; value: cfloat; lastValue: cfloat; timestamp: cdouble; context: pointer) =
  var js = device[]
  echo "axis(" & $js.deviceID & "): " & $axisID & " : " & $value

const GAMEPAD_POLL_ITERATION_INTERVAL=30
gamepad.deviceAttachFunc(onGamepadAttached)
gamepad.deviceRemoveFunc(onGamepadRemoved)
gamepad.buttonDownFunc(onButtonDown)
gamepad.buttonUpFunc(onButtonUp)
gamepad.axisMoveFunc(onAxisMoved)
gamepad.init()

var iterationsToNextPoll = GAMEPAD_POLL_ITERATION_INTERVAL
while not window.closeRequested:
  pollEvents()
  dec iterationsToNextPoll
  if iterationsToNextPoll == 0:
    gamepad.detectDevices()
    iterationsToNextPoll = GAMEPAD_POLL_ITERATION_INTERVAL
  gamepad.processEvents()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions