Advertisement
Guest User

Java Game Update Loop

a guest
Jun 26th, 2014
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. public class Test
  2. //Java Game Loop implementation based on http://www.koonsolo.com/news/dewitters-gameloop/
  3. //java port by b5cully
  4. {    
  5.     static int TICKS_PER_SECOND = 25;
  6.     static int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
  7.     static int MAX_FRAMESKIP = 5;
  8.    
  9.     static int tickcount = 1;
  10.     static int d; //just for simulating purpose
  11.    
  12.     public static void update( ) {
  13.         int next_game_tick = tickcount;
  14.         int loops;
  15.         float interpolation;
  16.  
  17.         boolean game_is_running = true;
  18.         while( game_is_running ) {
  19.             loops = 0;
  20.             while( tickcount > next_game_tick && loops < MAX_FRAMESKIP) {
  21.                 //UPDATE GAME HERE
  22.                 next_game_tick += SKIP_TICKS;
  23.                 loops++;
  24.                 System.out.println("update");
  25.             }
  26.  
  27.             interpolation = (float) tickcount + SKIP_TICKS - next_game_tick/ (float) SKIP_TICKS;
  28.             //DISPLAY GAME HERE  display_game( interpolation );
  29.             //use: view_position = position + (speed * interpolation)
  30.             //in all displaying methods
  31.             System.out.println("display " + interpolation + " " + tickcount );
  32.             d++;
  33.             tickcount++; //count up the ticks
  34.             if ( d > 10000) break; //just for simulating purpose, remove this
  35.     }          
  36.     }    
  37.  
  38.     public static void main(String[] args) {
  39.         update();
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement