01-19-2005, 03:00 AM
After MINUTES of grueling coding, I've put together the FreeBASIC version of my old ultimaInput lib. Since SDL used keyflags anyway, this will return an integer. The format of the numbers is below.
I haven't been able to test the joystick functions yet, but there's no reason they shouldn't work.
Now, I'm sure you're wondering what UltimaInput does. It basically makes all your input devices accessible from one command. Keyboard, mouse, and up to five joysticks each with a mad number of hats, balls, axes, and buttons are supported.
Let's say you wanted to have a game read the mouse button:
if keyflag(320) then print "Button 1 pushed!"
Or read to see if it's moving right:
if keyflag(326) then print "Right!"
Or see if space is pushed:
if keyflag (sdlk_space) then print "Space!"
Or check to see if the button on your joystick was pushed:
if keyflag(480) then print "Joystick 1, Button 1 Pushed!"
Or check to see if someone is pushing right:
if keyflag(401) then print "Joystick 1, right pushed!"
Now I know what you're thinking, we all are; Do I have to remember all these damned numbers? The answer is no. In fact, I don't think you'll want to hardcode any control numbers if you can help it. The second function in the group is called getSingleEvent, which makes detecting the key pressed for a configuration screen a piece of cake!
CurrentEvent = 0
while CurrentEvent = 0
getSingleEvent
wend
print "You activated input ";CurrentEvent
It's just that easy! Feel free to ask me questions about it, let me know what you think!
I haven't been able to test the joystick functions yet, but there's no reason they shouldn't work.
Now, I'm sure you're wondering what UltimaInput does. It basically makes all your input devices accessible from one command. Keyboard, mouse, and up to five joysticks each with a mad number of hats, balls, axes, and buttons are supported.
Let's say you wanted to have a game read the mouse button:
if keyflag(320) then print "Button 1 pushed!"
Or read to see if it's moving right:
if keyflag(326) then print "Right!"
Or see if space is pushed:
if keyflag (sdlk_space) then print "Space!"
Or check to see if the button on your joystick was pushed:
if keyflag(480) then print "Joystick 1, Button 1 Pushed!"
Or check to see if someone is pushing right:
if keyflag(401) then print "Joystick 1, right pushed!"
Now I know what you're thinking, we all are; Do I have to remember all these damned numbers? The answer is no. In fact, I don't think you'll want to hardcode any control numbers if you can help it. The second function in the group is called getSingleEvent, which makes detecting the key pressed for a configuration screen a piece of cake!
CurrentEvent = 0
while CurrentEvent = 0
getSingleEvent
wend
print "You activated input ";CurrentEvent
It's just that easy! Feel free to ask me questions about it, let me know what you think!
Code:
FUNCTION keyflag (x)
static keys(0 to 1024) as short
'HIGHEST = 319 LOWEST = 0
'So, keyflag protocol:
'0-320 will be keyflags
'320-330 will be mouseFlags
'350-370 will be Joystick 1
'380-400 will be Joystick 2
while( SDL_PollEvent ( @event ) )
select case event.type
case SDL_KeyDOWN:
keys(event.key.keysym.sym) = -1
case SDL_KeyUP:
keys(event.key.keysym.sym) = 0
case SDL_MOUSEMOTION:
'326,327,328,329 are mouse movement events
keys(326) = (event.motion.xrel < 0)
keys(327) = (event.motion.xrel > 0)
keys(328) = (event.motion.yrel < 0)
keys(329) = (event.motion.yrel > 0)
case SDL_MOUSEBUTTONDown:
'SDL mouse buttons 321->325
num = 320+event.button.button
keys(num) = -1
case SDL_MOUSEBUTTONUP:
num = 320+event.button.button
keys(num) = 0
case SDL_JOYAXISMOTION
'axis events on
'400 + (Joysticknumber * 100) + (axis * 2)
'There won't be more than 5 axises on a joystick, so x00-x20 are
'reserved...
num = 400 + (event.jaxis.which * 100) + (event.jaxis.axis * 2)
keys(num) = (event.jaxis.value < 0)
keys(num + 1) = (event.jaxis.value > 0)
case SDL_JoyBallMotion
'jball
'x20-x40 are Ball events. 20 is room for 5 balls.
'There's something fucked up if 5 balls isn't enough for you.
'num=420 + (event.jaxis.which * 100) + (which * 4)
num = 420 + (event.jball.which * 100) + (event.jaxis.axis * 2)
keys(num) = (event.jball.xrel < 0)
keys(num+1) = (event.jball.xrel > 0)
keys(num+2) = (event.jball.yrel < 0)
keys(num+3) = (event.jball.yrel > 0)
case SDL_JoyHatMotion
'jhat
'x40-x80 are Hat events.40 is room for 5 hats.
'If you need 5 hats, I suggest surgery.
num = 440 + (event.jhat.which * 100) + (event.jhat.hat * 8)
for a = 1 to 8
keys(num + a) = 0
next a
keys(num + event.jhat.value) = -1
case SDL_JoyButtonDown
'button events are x80-x99
num = 40 + (event.jbutton.which * 100) + (event.jbutton.button)
keys(num) = -1
case SDL_JoyButtonUp
num = 40 + (event.jbutton.which * 100) + (event.jbutton.button)
keys(num) = 0
end select
wend
keyflag = keys(x)
END FUNCTION
FUNCTION getSingleEvent as integer
'This function will return 0 if no event is in the queue, otherwise it'll return
'the keyflag protocol number.
'So, keyflag protocol:
'0-320 will be keyflags
'321-329 will be mouseFlags
'400-500 will be Joystick 1
'500-600 will be Joystick 2
'600-700 will be Joystick 3
'700-800 will be Joystick 4
'900-1000 will be Joystick 5
while( SDL_PollEvent ( @event ) )
select case event.type
case SDL_KeyDOWN:
getSingleEvent = event.key.keysym.sym
exit function
case SDL_MOUSEMOTION:
'326,327,328,329 are mouse movement events
if (event.motion.xrel < 0) then getSingleEvent = 326:exit function
if (event.motion.xrel > 0) then getSingleEvent = 327:exit function
if (event.motion.yrel < 0) then getSingleEvent = 328:exit function
if (event.motion.yrel > 0) then getSingleEvent = 329:exit function
case SDL_MOUSEBUTTONDown:
'SDL mouse buttons 321->325
getSingleEvent = 320+event.button.button
exit function
case SDL_JOYAXISMOTION
num = 400 + (event.jaxis.which * 100) + (event.jaxis.axis * 2)
getSingleEvent = num
exit function
case SDL_JoyBallMotion
'jball
'x20-x40 are Ball events. 20 is room for 5 balls.
'There's something fucked up if 5 balls isn't enough for you.
'num=420 + (event.jaxis.which * 100) + (which * 4)
num = 420 + (event.jball.which * 100) + (event.jaxis.axis * 2)
if (event.jball.xrel < 0) then getSingleEvent = num
if (event.jball.xrel > 0) then getSingleEvent = num + 1
if (event.jball.yrel < 0) then getSingleEvent = num + 2
if (event.jball.yrel > 0) then getSingleEvent = num + 3
exit function
case SDL_JoyHatMotion
'jhat
'x40-x80 are Hat events.40 is room for 5 hats.
'If you need 5 hats, I suggest surgery.
num = 440 + (event.jhat.which * 100) + (event.jhat.hat * 8)
getSingleEvent = num + event.jhat.value
exit function
case SDL_JoyButtonDown
'button events are x80-x99
num = 40 + (event.jbutton.which * 100) + (event.jbutton.button)
getSingleEvent = num
exit function
end select
wend
getSingleEvent = 0
exit function
END FUNCTION