Hello friends,
My code's process is (in a nutshell) like so:
- a Player class declares and initializes an Animator class
- the Animator class holds said Player's sprite sheet, and is, among other things, responsible for passing back the appropriate sprite to draw at any given time
- the game requests said sprite whenever it needs to draw.
My sprite is showing up as a blank white box. My current theory is that the sprite's image is going out of scope at some point, but I can't figure out where.
Here are (I hope all of) the relevant bits (a bunch of fluff taken out unless requested):
mj_animator.hclass mj_animator
{
public:
mj_animator(const std::string &Filename ... );
mj_animator();
sf::Sprite get_sprite();
private:
sf::Sprite sprite_sheet;
sf::Image img;
}
mj_animator.cppmj_animator::mj_animator(const std::string &Filename, ...) {
if(! img.LoadFromFile(Filename)) {
// error handling stuff
}
sprite_sheet.SetImage(img);
}
sf::Sprite mj_animator::get_sprite() {
// some calculations to determine x1, y1, x2, y2 of an IntRect
sprite_sheet.SetSubRect(sf::IntRect(x1, y1, x2, y2));
return sprite_sheet;
}
mj_player.cppmj_player::mj_player(sf::Image sheet) {
//animators is an array of mj_animators objects (in mj_player.h)
animators[0] = mj_animator("file.jpg", ...);
animators[1] = mj_animator("file.jpg", ...);
current_animator = 1; //an int to track which animator currently being used
}
// ... some functions to determine which animator we need to have going at any given time, etc
mj_player::Draw(sf::RenderWindow &App) {
// some calculations to determine current position
sf::Sprite draw_sprite = animators[current_animator].get_sprite();
draw_sprite.SetPosition(position.x, position.y);
App.Draw(draw_sprite);
}
If there's anything you think you need, I'll put it -- I didn't include a lot of what I think is unrelated because it's already a fairly bloated post.
My best guess, as I said, is that either somewhere in the process, the image goes out of scope, or that I can't pass sprite_sheet back as I think I can.
Any guesses? Thanks.
edit:
I should note that through some debugging (read: a bunch of couts), I did determine that the math that I didn't include here is actually all correct -- that the SubRect is being correctly calculated, so it's not trying to draw some weird part of the image that doesn't exist or anything like that.