Sign in to follow this  
wease

Is there such thing as a low resouce game loop?

Recommended Posts

I want to write a game that uses very low resources. I don't want it to interfer with other applications. It is a very simple 2d game so I don't think it will need a lot of resources anyway. I was planning on using a timer that sends a windows message every 20 ms or so and then render the scene on each of these messages. If I use a game loop, it will dominate the CPU. Is there someway to avoid this? I suppose I could just use a game loop and sleep(20ms) each time it loops. However, then the game might miss other messages. Any ideas of a way to do this with a game loop and still use very little resources?

Share this post


Link to post
Share on other sites
Quote:
Original post by wease
I was planning on using a timer that sends a windows message every 20 ms or so and then render the scene on each of these messages.

That'll work.
Quote:
I suppose I could just use a game loop and sleep(20ms) each time it loops.

That'll also work.
Quote:
However, then the game might miss other messages.

No, it won't. Window messages are buffered; you'll grab them next time through the game loop.

Now, either method will work fine. The only substantive difference is how you want to do input. With the WM_TIMER method, other events, such as input, are handled immediately, and redraws happen every n milliseconds. With the game loop method, you handle all the events that have come in since the last loop, and then draw. People tend to be more comfortable with the second approach; but see what works best for you.

Oh, one other difference: WM_TIMER will maintain a constant tick rate. The game loop, unless it is specifically designed not to, will drift over time; it'll run slow. That doesn't mean that your game will play slowly, since you can just decrease the sleep() time to compensate, but be aware that sleep(20ms) will not equate to 50fps.

Share this post


Link to post
Share on other sites
Sleeping in your loop will work reasonably, but don't just do "Sleep(20)". It may be worth computing how much time you have to waste before beginning the next loop iteration, and sleeping just a bit less than it. (experiment with it, I've never compared... but if you use this, at least as your game increases in complexity (and therefore time it takes to calculate frames), it won't slow down)

Share this post


Link to post
Share on other sites
Well, I think sleeping is a slightly easier way of doing it, but I've finally learned how to implement a timer in VC++ 7 so I'm just gonna do that. Thanks for your help though. Definately brought up some stuff on the sleep method which made me consider WM_TIMER more seriously.

Share this post


Link to post
Share on other sites
I'm using SDL.

I looked at the Task Manager, and my game was using 52% of my CPU. I put in SDL_Delay(); to parts of the loop, and it dropped to about 20% with no noticeable performance changes in-game.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this