Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modified example from FB for keyboard input for SDL
#1
Himeowth!

I have modified the keymouse.bas programm so that it show a better way to retrive keyboard input for games. Have fun and dont give up coding Wink

Code:
''
'' SDL events test
'' by marzec
'' modified by BastetFurry for showing how to grab game input
''

'$include: 'SDL\SDL.bi'

declare sub DoKeyPrint ()
declare sub doInit ( )
declare sub doQuit ( )
declare sub doMain ( )
declare sub exitError ( msg as string )

dim video as SDL_Surface ptr
dim shared event as SDL_Event    
doInit
doMain
doQuit



sub doInit ( )

    if(SDL_Init( SDL_INIT_EVERYTHING )) then exitError "couldn't init SDL"
    
    video = SDL_SetVideoMode ( 320, 200, 24, SDL_HWSURFACE )
    if( video = 0 ) then exitError "couldn't init videomode"
    
end sub

sub doMain ( )
        
   dim keysdown as Integer
    dim quit as Integer
    quit = 1

    print "-----------------------------------------------"
    print "                  SDL TEST"
    print "-----------------------------------------------"

    while( quit = 1)

      if keysdown = 1 then DoKeyPrint
    
        while( SDL_PollEvent ( @event ) )                         
          print "event happened, type:" + str(event.type)        

            select case event.type
            case SDL_KEYDOWN:
               keysdown = 1
            case SDL_KEYUP:
               keysdown = 0
                case SDL_MOUSEBUTTONDOWN:
                    print "mousebutton pressed, quitting"
                    quit = 0
            end select
    
        wend
        
    wend
    
    print "leaving doMain"
end sub

sub DoKeyPrint ()
   SDL_PollEvent ( @event )
    print "key event, pressed key:" + str(event.key.keysym.sym) + " " + chr$(event.key.keysym.sym)
    print
end sub  

sub doQuit ( )

    print "quiting"
    SDL_Quit

end sub

sub exitError ( msg as string )
    print "error: " + msg
    SDL_Quit
    end
end sub

(How to tag this as qbasic code? I have seen it somewhere but dont know how)

So Long, The Werelion!
color=red]Look at you, Hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?" - Shodan, AI at Citadel Station orbiting Earth[/color]
Reply
#2
Well I got it more this way:

Code:
dim shared keyboardkey(0 to 323) as integer

if keyboardkey(SDLK_F2) <> 0 then dostuff

'' Get Keyboard State
sub getkeyboardstate()
   dim e%
   dim keyboarddata as ubyte ptr
   keyboarddata = SDL_GetKeyState(0)
   for e% = 1 to 323
      keyboardkey(e%) = PEEK(keyboarddata + e%)
   next  
end sub

It's I think beter because it looks at all keys and make them 1 if they are pressed.
Reply
#3
Code:
Dim key as ubyte ptr
Do
       key = SDL_GetKeystate(0)
Loop Until Peek(key + SDLK_ESCAPE)
Can be simplified to:
Code:
Dim key as ubyte ptr
Do
       key = SDL_GetKeystate(0)
Loop Until key[SDLK_ESCAPE]
Fancy, ey?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)