• Advertisement
Sign in to follow this  

Calculating FPS?

This topic is 4520 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
Record the number of times you go through the mainloop over the course of a second:

currtime = timeGetTime();
nexttime = currtime + 1000;
fps = 0;

while (m_running) {
currtime = timeGetTime();
if (currtime > nexttime) {
// fps is your fps for this second
fps = 0;
nexttime = currtime + 1000;
} else {
fps++;
}

// do stuff
}

Share this post


Link to post
Share on other sites
You measure milliseconds with a suitable timer function. The code above uses timeGetTime(), which returns the time (in milliseconds) since the system booted. Its defined in <windows.h>, and you'll have to link in "winmm.lib" to use it.

DWORD timeGetTime(void)

Share this post


Link to post
Share on other sites
Quote:
Original post by C plus noob
ok sp i want to send FPS as setwindow text (i know how to do this) were would i put this loop in WinMain?


if (currtime > nexttime) {
// fps is your fps for this second
fps = 0;
nexttime = currtime + 1000;
char cMsg[256];
sprintf(cMsg,"APPTITLE :: %d", (int)fps);
SetWindowText(hwnd,cMsg)
} else {

Share this post


Link to post
Share on other sites
Quote:
Original post by C plus noob
ok sp i want to send FPS as setwindow text (i know how to do this) were would i put this loop in WinMain?

Presumably you've got a loop set up that continously updates the state of your app (does all the rendering and stuff). This is your main loop.

In the code sample I provided, the while (m_running) loop is a representative of your main loop. So take the rest of the stuff and stick it into your main loop in the appropriate places, as shown in the sample code.

Share this post


Link to post
Share on other sites

while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE]) // Was ESC Pressed?
{
done=TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
DrawGLScene(); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}

if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}



is this that loop?

Share this post


Link to post
Share on other sites
here is some code on how i do it.

void Fps()
{
static int fps=0;
static float ltimer=GetTickCount()*.001f;
float timer=GetTickCount()*.001f;
++fps;
if(timer-ltimer>1.0f){
ltimer=timer;
char buffer[MAX_PATH];
sprintf(buffer,"The FPS is:%d\n",(int)fps);
SetWindowText(hwnd,buffer);
fps=0;
}
}

Share this post


Link to post
Share on other sites
Yes that's the loop.


GetTickCount() returns the number of milliseconds.

Since 1 second = 1000 milliseconds, if you multiply the result by 0.001, you get the total number of seconds.
It's the same thing as dividing by 1000.

You do stuff generally in seconds so that it's much easier to move objects in the game world because you can use a standard "units/sec" velocity scalar.

Share this post


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

  • Advertisement