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

Author Topic: Is my game loop is correct ?  (Read 1980 times)

0 Members and 1 Guest are viewing this topic.

ShqrdX

  • Newbie
  • *
  • Posts: 12
    • View Profile
Is my game loop is correct ?
« on: November 27, 2015, 01:20:13 pm »
The code below worked correctly(and even with different framerates) - but I need to know - is everything ok ?(e.g -  order of calling functions or delta time calculation). Is there is some room for improvements ?


       
              Clock clock;
                Time accumulator = Time::Zero;
                Time updateRate = seconds(1.0f / 60.0f);
                Time dt = Time::Zero;
                Time fpsTime = Time::Zero;

                while (m_pWindow->isOpen())
                {
                        ProcessEvents();

                        accumulator += dt;

                        while (accumulator > updateRate)
                        {
                                update_state.DeltaTime = 1.0f / 60.0f;

                                accumulator -= updateRate;

                                Update(update_state);
                        }

                        Render(draw_state);

                        dt = clock.restart();

                        // calculating FPS
                       
                        fpsTime += dt;

                        static unsigned frameCounter = 0;

                        if (fpsTime >= seconds(1.0f)) // calculate FPS only one time per second
                        {
                                m_FPS = frameCounter;
                                fpsTime = Time::Zero;
                                frameCounter = 0;
                        }
                        else
                        {
                                ++frameCounter;
                        }
                }
 
« Last Edit: November 27, 2015, 01:43:34 pm by ShqrdX »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is my game loop is correct ?
« Reply #1 on: November 27, 2015, 11:05:08 pm »
I, personally, would calculate delta time at the beginning of the loop (but after events), just before adding to the accumulator where you have it. It seems to make sense to do it at the beginning and keep those timing things together. However, I can't see that it technically matters as dt will still changed each frame, which is its intention.

As long as I haven't missed some glaring error, I think this code looks fine  :)

EDIT: I think it would be better to not hard code the fixed timestep here:
update_state.DeltaTime = 1.0f / 60.0f;
especially since you have already fixed this here:
Time updateRate = seconds(1.0f / 60.0f);

i.e.
update_state.DeltaTime = updateRate.asSeconds()
« Last Edit: November 27, 2015, 11:08:18 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ShqrdX

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Is my game loop is correct ?
« Reply #2 on: November 28, 2015, 05:27:23 am »
Hapax Thanks for the advices !
Last question -

What better for performance in my code - set sprite parameters inside the Render or inside the Update(where it should be in my opinion) ? However the Update could be called more times in the cycle(inner while loop)...

eg
if (m_bInversion)
        m_Sprite.setScale(-1, 1);
else
        m_Sprite.setScale(1, 1);
« Last Edit: November 28, 2015, 05:56:55 am by ShqrdX »

Ungod

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: Is my game loop is correct ?
« Reply #3 on: November 28, 2015, 09:41:03 pm »
What better for performance in my code - set sprite parameters inside the Render or inside the Update(where it should be in my opinion) ? However the Update could be called more times in the cycle(inner while loop)...

Your rendering should be const, so I would do it in the update. Some scale calls wouldnt affect your performance anyway.

I dont know about the context of your example, but it would probably make sense to perform an action like this at the same place you would set this boolean value on true. This way you get rid of this if query at all.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is my game loop is correct ?
« Reply #4 on: November 29, 2015, 11:22:18 pm »
set sprite parameters inside the Render or inside the Update(where it should be in my opinion) ? However the Update could be called more times in the cycle(inner while loop)...
I think it depends.

"Update" is a very generic term.
I would suggest keeping your logic updates inside the inner loop and your graphical updates straight afterwards (but before rendering) thus:
while (isCompleteTimestepAvailable)
{
    performLogicUpdates(dt);
}
performGraphicalUpdates();
renderFrame();
Note that this will only work if your logic doesn't depend on any of the graphical representations. For example, if your collision detections use the final graphical objects, the graphical updates will also need to be inside that inner loop.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything