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

Author Topic: [SFML Game Dev Book] - Low FPS at Chapter 4 [SOLVED]  (Read 1979 times)

0 Members and 1 Guest are viewing this topic.

Artexflow

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
[SFML Game Dev Book] - Low FPS at Chapter 4 [SOLVED]
« on: October 08, 2015, 12:31:34 am »
Hi everyone,

tl;dr : is this normal ?

First of all, let me say SFML is a great library.
I've started to read this great book here : https://www.packtpub.com/game-development/sfml-game-development

I really like the overall architecture which is developped in this book, and the philosophy behind it. I even agree on most of your formatting style choices ! Ahaha.

Anyway I'm now delving into chapter 4 (input handling) and noticed I had slow FPS when drawing my background texture, which by the way seems to repeat itself badly (see the picture)



You can see I'm running 46 FPS and that I have a black line on top of every desert picture.

Now if I comment the code section where I add that background to the scene graph, here is what happens :
void    World::_buildScene()
{
        // Init layers
        for (std::size_t i = 0; i < LayerCount; ++i)
        {
                SceneNode::Ptr  layer(new SceneNode());

                _sceneLayers[i] = layer.get();
                _sceneGraph.attachChild(std::move(layer));
        }

        // Init background
        //sf::Texture & texture = _textures.get(Textures::Desert);
        //sf::IntRect           textureRect(_worldBounds);
        //texture.setRepeated(true);

        // Adding background sprite
        //std::unique_ptr<SpriteNode>   backgroundSprite(new SpriteNode{texture, textureRect});
        //backgroundSprite->setPosition(_worldBounds.left, _worldBounds.top);
        //_sceneLayers[Background]->attachChild(std::move(backgroundSprite));

        // Adding player aircraft
        std::unique_ptr<Aircraft>       leader(new Aircraft{Aircraft::Eagle, _textures});
        _playerAircraft = leader.get();
        ...


As you can see I'm now running at more than 450 FPS.
I guess I could hope for a small improvement on the FPS rate, removing my background texture, but it seems quite exagerated.

Here is how SpriteNodes are drawn :
void SpriteNode::_drawCurrent(sf::RenderTarget & target, sf::RenderStates states) const
{
        target.draw(_sprite, states);
}

Here is how ScenesNodes are updated and drawn (SpriteNode inherits from them) :
void    SceneNode::_updateCurrent(sf::Time)
{
        // lel
}

void    SceneNode::_updateChildren(sf::Time dt)
{
        for (const Ptr & child : _children)
                child->update(dt);
}

void    SceneNode::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
        states.transform *= getTransform();
        _drawCurrent(target, states);
        _drawChildren(target, states);
}

void    SceneNode::_drawCurrent(sf::RenderTarget &, sf::RenderStates) const
{
}

void    SceneNode::_drawChildren(sf::RenderTarget & target, sf::RenderStates states) const
{
        for (const Ptr & child : _children)
                child->draw(target, states);
}

If you haven't read this book, believe me it's really cool !
You can see how background is loaded in that method I've commented earlier.

I'm compiling using mingw-w64 under msys2 on Windows. I've compiled SFML myself using this toolchain. I've got a poor PC though, having an i3 @ 1.90Ghz and one of those NVIDIA GeForce 710M.

My question is :
Have I done something wrong, or is it the best performance I can achieve given my system ?
Thanks for your time,

Artexflow
(I'm obviously sorry if this has no place in this forum section)
« Last Edit: October 08, 2015, 02:55:25 am by Artexflow »

Satus

  • Guest
Re: [SFML Game Dev Book] - Low FPS at Chapter 4
« Reply #1 on: October 08, 2015, 12:38:52 am »
Do you have a fixed framerate?

Artexflow

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: [SFML Game Dev Book] - Low FPS at Chapter 4
« Reply #2 on: October 08, 2015, 12:46:26 am »
Do you have a fixed framerate?
Hi, thanks for your time !

I'm not using
sf::RenderWindow::setFramerateLimit()
if that is what you're asking, nonetheless, the game loop uses fixed timesteps, as you can see here :

void    Game::run()
{
        sf::Clock       clock;
        sf::Time        timeSinceLastUpdate = sf::Time::Zero;

        while (_window.isOpen())
        {
                sf::Time        elapsedTime = clock.restart();
                timeSinceLastUpdate += elapsedTime;

                while (timeSinceLastUpdate > _timePerFrame)
                {
                        timeSinceLastUpdate -= _timePerFrame;
                        _processInput();
                        _update(_timePerFrame);
                }
                _updateStatistics(elapsedTime);
                _render();
        }
}
 

Did it help ? :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SFML Game Dev Book] - Low FPS at Chapter 4
« Reply #3 on: October 08, 2015, 12:48:10 am »
Thanks for the nice feedback regarding the book :)

Are you compiling in Release mode, with no debugger running?

Is it really only the line
_sceneLayers[Background]->attachChild(std::move(backgroundSprite));
that makes the difference?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Artexflow

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: [SFML Game Dev Book] - Low FPS at Chapter 4
« Reply #4 on: October 08, 2015, 12:53:17 am »
Are you compiling in Release mode, with no debugger running?

Hi !
I may have compiled SFML in debug mode now that you're asking me, do you think this lead is worth a try ? No debugger is running.

Is it really only the line
_sceneLayers[Background]->attachChild(std::move(backgroundSprite));
that makes the difference?

Totally :)

EDIT :
I've updated my NVIDIA driver and now everything goes smoothly. I even run at 1000 FPS.
kek.
« Last Edit: October 08, 2015, 02:55:03 am by Artexflow »