a problem with sprites showing up as a white box. I found an explanation here
http://en.sfml-dev.org/forums/index.php?topic=8961.0 which makes complete sense to me.
I believe I'm running into the same issue, although I'm not completely sure why. It isn't clear to me exactly what the relationship in SFML is between Images, Textures, and Sprites, nor do I understand how their references are kept, and when they're released.
In particular, here's some code for a tetrimino factory function. Ignore the render window parameter,
it's there strictly for testing purposes so I could try and get an idea of how and where the sprites were losing their texture references:
Tetrimino buildTetrimino(sf::Vector2f vecs[], std::string block_name, sf::RenderWindow *w) {
sf::Texture bt;
if(!bt.loadFromFile(block_name)) {
std::cout << "failed to load " << block_name << std::endl;
exit(1);
}
sf::Vector2f sz(bt.getSize());
sf::Vector2f factors(30/sz.x,30/sz.y); // blocks are 30x30 pixels
sf::Sprite blocks[4];
for(int i = 0; i < 4; ++i) {
sf::Sprite sp(bt);
sp.setScale(factors);
sp.setPosition(vecs[i].x,vecs[i].y);
blocks[i] = sp;
}
Tetrimino tet(blocks,sf::Vector2f(0,0),w);
//tet.draw(*w);
return tet;
}
This is the function that's calling the factory function:
Tetrimino createI(sf::RenderWindow &w) {
sf::Vector2f vecs[] = {
sf::Vector2f(-1.5, 0.5),
sf::Vector2f(-0.5,-0.5),
sf::Vector2f( 0.5,-0.5),
sf::Vector2f( 0.5, 0.5),
};
Tetrimino t = buildTetrimino(vecs,"./resources/block6.jpg",&w);
t.draw(w);
return t;
Note the t.draw. The one in the factory function displays the block just fine. The one in the createI() function displays a white block.
Here are the relevant constructors.
Tetrimino::Tetrimino(sf::Sprite inpBlocks[], sf::Vector2f center, sf::RenderWindow *w) {
for(int i = 0; i < 4; i++) {
this->blocks[i] = inpBlocks[i];
}
this->center = center;
this->translateToTopLeft();
}
Tetrimino::Tetrimino(const Tetrimino &rhs) {
for(int i = 0; i < 4; i++) {
this->blocks[i] = rhs.blocks[i];
}
}
I would have expected the copy constructors for the Sprite's to do the right thing, but apparently they're not?
Also, this code is unfinished, it's true that all of the blocks currently will render into the exact same position. The vector you see in the code is defined in terms of a block size of 1 and the factory method will need to translate accordingly, but for now, I'm more worried about the white block being displayed