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

Author Topic: Low FPS with ~1000 objects  (Read 4145 times)

0 Members and 1 Guest are viewing this topic.

omariisan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Low FPS with ~1000 objects
« 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
« Last Edit: January 21, 2014, 06:04:28 pm by omariisan »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Low FPS with ~1000 objects
« Reply #1 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.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

omariisan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Low FPS with ~1000 objects
« Reply #2 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!
« Last Edit: January 21, 2014, 05:52:52 pm by omariisan »

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Low FPS with ~1000 objects
« Reply #3 on: January 21, 2014, 06:03:29 pm »
you must use vertexArrays to manage your objects better
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

omariisan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Low FPS with ~1000 objects
« Reply #4 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.

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Low FPS with ~1000 objects
« Reply #5 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

omariisan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Low FPS with ~1000 objects
« Reply #6 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.