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