[SDL] Using timers for simulation

Sam Lantinga slouken at devolution.com
Sun Oct 14 15:12:01 PDT 2001


> I have interactive virtual world simulation work
> in progress. Currently I have two threads:

> - The main thread sits in loop rendering scene and
>   polling inputs

The main thread should have a delay in it somewhere to allow the OS
to task switch to your simulation thread.

> - Before going to that loop I start timer which
>   is run at regular intervals. It updates world
>   simulation (objects positions and rotations are
>   updated). It also processes input events so that
>   if control key is being held down, that control
>   is applied.

You should probably run the simulation in a separate thread directly,
synchronizing with the main thread.  The timer code works, but is
more suited for small tasks than performing full simulation or doing
graphics work.

> I am not exactly sure how should I use timers. At the moment
> I have single SDL_AddTimer(), and that's it. How should I
> make changes to the timer update interval?

The return value of the timer function is the duration of the next
interval.

> Also, is the timer resolution still 'only 10ms', or can I
> actually get 'best resolution available' and maybe ask what
> is the resolution?

Nope, the 10ms number is a limitation of the multi-tasking slice
time of most current non-realtime operating systems.

See ya,
	-Sam Lantinga, Software Engineer, Blizzard Entertainment




More information about the SDL mailing list