Set frame rate
I need in my app to set how many frames per sec. are drawn...
I use the GetTickCount() function but there appears a problem that the CPU is allways on 100% of usage...
I suppose maybe usage of else function or some other way how to detect the time flow...
Thx very much for support
If you use an endless loop, yes, you will use up 100% CPU. You need to Sleep.
Something like this should do the job.
// Let's set it to update 50 frames/sec.
DWORD dwTickRate = 1000 / 50;
DWORD dwTick1 = 0; //GetTickCount();
DWORD dwSleepNeeded = dwTickRate;
for (int i = 0; i < 50; i++)
Sleep(dwSleepNeeded); // Sleep for the amount of time necessary before next drawing
dwTick1 = GetTickCount(); // Take a snapshot of current time
// Call rendering
dwSleepNeeded = dwTickRate - (GetTickCount() - dwTick1); // Calculate how much time we need to sleep
Do you want to clamp the framerate or do you only want to display it??? If you want to clamp it you are wasting tons of cycles.
Looks pretty interresting thank you very much
In one case I only need the framerate for a sort of program and in second case I need it for stopwatch time
Remember, the code doesn't take into account that Sleeping for 50 ms doesn't necessarily sleep for 50 ms (it can sleep more), so it's not 100% exact. That may require additional complexity, but it gives you the idea how to do work 50 times per second with little overhead.
Ok thx again
Originally Posted by Elysia
Remember that Sleep() will stop the app/thread doing anything else.
For the stop watch have you looked at WM_TIMER msgs?
Originally Posted by Gordon
Can create a timer which will send you a msg every time period (50ms in this case). Is not that accurate (never better than 10ms). Timer msgs are very low priority in the OS msg queue and can be 'ignored' like paint msgs.
I just tried the WM_TIMER but there is too many of laggs and it absolutely does not flow steadily... For stopwatch this is not appropriate for the second thing I am creating it is just OK... Thx for advise
Originally Posted by novacain
Here's an interesting link on timer functions.
Here's an updated code if you want it! It's more exact!
// Let's set it to update 50 frames/sec.
const DWORD dwTimeFrame = 1000; // The time frame we're targeting (in ms)
const DWORD dwFramesPerTimeFrame = 50; // Number of times per time frame cycle to do rendering
const DWORD dwTickRate = dwTimeFrame / dwFramesPerTimeFrame; // Calculate amount of time to sleep each time
INT32 nSleepNeeded; // Stores the amount of time we need to sleep
DWORD dwTimeElapsed; // Time elapsed since beginning of time frame
DWORD dwNextSleepBoundary; // The next "target" time to sleep until.
DWORD dwTick; // Used to hold a snapshot of timeGetTime so we can calculate elapsed time
DWORD Count = 1; // Simple counter just for cosmetics - just to print something
DWORD dwTick2 = timeGetTime(); // Snapshot of the beginning of the code sample
// Initialize variables
nSleepNeeded = 0;
dwTimeElapsed = 0;
dwNextSleepBoundary = dwTickRate; // Set the next sleep boundary to the next tick.
dwTick = timeGetTime(); // Get a snapshot of the current time
while (dwNextSleepBoundary <= dwTimeFrame)
// Call rendering here
dwTimeElapsed = timeGetTime() - dwTick; // Calculate elapsed time
nSleepNeeded = dwNextSleepBoundary - dwTimeElapsed; // Calculate amount of time we need to sleep (at least)
if (nSleepNeeded > 0) // Safesty check; we don't want to do Sleep(0) or sleep a negative value since Sleep takes a DWORD
Sleep(nSleepNeeded); // Sleep for the amount of time necessary before next drawing
dwNextSleepBoundary += dwTickRate; // Increase sleep "boundary" to next time frame tick
cout << Count++ << endl; // Print counter
DWORD dwTick3 = timeGetTime() - dwTick2; // Calculate amount of time the entire loop took
cout << "Took " << dwTick3 << " ms!\n"; // Print out the time it took
__asm int 3; // Do a debug break
On my machine, it printed 1-50 and then "Took 1000 ms!".
Cool, huh? ;)
Cool... Now it works even better than I expected it would...
Thanks everyone especially "Elysia"