Page 1 of 1

SDL Joystick

Posted: Wed Apr 11, 2012 7:48 pm
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()

Re: SDL Joystick

Posted: Thu Apr 12, 2012 8:36 pm
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

Re: SDL Joystick

Posted: Thu Apr 12, 2012 8:54 pm
by J. Baker
Works great! :D