Welcome! Log In Create A New Profile

Advanced

Measuring time

Posted by diego_pmc 
Measuring time
May 17, 2010 02:20PM
I have some problems with measuring time. Here's my function:
void IntroState::update(GameManager* game, UserController* userController)
{
	// Function updates the current screen that has to be diaplyed by either
	// keeping track of time and changing the screen when enough time has 
	// passed, or by listening to user input.

	clock_t currentTime = (clock_t)((double)(clock() / CLOCKS_PER_SEC) * 1000); 

	// Variable currentScreenBeginTime_ is 0 only if this is the first time 
	// IntroState singleton class has been updated.
	if (currentScreenBeginTime_ == 0)
		currentScreenBeginTime_ = currentTime;

	userController->scanButtons();

	bool shouldChangeScreen = 
		userController->isButtonPressed(UserController::SELECT) ||
		(currentTime >= currentScreenBeginTime_ + durationScreen_);
	bool shouldEndApplication =
		userController->isButtonPressed(UserController::EXIT);

	if (shouldChangeScreen) {
		currentScreenBeginTime_ = currentTime;
		if (++currentScreen_ == screens_.size())
			game->changeState(EndState::Instance());
	} else if (shouldEndApplication) {
		game->changeState(EndState::Instance());
	}
}

The class members are declared (and initialized before this function is called) as such:
unsigned int currentScreen_ = 0;
clock_t currentScreenBeginTime_ = 0;
clock_t durationScreen_ = 2000;

There are several problems with this code. I have two screens (PNGs) that I should see, stored in a vector. The first problem is that I cannot see the first, as if the if (++currentScreen_ == screens_.size()) line is read immediately. I inserted a line that adds a log entry, to see if the value of currentScreen_ is ever 0, and indeed at first the value is 0. Another problem is that the second screen stays up for a lot less than 2000 milliseconds.

What could the problem be?

Is there a better way to measure time?
Re: Measuring time
May 17, 2010 02:29PM
I'm using "gettime() / TB_TIMER_CLOCK" as miliseconds timer in my code.
Re: Measuring time
May 17, 2010 03:23PM
What header is that from?
Re: Measuring time
May 17, 2010 04:10PM
One of these:
gccore.h
ogc/lwp_watchdog.h

I use it in this file: [daid2.mine.nu]
Re: Measuring time
May 17, 2010 04:35PM
Thanks, it works (finally!). BTW, since I posted the code in this thread: do you think that function I posted is easy enough to read an understand?



Edited 1 time(s). Last edit at 05/17/2010 04:36PM by diego_pmc.
Re: Measuring time
May 17, 2010 04:55PM
I only think the underscores at the end of the class members look very silly.
And as I ready stupid/bad/horrible code almost every day I cannot really judge if your code is easy enough to understand, as I'm so used to understanding all kinds of code.
Re: Measuring time
May 17, 2010 05:25PM
The underscore is a common convention used to easily identify class members. Athor conventions are to put an 'm' (as in "member") at the beginning of the name (e.g.: mCurrentScreenBeginTime).

What I meant to ask is, do you have any difficulties understanding what the code does/how it does it?

EDIT: Odd indentation like this...
	bool shouldChangeScreen = 
		userController->isButtonPressed(UserController::SELECT) ||
		(currentTime >= currentScreenBeginTime_ + durationScreen_);
... is to keep code lines under 80 columns.

EDIT2: BTW, here's the new function:
void IntroState::update(GameManager* game, UserController* userController)
{
	// Function updates the current screen that has to be diaplyed by either
	// keeping track of time and changing the screen when enough time has 
	// passed, or by listening to user input.

	long currentTime = (long)(gettime() / TB_TIMER_CLOCK); 

	// Variable currentScreenBeginTime_ is 0 only if this is the first time 
	// IntroState singleton class has been updated after calling onOpen().
	if (currentScreenBeginTime_ == 0)
		currentScreenBeginTime_ = currentTime;

	userController->scanButtons();

	bool shouldChangeScreen = 
		userController->isButtonPressed(UserController::SELECT) ||
		(currentTime >= currentScreenBeginTime_ + durationScreen_);

	if (shouldChangeScreen) {
		currentScreenBeginTime_ = currentTime;
		if (++currentScreen_ == screens_.size())
			game->changeState(MenuState::Instance());
	}
}



Edited 5 time(s). Last edit at 05/17/2010 05:47PM by diego_pmc.
Sorry, only registered users may post in this forum.

Click here to login