[SDL] How to get constant framerates without busywaits ...
David Olofson
david.olofson at reologica.se
Mon Mar 18 18:26:01 PST 2002
On Monday 18 March 2002 00:02, Stephen Anthony wrote:
[...]
> I guess the main reason is that I learned in CS that busywaits are
> sloppy. They are, by definition, a waste of processor time.
Yes indeed - but if you don't have an OS that can do what you want, you
don't really have a choice...
> Besides,
> I've seen other emulators that do it, so I know it can be done.
> Problem is that by examining their code, I can't figure out how they
> did it :(
Did you check how they're doing audio and video output? I bet the answer
is in there...
Either they're using double buffering with h/w pageflipping (on Windows)
and blocking on the retrace, or they're "abusing" the audio card as a
timer, pretty much like many audio/MIDI applications do.
Note, however, that you probably have to use "shared DMA buffer mode" to
use audio for anything like the times you need, as just setting the
buffer size low enough would probably not even give you sound on Windows.
It works on Linux, though...
Of course, they could also be using some timers. On Windows you can use
multimedia timers (up to 1 kHz; can wake up threads), and on Linux you'd
use /dev/rtc.
> Thats why I was looking for a general algorithm, maybe
> something that could help me understand how other people did it.
There isn't one. If there isn't anything that you can block on (timer,
retrace, whatever) that will wake you up at the right time, busy-waiting
is your only option. You could *possibly* improve the situation slightly
by throwing a sched_yield()/Sleep(0) into the loop, but that's as nice as
it gets.
> Its a matter of pride I guess. The non busy-wait version would be much
> more 'elegant'. It may not be required, but it would be icing on the
> cake.
Yeah... :-)
> I come from an Amiga background, where you had to conserve every
> resource you had. I can't break free from that mentality, and
> honestly, I'm not sure that I want to :)
Same here.:-)
Wasting resources to compensate for poor operating systems or drivers is
rarely a good idea, and usually gives worse results than would a proper
solution.
//David Olofson --- Programmer, Reologica Instruments AB
.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
`----------------------------> http://www.linuxdj.com/maia -'
.- David Olofson -------------------------------------------.
| Audio Hacker - Open Source Advocate - Singer - Songwriter |
`-------------------------------------> http://olofson.net -'
More information about the SDL
mailing list