|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Calculate Frames Per Second |
|
![]() utilae Member since: 12/8/2004 From: Dargaville, New Zealand |
||||
|
|
||||
| Hi I want to calculate the frames per second, so i can display it on screen. Right now I do the following: //start time DWORD dwTimeStart=GetTickCount(); //. //.after a while //. //time difference between start and end(now) float fTimeDifference=GetTickCount()-dwTimeStart; if(fTimeDifference==0)fTimeDifference=0.1; //1000ms divide by time difference in ms int nFramesPerSecond=1000/fTimeDifference; My problem is that fTimeDifference=0, so I detect that and set it to 0.1, giving me 10,000 frames per second!!! I'm wondering if I am doing this wrong or something. |
||||
|
||||
![]() uncutno Member since: 4/4/2001 From: Norway |
||||
|
|
||||
| try something like: int nFramesPerSecond=(int)(1000.f/fTimeDifference); |
||||
|
||||
![]() mumpo Member since: 9/25/2004 From: Los Angeles, CA, United States |
||||
|
|
||||
| What uncutno said. Also, if the actual time difference is very small, GetTickCount() may not be accurate enough for you. GetTickCount() returns miliseconds, but I seem to recall that it is not actually guaranteed to be updated at anything all that close to once a milisecond. You might try QueryPerformanceFrequency() and QueryPerformanceCounter() instead. |
||||
|
||||
![]() benryves GDNet+ Member since: 9/4/2003 From: Purley, United Kingdom |
||||
|
|
||||
| My solution is probably more accurate, but less instant: int lastFPSlog = 0; int frames = 0; int fps = 0; while (true) { // Renderer code ++frames; int time = SDL_GetTicks(); if (time > lastFPSlog+1000) { fps = frames; frames = 0; lastFPSlog = time; printf("FPS: %i\n",fps); } } That way it only updates once a second, but is more accurate. |
||||
|
||||
![]() Emmanuel Deloget GDNet News Lead Member since: 8/27/2003 From: France |
||||
|
|
||||
Quote: To add more informations: GetTickCount() granularity is known by using GetSystemTimeAdjustment(). AFAIR it is close to 10ms. Regards, -- Emmanuel D. [blog, in French] [blog, very bad googlized translation] [NEW: English version of teh blog! (WIP)] |
||||
|
||||
![]() Zipster GDNet+ Member since: 3/11/2000 From: Las Vegas, NV, United States |
||||
|
|
||||
| Use a wrapper class around your timer services. It you have a high-resolution timer, use QueryPerformanceCounter. If that fails, maybe use the multimedia timer (timeGetTime), which has the potential to be set to use a higher resolution than GetTickCount for a short time. And lastly, resort to GetTickCount. However if this is just for a FPS counter, messing with high-resolution timers is just overkill. They'd be more useful if you were writing a code profiler where you actually have to measure each individual block of code. I personally just update my FPS counter once a second. You get a nice, clean value because you're counting frames ever second, not taking the inverse of elapsed time every frame. |
||||
|
||||
![]() Drethon Member since: 11/29/2004 From: Grand Rapids, MI, United States |
||||
|
|
||||
| Maybe I'm missing something but aren't your units in ms? So 1000/0.1 isn't 10,000 frames per second but 10,000 frames per millisecond = 10 frames per second. |
||||
|
||||
![]() Drew_Benton GDNet+ Member since: 7/29/2004 From: Katy, TX, United States |
||||
|
|
||||
| Here is a simple tutorial showing FPS from GameTutorials. |
||||
|
||||
![]() utilae Member since: 12/8/2004 From: Dargaville, New Zealand |
||||
|
|
||||
Quote: 1000ms = 1 second 1000ms/1000ms = 1 frame for this second, ie 1 fps EDIT: I am using c++ and Direct3D9 |
||||
|
||||
![]() utilae Member since: 12/8/2004 From: Dargaville, New Zealand |
||||
|
|
||||
Quote: Hey, I was wondering. The bit were you have 'while(true)'. While what is true? |
||||
|
||||
![]() nife Member since: 4/6/2004 From: Denmark |
||||
|
|
||||
| timeGetTime can get 1 ms accuracy by using timeBeginPeriod(1); in create function and timeEndPeriod(1); in destroy function. It's very important to remember the last one, because normally it only updates every 10 ms or so. |
||||
|
||||
![]() Zipster GDNet+ Member since: 3/11/2000 From: Las Vegas, NV, United States |
||||
|
|
||||
| Technically, you only request a 1 ms update time. It will return TIMERR_NOCANDO if it isn't possible. |
||||
|
||||
![]() GodlyGamr Member since: 9/15/2003 From: Huntingdon Valley, PA, United States |
||||
|
|
||||
Quote: Nothing in particular. It will just keep looping until the program is terminated. |
||||
|
||||
![]() helix Member since: 9/11/2002 From: Kirkland, WA, United States |
||||
|
|
||||
Quote: That's just a way to make an infinite loop. true is just a statement the same as any other conditional test that will resolve to a true or false. In his example, that's just the game loop that will run forever until the game exits. |
||||
|
||||
![]() MrRage Member since: 8/7/2004 |
||||
|
|
||||
Here is an exampel of using QueryPerformanceFrequency()
__int64 timerFreq; // How many counts in a second.
__int64 startTime, endTime; // Start and ends times
float fps;
if(QueryPerformanceFrequency((LARGE_INTEGER *)&timerFreq) != 0){
QueryPerformanceCounter((LARGE_INTEGER *)&startTime);
....
QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
fps = (float)((double)timerFreq / ((double)(endTime - startTime)));
}
Thant should do it |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|