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

Author Topic: [Solved] Update function for re-drawing sprite  (Read 6373 times)

0 Members and 1 Guest are viewing this topic.

ConnorJW

  • Newbie
  • *
  • Posts: 7
    • View Profile
[Solved] Update function for re-drawing sprite
« 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;
    }
« Last Edit: December 02, 2015, 07:16:10 pm by ConnorJW »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Update function for re-drawing sprite
« Reply #1 on: December 02, 2015, 07:50:10 am »
What's your draw function?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ConnorJW

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Update function for re-drawing sprite
« Reply #2 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
}
« Last Edit: December 02, 2015, 06:09:41 pm by ConnorJW »

blindley

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Update function for re-drawing sprite
« Reply #3 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.

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Update function for re-drawing sprite
« Reply #4 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
« Last Edit: December 02, 2015, 06:36:51 pm by Brax »

ConnorJW

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Update function for re-drawing sprite
« Reply #5 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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Update function for re-drawing sprite
« Reply #6 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..

ConnorJW

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Update function for re-drawing sprite
« Reply #7 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.

 

anything