PDA

View Full Version : clan::Sprite as a public member of a class



Pap
10-01-2013, 03:44 PM
I have some trouble using sprites... specifically, I need to define a clan::Sprite object as a public member of another class, namely Fading:Sprite. This is done by

namespace Fading {
class Sprite {
public:
clan::Sprite spr;
..........................
};
}
Now, in one of the public Fading:Sprite methods, I just want to draw the sprite (which is previously set properly via Sprite::resource). What I do is

this->spr.draw(canvas,spriteX,spriteY);
where canvas, spriteX, and spriteY are all set properly (I checked that). Compiler does not complain at all, as expected. However, the above command causes a segfault at runtime (as it does any attempt to use sprite methods). I am a bit confused, as this worked in another game API I used in the past (which defines sprites pretty much in a similar way as ClanLib).

Apparently, I am missing something fundamental here...

sphair
10-01-2013, 04:06 PM
exposing the sprite like that should not be a problem, so the problem does probably lie somewhere else. Maybe how you initialize your sprite?

If you get a segfault, what about running it through a debugger and see where it crashes?

Pap
10-01-2013, 04:53 PM
exposing the sprite like that should not be a problem, so the problem does probably lie somewhere else. Maybe how you initialize your sprite?

If you get a segfault, what about running it through a debugger and see where it crashes?

The Fading::Sprite class is used in another class to define the sprite as

Fading::Sprite helpWin;
then the sprite itself is initialized as follows:

const clan::ResourceManager &resources=clan::XMLResourceManager::create(clan::X MLResourceDocument("data/resources.xml"));
helpWin.sprite.resource(canvas,"help window",resources);
helpWin.set_position(canvas.get_width()/2.f,canvas.get_height()/2.f);
where data/resources.xml contains a proper sprite declaration:

<sprite name="help window">
<image file="help_window.png"/>
</sprite>
Of course, I used a debugger to figure out what's going on before posting here. It turns out segfault happens first time I am trying to draw the above sprite (and after all the above initializations of the sprite are executed), as described in my first post. I forgot, however, to add the debugger information when segfault happens:

Program received signal SIGSEGV, Segmentation fault.
clan::Sprite::draw (this=<optimized out>, canvas=..., x=200, y=150) at 2D/sprite.cpp:644

rombust
10-01-2013, 09:30 PM
clan::Sprite.resource is a static function

therefore you require: helpWin.sprite = clan::Sprite::resource(canvas,"help window",resources);

imo, that's a failing in the C++ language, in allowing sprite.resource(...)

Pap
10-01-2013, 09:41 PM
clan::Sprite.resource is a static function

therefore you require: helpWin.sprite = clan::Sprite::resource(canvas,"help window",resources);

imo, that's a failing in the C++ language, in allowing sprite.resource(...)
Indeed, that was the problem! Thank you very much rombust. :hat: