Set frame rate

This is a discussion on Set frame rate within the Windows Programming forums, part of the Platform Specific Boards category; Hello I need in my app to set how many frames per sec. are drawn... I use the GetTickCount() function ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    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

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    If you use an endless loop, yes, you will use up 100% 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.
    Last edited by Elysia; 12-10-2007 at 02:12 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    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

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Elysia View Post
    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

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Remember that Sleep() will stop the app/thread doing anything else.

    Quote Originally Posted by Gordon View Post
    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()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by novacain View Post
    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

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Here's an interesting link on timer functions.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    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?
    Last edited by Elysia; 12-13-2007 at 11:55 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Cool... Now it works even better than I expected it would...

    Thanks everyone especially "Elysia"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. Frame Rate
    By Tommaso in forum Game Programming
    Replies: 6
    Last Post: 04-04-2003, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21