okay i need some thor insight:
Im trying to implement the animatorclass for creating animations in my project, but somehow the animate() function crashes my project:
This contains the animator:
class AnimationComponent
:public ComponentBase<AnimationComponent>
{
public:
sf::Texture tex;
thor::FrameAnimation walk;
public:
AnimationComponent(const sf::Texture& t,const sf::Vector2f& p,const thor::Animator<sf::Sprite, std::string>& a)
:tex(t),animator(a)
{ spr.setTexture(tex); spr.setPosition(p); }
thor::Animator<sf::Sprite, std::string> animator;
sf::Sprite spr;
};
Here i create a dummy animation:
void addFrames(thor::FrameAnimation& animation, int x, int yFirst, int yLast, float duration = 1.f)
{
const int step = (yFirst < yLast) ? +1 : -1;
yLast += step; // so yLast is excluded in the range
for (int y = yFirst; y != yLast; y += step)
animation.addFrame(duration, sf::IntRect(36*x, 39*y, 36, 39));
}
void Factory::createDummyAnim()
{
sf::Image image;
sf::Texture texture;
if(!image.loadFromFile("animation.png")){
std::cout << "No such file" << std::endl;
}
image.createMaskFromColor(sf::Color::White);
if(!texture.loadFromFile("animation.png"))
std::cout << "Texture gone wrong\n";
// Define walk animation
thor::FrameAnimation walk;
addFrames(walk, 0, 0, 7); // Frames 0..7 Right leg moves forward
addFrames(walk, 0, 6, 0); // Frames 6..0 Right leg moves backward
// Register animations with their corresponding durations
thor::Animator<sf::Sprite, std::string> animator;
animator.addAnimation("walk", walk, sf::seconds(1.f));
animator.playAnimation("walk",true);
Entity& e = _entitymanager.createEntity();
_entitymanager.addComponent(new AnimationComponent(texture,sf::Vector2f(100.f,100),animator),e);
}
Here the animator::animate() method crashes my code:
void AnimationSystem::update(const sf::Time& dt)
{
entitylist& entities = _entitymanager.getEntitiesWithComponent<AnimationComponent>();
for(auto x: entities){
constptr<AnimationComponent> ac = _entitymanager.getComponent<AnimationComponent>(x);
ac->animator.update(dt);
ac->animator.animate(ac->spr);//This line crashes my program, ac->spr is valid
}
}
ac is valid and ac->spr 's texture is valid as well.
When i create the whole animator in the last code part my program does not crash. the whol entitysystem works, that is not the problem.