SFML community forums

Help => General => Topic started by: omariisan on January 21, 2014, 04:23:56 pm

Title: Low FPS with ~1000 objects
Post by: omariisan on January 21, 2014, 04:23:56 pm
I'm relatively new to both C++ and SFML. I've been slowly working my way through building a small game, but I feel as though my FPS is much lower than it should be. I am using SFML2, and when running my game with 1000 instances of the Player object, I get ~30 fps. This seems very low to me when I read about people drawing tens of thousands of bouncing cubes before their framerate dips like that. It is possible that it is just a hardware limitation, but I would appreciate it if someone would look over my code. The instances are created in the constructor of the World object. I have included all the code as well as the C::B project file.

Here is my draw method. It is slightly modified from the version in the .zip. I have also disabled calls up update() for testing purposes. As a note, my Python version handles drawing 4000+ circles at 60fps.

void Game::draw() {
    fpsDrawLoopCounter++;
    window.clear();
    std::stringstream ss;
    ss << "FPS: " << lastReportedFPS << std::endl;
    if (state == State::PAUSED) {
        ss << "PAUSED" << std::endl;
    }
    sf::Text fpsText(ss.str(), systemFont, 16);
    fpsText.setPosition(10, 10);
    window.draw(fpsText);
    sf::CircleShape s;
    std::vector<GameObject *>::const_iterator it = world.get_objectList().begin();
    while (it != world.get_objectList().end()) {
        //window.draw((**it).get_sprite());
        s.setRadius(10);
        s.setPosition((**it).get_sprite().getPosition());
        s.setFillColor(sf::Color::Green);
        window.draw(s);
        ++it;
    }
    window.display();
}

https://www.dropbox.com/s/wg9skyyyw7st9z0/FirstGame.zip
Title: Re: Low FPS with ~1000 objects
Post by: zsbzsb on January 21, 2014, 04:28:18 pm
It is because (most likely) you are making ~1000+ individual draw calls each frame to the GPU. You need to take a look at the vertex array tutorial to reduce your draw calls.  ;)
Title: Re: Low FPS with ~1000 objects
Post by: omariisan on January 21, 2014, 05:05:29 pm
It is because (most likely) you are making ~1000+ individual draw calls each frame to the GPU. You need to take a look at the vertex array tutorial to reduce your draw calls.  ;)

Huh... I'll have to do a bit of reading, because so far I don't really understand vertex arrays, and am confused as to how to get them to play nicely with sprites or, in this case, just a simple shape.\

UPDATE: So, I tried the Python bindings for SFML, and although my code isn't exactly the same, it has no problem drawing 2000 objects at 120fps. Whereas my C++ version barely manages 30fps. Clearly something is wrong here!
Title: Re: Low FPS with ~1000 objects
Post by: amir ramezani on January 21, 2014, 06:03:29 pm
you must use vertexArrays to manage your objects better
Title: Re: Low FPS with ~1000 objects
Post by: omariisan on January 21, 2014, 06:07:42 pm
you must use vertexArrays to manage your objects better

This doesn't explain why Python can draw 4000 circles without a problem, but the C++ version (which is in release and optimized) performs worse with fewer. There is an error in my C++ code.
Title: Re: Low FPS with ~1000 objects
Post by: AlejandroCoria on January 21, 2014, 07:23:08 pm
The program can go slow because you're setting the radio, the position and color of each of the objects, each frame
Title: Re: Low FPS with ~1000 objects
Post by: omariisan on January 21, 2014, 07:36:37 pm
The program can go slow because you're setting the radio, the position and color of each of the objects, each frame

You are correct Alejandro, and I had missed that. Moving the color out of the loop shot the fps way up. Above 60 with 5000 objects. Thanks.