gfxgfxFreeBASIC Games Directory Forumgfxgfx
gfx gfx
gfx
Welcome, Guest. Please login or register. May 18, 2013, 03:55:02 PM

Login with username, password and session length
11.5.2013 - Added a webpage for the latest FBGD competition.

13.3.2013 - Members registrations temporary disabled. For all membership requests, please email me: lachie13@yahoo.com

30.11.2012 - The ninth issue of BASIC Gaming is out! Read it here: http://games.freebasic.net/forum/index.php?topic=560.0

22.11.2012 - Be sure to check our currently running annual FBGD game making competition. This year's theme is SEASONS OF THE YEAR, 300 $ first place prize, and the competition runs till 18th of February. Link: http://games.freebasic.net/forum/index.php?topic=559.0
gfx
gfx
*
gfxgfx
gfxgfx gfxgfx
gfxgfx Home Help Search Login Register   gfxgfx
gfx gfx
gfx
Pages: [1]
Print
Author Topic: gampad setup udt (fbext)  (Read 724 times)
Merick
Amoeba
*
Posts: 11



View Profile
« on: April 26, 2008, 05:25:59 PM »

Here's a gamepad setup routine I just came up with using fbext's hashtables. To use it, you need to create two arrays, one for the button command list and one for the movement command list. Then when dimming a variable as the "pad" type, pass to the constructor the id# of the gamepad, pointers to the arrays, and the length of the arrays:

#include once "ext/ext.bi"

using ext

type command_list
action as string
button as integer
end type

type movement_list
action as string
axis as integer
end type

type pad
as integer id = 0'the device id number (0 - 15)
as FBEXT_HASHTABLE(integer) commands ' the button command list
as FBEXT_HASHTABLE(integer) moves ' the movement command list
as integer buttons 'the button status
as single axis(8)'axis values

declare constructor(byval ident as integer, byval clist as command_list ptr, byval cn as integer, byval mlist as movement_list ptr, byval mn as integer)
declare sub update()
declare function action(act as string) as integer
declare function move(act as string) as single
end type


constructor pad(byval ident as integer, byval clist as command_list ptr, byval cn as integer, byval mlist as movement_list ptr, byval mn as integer)
id = ident
if GetJoystick(id, buttons, axis(1), axis(2), axis(3), axis(4), axis(5), axis(6), axis(7), axis(8) ) = 0 then
for i as integer = 0 to cn-1
commands.insert(clist[i].action, clist[i].button)
next
for i as integer = 0 to mn-1
moves.insert(mlist[i].action, mlist[i].axis)
next
endif
end constructor

sub pad.update()
GetJoystick(id, buttons, axis(1), axis(2), axis(3), axis(4), axis(5), axis(6), axis(7), axis(8) )
end sub

function pad.action(act as string) as integer
var a = *commands.find(act)
If (buttons And (1 Shl a)) Then
return true
else
return false
endif
end function

function pad.move(act as string) as single
return axis(*moves.find(act))
end function

dim as command_list clist(10)
clist(0).action = "zero"
clist(0).button = 0
clist(1).action = "one"
clist(1).button = 1
clist(2).action = "two"
clist(2).button = 2
clist(3).action = "three"
clist(3).button = 3
clist(4).action = "four"
clist(4).button = 4
clist(5).action = "five"
clist(5).button = 5
clist(6).action = "six"
clist(6).button = 6
clist(7).action = "seven"
clist(7).button = 7
clist(8).action = "eight"
clist(8).button = 8
clist(9).action = "nine"
clist(9).button = 9

dim as movement_list mlist(8)
mlist(0).action = "one"
mlist(0).axis = 1
mlist(1).action = "two"
mlist(1).axis = 2
mlist(2).action = "three"
mlist(2).axis = 3
mlist(3).action = "four"
mlist(3).axis = 4
mlist(4).action = "five"
mlist(4).axis = 5
mlist(5).action = "six"
mlist(5).axis = 6
mlist(6).action = "seven"
mlist(6).axis = 7
mlist(7).action = "eight"
mlist(7).axis = 8


dim as pad pad1 = pad(0, @clist(0), 10, @mlist(0), 8)

screenres 640,480,32
width 640/8, 480/16

while not multikey(1)
pad1.update()
screenlock
cls
if pad1.action("zero") = true then ?"button 0: down" else ?"button 0: up"
if pad1.action("one") = true then ?"button 1: down" else ?"button 1: up"
if pad1.action("two") = true then ?"button 2: down" else ?"button 2: up"
if pad1.action("three") = true then ?"button 3: down" else ?"button 3: up"
if pad1.action("four") = true then ?"button 4: down" else ?"button 4: up"
if pad1.action("five") = true then ?"button 5: down" else ?"button 5: up"
if pad1.action("six") = true then ?"button 6: down" else ?"button 6: up"
if pad1.action("seven") = true then ?"button 7: down" else ?"button 7: up"
if pad1.action("eight") = true then ?"button 8: down" else ?"button 8: up"
if pad1.action("nine") = true then ?"button 9: down" else ?"button 9: up"

?using "axis 1: ##.####"; pad1.move("one")
?using "axis 2: ##.####"; pad1.move("two")
?using "axis 3: ##.####"; pad1.move("three")
?using "axis 4: ##.####"; pad1.move("four")
?using "axis 5: ##.####"; pad1.move("five")
?using "axis 6: ##.####"; pad1.move("six")
?using "axis 7: ##.####"; pad1.move("seven")
?using "axis 8: ##.####"; pad1.move("eight")

screenunlock
wend

sleep
Logged

The cake is a lie!
Pages: [1]
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Cerberus design by Bloc
Valid XHTML 1.0! Valid CSS!
gfx
gfxgfx gfxgfx