GameInput v2.0 Beta for Windows (untested on Android)

Previous topic - Next topic

spacefractal

based from this thread:
https://www.glbasic.com/forum/index.php?topic=11565.0

Im have choosen to update and reimplement GameInput to v2.0 beta. In this version the remapping has been totally rewrote and is now supporting gamepads as well keyboards. The formatting of codes is now based on a sdl database, but also added to possible to remap keyboard as well.

to implement to your game, you can do something like this:

Code (glbasic) Select

// top of the code should have those defines:
?IFDEF WIN64
?DEFINE WIN32
?ENDIF

?IFDEF WIN32
?DEFINE DESKTOP
?ENDIF


// start of the code somewhere under loading.
LOCAL SaveFile$=PLATFORMINFO$("DOCUMENTS")+"/Yourgame/Yourgame_AutoSave.ini" // should been APPDATA on other platforms.
IF STEAMDECK=1 // if you want to do that, howover users can quit remapping by mouse button now (you could get stuck).
GameInput.init(SaveController$, 1) // the first two devices should not been possible to been remapped on steamdeck.
ELSE
GameInput.init(SaveController$, -1) // if you dont want possible to remap keyboard, set it to 0.
ENDIF

// Somewhere in the game input code.
// actuelley does nothing now on Windows, but only on Android.
LOCAL profilechanged=GameInput.update()

IF GameInput.Remap_IsRemapping()=1 // the function is doing remapping, dont do any game code
LOCAL result=GameInput.Remap_ScanCode() // return a sound/visual effect.
IF result=1 THEN SoundPlay("selectlevel")
IF result=2 THEN SoundPlay("confirmlevel")
RETURN
ENDIF

// polling input from the api (incude keyboard) example. far from all games should do this.
IF ISPAUSE OR S_Status$<>"play"
GameInput.EnableKeyboard(TRUE)
ELSE
GameInput.EnableKeyboard(FALSE) // in some cases, you dont want keyboard remapped ingame.
ENDIF

// This is something like that when you want to poll a joystick. In PowerUp Elevation, wbich
// im toke this snippet from, here all sticks controls the bird (include the right one).
LOCAL K1, K2, K3, K4, KJ
LOCAL Players=GameInput.GetNumberOfPlayers()

GameInput.SetDeadZone(0, 0.6) // analoge as digital with 0.6 as deadzone.
FOR i=0 TO Players-1
//IF GameInput.GetControllerName$(i)<>""
IF Players>0 // could been 0 on Android, but allways a least 1 on Windows throught as first player is on keyboard.

KL=KL+GameInput.Left(i)+GameInput.LeftStick_Left(i)+GameInput.RightStick_Left(i); IF KL>1 THEN KL=1
KR=KR+GameInput.Right(i)+GameInput.LeftStick_Right(i)+GameInput.RightStick_Right(i); IF KR>1 THEN KR=1
KU=KU+GameInput.Up(i)+GameInput.LeftStick_Up(i)+GameInput.RightStick_Up(i); IF KU>1 THEN KU=1
KD=KD+GameInput.Down(i)+GameInput.LeftStick_Down(i)+GameInput.RightStick_Down(i); IF KD=1 THEN KD=1

IF GameInput.ActionButton("A", i) OR GameInput.ActionButton("B", i) THEN KJ=1
IF GameInput.ActionButton("X", i) OR GameInput.ActionButton("Y", i) THEN KJ=1
IF GameInput.LeftTrigger(i) OR GameInput.ActionButton("RB", i) THEN KJ=1
IF GameInput.RightTrigger(i)=1 OR GameInput.ActionButton("LB", i) THEN TURNED=1

LOCAL SE=GameInput.ActionButton("select", i); // AB cab been swapped on some controllers
LOCAL PA=GameInput.ActionButton("parent", i); // AB cab been swapped on some controllers
ENDIF
NEXT

// somewhere in the menu to start the remap
// First argument, is if you want keyboard remapped or not.
IF OPTIONSELECTNAME$="remap"
// this is a full remap for a game controller, but only required buttons for keyboard.
// if a game controller dont have all buttons, the user will escape.
// if you dont want a full remap for a controller, you can remove those you dont uses.
GameInput.Remap_Start(0,"back:back/pause|*,start:start|*,leftx:Left Stick|Movement,a:Face Bottom|Jump,b:Face Right|Turn,x:Face Left|*,y:Face Up|*,leftshoulder:Back Left Top|*,rightshoulder:Back Right Top|*,LeftTrigger:Back Left Bottom|*,RightTrigger:Back Right Bottom|*,dpdown:Dpad Down|*,dpright:Dpad Right|*,dpup:DPad Up|*,dpleft:DPad Left|*,rightx:Right Stick|*,leftstick:Left Stick|*,rightstick:Right Stick|*")
ENDIF

// somewhere to print, when under remapping (allways max 3 lines)
// also escape or back button will allways quit remapping with no save.
IF GameInput.Remap_IsRemapping()=1
LOCAL line$=GameInput.Remap_GetText$(1)
BMFont.DrawText(line$, 0, PY+PH+SCALING*70, SIN(FADING*90)*1, SIN(FADING*90)*0.5*2, 0, -1, -2)

line$=GameInput.Remap_GetText$(2)
IF line$<>"" THEN BMFont.DrawText(line$, 0, PY+PH+SCALING*75, SIN(FADING*90)*1, SIN(FADING*90)*0.5*2, 0, -1, -2)

line$=GameInput.Remap_GetText$(3)
IF line$<>"" THEN BMFont.DrawText(line$, 0, PY+PH+SCALING*80, SIN(FADING*90)*1, SIN(FADING*90)*0.5*2, 0, -1, -2)

RETURN
ENDIF


this code is only tested on Windows, while the code for Android is there, im have nowhere tested it at all. Its might or might not work, or not compile at all. No idea. Im have still yet to update Android Studio, if im want to do that.

PS. PowerUp Elevation has been updated on steam, which uses this api (that game became test game).
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

Just updated to beta 2. Various bug fix and the user has now possible to swap a and b buttons, which is used in some regions and controller (like Nintendo ones) and deadzone can been set. etc you want analoge output or not.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

just updated, so remapping can been started from a mouse and then wait the mousebutton to been released before countine. This was done if a user should screw the remapping and cant remapping, they could been remapped by touch/clicking on the reamp and then start it again. A fail safe procudure.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

bigsofty

Lookin good Spacefractal, I'll test it when I get back to my house, tomorrow night.  :good:
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

spacefractal

auto fire and buffede controls was not the main scope of this code, not this time. the main scope was actuelly remapping on both keyboard and gamepad. None of my games would have a advancment of a auto fire at all anyway. You are fell free to add that of course if you want.

Also if you ask me, im would not care about it in a single player game.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

btw autofire is something that could been implemenet in same style as a fpscounter.... this is something im actuelly cannot implement in this gameinput as the api does not get a call when a button is released. but should been easy to been implemented in a game, that want to checking that.

Also auto fire wasent fun in PowerUp Elevation, lol. That bird just keep jumping low and could not jump high at all. In a shooter game, well, im would property implement a such of a cool down feature anyway. Im think even Warhawk did that for the Speccy Next port.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

bigsofty

GameInput.GetPlayerName$()?

type is not defined : TYPE 'GameInputs' has no member 'GetPlayerName$'

Possibly a custom function for your game?
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

spacefractal

nice spotted and fixed (its was a renamed function using the old function name).

Im did not uses any costume and actuelly im did not need to uses it at all for that game anyway, because im used GameInput.GetNumberOfPlayers() instead (that wasent in the game from the start)...

Alternative example could been this, if you want to check whick type of controller (keyboard or touch). The GameInput does not check about that. Just a idea, if you using a touch/mouse interface as well.

Code (glbasic) Select

IF GameInput.Left(i)+GameInput.LeftStick_Left(i)+GameInput.RightStick_Left(i)>0
KL=1
CONTROLLER$="KEYBOARD"
ENDIF

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

Just updated and fixed few bugs as well also added one feature as well, which was needed for Greedy Mouse, where joypads should been threated like a keyboard (when only want a partical joypadremap).

here is what im need to do in Greedy mouse:

Im init the GameInput with a 3th argurment:

Code (glbasic) Select

GameInput.init(SaveController$, -1, 1)


To start the remapping, this was required:
Code (glbasic) Select

S_Status$="remap"
GameInput.Remap_Start(0,"back:back or pause|*,leftx:Left Stick|Movement,a:Face Bottom|Zoom Hold,b:Face Right|Menu,x:Face Left|Zoom")



The above only remap required controls and actuelly skip back, even you see it, but mightbeen requirede.

For checking the remapping is ongoing im was need to do this:

Code (glbasic) Select

IF GameInput.Remap_IsRemapping()=1 // the function is doing remapping, dont do any game code
LOCAL result=GameInput.Remap_ScanCode() // return a sound/visual effect.
IF result>0 THEN SoundPlay("button2")
IF result=2
IF GameInput.Remap_IsRemapping()=0
S_Status$="Menu"
ENDIF
ENDIF
RETURN
ENDIF


Im was need to change the status when the user leave the remapping in this case, which wasent needed for PowerUp Elevation.

For showing the text in a more simple 2 lines, im did this:
Code (glbasic) Select

LOCAL GAMEREMAP2$=LCASE$(GameInput.Remap_GetText$(2,2))
LOCAL GAMEREMAP3$=LCASE$(GameInput.Remap_GetText$(3,2))
LOCAL GAMEREMAP1$=GAMEREMAP2$
LOCAL GAMEREMAP2$=GAMEREMAP3$
GAMEREMAP3$=""
IF INSTR(GAMEREMAP1$, "dont")=0 THEN GAMEREMAP1$=""
IF INSTR(GAMEREMAP2$, "that device")=0 THEN GAMEREMAP2$=""
IF INSTR(GAMEREMAP2$, "wait")=0
GAMEREMAP2$=REPLACE$(GAMEREMAP2$,"wait","")
GAMEREMAP2$=REPLACE$(GAMEREMAP2$,"to finish","")
ENDIF
GAMEREMAP1$="**"+GAMEREMAP1$
GAMEREMAP2$="**"+GAMEREMAP2$
IF INSTR(GAMEREMAP1$, "left")>0 THEN GAMEREMAP1$="re1"
IF INSTR(GAMEREMAP1$, "down")>0 THEN GAMEREMAP1$="re4"
IF INSTR(GAMEREMAP1$, "up")>0 THEN GAMEREMAP1$="re2"
IF INSTR(GAMEREMAP1$, "right")>0 THEN GAMEREMAP1$="re3"
IF INSTR(GAMEREMAP1$, "hold")>0 AND INSTR(GAMEREMAP1$, "back")=-1 THEN GAMEREMAP1$="re5"
IF INSTR(GAMEREMAP1$, "menu")>0 THEN GAMEREMAP1$="re6"
IF INSTR(GAMEREMAP1$, "zoom")>0 THEN GAMEREMAP1$="re7"
IF INSTR(GAMEREMAP1$, "on a device")>0 THEN GAMEREMAP1$="re8"


Here its was easier to translate the text to various languages when only using 2 lines.

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

Im got loan a switch pro controller. Its does not works under Either DirectInput (input scramled) nor its seen wont work with Xinput. But its works fine in a browser as well when playing a game in steam.

Just make sure you in steam dev mark Switch controller is NOT supported, then steam will remap it for you. If its still not work, then you can in steam, right click on a game, and in the controller setting, just enable steam input there. Now its works.

Im found this up for some reasons, its worked perfectly fine in Greedy Mouse, but not in PowerUp Elevation......
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

#10
Karma Miwa is a game im not have updated and im mean its only support xbox controllers. But if you in the Steam Works in this page and mark any controllers your game you do NOT support:

https://partner.steamgames.com/apps/controller

Then the game would works with any steam connected controller, even you internally only support the xbox controller.

Nice to know, but of course its only works when the game was launched from steam and its allways would uses the xbox glyph layout and the game seen this as a xbox 360 controller, but its would works, that most important.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/