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

Author Topic: Help with a severe lag  (Read 1590 times)

0 Members and 1 Guest are viewing this topic.

ScullCrusher

  • Newbie
  • *
  • Posts: 1
    • View Profile
Help with a severe lag
« on: August 08, 2020, 09:00:46 pm »
This is my first time using SFML and while I was trying to make a game I noticed that whenever I ran the program, everything on my computer would start lagging. I opened the Task manager and it said that the program was using a lot of the performance. I have reproduced the bug so that it's easier to read the code.
I know the problem is in the the update function because when I comment out it's call, it doesn't cause the lag.
Here is the code
#include "SFML/Graphics.hpp"


class Game
{
private:
        sf::RenderWindow* window;

public:
        Game()
                : window{ new sf::RenderWindow(sf::VideoMode(500, 500), "Game") }
        {

        }

        ~Game()
        {
                delete window;
        }

        void handleInputs()
        {
                sf::Event event;
                while (window->pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window->close();
                }
        }

        void update()
        {
                window->clear();
                window->display();
        }

        void run()
        {
                while (window->isOpen())
                {
                        handleInputs();
                        update();
                }
        }
};


int main()
{
        Game game;
        game.run();

        return 0;
}

 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Help with a severe lag
« Reply #1 on: August 08, 2020, 11:30:53 pm »
It runs as fast as it can if you don't limit your FPS, thus using as much resources as needed.

To limit FPS, you can either use setVerticalSyncEnabled or setFramerateLimit
https://www.sfml-dev.org/tutorials/2.5/window-window.php#controlling-the-framerate

 

anything