Code-Beispiel
Joytest
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
k. A. | MOD | 03.03.2012 |
Der Code wurde von coderJeff erstellt und stellt ein umfassendes Beispiel zur Nutzung eines Joysticks/Gamepads mit FreeBASIC dar.
'' Joystick Diagnostic Program
'' Aug/2007 - coderJeff
''
'' Compile with fb 0.17 or higher
'' on dos/linux/win32
#include "vbcompat.bi"
declare function main() as integer
dim shared img_axis as any ptr
dim shared img_dot as any ptr
dim shared img_btn0 as any ptr
dim shared img_btn1 as any ptr
end main()
function FormatSng( byval x as single ) as string
return Format(x, "0.00")
end function
sub RoundBox _
( _
byval img as any ptr, _
byval x as integer, _
byval y as integer, _
byval w as integer, _
byval h as integer, _
byval r as integer, _
byval c as integer _
)
circle img, ( x + r , y + r ), r, c, , , , f
circle img, ( x + r , y + h - r - 1 ), r, c, , , , f
circle img, ( x + w - r - 1, y + r ), r, c, , , , f
circle img, ( x + w - r - 1, y + h - r - 1 ), r, c, , , , f
line img, ( x, y + r ) - ( x + w - 1, y + h - r ), c, bf
line img, ( x + r, y ) - ( x + w - r, y + h - 1 ), c, bf
end sub
sub gfx_init()
img_axis = ImageCreate( 128, 128, 0 )
RoundBox img_axis, 0,0,128,128,8,15
RoundBox img_axis, 4,4,120,120,4,1
line img_axis, ( 8, 64 ) - ( 120, 64 ), 7, , &h77777777
line img_axis, ( 64, 8 ) - ( 64, 120 ), 7, , &h77777777
img_dot = ImageCreate( 16, 16, 0 )
circle img_dot, ( 8, 8 ), 7, 14, , , , f
circle img_dot, ( 8, 8 ), 7, 15
img_btn0 = ImageCreate( 32, 32, 0 )
RoundBox img_btn0, 0,0,32,32,6,15
RoundBox img_btn0, 3,3,26,26,2,1
img_btn1 = ImageCreate( 32, 32, 0 )
RoundBox img_btn1, 0,0,32,32,6,15
RoundBox img_btn1, 3,3,26,26,2,14
end sub
sub gfx_shut()
deallocate img_axis
deallocate img_dot
deallocate img_btn0
deallocate img_btn1
end sub
sub DrawAxis _
( _
byval title as zstring ptr, _
byval xpos as single, _
byval ypos as single, _
byval x as single, _
byval y as single _
)
dim t as string
put( xpos, ypos ), img_axis, pset
put( _
xpos + 64 + x * 50 - 8, _
ypos + 64 + y * 50 - 8), _
img_dot, trans
Draw String ( xpos + 128 + 3, ypos + 64 - 4 ), FormatSng( x ), 15
t = FormatSng( y )
Draw String ( xpos + 64 - len(t) * 4, ypos - 10 ), t, 15
Draw String ( xpos + 64 - len(*title) * 4, ypos - 20), *title, 14
end sub
sub DrawButton _
( _
byval id as integer, _
byval xpos as integer, _
byval ypos as integer, _
byval state as integer _
)
if( state ) then
put ( xpos, ypos ), img_btn1, pset
draw string (xpos + 6, ypos + 6), ltrim(str(id)), 1
else
put ( xpos, ypos ), img_btn0, pset
draw string (xpos + 6, ypos + 6), ltrim(str(id)), 15
end if
end sub
function main() as integer
dim page as integer = 0
dim as integer id, b, i, c
dim as single x,y,z,r,u,v,d,f
dim as string k
screenres 640,480,8,2
gfx_init()
screenset page, page xor 1
id = 0
while multikey(1) = 0
getjoystick id,b,x,y,z,r,u,v,d,f
k = inkey
if( len(k) > 0 ) then
if( k = "+" or k = "=" ) then
id += 1
id mod= 16
elseif( k = "-" ) then
id += 15
id mod= 16
end if
end if
cls
Draw String ( 240, 8 * 1 ), "Joystick Test program"
Draw String ( 240, 8 * 3 ), "Press ESCAPE to quit", 14
Draw String ( 240, 8 * 5 ), "Press (+) or (-) to select controller ID", 14
Draw String ( 240, 8 * 8 ), "Current Controller ID: " + str( id ), 10
DrawAxis "Axis 1 and 2", 20, 50, x, y
DrawAxis "Axis 3 and 4", 20, 220, z, r
DrawAxis "Axis 5 and 6", 230, 220, u, v
DrawAxis "Axis 7 and 8", 440, 220, d, f
c = 1
for i = 0 to 31
DrawButton i + 1, _
(i mod 16) * 40 + 4, _
400 + (i \ 16) * 40, _
(( b and c ) <> 0 )
c shl= 1
next i
page xor= 1
screenset page, page xor 1
sleep 25
wend
gfx_shut()
return 0
end function
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|