Programs (.BAS), Tutorials and Libraries ($INCLUDE, DECLARE LIBRARY, ...) > Libraries

SDL GamePad support without pointers!

(1/4) > >>

unseenmachine:
This method i made for UnseenGDK, now a single lib so everyone can use it without needing GDK.

Put this code in a file called QBJoy.h (use notepad) and save it in your QB64 folder.

--- Code: ---void InitGamepad()
{
  SDL_Init(SDL_INIT_JOYSTICK);
}

int GetAxis(int Index, int Axis)
{
  SDL_Joystick *Js;
  Js = SDL_JoystickOpen(Index);
  int x = SDL_JoystickGetAxis(Js, Axis);
  return (x);
}

int GetButton(int Index, int Button)
{
  SDL_Joystick *Js;
  Js = SDL_JoystickOpen(Index);
  return(SDL_JoystickGetButton(Js, Button));
}


--- End code ---
And then plug in a gamepad/joystick and run this in QB64.

--- Code: ---InitGamepad
IF SDL_NumJoysticks > 0 THEN
  DO
    LOCATE 1, 1
    PRINT "Stick 1  X axis : ", GetAxis(0, 0)
    PRINT "Stick 1  Y axis : ", GetAxis(0, 1)
    PRINT
    PRINT "Stick 2 Y axis : ", GetAxis(0, 3)
    PRINT "Stick 2 X xxis: ", GetAxis(0, 4)
    PRINT
    PRINT "Z axis : ", GetAxis(0, 2)
    PRINT
    FOR i% = 0 TO 11
      PRINT "Button " + STR$(i%), GetButton(0, i%)
    NEXT
  LOOP
ELSE
  PRINT "No game pad detected"
  SLEEP 5
  SYSTEM
END IF


DECLARE LIBRARY "QBJoy"
  SUB InitGamepad
  FUNCTION GetAxis (BYVAL Index%, BYVAL Axis%)
  FUNCTION GetButton (BYVAL Index%, BYVAL Axis%)
  FUNCTION SDL_NumJoysticks
END DECLARE

--- End code ---

Please report any bugs/errors/suggestions for improvement.

Thanks folks,

Unseen

SkyCharger001:
What about HUD (or D-PAD)? I was hoping to use this for a NES emulator I'm working on.

unseenmachine:
Ok i will add them in.

Clippy:
Is this the same joystick stuff that Darth posted? If not, get your heads together and submit a combined post for the SDL functions so that I can add it to the Library page including any header files.

Gamepads or other devices could be used separately, so please post them separately.

Since GDK is your invention and your property I don't want references to that in the WIKI Library section. I will post a thread in the Community forum for links to member sites if you want them published.

I will be looking for Library submissions here.

Ted

unseenmachine:
Hey Clippy,

This is totally differant to the way Galleon showed us and Darth completed. It was the best way I could think of to implement basic gamepad support without the dodgy pointer stuff, which is confusing at best. Allthough i use this method in GDK, it has a differant header and GDK prefixes to the commands. I made this one for everyone so you dont have to use GDK just to get gamepad support. You in no way have to mention where it originated.

Hold on posting it in the wiki until i get the HAT/DPad support implemented and Darth gets back to us.

Thanks,

Unseen

Navigation

[0] Message Index

[#] Next page

Go to full version