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

Author Topic: sf::RenderTexture issue  (Read 1695 times)

0 Members and 1 Guest are viewing this topic.

bearmetal

  • Newbie
  • *
  • Posts: 1
    • View Profile
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.

#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();
    }
}
 

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!
« Last Edit: February 22, 2018, 09:49:23 pm by bearmetal »

Chaia*

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::RenderTexture issue
« Reply #1 on: February 23, 2018, 06:11:47 pm »
First of all, your main loop is very strange. Usually one would check all events for one frame and not go through a whole frame for each event - you will certainly get problems here when you later on begin to use more events and react in your game on those events. Look at the tutorials how it is done in a better way.

Regarding your actual problem to be honest I do not have a solution. There are no plausible reasons why it should work perfectly fine without your if statement compared to the case you've shown. I'd suggest to just slowly go through it with the debugger.

 

anything