• Create Account

#ActualSillyCow

Posted 19 September 2013 - 04:35 PM

limit your speed to a sliding window:

1. Keep a sliding window (queue) of your last 50 frames' times (or any other number)

2. If your current frame is too far ahead of it's time: You want 50 FPS, but a second has not passed since the last 50 frames, then sleep a little bit.

3. You will also want to check the last 2 frames, to set an upper bound for speed: It is ok for the game to accelerate to compensate for missed frames, but there should be a limit on the maximum rate. If you do not do this, and you have some long frames, then your game will jitter.

Regardless of these points, you should separate your logic code from your render code. Your logic should never fall behind as a result of frame drawing speed. If you use a single thread it should look something like this.

while(true){
bool wasUpdated=false;
int targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
while(currentTime()>targetLogicTime){
//calculate logic:
calculateStuff();//put all your logic code here
wasUpdated=true;
targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
}
if(wasUpdated){
drawStuff();//Draw everything here
}else{
sleep(1ms);//give the CPU time to rest to avoid busy waiting.
}
}

Basically, this loop makes sure:

1. Your frame rate doesn't explode

2. If your drawing rate goes down, your game rate stays the same (car still takes 60 seconds to drive from A to B regardless of frame rate)

3. You don't draw redundant frames (frames where nothing has changed)

4. If available, you give the CPU some time to rest. (Very important for desktop CPU temperature, and laptop/phone battery life).

You can get better performance if you do this on two threads, but that's a whole other story.

#4SillyCow

Posted 19 September 2013 - 04:31 PM

limit your speed to a sliding window:

1. Keep a sliding window (queue) of your last 50 frames' times (or any other number)

2. If your current frame is too far ahead of it's time: You want 50 FPS, but a second has not passed since the last 50 frames, then sleep a little bit.

3. You will also want to check the last 2 frames, to set an upper bound for speed: It is ok for the game to accelerate to compensate for missed frames, but there should be a limit on the maximum rate. If you do not do this, and you have some long frames, then your game will jitter.

Regardless of these points, you should separate your logic code from your render code. Your logic should never fall behind as a result of frame drawing speed. If you use a single thread it should look something like this.

while(true){
bool wasUpdated=false;
int targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
while(currentTime()>targetLogicTime){
//calculate logic:
calculateStuff();//put all your logic code here
wasUpdated=true;
targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
}
if(wasUpdated){
drawStuff();//Draw everything here
}else{
sleep(1ms);//give the CPU time to rest to avoid busy waiting.
}
}

You can get better performance if you do this on two threads, but that's a whole other story.

#3SillyCow

Posted 19 September 2013 - 04:31 PM

limit your speed to a sliding window:

1. Keep a sliding window (queue) of your last 50 frames' times (or any other number)

2. If your current frame is too far ahead of it's time: You want 50 FPS, but a second has not passed since the last 50 frames, then sleep a little bit.

3. You will also want to check the last 2 frames, to set an upper bound for speed: It is ok for the game to accelerate to compensate for missed frames, but there should be a limit on the maximum rate. If you do not do this, and you have some long frames, then your game will jitter.

Regardless of these points, you should separate your logic code from your render code. Your logic should never fall behind as a result of frame drawing speed. If you use a single thread it should look something like this.

while(true){
bool wasUpdated=false;
int targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
while(currentTime()>targetLogicTime){
//calculate logic:
calculateStuff();//put all your logic code here
wasUpdated=true;
targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
}
if(wasUpdated){
drawStuff();//Draw everything here
}else{
sleep(1ms);//give the CPU time to rest to avoid busy waiting.
}
}

You can get better performance if you do this on two threads, but that's a whole other story.

#2SillyCow

Posted 19 September 2013 - 04:30 PM

limit your speed to a sliding window:

1. Keep a sliding window (queue) of your last 50 frames' times (or any other number)

2. If your current frame is too far ahead of it's time: You want 50 FPS, but a second has not passed since the last 50 frames, then sleep a little bit.

3. You will also want to check the last 2 frames, to set an upper bound for speed: It is ok for the game to accelerate to compensate for missed frames, but there should be a limit on the maximum rate. If you do not do this, and you have some long frames, then your game will jitter.

Regardless of these points, you should separate your logic code from your render code. Your logic should never fall behind as a result of frame drawing speed. If you use a single thread it should look something like this.

while(true){
bool wasUpdated=false;
int targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
while(currentTime()>targetLogicTime){
//calculate logic:
calculateStuff();//put all your logic code here
wasUpdated=true;
targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
}
if(wasUpdated){
drawStuff();//Draw everything here
}
sleep(1ms);//give the CPU time to rest to avoid busy waiting.
}

You can get better performance if you do this on two threads, but that's a whole other story.

#1SillyCow

Posted 19 September 2013 - 04:22 PM

limit your speed to a sliding window:

1. Keep a sliding window (queue) of your last 50 frames' times (or any other number)

2. If your current frame is too far ahead of it's time: You want 50 FPS, but a second has not passed since the last 50 frames, then sleep a little bit.

3. You will also want to check the last 2 frames, to set an upper bound for speed: It is ok for the game to accelerate to compensate for missed frames, but there should be a limit on the maximum rate. If you do not do this, and you have some long frames, then your game will jitter.

Regardless of these points, you should separate your logic code from your render code. Your logic should never fall behind as a result of frame drawing speed. If you use a single thread it should look something like this.

while(true){
bool update=false;
int targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
while(currentTime()>targetLogicTime){
//calculate logic:
calculateStuff();//put all your logic code here
update=true;
targetLogicTime=CalculateTimeFromSlidingWindowAsMentionedAbove();
}
drawStuff();//Draw everything here
sleep(1ms);//give the CPU time to rest to avoid busy waiting.
}

You can get better performance if you do this on two threads, but that's a whole other story.

PARTNERS