It is currently Wed Jun 19, 2013 6:46 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: SDL Joystick
PostPosted: Wed Apr 11, 2012 7:48 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1624
Location: USA
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:
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()

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Last edited by J. Baker on Tue Aug 14, 2012 8:18 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: SDL Joystick
PostPosted: Thu Apr 12, 2012 8:36 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 821
Location: Germany
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:
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


Top
 Profile  
 
 Post subject: Re: SDL Joystick
PostPosted: Thu Apr 12, 2012 8:54 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1624
Location: USA
Works great! :D

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye