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.
// 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.