Advertisement
Guest User

Terrifying fixed-rate game loop

a guest
Jan 1st, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. //Fixed-rate logic ticks, that can be faster than the actual framerate.
  2. //Horrifying in its implications. Works great until whatever's in the While loop
  3. //takes longer than dt...
  4.  
  5. final float TARGET_RATE = 120.0f;
  6.  
  7. float accumulator = 0;
  8. float period = 1.0f / TARGET_RATE;
  9.  
  10. //Draw() is called in a loop, dt is the time elapsed since the last call
  11. public void draw(float dt) {
  12.     accumulator += dt;
  13.     while (accumulator > period) {
  14.         accumulator -= period;
  15.  
  16.         //Do do stuff at 120Hz, irrespective of framerate
  17.         //Just remember to use period anywhere you would normally use dt
  18.     }
  19.  
  20.     //Do the rest of your logic at the game's actual framerate
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement