Mogre GameLoop

andrej

12-02-2010 22:08:10

Hi, I am newbie in mogre and so I appeal to you for help. I don't know how to make loop for constant speed game independent of FPS. I read deWitters game loop article http://dewitters.koonsolo.com/gameloop.html but I am still of the fool.

Is this good implementation ?

const int TICKS_PER_SECOND = 25;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;

DWORD next_game_tick = GetTickCount();
int loops;
float interpolation;

bool game_is_running = true;
while( game_is_running ) {

loops = 0;
while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
update_game();

next_game_tick += SKIP_TICKS;
loops++;
}

interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
/ float( SKIP_TICKS );
display_game( interpolation );
}


Thank you for helping

ps : english with google translate help :D

AndroidAdam

13-02-2010 01:57:14


long currentUpdate;
Stopwatch clock; //add a using System.Diagnostics at the top
TimeSpan targetElapsedTime = TimeSpan.FromMilliseconds(1000 / 60);

void Tick()
{

currentUpdate += clock.ElapsedMilliseconds;

TimeSpan elapsed = TimeSpan.FromMilliseconds(clock.ElapsedMilliseconds);


long updatesDue = currentUpdate / targetElapsedTime.Milliseconds;

if (updatesDue > 0)
{
while (updatesDue > 0)
{
//Call an update function here.
updatesDue--;
}
currentUpdate = 0;

}

}

Call this Tick() function in a frame listener.

I made a system that works like XNA here that includes a time tracking system, if you're interested.

andrej

13-02-2010 17:13:29

thanks, I will try your solution

andrej

14-02-2010 18:29:41


long currentUpdate;
Stopwatch clock; //add a using System.Diagnostics at the top
TimeSpan targetElapsedTime = TimeSpan.FromMilliseconds(1000 / 60);

void Tick()
{

currentUpdate += clock.ElapsedMilliseconds;

TimeSpan elapsed = TimeSpan.FromMilliseconds(clock.ElapsedMilliseconds);


long updatesDue = currentUpdate / targetElapsedTime.Milliseconds;

if (updatesDue > 0)
{
while (updatesDue > 0)
{
//Call an update function here.
updatesDue--;
}
currentUpdate = 0;

}

}

Call this Tick() function in a frame listener.

I made a system that works like XNA here that includes a time tracking system, if you're interested.


OK, today I looked at your algorithm and I think this juice may not work correctly, targetElapsedTime is constant but currentUpdate was incrased in time so updatesDue = currentUpdate / targetElapsedTime.Milliseconds also increasing in time. Of course, just in case that clock runs constantly.

Now i have implemented deWiTTE solution and it work perfect.

andyhebear1

21-05-2010 14:13:51

good