SFML community forums

Help => Graphics => Topic started by: ConnorJW on December 02, 2015, 03:43:28 am

Title: [Solved] Update function for re-drawing sprite
Post by: ConnorJW on December 02, 2015, 03:43:28 am
The current update function should be moving the sprite to the left 10 pixels every time it's called, but the sprite (cup1Sprite) is static and not moving. I tried using sprite.rotate(); and the sprite rotated every frame (which was expected), so something is wrong with the way I've written the update function for updating the sprite position. Can anyone suggest what's going wrong?

Code:
DrawCups.h

    class DrawCups
    {
    public:
        DrawCups(sf::RenderWindow& window);
        ~DrawCups();
   
        void update();
   
        void drawCup1();
   
    private:
        sf::RenderWindow& _window;
        //Cup1
        sf::Texture _cup1;        // the texture which will contain our pixel data
        sf::Sprite _cup1Sprite;         // the sprite which will actually draw it
    };

Update function in DrawCups.cpp

 
   void DrawCups::update()
    {
        sf::Vector2f pos = this->_cup1Sprite.getPosition();
        pos.x+=10;
        this->_cup1Sprite.setPosition(pos);
    }

main.cpp

   
 int main()
    {
        sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
        window.setFramerateLimit(60);
        DrawCups drawToWindow(window);
   
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
   
                drawToWindow.update();
   
                window.clear(sf::Color::White);
                drawToWindow.drawBench();
                drawToWindow.drawCup1();
                window.display();
        }
   
        return 0;
    }
Title: Update function for re-drawing sprite
Post by: eXpl0it3r on December 02, 2015, 07:50:10 am
What's your draw function?
Title: Re: Update function for re-drawing sprite
Post by: ConnorJW on December 02, 2015, 05:59:34 pm
void DrawCups::drawCup1()
{
        // load our image
        loadImage("./images/InsertionSort/red_cup_1.png", _cup1, _cup1Sprite);
        _cup1Sprite.setPosition(sf::Vector2f(150, 230));
        _cup1.setSmooth(true);
        _window.draw(_cup1Sprite);
}

is the draw function

and loading the sprite:

void DrawCups::loadImage(const char* pathname, sf::Texture& texture, sf::Sprite& sprite)
{
        texture.loadFromFile(pathname);     // load it from the file
        sprite.setTexture(texture);         // put that texture in our sprite
        sprite.setTextureRect(sf::IntRect(0, 0, 139, 186));    // the rectangle of the texture to use for this sprite
}
Title: Re: Update function for re-drawing sprite
Post by: blindley on December 02, 2015, 06:33:01 pm
In your drawCup1 function, you're resetting the position of the sprite to (150, 230), undoing the work you did in the update function. Don't do that. As an aside, loading the texture every time you draw the sprite is a strange, and extremely inefficient thing to do.
Title: Re: Update function for re-drawing sprite
Post by: Brax on December 02, 2015, 06:33:23 pm
Your "drawCup1()" function has "_cup1Sprite.setPosition()" which will set the absolute position of the sprite on the window using the coordinates you gave it.

Since your function is called each time you call "drawToWindow.drawCup1()", which is also called after the "update" function, it will always negate the positions you changed inside "update" function and will have the same position on the screen.

Simply set the sprite starting position elsewhere.

Also, you don't need to move sprite like this:
        sf::Vector2f pos = this->_cup1Sprite.getPosition();
        pos.x+=10;
        this->_cup1Sprite.setPosition(pos);
 
Move it like this:
    _cup1Sprite.move(10,0);
 

By the way, loading the texture each frame is bad idea. Load it only once and keep it alive as long you need it. ;)


edit: Argh.. 20 seconds late   :P
Title: Re: Update function for re-drawing sprite
Post by: ConnorJW on December 02, 2015, 07:08:08 pm
Thanks for the replies all :)

Where would you suggest for me to initially set the location for the sprite on start-up? Also thanks for the hint for loading the sprite every frame - I'm literally brand new to SFML/graphics libraries.
Title: Re: Update function for re-drawing sprite
Post by: Jesper Juhl on December 02, 2015, 07:12:53 pm
Where would you suggest for me to initially set the location for the sprite on start-up?
The constructor of your class would seem to be the obvious place...
That's what constructors are for after all - setting initial values..
Title: Re: Update function for re-drawing sprite
Post by: ConnorJW on December 02, 2015, 07:15:58 pm
Where would you suggest for me to initially set the location for the sprite on start-up?
The constructor of your class would seem to be the obvious place...
That's what constructors are for after all - setting initial values..

Yeah, sorry haha. Feel pretty stupid at the fact it took me so long to fix, what now seems to be a simple mistake. Thanks all! It works perfectly now - I'll update the title to solved.