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

Author Topic: Strange Sprite Activities when using STL data structures  (Read 4645 times)

0 Members and 1 Guest are viewing this topic.

ExcessNeo

  • Newbie
  • *
  • Posts: 16
    • View Profile
Strange Sprite Activities when using STL data structures
« on: December 13, 2007, 06:27:51 pm »
I have written a block class to use with a vector data structure to represent blocks in a game of breakout. My problem is, if I create a single instance of the block the image displays fine, however if I stick it in a vector it renders the block completely white.

Here is the code from main.cpp, I have commented out the method that displays correctly.

Code: [Select]
// main.cpp
#include <SFML/Graphics.hpp>
#include "Paddle.h"
#include "Ball.h"
#include "Brick.h"
#include "Vector2.h"
#include <vector>

typedef std::vector<Breakout::Brick>::iterator Brick_it;

int main()
{
sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Break Out", sf::Window::Fixed, 4);

Breakout::Paddle player("./data/player.png", (Window.GetWidth() / 2.0f));
Breakout::Ball ball("./data/ball.png");

std::vector<Breakout::Brick> bricks;
Vector2 pos(0.0f, 0.0f);
for(int i = 0; i < 8; i++)
{
Breakout::Brick brick("./data/block.tga", pos);
bricks.push_back(brick);
pos.x +=101;
}

// Breakout::Brick brick("./data/block.tga", Vector2(500.0f, 500.0f));

bool Running = true;
while(Running)
{
sf::Event Event;
while(Window.GetEvent(Event))
{
if(Event.Type == sf::Event::Close)
Running = false;

if(Event.Type == sf::Event::KeyPressed)
{
if(Event.Key.Code == sf::Key::Escape)
Running = false;
}
}
float deltatime = Window.GetFrameTime();

// Handle player movements
if(Window.GetInput().IsKeyDown(sf::Key::Left))
player.move(deltatime, -300.0f);
if(Window.GetInput().IsKeyDown(sf::Key::Right))
player.move(deltatime, 300.0f);

ball.move(deltatime, player);

// This is where bricks are iterated through and drawn
Brick_it i = bricks.begin();
while(i != bricks.end())
{
if(i->destroy(ball) == false)
{
Window.Draw(i->GetSprite());
i++;
}
else
i = bricks.erase(i);
}

Window.Draw(player.getSprite());
Window.Draw(ball.GetSprite());
// Window.Draw(brick.GetSprite());
Window.Display();
}
return EXIT_SUCCESS;
}


I have a feeling it may be to do with the iterator, but to erase from data structures properly and effectively I should be able to use iterators.

If you need to see my class' I will post them but all my GetSprite function does is returns a reference to a sf::Sprite member called m_Sprite.

DrEvil

  • Newbie
  • *
  • Posts: 21
    • View Profile
Strange Sprite Activities when using STL data structures
« Reply #1 on: December 14, 2007, 01:53:13 am »
Try storing a block* in your vector instead of the entire object.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Strange Sprite Activities when using STL data structures
« Reply #2 on: December 14, 2007, 02:09:57 am »
A sprite always keeps a pointer to its source image. This means that if you move or copy the image instance, the pointer inside the sprite will no longer be valid.
I think you should define the copy constructor and assignment operator of your class, to properly reset the sprite's image.

Or, as DrEvil says, use a vector of Block*, so that the instances inside it will never get copied (only the opinters will be).
Laurent Gomila - SFML developer