Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UltimaInput for FreeBASIC
#1
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. Wink

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
Reply
#2
Was this even useful to anyone? 37 views and nobody says a thing! :rotfl:
Reply
#3
I think it is useful, indeed, but I have a question: You added "events" for mouse and joystick movement . Those are both "analog-like" events, I mean: how do you measure how much the mouse/joystick has moved?
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#4
I think it is useful, indeed, but I have a question: You added "events" for mouse and joystick movement . Those are both "analog-like" events, I mean: how do you measure how much the mouse/joystick has moved?
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
In the events, I don't. Just measuring whether a control is moving in a certain direction seems to work reasonably. I used the same technique in the original UltimaLib, and it works fairly well because of the way the values work.
Reply
#6
Quote:Was this even useful to anyone? 37 views and nobody says a thing! :rotfl:

Ummm... "a thing" there I said it LOLOL....

Seriously it's very useful....one thing lacking is simple joystick manipulation code :-)....
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#7
it didnt work for me.
Reply
#8
Is your program in SDL? Ultimalib is basically a wrapper for SDLs events system.

If it is, post the code and I'll see what I can do.
Reply
#9
:o how the hell can you post as a guest? I thought that was disabled on QBN...

anyways..

Code:
'$include: "sdl\sdl.bi"
declare function getSingleEvent as integer
CurrentEvent = 0
while CurrentEvent = 0
currentevent = getSingleEvent
wend
sdl_quit

its most likely my fault, im not very good with SDL yet.
Reply
#10
i assume you need to initialize SDL somewhere...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)