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

Author Topic: using vertexArray drops fps massively  (Read 1416 times)

0 Members and 1 Guest are viewing this topic.

ashleybeshir

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
using vertexArray drops fps massively
« on: April 03, 2015, 01:37:40 pm »
Hello, ı was looking for a way to draw tile in game dynamically(edit or change at run time). I though of is i use vertexarray it can be done. Well it does the job excellently but fps drops very quick.
Commended vertex code (release ) = 2000+-
UnCommended vertex code (release) = 21+-1

There is massive difference in fps

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
        sf::VertexArray quad(sf::Quads, 4);
       
        while (window.isOpen())
        {
                window.clear();
                sf::Event event;
                int size{ 20 };

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                for (int x = 0; x < 40; x++)
                {
                        for (int y = 0; y < 40; y++)
                        {
                                quad[0].position = sf::Vector2f(x, y);
                                quad[1].position = sf::Vector2f(x*size, y);
                                quad[2].position = sf::Vector2f(x*size, y*size);
                                quad[3].position = sf::Vector2f(x, y*size);
                                window.draw(quad);
                        }
                }
               
                window.display();
        }

        return 0;
}

The code is pretty much the one from tutorial with the vertex just added. I know the for loops in combine call 40*40 time but i didnt expect it to fall this quick. Is there a alternative way to do it ?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: using vertexArray drops fps massively
« Reply #1 on: April 03, 2015, 02:38:07 pm »
You draw the vertex array 40*40 = 1600 times per frame. The whole point of using vertex arrays is to reduce draw calls, however.

That is, build it with all the quads you need, but draw it only once. If you're unsure, refer to the tutorial, there's a good example.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ashleybeshir

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: using vertexArray drops fps massively
« Reply #2 on: April 03, 2015, 10:21:55 pm »
You draw the vertex array 40*40 = 1600 times per frame. The whole point of using vertex arrays is to reduce draw calls, however.

That is, build it with all the quads you need, but draw it only once. If you're unsure, refer to the tutorial, there's a good example.
Sorry for the late reply i found a tutorial in learn section. tried what u said but the results where still the same
int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
        sf::VertexArray Tquad(sf::Quads, 40*40*4);
        int size{ 20 };
        for (int x = 0; x < 40; x++)
        {
                for (int y = 0; y < 40; y++)
                {
                        sf::Vertex* quad = &Tquad[(x + y * 40) * 4];
                        quad[0].position = sf::Vector2f(x, y);
                        quad[1].position = sf::Vector2f(x*size, y);
                        quad[2].position = sf::Vector2f(x*size, y*size);
                        quad[3].position = sf::Vector2f(x, y*size);
                }
        }
       
        while (window.isOpen())
        {
                sf::Event event;
               

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
               
                window.clear();
                window.draw(Tquad);
                window.display();

        }

        return 0;
}
Still get the same amount of fps