SFML community forums

Help => Window => Topic started by: ChocolatePinecone on September 06, 2018, 01:16:36 pm

Title: [SOLVED]Window render is slow when not using std::cout
Post by: ChocolatePinecone on September 06, 2018, 01:16:36 pm
Hey Everyone,

I'm building my first engine and am stunned by this weird problem.

I have the following (simplified) setup for running my game loop:

// Open window
        while (Game::window.isOpen()) {
                //Register events
                sf::Event event;

                // Check if an event happened
                while (Game::window.pollEvent(event)) {
                        // Close windows if close button is pressed
                        if (event.type == sf::Event::Closed)

                // Clear window with black background

                // Let game logic catch up with real passed time
                lag += Game::getElapsedFrameTimeAndRestart(); // Add time that needs to be caught up in ms
                while (lag > 10) { // fixed update time is 10 ms
                        // Test moving ship to right
                        re::Sprite& player = scene.getSprite("player");
                        float movement = Game::getFixedUpdateTime() * 50;
                        float x = player.getPosition().x + movement;
                        player.setPosition(x, 50);

                        // Calculate remaining lag time to recover from
                        lag -= fixedUpdateTime; // Remove time that has been caught up

                // Render all visible sprites
                //std::cout << "text"<< std::endl;

The render function consists of:

        // Render all sprites
        for (re::Sprite& sprite : Game::scene.getAllSprites()) {

        // Draw sprites on screen

Now the weird problem is that when I run this code with the std::cout enabled, everything is rendered nicely and smoothly, but when I remove the std::cout, the rendering is slow and unstable.

I want to remove my unneccesary cout, but somehow I can't get a good rendering then...

What am I doing wrong?
Title: Re: Window render is slow when not using std::cout
Post by: ChocolatePinecone on September 06, 2018, 03:05:26 pm
Ok i solved it...

It seemed the "getElapsedFrameTimeAndRestart()" function was returning as int in milliseconds.

My current project is so fast that most times this value returns 0 because it is rounded down from the microseconds that a loop takes.

Probably a std::cout made some more movement happen because the statement made my loop just a bit longer to be rounded up to 1 millisecond more often.

I fixed this by now getting and returning the time in float seconds instead of int milliseconds