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

Author Topic: Should i use this game loop?  (Read 2264 times)

0 Members and 1 Guest are viewing this topic.

slucis7593

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Should i use this game loop?
« on: May 21, 2014, 05:40:21 pm »
Hi :)!
I put render() method into while loop like this:

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

        while (mWindow.isOpen())
        {
                sf::Time dt = clock.restart();
                timeSinceLastUpdate += dt;
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;

                        processInput();
                        update(TimePerFrame);

                        // Check inside this loop, because stack might be empty before update() call
                        if (mStateStack.isEmpty())
                                mWindow.close();

                        render();
                }

                updateStatistics(dt);
        }
}

and here is result, FPS is very high but it's seem not stability:


So, i want to ask that should i use this game loop?
Thanks :)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Should i use this game loop?
« Reply #1 on: May 21, 2014, 05:50:30 pm »
This post really belongs in the help section, but anyway; I believe this will help you: http://en.sfml-dev.org/forums/index.php?topic=15175.msg107603#msg107603

slucis7593

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Should i use this game loop?
« Reply #2 on: May 21, 2014, 05:59:59 pm »
This post really belongs in the help section, but anyway; I believe this will help you: http://en.sfml-dev.org/forums/index.php?topic=15175.msg107603#msg107603

Oh, i'm sorry :(, i'm newbie and thank you for your help :)

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Should i use this game loop?
« Reply #3 on: May 21, 2014, 08:03:00 pm »
                        render();
                }

                updateStatistics(dt);
        }
 
Looks like only these two have to be swapped and you should pass TimePerFrame to your updateStatistics function instead of dt, like you did with your update function.

Audio is a strange title btw haha :D
Failing to succeed does not mean failing to progress!

slucis7593

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Should i use this game loop?
« Reply #4 on: May 22, 2014, 04:13:52 am »
Quote
Looks like only these two have to be swapped and you should pass TimePerFrame to your updateStatistics function instead of dt, like you did with your update function.

Audio is a strange title btw haha :D

:D actually, this source code come from 'SFML Game Development' Book and original code is:
void Application::run()
{
    sf::Clock clock;
    sf::Time timeSinceLastUpdate = sf::Time::Zero;

    while (mWindow.isOpen())
    {
        sf::Time dt = clock.restart();
        timeSinceLastUpdate += dt;
        while (timeSinceLastUpdate > TimePerFrame)
        {
            timeSinceLastUpdate -= TimePerFrame;

            processInput();
            update(TimePerFrame);

            // Check inside this loop, because stack might be empty before update() call
            if (mStateStack.isEmpty())
                mWindow.close();
        }

        updateStatistics(dt);
        render();
    }
}