Sign in to follow this  
Alpha_ProgDes

Frames Per Second (FPS) display. Is there a better way?

Recommended Posts

    thisTickCount = timeGetTime();
    G.diffTickCount = thisTickCount - lastTickCount;
    lastTickCount = thisTickCount;
    if (G.diffTickCount == 0)
        G.diffTickCount = 1001;
    fps = 1.0 / (.001* (double)G.diffTickCount);
    int n = sprintf(fpsBuffer, "The frames per second are: %6.3f", fps);

    SetTextColor(G.hDC, RGB16COLOR(16, 2, 16));
    SetBkColor(G.hDC, RGB16COLOR(0, 0, 0));
    TextOut(G.hDC, 0,0, fpsBuffer, strlen(fpsBuffer));
if anyone has a better way, please let me know. also is there way i can get the FPS to slow down... I have a hard time reading it. edit: changed title so when I search for this topic again, it'll come up better. [Edited by - Alpha_ProgDes on February 13, 2005 10:49:08 PM]

Share this post


Link to post
Share on other sites
Use a variable to count the number of frames as you render them, then wait until a second has passed, then post how many frames were drawn. That way it only changes every one second and you actually get FPS. Here is some pseudo.

numberframes = 0
while(1)
logic_and_drawing()
curtime = gettime
if (curtime-lasttime > 1sec)
fps = numberframes
lasttime = curtime
numberframes = 0
numberframes++

Share this post


Link to post
Share on other sites

void GameLoop() {
DWORD begin, now = timeGetTime();
DWORD timeElapsed = 0;

int finalFPS = 0, fps = 0;

while (running) {
//Do various stuff.
now = timeGetTime();
timeElapsed = now - begin;
if ((timeElapsed) > 1000) {
finalFPS = fps;
fps = 0;
//Display finalFPS here
begin = now;
}
else {
fps++;
}
}
}




Beaten to it

Share this post


Link to post
Share on other sites
this is the way I have it now. also I figured out a slightly different way to FPSes visible

static DWORD framesTickCount = 0;
char fpsBuffer[64];
double fps = 0.0;
static int numberOfFrames = 0;
const DWORD resetCount = 1;

thisTickCount = timeGetTime();
G.diffTickCount = thisTickCount - lastTickCount;
lastTickCount = thisTickCount;
if (G.diffTickCount == 0)
G.diffTickCount = resetCount;
//G.diffTickCount = 1001;


while (framesTickCount >= 1000) { //this can be an if statement as well
fps = numberOfFrames / (.001* (double)framesTickCount);
sprintf(fpsBuffer, "The frames per second are: %6.3f", fps);

numberOfFrames = 0;
framesTickCount -= 1000;
}

while (/*time passes, I update the movement code*/) {
// movement code
}

SetTextColor(G.hDC, RGB16COLOR(16, 2, 16));
SetBkColor(G.hDC, RGB16COLOR(0, 0, 0));
TextOut(G.hDC, 0,0, fpsBuffer, strlen(fpsBuffer));

//Blt and flip images here

++numberOfFrames;
framesTickCount += G.diffTickCount;


does anyone else use similar code?

edit: minor alterations and comments

Share this post


Link to post
Share on other sites
Quote:
Original post by moeron
if timeGetTime is high enough resolution for you, thats fine, I would use QueryPerformanceCounter personally.

what's the major difference between the two?
will my fps get higher? cuz right now i'm maxing out at 75 fps and i think that weird seeing as all i'm doing is moving (automatically) one 32x32 sprite down screen and looping the sequence.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Quote:
Original post by moeron
if timeGetTime is high enough resolution for you, thats fine, I would use QueryPerformanceCounter personally.

what's the major difference between the two?
will my fps get higher? cuz right now i'm maxing out at 75 fps and i think that weird seeing as all i'm doing is moving (automatically) one 32x32 sprite down screen and looping the sequence.

I can't say I know what I'm talking about here, but the locked up FPS sounds more like something to do with V-Sync then your timer. Although I have been known to be wrong..

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Quote:
Original post by moeron
if timeGetTime is high enough resolution for you, thats fine, I would use QueryPerformanceCounter personally.

what's the major difference between the two?
will my fps get higher? cuz right now i'm maxing out at 75 fps and i think that weird seeing as all i'm doing is moving (automatically) one 32x32 sprite down screen and looping the sequence.


#1) 75 fps is locked to vsync being enabled

#2) timegettime is not a high resolution timer , it's only capable i believe of maximum 1ms resolution.

#3) timegettime should be sufficient for FPS counting, but you may want to ook into QueryPerformanceCounter when say dealing with physics or when you need the delta time. It is much more accurate and it uses as windows says a LARGE_INTEGER, instead of an int.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
so i take it that QPC and QFC have a resolution (ie. update time (?)_ of < 1 ms.
correct?
Yup. QueryPerformanceFrequency*() will tell you the number of ticks per second. On my machine (AMD 2600, Win2k), I get 3579545, which is 3579 ticks every millisecond (in theory - you won't get exactly that because of the overhead of calling QPC and other reasons).

I use a timer class to wrap up all this stuff for me. I call BeginFrame() every frame, and EndFrame() at the end of the frame (strangely enough :P). I can query the timer for the FPS or for the time it takes for one frame (useful for framerate independant movement). It averages the FPS over the last few frames to avoid juddering, and you can set a minimum FPS (So you don't get one frame taking 20 seconds if you jump to the debugger). Here's the source if you want a look:

#define NUM_FRAMES_TO_AVERAGE 10
#define MAX_FRAME_TIME (1.0/0.015) // 15 FPS

class CTimer
{
public:
CTimer()
{
QueryPerformanceFrequency(&m_liFreq);
m_liStart.QuadPart = 0;
m_dwFrames = 0;
for(DWORD i=0; i<NUM_FRAMES_TO_AVERAGE; ++i)
m_dFrameTimes[i] = 0.0;
m_dFrameTime = 9999.0;
}
~CTimer() {}

inline void BeingFrame()
{
QueryPerformanceCounter(&m_liStart);
}

inline void EndFrame()
{
// Get the duration of this frame //
LARGE_INTEGER liEnd;
double dFrameTime;
QueryPerformanceCounter(&liEnd);
liEnd.QuadPart -= m_liStart.QuadPart;
dFrameTime = ((double)liEnd.QuadPart) / ((double)m_liFreq.QuadPart) * 1000.0;
m_dwFrames++;

// Cap to minimum FPS //
if(dFrameTime > MAX_FRAME_TIME)
dFrameTime = MAX_FRAME_TIME;

// Add to frame times list //
if(m_dwFrames <= NUM_FRAMES_TO_AVERAGE)
{
// Spinning up, just fire frame times into array //
m_dFrameTimes[m_dwFrames-1] = dFrameTime;
dFrameTime = 0.0;
for(DWORD i=0; i<m_dwFrames; ++i)
dFrameTime += m_dFrameTimes[i];
m_dFrameTime = dFrameTime / (double)m_dwFrames;
}
else
{
// Shift list down, and push onto the end //
memmove(&m_dFrameTimes[0],&m_dFrameTimes[1],sizeof(double)*(NUM_FRAMES_TO_AVERAGE-1));
m_dFrameTimes[NUM_FRAMES_TO_AVERAGE-1] = dFrameTime;

// Average all the frames we have //
dFrameTime = 0.0;
for(DWORD i=0; i<NUM_FRAMES_TO_AVERAGE; ++i)
dFrameTime += m_dFrameTimes[i];
m_dFrameTime = dFrameTime / (double)NUM_FRAMES_TO_AVERAGE;
}
}

inline double GetFrameTime() const {return m_dFrameTime;}
inline double GetFPS() const {return 1.0/(m_dFrameTime/1000.0);}

protected:
LARGE_INTEGER m_liFreq;
LARGE_INTEGER m_liStart;
DWORD m_dwFrames;
double m_dFrameTimes[NUM_FRAMES_TO_AVERAGE];
double m_dFrameTime;
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this