analog joystick

Share your advanced PureBasic knowledge/code with the community.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

analog joystick

Post by gnasen »

hey,
when I played around with the joystick, I noticed that PB does not support analog joystick input. So I searched a little bit and found a very good code at PureArea.net and modified it for a more easy usage.

Maybe someone may need it, much thanks to the orignial author who did all the api stuff ;)

Code: Select all

; German forum: http://www.purebasic.fr/german/viewtopic.php?t=10149&postdays=0&postorder=asc&start=60
; Author: GPI (updated for PB 4.00 by hardfalcon), modified by gnasen
; Date: 02. February 2003, modified 30.07.2010
; OS: Windows
; Demo: No

EnableExplicit

Enumeration
  #joy_button_1  = %0000000000000001
  #joy_button_2  = %0000000000000010
  #joy_button_3  = %0000000000000100
  #joy_button_4  = %0000000000001000
  #joy_button_5  = %0000000000010000
  #joy_button_6  = %0000000000100000
  #joy_button_7  = %0000000001000000
  #joy_button_8  = %0000000010000000
  #joy_button_9  = %0000000100000000
  #joy_button_10 = %0000001000000000
  #joy_button_11 = %0000010000000000
  #joy_button_12 = %0000100000000000
  #joy_button_13 = %0001000000000000
  #joy_button_14 = %0010000000000000
  #joy_button_15 = %0100000000000000
  #joy_button_16 = %1000000000000000
  #joy_axis_x
  #joy_axis_y
  #joy_axis_z
  #joy_axis_r
  #joy_axis_u
  #joy_axis_v
  #joy_pov
EndEnumeration

Macro m_axis(field)
  If raw
    ProcedureReturn g_joy_status\field
  Else
    ProcedureReturn g_joy_status\field * 100 / 65535
  EndIf
EndMacro

Structure joy_infoex
  size.l 
  flags.l 
  xpos.l 
  ypos.l 
  zpos.l 
  rpos.l 
  upos.l 
  vpos.l 
  buttons.l 
  buttonNumber.l 
  pov.l 
  reserved1.l 
  reserved2.l 
EndStructure 
Global g_joy_status.joy_infoex 

Procedure.i joy_examine(number.i=0)
  
  g_joy_status\size=SizeOf(joy_infoex) 
  g_joy_status\flags=#JOY_RETURNALL 
  
  If number >= 0 
    If joyGetPosEx_(number,@g_joy_status) = 0 
      ProcedureReturn #true
    EndIf
  EndIf
  
  ProcedureReturn #False
  
EndProcedure
Procedure.i joy_pressed(input.i,raw.i=#False)
  
  Select input
    Case #joy_axis_x
      m_axis(xpos)
    Case #joy_axis_y
      m_axis(ypos)
    Case #joy_axis_z
      m_axis(zpos)
    Case #joy_axis_r
      m_axis(rpos)
    Case #joy_axis_u
      m_axis(upos)
    Case #joy_axis_v
      m_axis(vpos)
    Case #joy_pov
      If raw
        ProcedureReturn g_joy_status\pov
      Else
        If g_joy_status\pov = 65535
          ProcedureReturn -1
        Else  
          ProcedureReturn g_joy_status\pov * 360 / 36000
        EndIf
      EndIf
    Case #joy_button_1 To #joy_button_16
      If raw
        ProcedureReturn g_joy_status\buttons
      Else
        If g_joy_status\buttons & input
          ProcedureReturn #true
        Else
          ProcedureReturn #False
        EndIf
      EndIf
    Default
      ProcedureReturn #False
  EndSelect
  
EndProcedure


;///// Demo /////
Define Event.i, text.s, a.i

If OpenWindow(0,0,0,300,400,"demo",#PB_Window_SystemMenu) 
  
  TextGadget(0,0,0,300,400,"")
  
  Repeat
    Event = WindowEvent()
    
    If Not joy_examine() ;examine gamepad (before reading the inputs!)
      MessageRequester("woops","no joystick found at this number. Try another number (1-16?)")
      End
    EndIf

    text = "axis: " + #crlf$
    text + "x: " + Str(joy_pressed(#joy_axis_x)) + " raw: " + Str(joy_pressed(#joy_axis_x,#true)) + #crlf$
    text + "y: " + Str(joy_pressed(#joy_axis_y)) + " raw: " + Str(joy_pressed(#joy_axis_y,#true)) + #crlf$
    text + "z: " + Str(joy_pressed(#joy_axis_z)) + " raw: " + Str(joy_pressed(#joy_axis_z,#true)) + #crlf$
    text + "r: " + Str(joy_pressed(#joy_axis_r)) + " raw: " + Str(joy_pressed(#joy_axis_r,#true)) + #crlf$
    text + "u: " + Str(joy_pressed(#joy_axis_u)) + " raw: " + Str(joy_pressed(#joy_axis_u,#true)) + #crlf$
    text + "v: " + Str(joy_pressed(#joy_axis_v)) + " raw: " + Str(joy_pressed(#joy_axis_v,#true)) + #crlf$
    
    If joy_pressed(#joy_pov) = -1
      text + "pov: center"
    Else
      text + "pov: " + Str(joy_pressed(#joy_pov)) + " degree"
    EndIf
    text + " raw: " + Str(joy_pressed(#joy_pov,#true)) + #crlf$
     
    For a=1 To 16
      text + "button " + Str(a) + ": " + Str(joy_pressed(Pow(2,a-1))) + #crlf$ ;pow(2,a) := #joy_button_1,...,#joy_button_16
    Next
    text + "button raw: " + Str(joy_pressed(#joy_button_1,#true))
    SetGadgetText(0,text)
    
    Delay(50) 
  Until Event = #PB_Event_CloseWindow 
EndIf 

End
pb 5.11
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: analog joystick

Post by akj »

Thank you, this will be useful to me.
Anthony Jordan
Post Reply