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

Author Topic: sf::Sprite, cannot get to draw.  (Read 1633 times)

0 Members and 1 Guest are viewing this topic.

deathvango

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
sf::Sprite, cannot get to draw.
« on: March 12, 2015, 10:20:12 pm »
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.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: sf::Sprite, cannot get to draw.
« Reply #1 on: March 12, 2015, 11:33:11 pm »
For starters move the clear and display methods out into your main game loop where you are drawing your list of sprites.  Can't see anything else that might be causing it though.
I have many ideas but need the help of others to find way to make use of them.

deathvango

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: sf::Sprite, cannot get to draw.
« Reply #2 on: March 14, 2015, 04:31:03 pm »
For starters move the clear and display methods out into your main game loop where you are drawing your list of sprites.  Can't see anything else that might be causing it though.

Yea, I placed it that way mostly for the purposes of the example so that it's easy to get a general idea of the flow of the code.

But I've nailed down the issue after many long hours with the debugger. It turns out that my code is not the problem, but my Intel graphics card and its corresponding drivers, which seems to be not very friendly with OpenGl. It's nothing I can currently change :(.

 

anything