1
Graphics / sf::RenderTexture issue
« on: February 22, 2018, 07:35:48 pm »
Hi, I'm new to SFML development and I'm running into an issue with the game I am working on. I'm using a map generator to create random walls, or in this case, trees for the map. I have a grass tile and a tree tile, which has a transparent background, that I am trying to layer over-top one another in order to build out the scene.
If I plan on both always being there and always draw both sprites it works. But when I check if the particular tile is supposed to be a wall using an if-statement, and conditionally draw the tree sprite, then only the grass is drawn.
In the code above if you move the code inside the
Thanks!
If I plan on both always being there and always draw both sprites it works. But when I check if the particular tile is supposed to be a wall using an if-statement, and conditionally draw the tree sprite, then only the grass is drawn.
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
void drawMap(sf::RenderTexture& texture) {
texture.clear();
sf::Texture sprite_sheet;
sprite_sheet.loadFromFile("My32x64SpriteSheet.png")
bool isWall = true;
sf::Rect<int> grass_rect(0, 0, 32, 32);
sf::Sprite grass(sprite_sheet, grass_rect);
grass.setPosition(0, 0);
texture.draw(grass);
if(isWall) {
// this does not show the tree
sf::Rect<int> tree_rect(0, 32, 32, 32);
sf::Sprite tree(sprite_sheet, tree_rect);
tree.setPosition(0, 0);
texture.draw(tree);
}
texture.display();
}
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!");
window.setFramerateLimit(60);
sf::RenderTexture texture;
// I'll only be showing one tile so this is all the larger it needs to be
texture.create(32, 32);
drawMap(texture);
sf::Sprite map(texture.getTexture());
map.setPosition(0, 0);
sf::Event currentEvent;
bool isExiting = false;
while(window.isOpen()
&& !isExiting
&& window.pollEvent(currentEvent)) {
// Close window: exit
if (currentEvent.type == sf::Event::Closed) {
isExiting = true;
}
window.clear();
window.draw(map);
window.display();
}
}
#include <iostream>
using namespace std;
void drawMap(sf::RenderTexture& texture) {
texture.clear();
sf::Texture sprite_sheet;
sprite_sheet.loadFromFile("My32x64SpriteSheet.png")
bool isWall = true;
sf::Rect<int> grass_rect(0, 0, 32, 32);
sf::Sprite grass(sprite_sheet, grass_rect);
grass.setPosition(0, 0);
texture.draw(grass);
if(isWall) {
// this does not show the tree
sf::Rect<int> tree_rect(0, 32, 32, 32);
sf::Sprite tree(sprite_sheet, tree_rect);
tree.setPosition(0, 0);
texture.draw(tree);
}
texture.display();
}
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!");
window.setFramerateLimit(60);
sf::RenderTexture texture;
// I'll only be showing one tile so this is all the larger it needs to be
texture.create(32, 32);
drawMap(texture);
sf::Sprite map(texture.getTexture());
map.setPosition(0, 0);
sf::Event currentEvent;
bool isExiting = false;
while(window.isOpen()
&& !isExiting
&& window.pollEvent(currentEvent)) {
// Close window: exit
if (currentEvent.type == sf::Event::Closed) {
isExiting = true;
}
window.clear();
window.draw(map);
window.display();
}
}
In the code above if you move the code inside the
if(isWall){}
to outside of that if-statement it works just fine. Been googling and searching for an answer for a few days now and need help.Thanks!