Advertisement

Game loop time management

Started by February 24, 2019 10:42 PM
6 comments, last by ABud 5 years, 9 months ago

Apologies if this has been asked many times before...

I'm new to game coding and thought it would be fun to write my own isometric engine. I'm trying to limit how fast the game loop runs so that my engine will run at the same rate on all systems (I'm able to test the idea out on my netbook vs main PC), but for some reason this little bit of code just won't work...

As you can see I'm using c++11 and allegro 4.4.2

The idea is that using <chrono> I could tell how long the system took to run 1 loop of the game loop and subtract that from 20 (target framerate being 50 fps) so that it takes exactly 20 milliseconds to run the whole thing. However when I run the game on both systems the hero sprite moves noticeably slower on my netbook even when I lower the target framerate. Is this duff coding, bad idea etc. etc.

Thanks for your help,

Andy

 


#include <allegro.h>
#include <chrono>

#define FPS 50
  
int main() {

    while(!game.QUIT) {

                 auto started = std::chrono::high_resolution_clock::now();

  				 process_input();
  				 update();
   				 render();

                 auto done = std::chrono::high_resolution_clock::now();
                 int elapsed_t = std::chrono::duration_cast<std::chrono::milliseconds>(done-started).count();
                 int rest_t = rest_calc(elapsed_t);
                 
                 rest(rest_t);
                 }

    deinit();
    return 0;
}
END_OF_MAIN();
  
  
int rest_calc(int elapsed_t) {

    int rest_time = (1000 / FPS) - elapsed_t;  //calculate rest time to achieve framerate set by FPS
    if(rest_time > 0) return rest_time;
    else return 0;
}

  

 

Fixjng framerate is nothing you do for synchronizing your character movement! Use delta time instead and multiply that to the movement speed of your character to get a framerate independent speed value regardless of how fast your system is

Advertisement

 I also struggled with that at first.

I would consider you to read/watch information about time-based & frame-based movement/animation/etc

Little exemple :

 

44 minutes ago, Shaarigan said:

Fixjng framerate is nothing you do for synchronizing your character movement! Use delta time instead and multiply that to the movement speed of your character to get a framerate independent speed value regardless of how fast your system is

Delta-time based update is a recipe for difficult to find bugs that occur on some systems and not others. I strongly recommend fixing your update framerate so that your physics, collision, whatever all run the same on any hardware. Your render framrate on the other hand you will want to let vary more. The best way is to interpolate between update frames, and into the future, but the lazier approach of caping it to the update framerate is okay for most purposes.

An insightful article is "fix your timestep":

https://gafferongames.com/post/fix_your_timestep/

I personally like the write up for deWiTTERS Game Loop:

https://www.koonsolo.com/news/dewitters-gameloop/

Programmer and 3D Artist

Advertisement

Thanks for all the info, will have a read up

This topic is closed to new replies.

Advertisement