tmack Posted January 21, 2006 Posted January 21, 2006 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 =)! Quote
Nate Bross Posted January 22, 2006 Posted January 22, 2006 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
tmack Posted January 22, 2006 Author Posted January 22, 2006 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? Quote
Nate Bross Posted January 22, 2006 Posted January 22, 2006 I know you said you're using C#, but this tutorial should help you with the logic, even though it's in VB. http://www.vbforums.com/showthread.php?t=337571 Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.