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

Author Topic: Issue with rendering textures to the screen  (Read 1847 times)

0 Members and 1 Guest are viewing this topic.

JacobHofer

  • Newbie
  • *
  • Posts: 4
    • View Profile
Issue with rendering textures to the screen
« on: November 29, 2018, 07:14:01 am »
So, I have a tile game that I am working on. This is a very odd bug, and I'm not sure if it is due to my own code, or something in SFML. I've looked all over my code, and don't think that's where it is originating from.
 
Anyways, the bug is a small visual glitch. Some of my tiles seem to be rendered as a solid color from time to time, but for only a frame. In addition, the color seems to match somewhat with the base texture of the tile. I have attached a short video showing the bug in it's entirety.

https://www.youtube.com/watch?v=93n_V0vjkHI&feature=youtu.be

If you need to see any code, ask and I'll be happy to provide it

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Issue with rendering textures to the screen
« Reply #1 on: December 01, 2018, 07:42:28 pm »
You have to post the code to get an answer to this. If I had to guess though, I would say that you set the top and left coordinates of the texture rect of those tiles to (0, 0) somewhere in your code while keeping the normal width and height of the tile, therefore causing only the top left pixel color of the tile to be rendered. That would explain their color. Is your tilemap static or does it have an update function of some sort for animating it?

JacobHofer

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issue with rendering textures to the screen
« Reply #2 on: December 01, 2018, 09:59:39 pm »
My tilemap only changes when a player places a new tile, which is not happening here, so in this instance, the tilemap is static. None of the tiles are animating or anything either. Here is the code that handles the rendering of the tiles.

sf::Vector2f v = handler->window->getView().getSize();
        //std::cout << v.x << " " << v.y << std::endl;
        std::vector<std::vector<sf::RectangleShape>> renderOrder(256,
                std::vector<sf::RectangleShape>(0));


        for (int x = 0; x < world->getWidth(); x++) {
                for (int y = 0; y < world->getHeight(); y++) {



                        if (!(x * tileSize + tileSize < xOffset + (handler->window->getSize().x / 2 - v.x / 2) ||
                                x * tileSize > handler->window->getSize().x + xOffset - (handler->window->getSize().x / 2 - v.x / 2) ||
                                y * tileSize + tileSize < yOffset + (handler->window->getSize().y / 2 - v.y / 2) ||
                                y * tileSize > handler->window->getSize().y + yOffset - (handler->window->getSize().y / 2 - v.y / 2))) {


                                sf::Uint8 tileId = world->getTile(x, y);

                                sf::RectangleShape tile(sf::Vector2f(tileSize, tileSize));
                                tile.setPosition((x * tileSize - xOffset), (y * tileSize - yOffset));

                                sf::Uint8 data = 0;

                                tile.setTexture(handler->assets->getBaseTexture(tileId));



                                renderOrder[0].push_back(tile); // We always want the base tiles to be drawn first

                                TileData currentTileData = world->getTileData(x, y);
                                std::map<sf::Uint8, sf::Uint8> adjInfo = currentTileData.getAdjacentInfo();
                                for (std::map<sf::Uint8, sf::Uint8>::iterator it = adjInfo.begin(); it != adjInfo.end(); it++) {

                                        sf::Uint8 key = it->first;
                                        sf::Uint8 value = it->second;

                                        sf::Texture* newT = handler->assets->getOuterTexture(key, value, x, y);
                                        if (newT == nullptr) continue;
                                        tile.setTexture(newT);
                                        renderOrder[handler->assets->getTilePriority(key)].push_back(tile);

                                }


                        }



                }
        }

        for (int i = 0; i < renderOrder.size(); i++) {
                for (int j = 0; j < renderOrder[i].size(); j++) {
                        handler->window->draw(renderOrder[i][j]);
                }
        }
 

BTW tileSize, xOffset, and yOffset are variables declared elsewhere. Tilesize is simply the size of a tile, and x and y offset is the offset from the camera

*EDIT* sorry that the code looks bad in the window. I think it messed up my formatting. Here is a better view of it https://pastebin.com/LNgzCb61
« Last Edit: December 01, 2018, 10:02:17 pm by JacobHofer »