SFML community forums

Help => Graphics => Topic started by: Justbod on February 09, 2019, 08:29:11 pm

Title: Need help with reducing draw calls to increase performance
Post by: Justbod on February 09, 2019, 08:29:11 pm
Hi!

Right now I'm rendering my tilemap like this :

for (int x = 0; x < 50; x++) {
      for (int y = 0; y < 50; y++) {
         if (mapdata
            sf::Texture tile;
            if (!tile.loadFromFile("floor_tile_1.png"))
            {
               std::cout << "Error could not load floor tile!";
            }
            sf::Sprite tilesprite;
            tilesprite.setTexture(tile);
            int px = (x - y) * 20;
            int py = (x + y) * 20 / 2;
            tilesprite.setPosition(px + 800, py);
                                window.draw(tilesprite);
         }
      }
   }

   window.display();
}

I did some research and found out that you can use vertex arrays to reduce the draw calls. But I have no idea how I would implement a system like this.
Title: Re: Need help with reducing draw calls to increase performance
Post by: eXpl0it3r on February 10, 2019, 01:06:14 am
Please use the [code=cpp][/code] tags when posting code.

The major slow-down you get in your code is because you're creating and loading the texture in each loop iteration.
Please follow the official tutorial (https://www.sfml-dev.org/tutorials/2.5/), which will teach you how to properly use sf::Texture and even how to use sf::VertexArray. You have all the tools and documentation you need, now it's your time to sit down, read and understand it. ;)
Title: Re: Need help with reducing draw calls to increase performance
Post by: Justbod on February 10, 2019, 12:39:26 pm
Thanks for your reply! I found & read the tutorial and managed to draw my tile using a vertex array. But I still need to make a lot of draw calls to draw the entire tilemap.

for (int x = 0; x < 50; x++) {
                for (int y = 0; y < 50; y++) {
                        //Check if tile should be drawn
                        if (mapdata[x][y] == 1) {
                                sf::Texture tile;
                                if (!tile.loadFromFile("floor_tile_1.png"))
                                {
                                        std::cout << "Error could not load floor tile!";
                                }
                                //sf::Sprite tilesprite;
                                //tilesprite.setTexture(tile);
                                int px = (x - y) * 20;
                                int py = (x + y) * 20 / 2;
                                //tilesprite.setPosition(px + 800, py);
                                //window.draw(tilesprite);
                                //sf::VertexArray vertices;
                                //window.draw(vertices, &tile);
                                sf::VertexArray quad(sf::Quads, 4);

                                //this serves as some kind of wrapper for the texture?
                                //tiles; width:42px height: 21px;
                                //TODO draw with px & py coordinate input
                                quad[0].position = sf::Vector2f(10.f, 10.f); //top left
                                quad[1].position = sf::Vector2f(52.f, 10.f); //top right
                                quad[2].position = sf::Vector2f(52.f, 31.f); //bottom right
                                quad[3].position = sf::Vector2f(10.f, 31.f); //bottom left

                                //texture itself is 42x21px
                                quad[0].texCoords = sf::Vector2f(0.f, 0.f);
                                quad[1].texCoords = sf::Vector2f(42.f, 0.f);
                                quad[2].texCoords = sf::Vector2f(42.f, 21.f);
                                quad[3].texCoords = sf::Vector2f(0.f, 21.f);

                                window.draw(quad, &tile);
                        }
                }
        }

        window.display();
 
Title: Re: Need help with reducing draw calls to increase performance
Post by: eXpl0it3r on February 10, 2019, 01:08:06 pm
You're still loading the texture every loop iteration. ;)