Limiting FPS and conserving CPU usage

Started by
11 comments, last by Ivko 22 years, 3 months ago
How would I make my game run at a certain FPS and not take 100% CPU usage while it''s idling? The while(GetTickCount().. method wouldn''t work for me because it consumes all CPU power.
Advertisement
The easiest way in my opinion would be to use multimedia timers and run your graphics through a callback. The associated functions are:
timeBeginPeriod
timeEndPeriod
timeGetDevCaps
timeGetSystemTime
timeGetTime
timeKillEvent
TimeProc
timeSetEvent

These can be looked up in MSDN for explanations and usage.
Good luck! =]

I haven''t looked into this much, and it could also be a borland c++ builder thing, but you could try OnIdle() - actually I think it is part of Builder.


Beer - the love catalyst
good ol'' homepage
Beer - the love catalystgood ol' homepage
Thanks, I'll look into those.. I think also that I remember there being a function which you call in such times when you're doing nothing that will transfer the CPU over to another application.. Anyone know if I'm imagining things or if there's such a function?

Just for clarification, this is MSVC++6 and a windows app.

Edited by - Ivko on December 30, 2001 1:54:26 PM
Try Sleep(1). That will give up your remaining timeslice. Or if you have an idea of how many milliseconds you need to burn, just to Sleep(milliseconds). Remember though that no timer function is going to be exact, so err on the side of caution. Also, GetTickCount() has a granularity of 10ms.


-Brannon
-Brannon
I guess my first question would be what are you saving the CPU for? Unless you are running under an operating system that is not pre-emptive or you have to pay for the CPU you use or your boss is monitoring how much CPU you are using then I don''t see the point. Use it or lose it. Running at 50% for a week doesn''t mean you can run at 150% the next week.
Keys to success: Ability, ambition and opportunity.
Even under a pre-emptive OS, if your process is constantly trying to run, it can still "choke out" other processes. There are a finite number of timeslices per second. The OS can only do so much to try to give each process enough time..

Ivko, you have the right idea .. if you aren''t going to use the timeslice, give it up for another process/thread to use. Be a good citizen.


-Brannon
-Brannon
Tell me. The last time you played a real-time game what important task were you running in the background? Hum, maybe you were defraging the hard drive. That is I/O bound, not CPU bound, but would you seriously do that while playing a game?
Keys to success: Ability, ambition and opportunity.
Thanks for your input. What would I do? Play mp3s, use IRC, and be able to switch tasks to do something quick without the system grinding to a halt. It's just good practice, at the moment my game does nothing other than run the main loop and it takes up 100% CPU usage. There's no reason not to do it, and it just doesn't seem right. Another example is if I decided later to make something that's not a fullscreen game, I don't want it wasting all CPU power.

Thanks all, I'll try the Sleep method.

Edit: That's just what I was working for, thanks

Edited by - Ivko on December 31, 2001 10:45:13 PM
That´s strange...My game uses 0% of the CPU...Perhaps it´s XP =/

BTW, it´s a good thing NOT to have 100% CPU-use a all time, since it strains the CPU very hard, increases the temperature in the computer and so on...
delete this;

This topic is closed to new replies.

Advertisement