Question re UpdateAnimation and currentFrameExposure

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
ryanbowles42
Posts: 3
Joined: January 25th, 2013, 7:25 pm

Question re UpdateAnimation and currentFrameExposure

Post by ryanbowles42 » January 25th, 2013, 7:34 pm

This is my first post in the forums. I'm a total C++ noob, but I've watched all the beginner series videos, and I wanted to first thank Chili for providing such awesome content.

I've been toying around with sprite animation and I had a question regarding the method we used in Lessons 20 and 21 regarding the variable currentFrameExposure in our UpdateAnimation function. My understanding is that we use currentFrameExposure to force the program to go through a loop a certain number of times before updating to the next animation frame. The amount of time is based on the speed at which the computer goes through the loop, which will vary from machine to machine. I am looking for a way to update animation smoothly based on a time between animation frames that will remain constant no matter the computer. I've toyed around with Sleep(); but have had limited success. Is there something specific I should be doing here? Am I understanding currentFrameExposure correctly?

Thanks for any help,
Ryan

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Question re UpdateAnimation and currentFrameExposure

Post by Musi » January 27th, 2013, 10:29 am

Yeah that's exactly what happens. What i did was use Chili's Timer class to get time passed per frame in seconds.

Edit: I realized my addition to the timer class was unnecessary.
Just add this function to the Timer class.

Code: Select all

float Timer::GetTimeSec() const
{
	if( !watchStopped )
	{
		QueryPerformanceCounter( (LARGE_INTEGER*)&currentCount );
		return ((float)(currentCount - startCount) * invFreqMilli) / 1000;
	}
	else
	{
		return ((float)(currentCount - startCount) * invFreqMilli) / 1000;
	}
}
Now you can get how much time has passed per frame in seconds. Create a Timer object and also a time variable in the Game class.

Code: Select all

     Timer timer;
     float time;
Add this to the bottom of Game's Go() function to get the time passed in seconds.

Code: Select all

	timer.StopWatch();
	time = timer.GetTimeSec();
	timer.StartWatch();
All you have to do now is, whenever you have something that moves a certain distance just multiply it by "time". for example:

Code: Select all

if( kbd.RightIsPressed() )
{
     x += 10 * time;
}
This will move to the right at 10 pixels per second, no matter what computer its on.

Edit2: I forgot you were talking about changing frames, not movement :D.
Last edited by Musi on January 27th, 2013, 9:07 pm, edited 3 times in total.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

User avatar
ryanbowles42
Posts: 3
Joined: January 25th, 2013, 7:25 pm

Re: Question re UpdateAnimation and currentFrameExposure

Post by ryanbowles42 » January 27th, 2013, 5:47 pm

Thanks for the reply, I'll try implementing the timer class in my code.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: Question re UpdateAnimation and currentFrameExposure

Post by Clodi » January 27th, 2013, 7:43 pm

wow,
good to know..
..same problem here. My games look very different from when they are being used on other machines. my laptop is so slow.. 40 per sec :D

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: Question re UpdateAnimation and currentFrameExposure

Post by indus » January 28th, 2013, 9:19 am

Here is a great article on game loop. Pretty good explained and outlining pluses vs. minuses of each implementation.

User avatar
ryanbowles42
Posts: 3
Joined: January 25th, 2013, 7:25 pm

Re: Question re UpdateAnimation and currentFrameExposure

Post by ryanbowles42 » January 30th, 2013, 7:22 pm

Indus, I was able to get my animation working as intended using information in that article, thanks for the link.

Post Reply