Hi there! I'm trying to make a class that handles animation (and before you ask, I checked out the wiki, what was there didn't fit my needs), and am having some problems
Basically, my class takes a pointer to a sprite and changes that sprites image based on what frame it should be at (the frames are pointers to images). Here's the code, which'll hopefully speak for itself
Animation.hpp
#ifndef Animation_hpp
#define Animation_hpp
#include <string>
#include <vector>
#include <SFML/Graphics.hpp>
class Animation {
public:
Animation();
void SetSprite(sf::Sprite*);
void AddFrame(const std::string& path);
void SetSpeed(unsigned int speed);
void AdvanceFrame();
//private:
std::vector< sf::Image* > mFrames;
sf::Sprite* mSpr;
unsigned int mSpeed;
unsigned int mCurrentFrame;
unsigned int mCount;
};
#endif
Animation.cpp
#include "Animation.hpp"
#include "Main.hpp"
//imagemanager.hpp is just my images manager, surpisingly
#include "ImageManager.hpp"
#include <iostream>
Animation::Animation() {
mCount = 0;
mSpeed = 10;
mCurrentFrame = 0;
}
void Animation::SetSprite(sf::Sprite* spr) {
mSpr = spr;
}
void Animation::AddFrame(const std::string& path) {
//ImageManager.LoadImage() just returns a pointer to an image.
sf::Image* timg = ImageManager.LoadImage(path);
mFrames.push_back(timg);
}
void Animation::SetSpeed(unsigned int speed) {
mSpeed = speed;
}
void Animation::AdvanceFrame() {
if (mFrames.size() > 0) {
mCount += mSpeed;
if (mCount >= 100) {
mCount = 0;
mCurrentFrame++;
if (mCurrentFrame == mFrames.size()-1) {
mCurrentFrame = 0;
}
//std::cout << mFrames.size()-1 << " : " << mCurrentFrame << std::endl;
mSpr->SetImage( *( mFrames[mCurrentFrame] ) );
}
}
}
So now you hopefully know what I'm getting at.
Here's the code I'm using to use this class:
mAnim.SetSprite(&mSpr);
mAnim.AddFrame("images/trail-b_u.png");
mAnim.AddFrame("images/trail-b_r.png");
mAnim.AddFrame("images/trail-b_d.png");
mAnim.AddFrame("images/trail-b_l.png");
and in the main loop I call mAnim.AdvanceFrame();
I did a bit of testing, and replaced
void Animation::AddFrame(const std::string& path) {
//ImageManager.LoadImage() just returns a pointer to an image.
sf::Image* timg = ImageManager.LoadImage(path);
mFrames.push_back(timg);
}
with
void Animation::AddFrame(const std::string& path) {
sf::Image* timg = ImageManager.LoadImage(path);
mFrames.push_back(timg);
sf::Image* img;
std::cout << "a" << std::endl;
img = (mFrames[0]);
std::cout << "b" << std::endl;
mSpr->SetImage(*img);
std::cout << "c" << std::endl;
}
and my output is
a
b
c
a
b
c
a
b
Segmentation fault
Isn't this fun? So I seem to be getting the error when I do "mSpr->SetImage(*img);". Quite frankly, I have no idea why. It might be my imagemanager's fault, but I doubt it.
Thanks for reading that through and even if you've just got an idea of what's wrong, I'd love to hear it
I just thought I'd put my twenty-cents in and say I reckon SFML would do nicely with an animation class of it's own
If I forgot anything don't hesitate to remind me
Thanks
Stewart
[EDIT] Sorry if this is in the wrong section, or has nothing to do with SFML. I just didn't know where to put it