I recently read this article on Game Loops: http://www.koonsolo.com/news/dewitters-gameloop/
And the recommended last implementation is confusing me deeply. I don't understand how it works, and it looks like a complete mess.
I understand the principle: Update the game at a constant speed, with whatever is left render the game as many times as possible.
I take it you cannot use a:
- Get input for 25 ticks
- Render game for 975 ticks
Approach since you would be getting input for the first portion of the second and this would feel weird? Or is that what is going on in the article?
Essentially:
while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP)
How is that even valid?
Let's assume his values.
MAX_FRAMESKIP = 5
Let's assume next_game_tick, which was assigned moments after initialization, before the main game loop is say... 500.
Finally, as I am using SDL and OpenGL for my game, with OpenGL being used for rendering only, let's assume that GetTickCount()
returns the time since SDL_Init was called, which it does.
SDL_GetTicks -- Get the number of milliseconds since the SDL library initialization.
Source: http://www.libsdl.org/docs/html/sdlgetticks.html
The author also assumes this:
DWORD next_game_tick = GetTickCount();
// GetTickCount() returns the current number of milliseconds
// that have elapsed since the system was started
If we expand the while
statement we get:
while( ( 750 > 500 ) && ( 0 < 5 ) )
750 because time has passed since next_game_tick
was assigned. loops
is zero as you can see in the article.
So we have entered the while loop, let's do some logic and accept some input.
Yadayadayada.
At the end of the while loop, which I remind you is inside our main game loop is:
next_game_tick += SKIP_TICKS;
loops++;
Let's update what the next iteration of the while code looks like
while( ( 1000 > 540 ) && ( 1 < 5 ) )
1000 because time has elapsed getting input and doing stuff before we reached the next ineteration of the loop, where GetTickCount() is recalled.
540 because, in the code 1000 / 25 = 40, therefore, 500 + 40 = 540
1 because our loop has iterated once
5, you know why.
So, since this While loop is CLEARLY depedent on MAX_FRAMESKIP
and not the intended TICKS_PER_SECOND = 25;
how is the game supposed to even run correctly?
It was no surprise to me that when I implemented this into my code, correctly I might add as I simply renamed my functions to handle user input and draw the game to what the author of the article has in his example code, the game did nothing.
I placed an fprintf( stderr, "Test\n" );
inside the while loop which doesn't get printed until the game ends.
How is this game loop running 25 times a second, guaranteed, while rendering as fast as it can?
To me, unless I am missing something HUGE, it looks like... nothing.
And isn't this structure, of this while loop, supposedly running 25 times a second and then updating the game exactly what I mentioned beforehand at the start of the article?
If that is the case why couldn't we do something simple like:
while( loops < 25 )
{
getInput();
performLogic();
loops++;
}
drawGame();
And count for interpolation some other way.
Forgive my extremely long question, but this article has done more harm than good to me. I am severely confused now - and have no idea how to implement a proper game loop because of all these arisen questions.