SDL_AddTimer function

SDL_AddTimer -- Adds a timer which will call a callback after the specified number of milliseconds has elapsed.

Syntax

SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void* param);

Callback

/* type definition for the "new" timer callback function */
typedef Uint32 (*SDL_NewTimerCallback)(Uint32 interval, void* param);

Description

Adds a callback function to be run after the specified number of milliseconds has elapsed. 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 the same as the one passed in, the timer continues at the same rate. If the returned value from the callback is 0, the timer is cancelled.

Another way to cancel a currently-running timer is by calling SDL_RemoveTimer with the timer's ID (which was returned from SDL_AddTimer).

The timer callback function may run in a different thread than your main program, and so shouldn't call any functions from within itself. However, you may always call SDL_PushEvent.

Note : If you use this function, you need to pass SDL_INIT_TIMER to SDL_Init.

Parameters

interval [in]

callback [in]

param [in]

Return value

NULL

SDL_TimerID

See also

SDL_RemoveTimer, SDL_PushEvent

Example

   1 /* Start the timer; the callback below will be executed after the delay */
   2 
   3 delay = (33 / 10) * 10;  /* To round it down to the nearest 10 ms */
   4 my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);
   5 
   6 ...
   7 
   8 Uint32 my_callbackfunc(Uint32 interval, void *param)
   9 {
  10     SDL_Event event;
  11     SDL_UserEvent userevent;
  12 
  13     /* In this example, our callback pushes an SDL_USEREVENT event
  14     into the queue, and causes ourself to be called again at the
  15     same interval: */
  16 
  17     userevent.type = SDL_USEREVENT;
  18     userevent.code = 0;
  19     userevent.data1 = NULL;
  20     userevent.data2 = NULL;
  21 
  22     event.type = SDL_USEREVENT;
  23     event.user = userevent;
  24 
  25     SDL_PushEvent(&event);
  26     return(interval);
  27 }

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.

   1 /* with the same code as before: */
   2 Uint32 my_callbackfunc(Uint32 interval, void *param)
   3 {
   4     /* ... */
   5     userevent.data1 = &my_function;
   6     userevent.data2 = param;
   7 
   8     /* ... */
   9     return(interval);
  10 }
  11 
  12 /* Now the event loop */
  13 SDL_Event event;
  14 while (SDL_PollEvent (&event))
  15 {
  16     switch(event.type)
  17     {
  18         case SDL_USEREVENT: {
  19             /* and now we can call the function we wanted to call in the timer but couldn't because of the multithreading problems */
  20             void (*p) (void*) = event.user.data1;
  21             p(event.user.data2);
  22             break;
  23         }
  24         /* ... */
  25     }
  26 }

Requirements

Header

SDL.h

Version

1.2.13

Shared object

libSDL.so

DLL

SDL.dll


CategorySDLTime

SDL_AddTimer (last edited 2009-07-28 19:44:47 by ARennes-252-1-78-31)