Sign in to follow this  
Lith

Weired SFML Clock Problems

Recommended Posts

Hello.

I am making an animation class to use in my game that, when asked, will give the frame of animation that the object needs to be on. You can do something like this:

[code]
Animation TestAnimation;
GameObject TestGameObject;

TestAnimation.Create(2,4); // 2 seconds for each frame and 4 frames in total
TestAnimation.Start();

// In the main loop somewhere...

TestGameObject.Draw(TestAnimation.GetFrame());

[/code]

The problem i have resides in the GetFrame function:

[code]

if(running)
{
float timeElapsed = mainClock.GetElapsedTime();
float framesPassed = timeElapsed / timeForEachFrame;
int curFrame = currentFrame + (int)framesPassed;

if(curFrame > numberOfFrames)
{
currentFrame = curFrame % numberOfFrames;
}
else
{
currentFrame = curFrame;
}

return currentFrame;
}
else
{
return 0;
}
[/code]

The function was returning 0 for about 2 to 3 seconds and then would seem to spit out random numbers between and including 0 to the total frame number. I did some investigation and found out that the number i got from mainClock.GetElapsedTime() was suddenly increasing by 3 to 10 when the function stopped returning 0.

Is this a bug in sfml or am i doing something stupid?

Share this post


Link to post
Share on other sites
[font="arial, verdana, tahoma, sans-serif"][size="2"][quote name='Wooh' timestamp='1318000372' post='4870142']
Are you calling mainClock.Reset() anywhere?
[/quote]

I was only calling it in the Start() function:

[code]

void Animation::Start()
{
running = true;
mainClock.Reset();
}
[/code]

I added this code to the function and made it work!

[code]
int oldCurrentFrame = currentFrame;

// functions goes here...


if(oldCurrentFrame != currentFrame)
{
mainClock.Reset();
}

return currentFrame;
[/code]
[/size][/font]
[quote name='Lith' timestamp='1317998939' post='4870139']
Is this a bug in sfml or am i doing something stupid?
[/quote]

I was doing something stupid...
Anyway, thanks for your help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this