Archived

This topic is now archived and is closed to further replies.

Time recording for fps system - how to?

This topic is 5993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am setting up a fps recorder in my game, but i dont know the rutines for getting the system time - anyone? ------------- E-)mil http://eej.dk - Just another crazy dane

Share on other sites
timeGetTime()

or

GetTickCount()

or

QueryPerformanceCounter()

Share on other sites
timeGetTime() is perhaps the most accurate time function without delving into Multimedia event timer callback function.
It requires a little preparation, though. timeBeginPeriod() needs to be called at the beginning of the program and timeEndPeriod() at the end. Also, the mmsystem.h file needs to be included and the winmm.lib file needs to be linked.

Share on other sites
Err - sounds dangerus. Dont any of you have a fps counting system? I just need a function to measure and calculate the difference between to time recordings (in seconds).

-------------
E-)mil
http://eej.dk

- Just another crazy dane

Share on other sites
Actually, it's not really that difficult. Here's what to do:

Declare these global variables outside of your functions:
DWORD framerate = 0;
DWORD newtimer = 0;
DWORD oldtimer = 0;

Put these lines in your game initialization code

if(timeBeginPeriod(1) != TIMERR_NOERROR)
{
// bail out of the program if the timer can't initialize
// because this means your system is out of resources
exit(65535);
}

and put this at the beginning of your main game cycle

oldtimer = newtimer;
newtimer = timeGetTime();
framerate = newtimer - oldtimer;

framerate now represents the amount of time in Milliseconds between each frame. You can convert this to fps or whatever and display it on the screen. I took this directly out of my Pong code where I used it mainly to determine a frame-rate independent speed for my game objects so that the movement wouldn't look choppy.

Then to clean it up, put this line in your game cleanup code.

timeEndPeriod(1);

This is how I thought would be the easiest way to implement this. Of course it takes some global variables, which to OOP gurus are an absolute no-no, and I can't imagine a way of abstracting this kind of functionality into a neat little function, though, given time I probably could. Don't forget to include mmsystem.h at the top of the program and link winmm.lib to your project in the settings box.

Hope this helps.

Edited by - Paradigm Shift 2000 on September 23, 2001 2:25:15 PM

Share on other sites
You don''t have to do beginperiod and endperiod, it works fine without them, and the timer is running anyways cause windows starts it automatically. In my programs I just call timeGetTime with no preparation and it works fine.

Share on other sites
The performance counter functions will give you the highest resolution and don''t require callbacks. However, the actual time it takes to return is higher than the other functions (in relative terms).

This means that it you are timing something that takes some time you can get down to the microsecond, but if you are timing something that is very very quick, you may get better results with a lower resolution timer.

Share on other sites
DWORD g_dwFrameRate = 0; // FPS you want
DWORD g_dwLastTickCount = GetTickCount();
DWORD g_dwFrameCount = 0;

g_dwFrameCount++;

if (abs(GetTickCount() - g_dwLastTickCount) >= 1000)
{
g_dwLastTickCount = GetTickCount();
g_dwFrameRate = g_dwFrameCount;
g_dwFrameCount = 0;
}

Share on other sites
That''s the way I was doing it originally, but then I read that timeGetTime() had better resolution. In the documentation, GetTickCount() only has a resolution of 55ms which means that repeated calls to GetTickCount() less than 55ms apart are going to be inaccurate. I tested this and actually got better results with timeGetTime() so long as i initialized it with timeBeginPeriod(1) for a resolution of 1ms. I suppose the inaccuracies of the resolution are going to have less of an effect on methods that take very little time, like the main cycle of a Pong game, but the multimedia timer functions have better accuracy overall.

Share on other sites
Hehe - sorry for the trouble im making, but im not thinking clear now
I use paradigm''s code to measure the time, now how do i calcuate the fps from that time recording?

-------------
E-)mil
http://eej.dk

- Just another crazy dane

Share on other sites
frames per second = 1 / (seconds per frame)

codeka.com - Just click it.

Share on other sites
(int)((float)1/(float)framerate)
But it gives 0 no matter what...

-------------
E-)mil
http://eej.dk

- Just another crazy dane

Share on other sites
#include
#pragma comment(lib,"winmm.lib")

DWORD g_dwFrameRate = 0; // FPS you want
DWORD g_dwLastTickCount = 0;
DWORD g_dwFrameCount = 0;

timeBeginPeriod(1);
g_dwFrameCount++;

if (abs(timeGetTime() - g_dwLastTickCount) >= 1000)
{
g_dwLastTickCount = timeGetTime();
g_dwFrameRate = g_dwFrameCount;
g_dwFrameCount = 0;
}

timeEndPeriod(1);

Share on other sites
>>Payne
Shure - shure - why should that be better than the other method, and how do i calculate the fps value here?

-------------
E-)mil
http://eej.dk

- Just another crazy dane

Share on other sites
seem my timeGetTime run at 1000 ticks per second,
should it be 1024? i wonder.........

whatever timeGetTime or timeGetTime,
fps = 1000/(currentframetick-lastframetick)
and that is, u have ur fps every frame.

Share on other sites
OK

// Global variables
DWORD g_dwFrameRate = 0; // FPS you want
DWORD g_dwLastTickCount = 0;
DWORD g_dwFrameCount = 0;

// Put the following lines after IDirect3DDevice:resent() method
timeBeginPeriod(1);
g_dwFrameCount++;

if (abs(timeGetTime() - g_dwLastTickCount) >= 1000)
{
g_dwLastTickCount = timeGetTime();
g_dwFrameRate = g_dwFrameCount;
g_dwFrameCount = 0;

char szText[22];
wsprintf(szText, "Frame Rate = %d fps", g_dwFrameRate);
SetWindowText(hWnd, szText);
}

timeEndPeriod(1);

Test

Share on other sites
Gah! Sorry, emileej... the proper fps conversion *should* be this:

  float framespersecond = (float)(1000)/(float)(framerate);

This way, if a frame takes approx 17ms, it should give you a result of 60fps

Share on other sites
Hehe - i also found it kinda wired, when my fps climbed, when i moved the mouse, and fell when i stopped moving it

-------------
E-)mil
http://eej.dk

- Just another crazy dane