Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nim wrapper #15

Open
konsumer opened this issue Apr 13, 2023 · 1 comment
Open

Nim wrapper #15

konsumer opened this issue Apr 13, 2023 · 1 comment

Comments

@konsumer
Copy link

konsumer commented Apr 13, 2023

Hi,
I love this library. Super-simple and seems to work, well. I made a nim-wrapper here.

Usage looks like this:

import gamepad

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

# in your loop
while whatever:
  dec iterationsToNextPoll
  if iterationsToNextPoll == 0:
    gamepad.detectDevices()
    iterationsToNextPoll = GAMEPAD_POLL_ITERATION_INTERVAL
  gamepad.processEvents()

You can see an example here.

I could probly wrap things further to deref things, and convert non-nim ints and things, but this works ok for my purposes.

@konsumer
Copy link
Author

konsumer commented May 9, 2023

I ended up making this wrapper. Linux and Mac are good, but I could really use some help getting it to build on windows.

when defined(windows):
  {.compile: "src/libstem_gamepad/source/gamepad/Gamepad_windows_dinput.c".}
  {.compile: "src/libstem_gamepad/source/gamepad/Gamepad_windows_mm.c".}
  {.passC: "-DFREEGLUT_STATIC".}
  {.passL: "-lXinput -ldinput8 -ldxguid -l/WbemUuid -lOle32 -lOleAut32".}

I get this:

D:\a\nim-gamepad\nim-gamepad\src\libstem_gamepad\source\gamepad\Gamepad_windows_mm.c:32:9: error: unknown type name 'JOYINFOEX'
   32 |         JOYINFOEX lastState;
      |         ^~~~~~~~~
D:\a\nim-gamepad\nim-gamepad\src\libstem_gamepad\source\gamepad\Gamepad_windows_mm.c:95:53: error: unknown type name 'JOYCAPS'
   95 | static char * getDeviceDescription(UINT joystickID, JOYCAPS caps) {
      |                                                     ^~~~~~~
D:\a\nim-gamepad\nim-gamepad\src\libstem_gamepad\source\gamepad\Gamepad_windows_mm.c: In function 'Gamepad_detectDevices':
D:\a\nim-gamepad\nim-gamepad\src\libstem_gamepad\source\gamepad\Gamepad_windows_mm.c:[14](https://github.com/konsumer/nim-gamepad/actions/runs/4923070589/jobs/8794479972#step:5:15)7:9: error: unknown type name 'JOYINFOEX'; did you mean 'CPINFOEX'?
  147 |         JOYINFOEX info;
      |         ^~~~~~~~~
      |         CPINFOEX

I am trying to build at the very least in Github action, because I don't have a Windows box to test on, easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant