The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL poll keyboard/write renderer in timer callback function
Guenter


Joined: 24 Aug 2015
Posts: 2
Hello folks,
I'm using SDL for emulating an embedded system for "off-device" prototyping and software testing. My Setup contains MinGw64 using plain C with SDL2.0 under Windows 7 - so maybe it's a bit unusual. SDL serves for emulating a Dot Matrix LC-Display (by a Window) and input devices (by keyboard). I call this setup a "mock up".

What makes this topic specific is that there is no main loop. Well there is one, but in this context, the main loop is comparable to the operating system on a PC. The programm can run many days without ever entering the main loop.

On the target plattform, stuff like reading keys and writing the lcd is usually done in Interrupt service routines (which are emulated by timer callback functions on the PC/SDL).

At the moment, there are many calls to a "pollSDLstuff()"-like routine scattered into my mock up's code to ensure its reactivity. But it would be much more convenient to keep all this outside the normal control flow, hidden in a timer function.

Trying something like this:
Code:

/* Emulation of ISR-like Callback */
static uint32_t timer_callback(uint32_t interval, void *param)
{
    (void) param;
    (void) interval;

    SDL_Event event;
    while(SDL_PollEvent(&event)) {
   printf("event: %x\n",event.type);

   // Keyboard handling here later
    }
   
    //mock_input_update();   
    //mock_glcd_draw();   
    return 1;
}

perfectly reliable blocks the windows after three events (200, 302, 200) and the program stays unresponsive from this.

So it seems that there is some interference between the timer callback and the SDL_PollEvent.

Now I'm searching for the documentation and/or help for polling events inside a timer callback function. I'm sorry if I just missed the point in the wiki.


Kind regards
Guenter
Guenter


Joined: 24 Aug 2015
Posts: 2
OK, my problem was between chair and keyboard. I did not notice the return value of the callback function sets the next timeout, so return value 1 totally overran it with timer callbacks.

Input can be evaluated in the timer callback function now, so everything's fine.

Thanks for reading!