Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Calculating Frames Per Second
Topic Started: Sep 28 2009, 09:14 PM (1,628 Views)
NGen
Member Avatar
Advanced Member
[ *  *  * ]
[NOTE]: This method is Windows specific. If you want a cross-platform option, the SDL is cross-platform and can allow you to just load the timer aspect if that's all you need. However, it's resolution is limited to 10 ms. Since it's open-source, you can just remove what you don't need and then rebuild the DLL. ( Of course, it won't be that simple, but it's an option )

FPS is often an important statistic in a game, and when there's no FPS available for viewing then we don't know when our game is running slow until we see a sudden drop in it, or we look back at previous versions of a program.

--- Getting The Header ---
First, we include the library needed to prototype our timing function:

Code:
 
#include <mmsystem.h>

( If you don't have this file, make sure you have the Windows SDK )

This will prototype the function timeGetTime, which returns the time (in milliseconds) since when the system was started. It's similar to the current_time variable in GM. We need this to see how long it takes for the code to execute.

--- Defining timeGetTime ( ) ---
Next, we need to load the information that defines timeGetTime, which is in the Winmm.lib file. We load it using:

Code:
 
#pragma comment ( lib, "Winmm.lib" )

( Note, no semicolon is used because the hash symbol defines a pre-processor directive )

This loads the information to define the functions in mmsystem.h. Well, at least, the timeGetTime function. Now, let's put this function to work.


--- Putting it Together ---
The general idea is to get the amount of time it takes to process a piece of code, and then divide 1000 by the time. This should give us our FPS. Here's a small example:

Code:
 
// A DWORD is just an unsigned int ( technically an unsigned long, according to the definition )
DWORD Start = timeGetTime ( );
int FPS = 0;

...

FPS = 1000 / ( timeGetTime ( ) - Start );



Short, simple, and to the point.


Want to know more about the timeGetTime function, and possible alternatives? Here's the documentation:
http://msdn.microsoft.com/en-us/library/aa912626.aspx


//////////////////////////////////////////////////////////////////

Eh, I know it's not my usual quality, but it's basically just telling you about the timeGetTime function, how to use #pragma comment, and how to put them together to get the FPS. I'll edit this every now and then to maybe organize it a bit more like my other tutorials.
Edited by NGen, Sep 28 2009, 10:06 PM.
Offline Profile Quote Post Goto Top
 
NGen
Member Avatar
Advanced Member
[ *  *  * ]
Ah, crap.. I put this in the wrong forum, methinks. Could someone put this in with the Templates tutorial?
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
Nice. This tutorial taught me the following:

  • How Newton imports the LIB file
  • How not to use the CTime class


:P
Blog|EHS
Offline Profile Quote Post Goto Top
 
NGen
Member Avatar
Advanced Member
[ *  *  * ]
Well, CTime is actually cross-platform, so I suppose using that would be better. I've just never used it, so I've been using timeGetTime instead. Although I suppose timeGetTime gives a more accurate value...
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
NGen
Sep 28 2009, 11:38 PM
Well, CTime is actually cross-platform, so I suppose using that would be better. I've just never used it, so I've been using timeGetTime instead. Although I suppose timeGetTime gives a more accurate value...
If you want totally accurate values (on most systems the resolution is higher than nano seconds) use the query performance counter. That is what the performance log feature in Ultimate 3D 2.1.2 is based upon. If you want to be platform independent use the timer only internally in a timer class. If you ever switch to another platform you can add some #ifdef directives to use other timers for this case.
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
Quote:
 
If you ever switch to another platform you can add some #ifdef directives to use other timers for this case.


*remembers something and in a hurry opens up MSVC++*

I'm wondering, Dr. Best, how would one go about using this 'query performance counter' that I've got no idea what it is?
Blog|EHS
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
Using the query performance counter is quite simple. There are just three things, which make it a little bit more complicated:

  • It uses 64-bit integers.
  • You have to ask for its frequency and use that to work with the resulting values.
  • It is not guaranteed that it is supported. Having a fallback is recommended.
Here is a little excerpt from my CTimer class:
Code:
 
class CTimer{
public:
/*[...]*/
private:
// This constructor initializes this class
CTimer::CTimer()/*[...]*/{
/*[...]*/
QueryPerformanceFrequency(&CounterFrequency);
TickLength=1000.0/static_cast<double>(CounterFrequency.QuadPart);
/*[...]*/
}
// This function returns the current time in seconds/frequency
inline LARGE_INTEGER GetCurrentTime() const{
LARGE_INTEGER Result;
if(QueryPerformanceCounter(&Result)){
return Result;
}
else{
Result.QuadPart=timeGetTime();
return Result;
}
}
/*[...]*/
// This variable saves the frequency of the query performance counter
LARGE_INTEGER CounterFrequency;
// This variable saves the length of a tick in milliseconds. A tick is the time
// that passes in 1/CounterFrequency seconds.
double TickLength;
/*[...]*/
};

You can look up all used functions on the MSDN.
Offline Profile Quote Post Goto Top
 
NGen
Member Avatar
Advanced Member
[ *  *  * ]
*Makes note of above post to check again when laptop is available*

So, how exactly did you learn about this function? Google?
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
Right, thanks for the function names, that's all I needed.

I should probably move the FPS to an object instead of having it out in the open. Causes some confusion...

@NGen - I use Yahoo, but I think that's where most of us learned it. Well, that's where I learned it all.
Blog|EHS
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
NGen
Sep 29 2009, 05:36 AM
So, how exactly did you learn about this function? Google?
It's been explained in a programming book I read years ago. When I decided to use it again I just looked for it on the MSDN.
Offline Profile Quote Post Goto Top
 
Adolph
Newbie
[ * ]
thanks for the function names, that's all I needed :D Posted Image
Offline Profile Quote Post Goto Top
 
« Previous Topic · Tutorials · Next Topic »
Add Reply