# Set frame rate

• 12-10-2007
Gordon
Set frame rate
Hello

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
• 12-10-2007
Elysia
If you use an endless loop, yes, you will use up 100&#37; CPU. You need to Sleep.
Code:

```// 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 }```
Something like this should do the job.
• 12-10-2007
VirtualAce
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.
• 12-11-2007
Gordon
To Elysia:
Looks pretty interresting thank you very much

To Bubba:
In one case I only need the framerate for a sort of program and in second case I need it for stopwatch time
• 12-11-2007
Elysia
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&#37; exact. That may require additional complexity, but it gives you the idea how to do work 50 times per second with little overhead.
• 12-11-2007
Gordon
Quote:

Originally Posted by Elysia
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
• 12-11-2007
novacain
Remember that Sleep() will stop the app/thread doing anything else.

Quote:

Originally Posted by Gordon
To Elysia:
In one case I only need the framerate for a sort of program and in second case I need it for stopwatch time

For the stop watch have you looked at WM_TIMER msgs?

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.

SetTimer()
KillTimer()
• 12-12-2007
Gordon
Quote:

Originally Posted by novacain
Remember that Sleep() will stop the app/thread doing anything else.

For the stop watch have you looked at WM_TIMER msgs?

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.

SetTimer()
KillTimer()

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
• 12-12-2007
BobS0327
Here's an interesting link on timer functions.
• 12-13-2007
Elysia
Code:

```        // 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         for(;;)         {                 // 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         }```
Here's an updated code if you want it! It's more exact!
On my machine, it printed 1-50 and then "Took 1000 ms!".
Cool, huh? ;)
• 12-14-2007
Gordon
Cool... Now it works even better than I expected it would...

Thanks everyone especially "Elysia"