Jump to content
  • Advertisement
Sign in to follow this  
Baf

how to compute fps?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Ok. Here is a short guide:

1) Store the current time as LastTime.
2) Count each frame you render as Count.
3) If Count reaches some predefined number NumFrames update the FPS:
(a) Set ElapsedTime is current time minus LastTime
(b) FPS is now NumFrames / ElapsedTime.
(c) Set LastTime to current time.

The number NumFrames determines your resolution -- the more frames the better the accuracy but the less up-to-date on average.

Greetz,

Illco

Share this post


Link to post
Share on other sites

// C++
DWORD EndTime = GetTickCount() + 1000;
int frames = 0;

// Game Loop
while(true)
{
if(GetTickCount() > EndTime)
{
// Display your FPS, perhaps in the title bar of your application?
// That seems to be easiest.

EndTime = GetTickCount() + 1000;
frames = 0;
}

YourNormalGameLoop();

frames++;
}



// C#
int lastSecond = DateTime.Now.Second;
int frames = 0;

// Game Loop
while(app.Created)
{
if(DateTime.Now.Second != lastSecond)
{
app.Text = String.Format("{0} fps", frames);

lastSecond = DateTime.Now.Second;
frames = 0;
}

YourNormalGameLoop();

fames++;
}


Neither solution is overly accurate but it'll ballpark it.

Share this post


Link to post
Share on other sites
Yet another one! I think timeGetTime() requires winmm.lib to be linked, unless you're linking it already.

DWORD FPS()
{
static DWORD fpstimer=0;
static uint fps=0;
static uint frames=0;

frames++;

if(timeGetTime()-fpstimer>1000)
{
fpstimer=timeGetTime();
fps=frames;
frames=0;
}
return fps;
}


Call it once per loop, and use the value it returns to display the frame count.

Share this post


Link to post
Share on other sites
Hi there Baf,
Thanks for that Grozzler, fantastic reply :)
Here is my 2 cents :)

[Calculating the FPS using C#]
[source lang = c#]
public void CalcFPS()
{
frameCount++;
if(Environment.TickCount - counter >= 1000)
{
fps = frameCount;
counter = Environment.TickCount;
frameCount = 0;
}
}
private static float counter = 0;
private static float frameCount = 0;
private static float fps = 0;




I hope this helps a bit.
Keep cool.
Once again, Thank you Grozzler for the reply.

[Edited by - Armadon on August 11, 2005 12:16:33 PM]

Share this post


Link to post
Share on other sites
Hey, that code looks awfully familiar. Can't place where you must've gotten it, though. Though I'm not sure it'll compile. Don't know what C# would think of a "private public" function.

Share this post


Link to post
Share on other sites
thank you all
my fps is about 40 is it too slow? game is not finished. if i bypass gameloop i have 8000!!!




Share this post


Link to post
Share on other sites
Quote:
Original post by Baf
thank you all
my fps is about 40 is it too slow? game is not finished. if i bypass gameloop i have 8000!!!

Well, it depends. What kind of system is it? What kind of application? If you think it's too slow, take a preliminary look at it right now, to check for any major design flaws. However, save the optimizations for the end.

Share this post


Link to post
Share on other sites
NOooooooo biggest problem!!!

i only added the code for fps view and computing but
if i move my created window i get a sistem crash

this is the code
DWORD EndTime = GetTickCount() + 1000;
int frames = 0;
char s[50];
RECT *rectTxt=new RECT();
rectTxt->......
//********************************
while (....) //MAIN MESSAGE LOOP
{
if(GetTickCount() > EndTime)
{
sprintf( s, " FPS= %d\n", frames );
EndTime = GetTickCount() + 1000;
frames = 0;
}
frames++;
DrawText ( GetDC(wndHandle), s, -1, rectTxt, DT_NOCLIP);

GameLoop();
}

whaz heppened? my mind is crashing

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!