SDL Joystick

Mac OSX specific forum
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

SDL Joystick

Post by J. Baker »

Here's an import of the SDL Joystick. You'll need to download the SDL Runtime Library and put the "SDL.framework" in, "/System/Library/Frameworks".
http://www.libsdl.org/download-1.2.php

NOTE: Press joystick button 2 when you want to close/kill it. ;)

Thanks to Shardik for informing me about PeekS() to get the joystick name. :D

Code: Select all

ImportC "/System/Library/Frameworks/SDL.framework/SDL"
  SDL_Init(flags)
  SDL_Quit()
  SDL_InitSubSystem (flags)
  SDL_QuitSubSystem(flags)
  SDL_NumJoysticks()
  SDL_JoystickName(device_index)
  SDL_JoystickOpen(device_index)
  SDL_JoystickOpened(device_index)
  SDL_JoystickIndex(*joystick)
  SDL_JoystickNumAxes(*joystick)
  SDL_JoystickNumBalls(*joystick)
  SDL_JoystickNumHats(*joystick)
  SDL_JoystickNumButtons(*joystick)
  SDL_JoystickUpdate()
  SDL_JoystickEventState(state)
  SDL_JoystickGetAxis(*joystick, axis)
  SDL_JoystickGetHat(*joystick, hat)
  SDL_JoystickGetBall(*joystick, ball, *dx, *dy)
  SDL_JoystickGetButton(*joystick, button)
  SDL_JoystickClose(*joystick)
EndImport

#SDL_INIT_JOYSTICK = 512
#SDL_ENABLE = 1 ; SDL_JoystickEventState
#SDL_QUERY = -1 ; SDL_JoystickEventState
#SDL_IGNORE = 0 ; SDL_JoystickEventState
#SDL_JOYBUTTONDOWN = 10
#SDL_JOYBUTTONUP = 11
#SDL_JOYAXISMOTION = 7

SDL_Init(#SDL_INIT_JOYSTICK)

 ;SDL_InitSubSystem(#SDL_INIT_JOYSTICK)

  Num = SDL_NumJoysticks()
  Debug Num
  
    JoyName = SDL_JoystickName(0)
    Debug PeekS(JoyName)
  
  joy = SDL_JoystickOpen(0)
  Debug joy
    
    Index = SDL_JoystickIndex(joy) 
    Debug Index
    
  NumButtons = SDL_JoystickNumButtons(joy)
  Debug NumButtons
    
    NumAxis = SDL_JoystickNumAxes(joy)
    Debug NumAxis
       
  Repeat
    
    SDL_JoystickEventState(#SDL_ENABLE)
    
      B = SDL_JoystickGetButton(joy, 1)

      Debug B
      
      X = SDL_JoystickGetAxis(joy, 0)
      
      Debug X
      
      Y = SDL_JoystickGetAxis(joy, 1)
      
      Debug Y
    
    Delay(10)
    
  Until SDL_JoystickGetButton(joy, 2)
    
   SDL_JoystickClose(joy)
  
 ;SDL_QuitSubSystem(#SDL_INIT_JOYSTICK)

SDL_Quit()
Last edited by J. Baker on Tue Aug 14, 2012 8:18 pm, edited 1 time in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: SDL Joystick

Post by Shardik »

This is a simple example which demonstrates how to move around a
sprite on a windowed screen with a joystick or joypad. Before testing
it you have to download the SDL library from Joe's above link and to
copy "SDL.framework" into "/System/Library/Frameworks".

Code: Select all

EnableExplicit

ImportC "/System/Library/Frameworks/SDL.framework/SDL"
  SDL_Init(Flags.L)
  SDL_Quit()
  SDL_NumJoysticks()
  SDL_JoystickEventState(State.I)
  SDL_JoystickGetAxis(*Joystick, Axis.I)
  SDL_JoystickOpen(JoystickIndex.I)
  SDL_JoystickClose(*Joystick)
EndImport

#SDL_ENABLE = 1
#SDL_INIT_JOYSTICK = 512

Define Button.A
Define *Joystick
Define NumJoysticks.I
Define x.I
Define xMove.W
Define y.I
Define yMove.W

If InitSprite() = 0
  MessageRequester("Error", "InitSprite() failed!")
  End
EndIf

If SDL_Init(#SDL_INIT_JOYSTICK) <> 0
  MessageRequester("Error", "SDL initialization failed!")
Else
  NumJoysticks = SDL_NumJoysticks()
  
  Select NumJoysticks
    Case 0
      MessageRequester("Error", "Sorry, but no joystick is currently connected!")
    Case 1 To 8
      *Joystick = SDL_JoystickOpen(0)
        
      If *Joystick = 0
        MessageRequester("Error", "Opening of joystick #1 failed!")
      EndIf
    Default
      MessageRequester("Error", "Querying connected joysticks failed!")
  EndSelect      

  If *Joystick
    OpenWindow(0, 270, 100, 250, 220, "Move square with joypad")
    OpenWindowedScreen(WindowID(0), 0, 0, 400, 370, 0, 0, 0)
    CreateSprite(0, 20, 20)

    If StartDrawing(SpriteOutput(0))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
    EndIf

    x = 115
    y = 100

    Repeat
      SDL_JoystickEventState(#SDL_ENABLE)
      xMove = SDL_JoystickGetAxis(*Joystick, 0)
      yMove = SDL_JoystickGetAxis(*Joystick, 1)

      If xMove > 10000 And x < 230
        x = x + 1
      ElseIf xMove < -10000 And x > 0
        x = x - 1
      EndIf

      If yMove > 10000 And y < 200
        y = y + 1
      ElseIf yMove < -10000 And y > 0
        y = y - 1
      EndIf

      If WindowEvent() = #PB_Event_CloseWindow
        Break
      EndIf

      FlipBuffers() 
      ClearScreen(RGB(0, 0, 0))
      DisplaySprite(0, x, y)
      Delay(10)
    ForEver

    SDL_JoystickClose(*Joystick)
  EndIf

  SDL_Quit()
EndIf
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: SDL Joystick

Post by J. Baker »

Works great! :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply