Re: SDL, keyboard and event handling

new topic     » goto parent     » topic index » view thread      » older message » newer message

SDL_PollEvent() (and SDL_WaitEvent()) expect a pointer to an SDL_Event union. You're passing a value of ev = 0 which it reads as NULL, and that is technically fine except that SDL_PollEvent() has nowhere to put the event it found, so it just returns 1 or 0.

Like I said in this thread, I simply do not have anything for dealing with unions added to my FFI library yet. But you should be able to get along without it as shown below. And keep in mind I'm looking for feedback on this. Does this design work? does it make sense?

include std/ffi.e 
include std/math.e 
 
constant SDL_CommonEvent = define_c_type({ 
    C_UINT32, -- type 
    C_UINT32  -- timestamp 
}) 
 
constant SDL_DisplayEvent = define_c_type({ 
    C_UINT32, -- type 
    C_UINT32, -- timestamp 
    C_UINT32, -- display 
    C_UINT8,  -- event 
    C_UINT8,  -- padding1 
    C_UINT8,  -- padding2 
    C_UINT8,  -- padding3 
    C_INT32   -- data1 
}) 
 
constant SDL_WindowEvent = define_c_type({ 
    C_UINT32, -- type 
    C_UINT32, -- timestamp 
    C_UINT32, -- windowID 
    C_UINT8,  -- event 
    C_UINT8,  -- padding1 
    C_UINT8,  -- padding2 
    C_UINT8,  -- padding3 
    C_INT32,  -- data1 
    C_INT32   -- data2 
}) 
 
constant SDL_Keysym = define_c_type({ 
    C_INT,    -- scancode 
    C_INT,    -- sym 
    C_UINT16, -- mod 
    C_UINT32  -- unused 
}) 
 
constant SDL_KeyboardEvent = define_c_type({ 
    C_UINT32,  -- type 
    C_UINT32,  -- timestamp 
    C_UINT32,  -- windowID 
    C_UINT8,   -- state 
    C_UINT8,   -- repeat 
    C_UINT8,   -- padding2 
    C_UINT8,   -- padding3 
    SDL_Keysym -- keysym 
}) 
 
 
-- allocate enough memory to hold the largest member of SDL_Event union 
 
constant SIZEOF_SDL_EVENT = math:max({ 
    sizeof(C_UINT32),          -- type 
    sizeof(SDL_CommonEvent),   -- common 
    sizeof(SDL_DisplayEvent),  -- display 
    sizeof(SDL_WindowEvent),   -- window 
    sizeof(SDL_KeyboardEvent), -- key 
    -- etc. 
}) 
 
atom event = allocate_data( SIZEOF_SDL_EVENT ) 
 
while SDL_WaitEvent( event ) do -- SDL_WaitEvent() fills our memory with the event it found and returns 1 (or 0 on error) 
 
    atom etype = peek_type( event, C_UINT32 ) 
 
    switch etype do 
 
        case SDL_DISPLAYEVENT then 
            sequence display_event = peek_struct( event, SDL_DisplayEvent ) 
 
        case SDL_WINDOWEVENT then 
            sequence window_event = peek_struct( event, SDL_WindowEvent ) 
 
        case SDL_KEYDOWN, SDL_KEYUP then 
            sequence key_event = peek_struct( event, SDL_KeyboardEvent ) 
 
        -- etc. 
 
    end switch 
 
end while 
 
free( event ) 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu