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

Author Topic: [SOLVED]Window render is slow when not using std::cout  (Read 1926 times)

0 Members and 1 Guest are viewing this topic.

ChocolatePinecone

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
[SOLVED]Window render is slow when not using std::cout
« 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)
                                Game::window.close();
                }

                // Clear window with black background
                Game::window.clear(sf::Color::Black);

                // 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;
                Game::renderer.render();
        }
 

The render function consists of:

        // Render all sprites
        for (re::Sprite& sprite : Game::scene.getAllSprites()) {
                Renderer::mWindow.draw(sprite.getSprite());
        }

        // Draw sprites on screen
        Renderer::mWindow.display();
 

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?
« Last Edit: September 06, 2018, 03:05:50 pm by ChocolatePinecone »

ChocolatePinecone

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Window render is slow when not using std::cout
« Reply #1 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