Jump to content
  • Advertisement

Archived

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

Qa303asmGuru

Question about obtaining FPS

This topic is 5675 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

I'm currently working on a new project with 2 other people. It's a 2D shmup using the OpenGL API for graphics and DirectX API for input and sound. I've been trying to gauge performance on difference machines. Is there an easy way to get FPS? If you could post some sample code that would be helpful. I hope to get a beta out for this game in March. Check the site for updates. -Q [The Zmatrix Project] - My page maddox.xmission.com - Maddox's page Edit: had to fix sig [edited by - Qa303asmGuru on February 3, 2003 2:36:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
wait more than 20 minutes before bumping your posts.

and i'm sure google has more than a few code samples on calculating fps, why don't you give it a shot?

[edited by - niyaw on February 3, 2003 2:48:29 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you have the DirectX SDK

check out DXUtil_Timer() in dxutil.h and dxutil.cpp

very easy to use and calculate frames per second

directx is not required to use this timer
(i think)

Share this post


Link to post
Share on other sites
Oh, man, this is a rather basic question, and I wonder why others can''t figure it out.

All you need to do is learn how to time things:

// outside of the game loop for scope reasons
DWORD difference;

//beginning of the game loop
DWORD currentTime = timeGetTime();

// middle of the game loop
if(difference != 0)
int framesPerSecond = 1000/difference;
print out framesPerSecond; // whatever function you''re going to use

// end of the game loop
difference = timeGetTime - currentTime();

You can play with averages, or take the differenc every other frame or every 5 frames or whatever if you don''t want the number to jump around as much.

I''ll just end the post now to keep myself from saying anything rude

--Vic--

Share this post


Link to post
Share on other sites
quote:
Original post by Roof Top Pew Wee
Oh, man, this is a rather basic question, and I wonder why others can''t figure it out.

All you need to do is learn how to time things:

// outside of the game loop for scope reasons
DWORD difference;

//beginning of the game loop
DWORD currentTime = timeGetTime();

// middle of the game loop
if(difference != 0)
int framesPerSecond = 1000/difference;
print out framesPerSecond; // whatever function you''re going to use

// end of the game loop
difference = timeGetTime - currentTime();

You can play with averages, or take the differenc every other frame or every 5 frames or whatever if you don''t want the number to jump around as much.

I''ll just end the post now to keep myself from saying anything rude

--Vic--


I don''t know what you have to get mad about. I appreciate your help but I already knew about this and tried it. The problem I''m having is that the timers don''t carry enough decimal places to effectively calculate elapsed time. The ''elapsed'' time comes out to zero, and obviously you can''t divide by zero.

I need an extremely high resolution timer that can carry enough decimal places to do the frame calculation.

BTW, if I put in the Sleep(ms) function I get accurate results, but I don''t want a slowed down version.

I haven''t tried the DX utility yet. I''ll have a look at that later...hopefully it''s what I''m looking for.

-Q


[500x2]


[ The Zmatrix Project ] - My page
maddox.xmission.com - Maddox''s page

Share this post


Link to post
Share on other sites
When dealing with low resolution timers, just use 20-50 frames as the sample. This removes a lot of the resolution error in low resolution timers. It is still less accurate, but it's accurate enough for the majority of applications.

Mark your start time.

Run 50 frames.

Mark your end time.

fps = 50000/(end time - start time)

It's 50000 instead of 1000 because your sample was 50 frames. It would actually end up being 50 * 1000/(end time - start time), but no reason to do the extra math when it's constant.

It's really not completely essential to use a high resolution timer with fps calculations, and most people end up sampling over larger number of frames because it's kind of hard to read a value that changing every 100msec or less.

[edited by - jRaskell on February 3, 2003 4:56:34 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
// ok well here it is removed from dxutil.h/cpp
// -------------------------------------------------------------

// -------------------------------------------------------------
// timer.h


enum TIMER_COMMAND { TIMER_RESET, TIMER_START, TIMER_STOP, TIMER_ADVANCE,
TIMER_GETABSOLUTETIME, TIMER_GETAPPTIME, TIMER_GETELAPSEDTIME };
FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command );


// -------------------------------------------------------------

// -------------------------------------------------------------
// timer.cpp


#include <windows.h>
#include <mmsystem.h>
#include "timer.h"

FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
{
static BOOL m_bTimerInitialized = FALSE;
static BOOL m_bUsingQPF = FALSE;
static BOOL m_bTimerStopped = TRUE;
static LONGLONG m_llQPFTicksPerSec = 0;

// Initialize the timer
if( FALSE == m_bTimerInitialized )
{
m_bTimerInitialized = TRUE;

// Use QueryPerformanceFrequency() to get frequency of timer. If QPF is
// not supported, we will timeGetTime() which returns milliseconds.
LARGE_INTEGER qwTicksPerSec;
m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
if( m_bUsingQPF )
m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
}

if( m_bUsingQPF )
{
static LONGLONG m_llStopTime = 0;
static LONGLONG m_llLastElapsedTime = 0;
static LONGLONG m_llBaseTime = 0;
double fTime;
double fElapsedTime;
LARGE_INTEGER qwTime;

// Get either the current time or the stop time, depending
// on whether we''re stopped and what command was sent
if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
qwTime.QuadPart = m_llStopTime;
else
QueryPerformanceCounter( &qwTime );

// Return the elapsed time
if( command == TIMER_GETELAPSEDTIME )
{
fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
m_llLastElapsedTime = qwTime.QuadPart;
return (FLOAT) fElapsedTime;
}

// Return the current time
if( command == TIMER_GETAPPTIME )
{
double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
return (FLOAT) fAppTime;
}

// Reset the timer
if( command == TIMER_RESET )
{
m_llBaseTime = qwTime.QuadPart;
m_llLastElapsedTime = qwTime.QuadPart;
m_llStopTime = 0;
m_bTimerStopped = FALSE;
return 0.0f;
}

// Start the timer
if( command == TIMER_START )
{
if( m_bTimerStopped )
m_llBaseTime += qwTime.QuadPart - m_llStopTime;
m_llStopTime = 0;
m_llLastElapsedTime = qwTime.QuadPart;
m_bTimerStopped = FALSE;
return 0.0f;
}

// Stop the timer
if( command == TIMER_STOP )
{
if( !m_bTimerStopped )
{
m_llStopTime = qwTime.QuadPart;
m_llLastElapsedTime = qwTime.QuadPart;
m_bTimerStopped = TRUE;
}
return 0.0f;
}

// Advance the timer by 1/10th second
if( command == TIMER_ADVANCE )
{
m_llStopTime += m_llQPFTicksPerSec/10;
return 0.0f;
}

if( command == TIMER_GETABSOLUTETIME )
{
fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
return (FLOAT) fTime;
}

return -1.0f; // Invalid command specified
}
else
{
// Get the time using timeGetTime()
static double m_fLastElapsedTime = 0.0;
static double m_fBaseTime = 0.0;
static double m_fStopTime = 0.0;
double fTime;
double fElapsedTime;

// Get either the current time or the stop time, depending
// on whether we''re stopped and what command was sent
if( m_fStopTime != 0.0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
fTime = m_fStopTime;
else
fTime = timeGetTime() * 0.001;

// Return the elapsed time
if( command == TIMER_GETELAPSEDTIME )
{
fElapsedTime = (double) (fTime - m_fLastElapsedTime);
m_fLastElapsedTime = fTime;
return (FLOAT) fElapsedTime;
}

// Return the current time
if( command == TIMER_GETAPPTIME )
{
return (FLOAT) (fTime - m_fBaseTime);
}

// Reset the timer
if( command == TIMER_RESET )
{
m_fBaseTime = fTime;
m_fLastElapsedTime = fTime;
m_fStopTime = 0;
m_bTimerStopped = FALSE;
return 0.0f;
}

// Start the timer
if( command == TIMER_START )
{
if( m_bTimerStopped )
m_fBaseTime += fTime - m_fStopTime;
m_fStopTime = 0.0f;
m_fLastElapsedTime = fTime;
m_bTimerStopped = FALSE;
return 0.0f;
}

// Stop the timer
if( command == TIMER_STOP )
{
if( !m_bTimerStopped )
{
m_fStopTime = fTime;
m_fLastElapsedTime = fTime;
m_bTimerStopped = TRUE;
}
return 0.0f;
}

// Advance the timer by 1/10th second
if( command == TIMER_ADVANCE )
{
m_fStopTime += 0.1f;
return 0.0f;
}

if( command == TIMER_GETABSOLUTETIME )
{
return (FLOAT) fTime;
}

return -1.0f; // Invalid command specified
}
}

Share this post


Link to post
Share on other sites

  • 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!