|Hosting by Solid Eight Studios ,PhotoTangler Collage Maker|
Constant FPS Under DirectDraw
Question submitted by (10 July 2000)
|Return to The Archives|
|Yes. How to do it? Should I use timeSetEvent-type functions to put my drawing routine into a periodic loop? I've done it, but my animation didn't go smoothly...|
You don't need to put timers or delays into your code (which is effectively what
you do when using those events.) You want to draw this stuff as fast as
possible, and let the video hardware worry about timing issues.
First, enable your vertical sync (see the properties dialog for your primary display adapter). This will put you on a constant frame rate that is synchronized with your monitor. The reason why it's important to have faster frame rates than your refresh rate because it provides you with a buffer for those times when your application has to do more work (i.e. spend more time) to render a frame of animation.
This sounds like the problem you're running into. Some frames of animation can be rendered quickly, and some not so quickly. If your rendering frame rate is 75Hz, and your monitor is running at 60Hz, then you'll end up with a 60Hz frame rate. However, if your animation hits a "lag" period where it has to do more work to render a frame of animation, your rendering frame rate might drop from 75Hz to 58Hz. Unfortunately, if your vertical sync is enabled, you'll find that you only get 30Hz because you can't keep up with your monitor's refresh rate of 60Hz. This is why you want to enable the vertical sync; it will make those lags very obvious.
Profile your code and find out where you're spending your time during those lags. Speed that up as much as possible.
Now you've done everything you can to make it run as fast as possible. Any lags are simply unavoidable. Or are they?
The next step is to use triple buffering. Triple buffering allows you to maintain an average frame rate over a series of three frames. The display adapter will be showing the frame that you rendered two frames ago. There will be one frame in the buffer, and you'll be working on rendering a frame. If you end up with a lag frame between two faster frames, the lag will not be noticed because it's averaged out. If your animation enters a long period of lag, then your frame rate will drop, because 'slow' averaged with 'slow' is 'slow'. :) In this case, it's back to the optimization stage.
Response provided by Paul Nettle
This article was originally an entry in flipCode's Ask Midnight, a Question and Answer column with Paul Nettle that's no longer active.