What is wrong with this class?

Started by
3 comments, last by deadlydog 19 years, 11 months ago
I developed a class to use to control how many frames per seconds my games ran at, but it doesn''t seem to work. Here''s the class

// CClock.h

class CClock
{
public:
		
CClock();			
void Start();	// Only needs to be called once initially	


void Wait(unsigned long WaitTime);
void Wait30FPS();				

bool HasXTimePassed(unsigned long WaitTime);
bool Has30FPSPassed();

unsigned long TellStartTime() const
unsigned long GetTime() const
	
private:
unsigned long ClockTime;
};


// CClock.cpp

#include <windows.h>
#include "CClock.h"

// Class constructor

CClock::CClock()
{
	ClockTime = 0;
}

// Record time clock started

void CClock::Start()
{
	ClockTime = GetTime();
}

// Wait in a While loop until specified time has passed

void CClock::Wait(unsigned long WaitTime)
{
	while ((GetTime() - ClockTime) < WaitTime)
	{};

	Start();	// Restart Clock

}

// Wait in a While loop to achieve 30 Frames Per Second

void CClock::Wait30FPS()
{
	Wait(30);
}

// Returns True if specified WaitTime has passed, False if not

bool CClock::HasXTimePassed(unsigned long WaitTime)
{
	// If specified time has passed

	if ((GetTime() - ClockTime) < WaitTime)
	{
		return false;
	}

	Start();	// Restart Clock


	return true;
}

// Returns True if 30 FPS count has been achieved

bool CClock::Has30FPSPassed()
{
	return (HasXTimePassed(30));
}

// Returns the time the clock was last Started

unsigned long CClock::TellStartTime() const
{
	return ClockTime;
}

// Returns the current time

unsigned long CClock::GetTime() const
{
	return GetTickCount();
}
You can use either Wait(X) or HasXTimePassed(X). Wait(X) will simply sit in a While loop until X time has passed, and HasXTimePassed(X) will return False while X milliseconds have not passed, but they both essentially do the same thing. Now if I set X to 30 I get, surprise, 33 FPS. And if I set X to 15 I get 60 FPS, which is all good. Now, if I set X to 16 I get 40 FPS (is supposed to still be up around 60), and if I set X to 17 I get 33 FPS (should be still around 55-60). Infact, setting X to anything between 17 and 31 I get 30 FPS. And anything between 32 and 46 I get 22 FPS. It does this using both the Wait() and HasXTimePassed() functions. It seems like it''s maybe rounding off X somehow or something. Can you spot the problem in my code? because I can''t. Thanks. - God is my favorite fictional character
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Advertisement
The resolution of the time function you are using is probably the cause of the problem. Look into the high resolution timers.

See this article

edit fixed link

[edited by - Alvarny on May 20, 2004 11:57:49 PM]

The resolution of your timer is a good place to look for your problem, I agree. There was also an article featured here recently, at: http://gamedev.net/reference/programming/features/timing/

But note:
while ((GetTime() - ClockTime) < WaitTime){};


A good compiler will be able to determine that loop does nothing (unless GetTime() is nontrivial, maybe) and will optimize it away when you compile with optimization enabled (f.e.x, in Release mode in Visual C++); suddenly your "throttle" class (which is a more appropriate term, imo) will do nothing.

Even without taking the above into account, there are usually better things to do in a game than "wait" around at the end of your game loop.
"Tis better to sleep then to wait"
Yeah, I originally had the Wait() function, then I realized how system unfriendly that was so I added the HasXTimePassed() function. I just kept Wait() around just in case I''d ever need it.

- God is my favorite fictional character
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA

This topic is closed to new replies.

Advertisement