FryPad - virtual control system for game input

BlitzMax Forums/BlitzMax Programming/FryPad - virtual control system for game input

Fry Crayola(Posted 2009) [#1]
FryPad is a free module that provides an abstract control interface that sits between your game logic and the various control systems (keyboards, joypads etc.) the player wishes to use.

After creating a virtual joypad, you add new buttons or control sticks to it (in any combination and number), and map these to keys, joystick buttons, axes and the pov hat, and also the mouse buttons.

Then, when your game wants to poll for any control input, instead of managing all the different control options available, you simply poll the virtual joypad then retrieve the information from its various controls as you desire.

Both virtual buttons and control sticks support JoyHit() and JoyDown() equivalents, whilst sticks also provide direct access to their axes, and the angle at which the stick is pushed. Buttons and sticks may be either digital or analogue depending on your needs, and you can individually set the deadzones on any virtual control.

You can map any number of real inputs to a virtual control, although take care not to map the same input to multiple controls as it can cause difficulties, particularly with the JoyHit() equivalents (as the number of hits gets cleared).

Benefits of this can include mapping both keyboard and joypad controls to the same virtual control, allowing the player to change between keyboard and pad control at a whim. You can also use it to have several buttons carry out the same action, such as both the dpad and the analogue stick of a pad being used for control. Also, it's easily redefinable, clearing the mappings and adding new ones to provide the same functionality with a different set of keys.

Get it here.

The download comes complete with a sample program which will set up a virtual joypad with all the functionality of an Xbox 360 controller, together with keyboard controls to use it alongside a 360 pad. It's fully commented with explanations on how things are set up.

The module's totally free for you to use and abuse as you wish. A bit of credit's always nice if you do use it for anything but that's entirely up to you. Any problems, just let me know. I'm not currently looking to add new features unless there are any terrific ideas, but definitely if any bugs crop up I'll be eager to fix them.

MikeHart(Posted 2009) [#2]
Awesome, thanks a lot!

siread(Posted 2009) [#3]
Nice one Fry. I will check it out. :)

siread(Posted 2009) [#4]
This is pretty sweet. One thing... Shouldn't TFryPadStick.Angle() return ATan2(y, x)?

Htbaa(Posted 2009) [#5]
Download link seems to be broken.

Fry Crayola(Posted 2009) [#6]
Odd. I'll get that sorted out when I post up the latest version tomorrow.

Edit: That'll have to wait for the weekend, I've got a bunch of stuff on over the next few days. Sorry about any delay.

Regarding the TFryPadStick.Angle() thing, I guess that's just a remnant of some old code I had. I never actually looked into ATan2 when I was making this a couple of weeks ago, just used old stuff. Swapping X and Y obviously changed where 0 degrees was. Easily tweaked though.

The newer version tidies some things up, allows you to access the buttons and sticks of the pad without having to store the objects themselves all the time (useful for just passing the whole joypad as a parameter) and a few other things.

Volker(Posted 2009) [#7]
*Sigh*, the download is broken.

BlitzSupport(Posted 2009) [#8]
I've uploaded it here for now:

(Fry, just let me know if you want it removed -- I'm just assuming the download link has expired.)

Volker(Posted 2009) [#9]
Very accommodating, thanks.

Fry Crayola(Posted 2009) [#10]
Cheers James.

I'll have a newer version hosted at the weekend. Of course, I said that last time but I mean it this time!

Volker(Posted 2009) [#11]
Hi Fry,

the 'control test.bmx' works fine with my Saitek X52 Flight Controller.
But with my Saitek Gamepad I get no response from the prg.
The system shows it and a 'Print joydown(1,0)' works fine
under Blitzmax.
Do you have a hint where to start?

Fry Crayola(Posted 2009) [#12]
If JoyDown(1, 0) works fine, and that button has been properly mapped to a frypad button, then it ought to work. One exception is if the same real button is mapped to two or more virtual buttons, although that only affects hits and not buttons held down.

If you haven't already, try commenting out every button mapping bar button 1 and see if it works or not.

I've currently only tested it with a 360 pad. I'll give it a whirl with my PS2 pad adapter as well, but that's it for my own testing.

Volker(Posted 2009) [#13]
It's not in your mod, it looks like a bug in Blitzmax.
The gamepad can't be read, whatever port I use, if there
isn't called Joycount() before.
My X52 doesn't need it, curiously.
Perhaps you want to call it once in the sample to
avoid others spending their time for this s**t.

Fry Crayola(Posted 2009) [#14]
Odd issue. Thanks for the heads up on it, I'll add it to the code.

Fry Crayola(Posted 2009) [#15]
Uploaded the latest version to here

Adds the fix for angles that Siread pointed out, allows you to flush the controls, lets you access the buttons and sticks directly on the virtual pad instead of retrieving them first (much cleaner), and a few other things.

I'll still be adding new things from time to time, as I'm working on things that use it and adding features as I need them.

As ever, any questions just ask and I hope I'm alert enough to see them!

Volker(Posted 2009) [#16]
Thanks Fry, I finished today the integration of joypad
an keyboard steering in my little project. Works really fine
here and the structur of the mod is very nice.

siread(Posted 2009) [#17]
Cheers fry. I will be updating my New Star Framework project with this. :)

doswelk(Posted 2010) [#18]
Here's me resurrecting another dead thread!

Thanks for this it has been most helpful and useful tool when coding stuff for myself.


My 4 year off and on project (first ever game I've written) is nearly there
, except for one snag.

I can now (using Frypad) easily test for joypad/keyboard movement as long as the config I want to use is used.

How do I (using Frypad) let the user re-map the keys/joypad controls as they want?

I know I just need to have some code to wait for input from a keyboard or joypad but seem a bit thick of how I do it!

Can some helpful soul post up a bit of code to help me?