Hello,
Had a quick question about RenderTexture, which I'm trying to understand better (off screen drawing).
I have a initialize class where I do this
init() {
...
if (!renderTexture.create(50*32, 50*32)) {
std::cout << "Error: creating renderTexture\n";
}
renderTexture.clear();
for (auto i = 0; i < 50; i++) {
for (auto j = 0; j < 50; j++) {
tiles[j][i].sprite.setPosition(tiles[j][i].pos);
renderTexture.draw(tiles[j][i].sprite);
}
}
renderTexture.display();
}
In my draw function, I have...
void draw(){
const sf::Texture& tex = renderTexture.getTexture();
m_window.clear();
m_window.draw(sf::Sprite(tex));
m_window.draw(m_player);
m_window.display();
}
Now here is the part it is messing up. I added a space key to change tile[10][10] to a red tile and try to update it all and it creates all the tiles (50x50 Map/32x32 Tiles) white (losing the texture) except the 10x10 tile.
void handleInput(){
...
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
// drawing uses the same functions
if (!renderTexture.create(50 * 32, 50 * 32)) { // may not need this?
std::cout << "error\n";
}
renderTexture.clear();
for (auto i = 0; i < 50; i++) {
for (auto j = 0; j < 50; j++) {
tiles[j][i].sprite.setPosition(tiles[j][i].pos);
if (i == 10 && j == 10)
tiles[j][i].sprite.setColor(sf::Color::Red);
renderTexture.draw(tiles[j][i].sprite);
}
}
renderTexture.display();
}
}
}
Any help would be appreciated, I'm just trying to learn how to use this more in this example since a few times I feel this concept would have been handy to know. I understand that it would be better to just put a red color sprite on top since this seems to be demanding to constantly run except the initial part.