Wiki Page Content

Differences between revisions 13 and 14
Revision 13 as of 2019-12-23 17:55:20
Size: 3668
Editor: ProgramGamer
Comment: Clarified that timers take into account the time it takes to execute the callback function.
Revision 14 as of 2019-12-23 18:01:33
Size: 3680
Editor: ProgramGamer
Comment: Slightly less ambiguous wording in Remarks.
Deletions are marked like this. Additions are marked like this.
Line 101: Line 101:
The callback function is passed the current timer interval and the user supplied parameter from the [[SDL_AddTimer]]() call and returns the next timer interval. If the returned value from the callback is 0, the timer is canceled. The callback function is passed the current timer interval and the user supplied parameter from the [[SDL_AddTimer]]() call and should return the next timer interval. If the value returned from the callback is 0, the timer is canceled.
Line 103: Line 103:
The callback is run on a separate thread.  See the code examples for a method of processing the timer callbacks on the main thread if that's desired. The callback is run on a separate thread. See the code examples for a method of processing the timer callbacks on the main thread if that's desired.
Line 105: Line 105:
Timers take into account the amount of time it took to execute the callback. For example, if the callback took 250 ms to execute and returned 1000 (ms), it would only wait another 750 ms before its next iteration. Timers take into account the amount of time it took to execute the callback. For example, if the callback took 250 ms to execute and returned 1000 (ms), the timer would only wait another 750 ms before its next iteration.

SDL_AddTimer

Use this function to set up a callback function to be run on a separate thread after the specified number of milliseconds has elapsed.

Syntax

SDL_TimerID SDL_AddTimer(Uint32            interval,
                         SDL_TimerCallback callback,
                         void*             param)

Function Parameters

interval

the timer delay (ms) passed to callback

callback

the function to call when the specified interval elapses; see Remarks for details

param

a pointer that is passed to callback

Return Value

Returns a timer ID or 0 if an error occurs; call SDL_GetError() for more information.

Code Examples

/* Start the timer; the callback below will be executed after the delay */

Uint32 delay = (33 / 10) * 10;  /* To round it down to the nearest 10 ms */
SDL_TimerID my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);

...

Uint32 my_callbackfunc(Uint32 interval, void *param)
{
    SDL_Event event;
    SDL_UserEvent userevent;

    /* In this example, our callback pushes an SDL_USEREVENT event
    into the queue, and causes our callback to be called again at the
    same interval: */

    userevent.type = SDL_USEREVENT;
    userevent.code = 0;
    userevent.data1 = NULL;
    userevent.data2 = NULL;

    event.type = SDL_USEREVENT;
    event.user = userevent;

    SDL_PushEvent(&event);
    return(interval);
}

Note that it is possible to avoid the multithreading problems with SDL timers by giving to userevent.data1 the address of a function you want to be executed and to userevent.data2 its params, and then deal with it in the event loop.

/* with the same code as before: */
Uint32 my_callbackfunc(Uint32 interval, void *param)
{
    SDL_Event event;
    SDL_UserEvent userevent;

    /* In this example, our callback pushes a function
    into the queue, and causes our callback to be called again at the
    same interval: */

    userevent.type = SDL_USEREVENT;
    userevent.code = 0;
    userevent.data1 = &my_function;
    userevent.data2 = param;

    event.type = SDL_USEREVENT;
    event.user = userevent;

    SDL_PushEvent(&event);
    return(interval);
}

/* Now the event loop */
SDL_Event event;
while (SDL_PollEvent (&event))
{
    switch(event.type)
    {
        case SDL_USEREVENT: {
            /* and now we can call the function we wanted to call in the timer but couldn't because of the multithreading problems */
            void (*p) (void*) = event.user.data1;
            p(event.user.data2);
            break;
        }
        /* ... */
    }
}

Remarks

If you use this function, you must pass SDL_INIT_TIMER to SDL_Init().

The callback function is passed the current timer interval and the user supplied parameter from the SDL_AddTimer() call and should return the next timer interval. If the value returned from the callback is 0, the timer is canceled.

The callback is run on a separate thread. See the code examples for a method of processing the timer callbacks on the main thread if that's desired.

Timers take into account the amount of time it took to execute the callback. For example, if the callback took 250 ms to execute and returned 1000 (ms), the timer would only wait another 750 ms before its next iteration.


CategoryAPI, CategoryTimer

None: SDL_AddTimer (last edited 2019-12-23 18:01:33 by ProgramGamer)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit