Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello everyone,

 

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

 

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

 

       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 don't 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 I'd apperciate it =)!

Posted

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

Thank you. I read this, I'm pretty well at using it. That's 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.

 

I'm not sure how to actually calculate the FPS.. or am I missing something?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...