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

Author Topic: SFML slows down over time  (Read 1481 times)

0 Members and 1 Guest are viewing this topic.

ThoseCoolGamers

  • Newbie
  • *
  • Posts: 3
    • View Profile
SFML slows down over time
« on: July 18, 2018, 01:58:09 am »
So I've been using SFML for a little bit now and I've ran into and bug.
After about 6 minutes my game will slow down going from around 1000 - 400 fps down to 100- 20 fps. This causes the player movement to jerk around, going forward and back spontaneously, I don't know if this is due to poorly written code or if its to do with my computer.

Here's the code I wrote:

[
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 800), "game");

        sf::Sprite Player;
        Player.setScale(5,5);
        Player.setPosition(400, 400);
       
        sf::Texture Texture;
        Texture.loadFromFile("t.png");
        Player.setTexture(Texture);

        sf::Clock clock;
        sf::Time dt;

        sf::Clock FPSClock;
        sf::Time FPS;

        int Speed = 500;
        sf::Vector2f Pos;
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
       
                sf::Time time = FPSClock.getElapsedTime();

                if (1.0f / time.asSeconds() < 100)
                        cout << 1.0f / time.asSeconds() << endl;

                FPSClock.restart().asSeconds();
                dt = clock.restart();

                Player.setPosition(Pos);

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        Pos.y -= Speed * dt.asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        Pos.y += Speed * dt.asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        Pos.x -= Speed * dt.asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        Pos.x += Speed * dt.asSeconds();

                window.clear();

                window.draw(Player);

                window.display();
        }

        return 0;
}]

Hapax

  • Hero Member
  • *****
  • Posts: 3357
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML slows down over time
« Reply #1 on: July 18, 2018, 02:21:02 am »
It could be your operating system wanting to cut into your processor time since it's running at 100%. Consider setting a framerate limit or adding vsync. Or, at least, add a tiny sleep to allow the OS chance to be involved.

Could it also be the output stream getting full? You're outputting to it every frame (since pretty much the beginning) and it could be getting "unhappy" with that after 6 minutes.

Also, are you running in debug or release mode?

Other stuff...
FPSClock.restart().asSeconds();
restart() actually returns the current time before restarting and you're converting it to seconds. However, you're not actually using this value so you can remove the to-seconds conversion (just restart is fine, as you did on the following line).
A suggestion, then, is to use restart instead of getElapsedTime():
sf::Time time = FPSClock.restart();
Again, you already did this with dt...

Not sure why there are square brackets at the beginning and end of your code. :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

verdog

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: SFML slows down over time
« Reply #2 on: July 18, 2018, 02:31:18 am »
I ran your code for 5 minutes and the framerate stayed more or less constant.

ThoseCoolGamers

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML slows down over time
« Reply #3 on: July 18, 2018, 03:09:15 am »
Thanks for the insight Hapax I'll use what you said to hopefully eliminate my bug.  :)
Not sure why there are square brackets at the beginning and end of your code. :P
Also the brackets are there because when I added the code = cpp part I must have left in the brackets!

ThoseCoolGamers

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML slows down over time
« Reply #4 on: July 18, 2018, 03:12:23 am »
I ran your code for 5 minutes and the framerate stayed more or less constant.

Okay thanks I'll try and run this on my parents laptop and see if the framerate stay stable!  :)

 

anything