Joystick position: Difference between revisions

(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 828:
Run with the command:
$ ocaml -I /tmp/ocaml-sfml/src sfml_system.cma sfml_window.cma sfml_graphics.cma joy.ml
 
=={{header|Phix}}==
Windows only, taken from http://phix.x10.mx/pmwiki/pmwiki.php?n=Main.JoystickPadLibrary
 
First, joy.e:
<lang Phix>-- Joystick library for Euphoria (Windows)
-- /Mic, 2002
--
-- integer joy_init()
-- returns the number of joysticks attached to the computer
--
-- sequence joy_get_state(integer joy_num)
-- returns the current state of joystick #joy_num (can be either 1 or 2).
-- the format of the return sequence is:
-- {X_direction, Y_direction, Z_direction, buttons}
-- the X,Y and Z directions have 3 possible values; 0 (negative), 32768 (neutral) or 65535 (positive)
-- the buttons' status are represented by a single bit (button up, button down). e.g. to get the status
-- of button #3 on joystick #1 you'd use:
-- sequence state
-- state = joy_get_state(1)
-- if and_bits(state[4],4) then ... end if
--
include dll.e
include machine.e
 
constant joyinfo = allocate(32)
atom winmm
integer joyGetNumDevs,joyGetPos
 
winmm = open_dll("winmm.dll")
if (winmm <= 0) then
puts(1,"Unable to open winmm.dll")
abort(0)
end if
 
joyGetNumDevs = define_c_func(winmm,"joyGetNumDevs",{},C_UINT)
joyGetPos = define_c_func(winmm,"joyGetPos",{C_INT,C_POINTER},C_INT)
if (joyGetNumDevs<0) or (joyGetPos<0) then
puts(1,"Unable to link functions")
abort(0)
end if
 
global function joy_init()
integer joy1Attached,joy2Attached
integer numDevs = c_func(joyGetNumDevs,{})
if numDevs=0 then
return 0
end if
joy1Attached = (c_func(joyGetPos,{0,joyinfo}) != 167)
joy2Attached = (numDevs=2) and (c_func(joyGetPos,{1,joyinfo}) != 167)
return joy1Attached + (joy2Attached*2)
end function
 
global function joy_get_state(integer joy_num)
if joy_num=1 or joy_num=2 then
joy_num -= 1
if c_func(joyGetPos,{joy_num,joyinfo+(joy_num*16)}) then
-- ERROR
return {}
end if
return peek4u({joyinfo+(joy_num*16),4})
end if
return {}
end function</lang>
And a test program:
<lang Phix>include joy.ew
 
if joy_init()=0 then
puts(1,"No joystick(s) attached!")
abort(0)
end if
 
sequence joy_info = {}, s
integer button_mask
 
puts(1,"Joystick test\nEntering input loop. Press a key to exit..\n\n")
 
while get_key()=-1 do
-- Get the state of joystick #1
s = joy_get_state(1)
-- Do not print info unless the state has changed
if not equal(s,joy_info) then
joy_info = s
 
printf(1,"X = %d, Y= %d ",{floor((s[1]-32767)/32768),floor((s[2]-32767)/32768)})
 
button_mask = 1
for i=1 to 8 do
if and_bits(s[4],button_mask) then
printf(1,"BTN%d ",i)
else
puts(1," ")
end if
button_mask *= 2
end for
puts(1,"\n")
end if
end while</lang>
 
=={{header|PicoLisp}}==
7,818

edits