Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

deadlydog

What is wrong with this class?

This topic is 5294 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 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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"Tis better to sleep then to wait"

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!