Game Loop

Started by
4 comments, last by MaulingMonkey 18 years, 10 months ago
Hi, I have a question on my game loop. I'm developing a game with SDL+OpenGL, and am currently rendering at a fixed interval, leaving spare time for AI processing. How would this interfere with the v-sync? If the monitor refresh rate is higher than what my frame interval allows, what would happen? I'm concerned because I'm getting tearing occasionally, even though v-sync is enabled. Thanks in advance.
Advertisement
I've never used SDL, and I haven't done much OpenGL either. But, generally it is a bad idea to fix your framerate like that. At best, your application will appear choppy on monitors that have a refresh rate different than your framerate, because some frames will be seen longer than other frames, and some frames may never be seen at all. I don't know why you would see any tearing if you have v-sync enabled. But, it might be better if you change your code to rely on v-sync to control your framerate and see if that doesn't fix your problem. In the end, that is a better solution anyway.
Thanks for the quick response:D. In that case, how would I go about doing AI processing?
Since I'm expecting to spread out the processing over multiple frames, I would like to know how much time my idle processing function has left before the next frame.
Well you could allocate a fixed interval say 10ms for a single frame, then you'd time how long it takes to do the rendering and anything else you want to do in a frame and use the remaining time to do your AI processing. Though this is effectively fixing your frame rate.

Using threads would be a better solution, you'd have one thread doing the AI and another doing everything else. SDL has thread support that you can use.
Thanks for the suggestions. I'm going to go with a separate AI thread (Monder) and not fixing the framerate (nimrand).
Quote:Original post by yzq89
I'm concerned because I'm getting tearing occasionally, even though v-sync is enabled.


Use double buffering. If you allready are, it's probably a problem outside of your program - not all API+OS+Driver+Hardware combinations are set up to allow for tear-free animation. Unless waiting for the vertical retrace is done with via interrupt or by the card itself, if another thread is running during the vertical retrace period, you can end up missing the period and be forced to either never display the frame or tear.

This topic is closed to new replies.

Advertisement