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

Author Topic: Pure virtual function call error when I do window.Draw (sprite)  (Read 1362 times)

0 Members and 2 Guests are viewing this topic.

TreeTree

  • Newbie
  • *
  • Posts: 3
    • View Profile
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

using namespace std;
using namespace sf;

Sprite CreateBall (Image img, Vector2f coords) {
        Sprite ball (img);
        ball.SetPosition (coords);
        return ball;
}

int main () {
        RenderWindow window (VideoMode (500, 500, 32), "Squash", Style::Close);
        window.SetFramerateLimit (30);

        Image imgPaddle;
        imgPaddle.LoadFromFile ("paddle.png");
        Sprite paddle (imgPaddle);
        paddle.SetCenter (paddle.GetSize ().x / 2, 0);

        Image imgBall;
        imgBall.LoadFromFile ("ball.png");
       
        vector <Sprite*> sprites;
        sprites.push_back (&paddle);

        int frame = 0;
        bool running = true;

        while (running) {
                Event theEvent;

                while (window.GetEvent (theEvent)) {
                       
                }
               
                if (frame % 30 == 0) {
                        Sprite ball = CreateBall (imgBall, Vector2f (window.GetWidth () / 2, window.GetHeight () / 2));
                        sprites.push_back (&ball);
                }

                frame++;

                window.Clear ();
               
                for (int i = 0; i < sprites.size (); i++) {
                        window.Draw (*sprites [i]);
                }

                window.Display ();
        }

        return 0;
}
 
The error doesn't occur when I comment out window.Draw. Also if I stop the loop from adding more balls to the vector and instead I add one before the loop starts, it works.

All I know about this error is that I'm trying to call a virtual function from an abstract class or something like that and I don't really know how that is my case.
« Last Edit: March 30, 2012, 07:56:06 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Pure virtual function call error when I do window.Draw (sprite)
« Reply #1 on: March 30, 2012, 07:57:31 am »
Your vector contains the address of elements that don't exist anymore when you draw them. Your sprites are created locally in the if { } block, they are destroyed as soon as the block ends.
Laurent Gomila - SFML developer

TreeTree

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Pure virtual function call error when I do window.Draw (sprite)
« Reply #2 on: March 30, 2012, 03:14:06 pm »
I see, so I'll need to save a copy of the ball somewhere during the if {}. I changed the vector to a non-pointer type and used references to edit individual sprites like the paddle. This is all slightly strange to me, I came from a ActionScript background and doing the above has no issues.