Author Topic: Simple, Consolidated Game Device Input  (Read 5558 times)

0 Members and 1 Guest are viewing this topic.

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Simple, Consolidated Game Device Input
« on: January 11, 2022, 01:02:54 am »
Hi All,

I put a good deal of work into a simple, consolidated game input API that allows for simple detection and testing of input from keyboard, mouse or game controller devices and thought I would share as there have been recent discussions on this topic.

With this approach device input can be detected with the following:

Code: QB64: [Select]
  1. Dim Shared diMoveLeft As GXDeviceInput
  2. GXDeviceInputDetect diMoveLeft
  3.  

The program will wait until a keyboard key, mouse button, or game controller button or axis movement has been detected.  Then later in your code when you want to test for that device input, simply do this:

Code: QB64: [Select]
  1. If GXDeviceInputTest(diMoveLeft) Then
  2.    ' Do your move left logic here
  3.  

I've added specific logic for linux and maxos to handle the fact that keyboard button presses are not returned by the _DEVICEINPUT method on those environments so that the behavior can be consistent cross-platform.  This is actually related to my very first post on the forum: https://qb64forum.alephc.xyz/index.php?topic=4448.msg138691#msg138691.

I've included a few demonstration programs in the attached zip.

1) The detect.bas program will display the last detected input and print information about the device input:
Code: QB64: [Select]
  1. '$Include:'gx/gx.bi'
  2. _Title "Detect Device Input"
  3.  
  4. Print "Waiting for input..."
  5.     Dim di As GXDeviceInput
  6.     GXDeviceInputDetect di
  7.     Cls
  8.     Print "Device #:    " + Str$(di.deviceId)
  9.     Print "Device Name: " + GXDeviceName(di.deviceId)
  10.     Print "Device Type: " + GXDeviceTypeName(di.deviceType)
  11.     Print "Input Type:  " + GXInputTypeName(di.inputType)
  12.     Print "Input Id:    " + Str$(di.inputId)
  13.     If di.deviceType = GXDEVICE_KEYBOARD Then
  14.         Print "Key Name: " + GXKeyButtonName(di.inputId)
  15.     End If
  16.     Print "Input Value: " + Str$(di.inputValue)
  17.     Print
  18.  
  19. Sub GXOnGameEvent (e As GXEvent): End Sub
  20. '$Include:'gx/gx.bm'
  21.  

detect.png


2) The input.bas demonstrates detecting input for certain actions and then testing for those inputs in a game loop:
Code: QB64: [Select]
  1. '$Include:'gx/gx.bi'
  2. _Title "Test Device Input"
  3.  
  4. GXSceneCreate 500, 300
  5. GXFrameRate 10
  6.  
  7. Dim Shared dleft As GXDeviceInput
  8. Print "Press Left... ";
  9. GXDeviceInputDetect dleft
  10. PrintDeviceInput dleft
  11.  
  12. Dim Shared dright As GXDeviceInput
  13. Print "Press Right...";
  14. GXDeviceInputDetect dright
  15. PrintDeviceInput dright
  16.  
  17. Dim Shared djump As GXDeviceInput
  18. Print "Press Jump...";
  19. GXDeviceInputDetect djump
  20. PrintDeviceInput djump
  21.  
  22.  
  23. GXSceneStart
  24.  
  25. Sub GXOnGameEvent (e As GXEvent)
  26.     Dim ldown As Integer
  27.     Select Case e.event
  28.         Case GXEVENT_UPDATE
  29.             If GXKeyDown(GXKEY_ESC) Then GXSceneStop
  30.  
  31.         Case GXEVENT_DRAWSCREEN
  32.             GXDrawText GXFONT_DEFAULT, 10, 10, "Press one or more mapped inputs:"
  33.             If GXDeviceInputTest(dleft) Then
  34.                 GXDrawText GXFONT_DEFAULT, 100, 100, "Left"
  35.             End If
  36.             If GXDeviceInputTest(dright) Then
  37.                 GXDrawText GXFONT_DEFAULT, 150, 100, "Right"
  38.             End If
  39.             If GXDeviceInputTest(djump) Then
  40.                 GXDrawText GXFONT_DEFAULT, 125, 125, "Jump"
  41.             End If
  42.     End Select
  43.  
  44.  
  45. Sub PrintDeviceInput (di As GXDeviceInput)
  46.     Print GXDeviceName(di.deviceId) + " : ";
  47.     Print GXInputTypeName(di.inputType) + " : ";
  48.     Print Str$(di.inputId) + " : ";
  49.     If di.deviceType = GXDEVICE_KEYBOARD Then
  50.         Print " [ "; GXKeyButtonName(di.inputId); " ]";
  51.     Else
  52.         Print ;
  53.     End If
  54.     Print " : "; di.inputValue
  55.     Print
  56.  
  57. '$Include:'gx/gx.bm'
  58.  

3) The nes-config.bas program is an example of using the API in a device setup screen with the ability to save the config:
Code: QB64: [Select]
  1. '$INCLUDE:'gx/gx.bi'
  2. '$INCLUDE:'control-config.bi'
  3.  
  4. GXSceneCreate 256, 240
  5. GXSceneScale 2
  6.  
  7. ControlConfigInit
  8. ControlConfigShow
  9.  
  10. GXSceneStart
  11.  
  12. Sub GXOnGameEvent (e As GXEvent)
  13.     OnControlConfigEvent e
  14.  
  15. '$INCLUDE:'gx/gx.bm'
  16. '$INCLUDE:'control-config.bm'
  17.  

nes-config.png


Feel free to use any of the examples in your own projects.  I would be very interested in any feedback you have.
* device-input.zip (Filesize: 73.81 KB, Downloads: 239)

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Simple, Consolidated Game Device Input
« Reply #1 on: January 14, 2022, 01:49:33 pm »
I put a good deal of work into a simple, consolidated game input API that allows for simple detection and testing of input from keyboard, mouse or game controller devices and thought I would share as there have been recent discussions on this topic.
...
Feel free to use any of the examples in your own projects.  I would be very interested in any feedback you have.

Thanks for sharing.
One question - is this cross-platform (ie will it work on QB64 for Linux & Mac)?

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: Simple, Consolidated Game Device Input
« Reply #2 on: January 14, 2022, 02:50:02 pm »
Yes!  In fact, there is additional logic in the library to handle the fact that keyboard DeviceInput support is different between windows and the other supported platforms.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Simple, Consolidated Game Device Input
« Reply #3 on: January 15, 2022, 02:46:28 pm »
Yes!  In fact, there is additional logic in the library to handle the fact that keyboard DeviceInput support is different between windows and the other supported platforms.

Great, thanks. I'll give it a look...

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: Simple, Consolidated Game Device Input
« Reply #4 on: January 15, 2022, 07:57:44 pm »
Great, I’d be interested to hear any feedback you have.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Simple, Consolidated Game Device Input
« Reply #5 on: January 15, 2022, 08:06:48 pm »
I need to send you code that I did for Xinput using Windows API
Shuwatch!