I agree with most of your points here.
I think the problem is that you want gauranteed constant framerate. If
you relax this condition to average framerate then it will be a lot
easier to code and noone will know the difference anyway.
The timing loops people have put up here will achieve an average
framerate. The yielding or sleeping is only used to keep it an
approximate sync. The time your program sleeps is not controlled by your
program, it is controlled by the os. You are only giving hints to the os
that your program doesn’t need to do anything for a while. Sometimes the
os will take too much time from your program so you may not want to
sleep at all. In the end you will get your desired framerate, but only
an average, not a constant.
In your example:
For example, assume that I want to run at 60 fps (default). Assuming
that
the frame and event handling takes 8 ms to complete, then I want to wait
another 8-9 ms (since 60 fps implies 16 ms/frame). Problem is that
when I
try to wait 8 ms, I will actually have to wait 10 ms (at least). So the
effective framerate now becomes 50 fps, since the whole loop actually
took closer to 20 ms.
But for the next frame it takes 8ms to render. So now you only need to
sleep for 6ms. The idea is that you need to track your framerate against
realtime and constantly adjust the amount of time you sleep for. You may
want to decide at the end of frame that if your calculated sleep period
is too small and you may not want to sleep at all.
Stephen Anthony wrote:> On March 17, 2002 07:04 pm, you wrote:
Why don’t you want a busy wait? Why is everybody concerned with their
CPU usage while running games?I don’t think it is possible to get constant framerates without
busywaits. If you try any other method you are blocking on some
operating system controlled event. When you do that you are leaving it
up to the operating system to wake your thread again. Even a sleep for
2ms call is not gauranteed to return in 2ms, all it gaurantess is that
it will be at least 2ms before it returns. You need a real time
operating system to get gaurantees on how long your thread will sleep,
thats why real time operating systems exist.Well, there are a few reasons. One is that this is an emulator that is
extremely low powered. Why use up the entire CPU when less than 1% would
have been enough, even on a Pentium 100?Also, what if it is ported to some portable device or something? More
CPU usage means more power consumption. Actually, this is also related
to current CPU’s. More processing translates to more heat being
generated.I guess the main reason is that I learned in CS that busywaits are
sloppy. They are, by definition, a waste of processor time. 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
Thats why I was looking for a general algorithm, maybe something that
could help me understand how other people did it.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. 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 toSteve