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

Author Topic: sf::Sprite problem with std::vector  (Read 3113 times)

0 Members and 1 Guest are viewing this topic.

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
sf::Sprite problem with std::vector
« on: August 12, 2010, 03:19:22 pm »
Hello I've been having a problem with using a vector of sf::Sprites/sf::Images:

Code: [Select]

std::vector<sf::Image> IMAGE;
std::vector<sf::Sprite> SPRITE;

IMAGE.push_back(sf::Image());
SPRITE.push_back(sf::Sprite());
IMAGE.at(0).LoadFromFile("Remilia0.png");
SPRITE.at(0).SetImage(IMAGE.at(0));

IMAGE.push_back(sf::Image());
SPRITE.push_back(sf::Sprite());
IMAGE.at(1).LoadFromFile("Remilia1.png");
SPRITE.at(1).SetImage(IMAGE.at(1));

IMAGE.push_back(sf::Image());
SPRITE.push_back(sf::Sprite());
IMAGE.at(2).LoadFromFile("Remilia2.png");
SPRITE.at(2).SetImage(IMAGE.at(2));

IMAGE.push_back(sf::Image());
SPRITE.push_back(sf::Sprite());
IMAGE.at(3).LoadFromFile("Remilia3.png");
SPRITE.at(3).SetImage(IMAGE.at(3));

IMAGE.push_back(sf::Image());
SPRITE.push_back(sf::Sprite());
IMAGE.at(4).LoadFromFile("Remilia4.png");
SPRITE.at(4).SetImage(IMAGE.at(4));

SPRITE.at(0).SetPosition(20, 20);
SPRITE.at(1).SetPosition(60, 20);
SPRITE.at(2).SetPosition(100, 20);
SPRITE.at(3).SetPosition(140, 20);
SPRITE.at(4).SetPosition(180, 20);

while(Window.IsOpened())
{
Window.GetEvent(Event);
if(Event.Type == sf::Event::Closed)Window.Close();
Window.Clear();
Window.Draw(SPRITE.at(0));
Window.Draw(SPRITE.at(1));
Window.Draw(SPRITE.at(2));
Window.Draw(SPRITE.at(3));
Window.Draw(SPRITE.at(4));
Window.Display();
}


It seems as though when I push a new Sprite (or Image) into their vectors all of the other objects get erased because every sprite shows up as a white square except for the last sprite to be pushed into the vector (SPRITE.at(4) draws fine).

Can someone explain why this is happening?

BTW: I'm using SFML 1.6 in VS08

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Sprite problem with std::vector
« Reply #1 on: August 12, 2010, 03:36:36 pm »
Hi

Read the last paragraph of the sprite tutorial ("Images and sprites management"), everything is explained.
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
sf::Sprite problem with std::vector
« Reply #2 on: August 12, 2010, 03:50:07 pm »
Ahhhh ok I see, thanks.

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
sf::Sprite problem with std::vector
« Reply #3 on: August 12, 2010, 03:56:39 pm »
Actually... it should be fine, shouldn't it?  After I push the image/sprite into their vectors that's when I load the data and then assign the sprite to the image so the image isn't moving...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Sprite problem with std::vector
« Reply #4 on: August 12, 2010, 04:05:09 pm »
The elements of a vector move in memory when the vector needs to grow. Any call to push_back or resize potentially invalidates all pointers and iterators to the previous elements.
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
sf::Sprite problem with std::vector
« Reply #5 on: August 12, 2010, 04:07:34 pm »
Ah alright.  Thanks again.