Calculating FPS with QueryPerformanceCounter()

tmack

New member
Joined
Jan 21, 2006
Messages
3
Hello everyone,

Im writing a engine in C# using Visual Studio 2005 (.NET 2.0). Right now I am using QueryPerformanceCounter().

Im not sure exactly how to calculate FPS with QueryPerformanceCounter, but I know how with timeGetTime. Here is my code for timegettime:

Code:
        public struct FPSManager {
            public int fps;
            public int CurrentTime;
            public int LastTime;
            public int frames;
        }

        public void MainLoop() {
            while (GameLoop) {
                fps.CurrentTime = timeGetTime();
                fps.frames++;
                Application.DoEvents();

                if (fps.CurrentTime - fps.LastTime >= 1000) {
                    fps.LastTime = fps.CurrentTime;
                    fps.fps = fps.frames;
                    fps.frames = 0;
                }
            }  
        }

I never even tested it, so i dont know if it works. But I was told to use QueryPerformanceCounter instead.

Anyhow, if someone can provide me with a nice example to get me started Id apperciate it =)!
 
It looks as if QueryPerformanceCounter is a part of the windows API, personally I think that Enviornment.TickCount() works good enough; however, MSDN has this to say on the subjecet.

[csharp]
// QueryPerfCounter.cs
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;



public class QueryPerfCounter
{
[DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

private long start;
private long stop;
private long frequency;
Decimal multiplier = new Decimal(1.0e9);

public QueryPerfCounter()
{
if (QueryPerformanceFrequency(out frequency) == false)
{
// Frequency not supported
throw new Win32Exception();
}
}

public void Start()
{
QueryPerformanceCounter(out start);
}

public void Stop()
{
QueryPerformanceCounter(out stop);
}

public double Duration(int iterations)
{
return ((((double)(stop - start)* (double) multiplier) / (double) frequency)/iterations);
}
}
[/csharp]

And this would be the usage of the above class.

[csharp]
QueryPerfCounter myTimer = new QueryPerfCounter();
// Measure without boxing
myTimer.Start();
for(int i = 0; i < iterations; i++)
{
// do some work to time
}
myTimer.Stop();
// Calculate time per iteration in nanoseconds
double result = myTimer.Duration(iterations);
[/csharp]

Click here to see the full article for QueryPerformanceCounter in Managed Code.
 
Thank you. I read this, Im pretty well at using it. Thats not really the issue.

What I need to know is how to calculate the frame rate (of a game) using this rather then timeGetTime(). TimeGetTime was easier to use, actually but it seems QueryperformanceCounter works differently.

Im not sure how to actually calculate the FPS.. or am I missing something?
 
Back
Top