• Advertisement
Sign in to follow this  

Elapsed Time in C++ .NET

This topic is 4046 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 am sure it is quite obvious, but I somehow cannot find a good way to track elapsed time in C++ .NET. I believe most folks just use ftime or some such when in unmanged C++, and I need an equivalent for managed C++. It would of course need to have descent resolution for a game clock. Can anyone point me to the most effective method? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I personally use C++ V6.0, but I can't imagine why this wouldn't work. I use a Timer class I made based off information in the book Beginning DirectX 9

Before the loop call timer.FindFreq(). In the loop begin by calling timer.Begin(), and after you update and render each frame, call timer.End() which returns a float equal to the number of seconds that have passed since your call to begin(). I typically use this float to pass to my update functions on the next time through the loop, so they can each interpolate the new positions of all game objects.

Hope this helps, sorry if not.


//ZTimer.h
#include <windows.h>

class ZTimer{
private:
LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
LARGE_INTEGER timerFreq;

public:
ZTimer();
~ZTimer();
void Begin();
float End();
void FindFreq();
};





//ZTimer.cpp

#include "ZTimer.h"

ZTimer::ZTimer(){
}

ZTimer::~ZTimer(){
}

void ZTimer::Begin(){
QueryPerformanceCounter(&timeStart);
}

float ZTimer::End(){
QueryPerformanceCounter(&timeEnd);
return ((float)timeEnd.QuadPart - (float)timeStart.QuadPart) / (float)timerFreq.QuadPart;
}

void ZTimer::FindFreq(){
QueryPerformanceFrequency(&timerFreq);
}



Share this post


Link to post
Share on other sites
There's System.Environment.TickCount which is an int with the number of elapsed milliseconds since the system started. You may need higher resolution, although I suspect this would be alright.

Share this post


Link to post
Share on other sites
This will help you out;

http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/

theTroll

Share this post


Link to post
Share on other sites
Thanks for the replies. I believe that for my current purpose TickCount is sufficient, but there are quite a few more options than I had realized. Plenty of choices to go with here it looks like.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement