Welcome, Guest. Please login or register. Did you miss your activation email?

Poll

Is SFML awesome?

Yes
1 (9.1%)
Yes
1 (9.1%)
No, it's freaking awesome
9 (81.8%)

Total Members Voted: 11

Voting closed: January 08, 2011, 01:22:21 am

Author Topic: [solved] sf::Image* problem ;)  (Read 2282 times)

0 Members and 1 Guest are viewing this topic.

Stew_822

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • http://circean-studios.co.cc
    • Email
[solved] sf::Image* problem ;)
« on: January 01, 2011, 01:22:21 am »
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
Code: [Select]
#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
Code: [Select]
#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:
Code: [Select]
     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
Code: [Select]
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
Code: [Select]

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
Code: [Select]
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 :(
It's better to keep your mouth shut and give the impression that you're stupid than to open it and remove all doubt.

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
[solved] sf::Image* problem ;)
« Reply #1 on: January 01, 2011, 11:51:37 pm »
Usually segmentation fault happens, when you try to reach unallocated data. In this case, I think that mSpr is NULL at that moment (which is likely), or you feed SetImage with invalid data (which is not unlikely :D).

Stew_822

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • http://circean-studios.co.cc
    • Email
yay :)
« Reply #2 on: January 02, 2011, 03:35:48 am »
thanks for the reply :)
You were right, I fed SetImage invalid data. I could have sworn my ImageManager was working, but there you go. Epic failure by myself :)

Thanks for spending your time to help me out :)

Take care :)

[edit] Case closed!
It's better to keep your mouth shut and give the impression that you're stupid than to open it and remove all doubt.

 

anything