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

Author Topic: Need help with reducing draw calls to increase performance  (Read 1803 times)

0 Members and 1 Guest are viewing this topic.

Justbod

  • Newbie
  • *
  • Posts: 9
    • View Profile
Need help with reducing draw calls to increase performance
« 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
  • [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);
         }
      }
   }

   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.
🧃🦔

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Need help with reducing draw calls to increase performance
« Reply #1 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, 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Justbod

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Need help with reducing draw calls to increase performance
« Reply #2 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();
 
🧃🦔

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Need help with reducing draw calls to increase performance
« Reply #3 on: February 10, 2019, 01:08:06 pm »
You're still loading the texture every loop iteration. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/