Skip to content

Instantly share code, notes, and snippets.

@Groogy
Created March 14, 2013 21:25
Show Gist options
  • Save Groogy/5165427 to your computer and use it in GitHub Desktop.
Save Groogy/5165427 to your computer and use it in GitHub Desktop.
void Animation::update(sf::Time dt)
{
sf::Time timePerFrame = mLength / mNumFrames;
mElapsedTime += dt;
sf::Vector2i textureBounds = mSprite.getTexture().getSize();
sf::FloatRect textureRect = mSprite.getTextureRect();
if (mCurrentFrame == 0)
textureRect = sf::FloatRect(0, 0, mFrameSize.x, mFrameSize.y);
while (mElapsedTime >= timePerFrame)
{
textureRect.left += textureRect.width;
if (textureRect.left + textureRect.width > textureBounds.x)
{
textureRect.left = 0;
textureRect.top += textureRect.height;
}
mCurrentFrame = mCurrentFrame + 1 % mNumFrames;
mElapsedtime -= timePerFrame;
if (mCurrentFrame == 0)
textureRect = sf::FloatRect(0, 0, mFrameSize.x, mFrameSize.y);
}
mSprite.setTextureRect(textureRect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment