Hey guys, I'm new to using SFML, and I was trying to get a sprite to animate through some frames. I've done this before in XNA and never had much of a problem with it. However when I'm doing it now, it plays one extra frame for some reason
I adapted the c# animated sprite class that can be found on the wiki here :
https://github.com/SFML/SFML/wiki/SpriteAnimated into c++ and it seems to me like I'm doing everything correctly, and I just can't see where the problem is.
Here's my AnimatedSprite class:
#include "animatedsprite.h"
#include <iostream>
using namespace std;
AnimatedSprite::AnimatedSprite(Texture text, float frameWidth, float frameHeight, int fps,int firstFrame, int lastFrame,bool isAnimated = false, bool isLooped = true)
{
_text = text;
_frameWidth = frameWidth;
_frameHeight = frameHeight;
_fps = fps;
_interval = _fps/250;
_firstFrame = firstFrame;
_lastFrame = lastFrame;
_frameCount = lastFrame - firstFrame;
_currentFrame = _firstFrame;
_isAnimated = isAnimated;
_isLooped = isLooped;
_clock = 0;
setTexture(_text);
setTextureRect(getFramePositon(_currentFrame));
}
IntRect AnimatedSprite::getFramePositon(int frame)
{
int count = (getTexture()->getSize()).x/ _frameWidth;
int newX = frame% count;
int newY = frame/count;
IntRect pos(_frameWidth*newX,_frameHeight*newY,_frameWidth,_frameHeight);
return pos;
}
void AnimatedSprite::update(float deltaTime)
{
_clock += deltaTime;
if(_clock >= _interval && _isAnimated)
{
IntRect temp = getFramePositon(_currentFrame);
setTextureRect(temp);
if (_currentFrame < _lastFrame)
{
_currentFrame++;
}
else
{
_currentFrame = _firstFrame;
}
_clock = 0;
}
if(!_isLooped && _currentFrame == _lastFrame)
{
_isAnimated = false;
}
}
float AnimatedSprite::getFPS()
{
return _fps;
}
void AnimatedSprite::setFPS(float value)
{
_fps = value;
}
and here's my main:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "AnimatedSprite.h"
using namespace sf;
using namespace std;
int main()
{
RenderWindow window(VideoMode(1280, 720), "Mazian Chronicles");
window.setFramerateLimit(60);
Texture text;
Clock deltaClock;
Time dt;
if(!text.loadFromFile("sprite.png"))
{
return EXIT_FAILURE;
}
AnimatedSprite crono(text,33,35,60,0,12,true,true);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
dt =deltaClock.restart();
crono.update(dt.asSeconds());
window.clear();
window.draw(crono);
window.display();
}
return 0;
}
I would be very appreciative if someone could help me with my (most likely stupid) mistake. Thanks!