Jump to content
  • Advertisement
Sign in to follow this  
Programmer16

Timer problem

This topic is 4871 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 don't understand why my code isn't working. Every time it gets to the line that I've marked (in the update function), it changes both m_nCurrentTime and m_nLastTime. So, no matter what, there is no frame rate.
class Timer
{
	float			m_fTimeScale;
	float			m_fTimeElapsed;
	unsigned int	m_nCurrentTime;
	unsigned int	m_nLastTime;
	unsigned int	m_nFrameRate;
	unsigned int	m_nFPSFrameCount;
	float			m_fFPSTimeElapsed;
public:
	Timer()
	{
		m_nLastTime			= timeGetTime();
		m_fTimeScale		= 0.001f;
		m_fTimeElapsed		= 0;
		m_nCurrentTime		= 0;
		m_nFrameRate		= 0;
		m_nFPSFrameCount	= 0;
		m_fFPSTimeElapsed	= 0.0f;
	}

	void Update()
	{
		m_nCurrentTime	= timeGetTime(); 
		/* when it gets to this line */
		m_fTimeElapsed	= (m_nCurrentTime - m_nLastTime) * m_fTimeScale;
		m_nLastTime		= m_nCurrentTime;

		++m_nFPSFrameCount;
		m_fFPSTimeElapsed += m_fTimeElapsed;
		if(m_fFPSTimeElapsed > 1.0f)
		{
			m_nFrameRate = m_nFPSFrameCount;
			m_nFPSFrameCount = 0;
			m_fFPSTimeElapsed = 0.0f;
		}
	}

	float			GetTimeElapsed() const	{ return m_fTimeElapsed;	}
	unsigned int	GetFrameRate() const	{ return m_nFrameRate;		}
};

Am I missing something or doing something wrong? I'm using Visual C++ .NET 2k3. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
It probably doesn't actually change both, except if you're compiling with optimizations it might look like it. However, it may be that the time between samples is too short, so you don't get the frame rate measurement anyway.

Typically, you'll want to wait about 1 second between each reading before actually calculating a new frame rate. Thus, the code looks something like:


void step() {
++framesRendered_;
curTime = ReadTime();
if( curTime - lastTime_ >= OneSecond ) {
frameRate = framesRendered_ / float(TicksPerSecond * (curTime-lastTime_));
lastTime_ = curTime;
framesRendered_ = 0;
}
}

Share this post


Link to post
Share on other sites
That's pretty much what my code is doing, isn't it? It subtracts the last time from the current time, which would be the frame's time elapsed, and then adds it to the FPS time elapsed. Then, if that is greater than 1, it updates everything.

But, that actually isn't my problem. Dumb ol' me was instancing the timer inside of the loop, so that it was setting the current time and last time, and then updating.

Thanks!

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!