Hello,
I'm having a lot of trouble with drawing the image present within the sf::Sprite class. Here is the simplified fragment of code which is giving me issues:
class SFMLDrawer {
public:
void draw( sf::Sprite& passed_sprite );
private:
sf::RenderWindow mScreen;
};
//Program runs successfully but does not draw anything to the screen.
void SFMLDrawer::draw( sf::Sprite& passed_sprite )
{
this->mScreen.clear();
this->mScreen.draw( passed_sprite );
this->mScreen.display();
}
In the above example, The program runs and exits successfully but the screen is blank.The texture present within the variable passed_sprite is dynamically allocated( using new, not shown in above code. ), and thus, I imagine, shouldn't have any issues related to scope. I could be wrong, though.
However, when I change the draw() method to the following:
//This function exits the code in failure.
//Crashes when using a debugger with no information.
void SFMLDrawer::draw( sf::Sprite& passed_sprite )
{
this->mScreen.clear();
sf::Sprite crash_sprite;
//getTexture() returns a pointer, which is why the '*' is there.
crash_sprite.setTexture( *(passed_sprite.getTexture()) );
crash_sprite.setTextureRect( passed_sprite.getTextureRect() );
this->mScreen.draw( crash_sprite );
this->mScreen.display();
}
The above code crashes the program immediately on startup, resulting in exit failure. When I tried to run gdb, the window drew my image in the appropriate location but crashed the second I tried closing the window. I don't understand why it would suddenly cause such undefined behavior, though, considering I am effectively using the same values for crashed_sprite as I was for passed_sprite.
If anyone has any suggestions or advice, I would appreciate it. If I left something unclear, then let me know.