SHARE
TWEET

Untitled

a guest May 26th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package engine;
  2.  
  3. public class GameEngine implements Runnable{
  4.  
  5.     public static final int TARGET_FPS = 75;
  6.     public static final int TARGET_UPS = 30;
  7.  
  8.     private final Window window;
  9.     private final Thread gameLoopThread;
  10.     private final Timer timer;
  11.     private final GameLogic gameLogic;
  12.  
  13.     public GameEngine(String windowTitle, int width, int height, boolean vSync, GameLogic gameLogic) {
  14.         this.window = new Window(windowTitle, width, height, vSync);
  15.         this.gameLoopThread = new Thread(this, "GAME_LOOP_THREAD");
  16.         this.timer = new Timer();
  17.         this.gameLogic = gameLogic;
  18.     }
  19.  
  20.     public void start(){
  21.         String osName = System.getProperty("os.name");
  22.         if (osName.contains("Mac")){
  23.             gameLoopThread.run();
  24.         }else{
  25.             gameLoopThread.start();
  26.         }
  27.     }
  28.  
  29.     @Override
  30.     public void run() {
  31.         try {
  32.             init();
  33.             gameLoop();
  34.         }catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     private void init() throws Exception{
  40.         window.init();
  41.         timer.init();
  42.         gameLogic.init();
  43.     }
  44.  
  45.     private void gameLoop(){
  46.  
  47.         float elapsedTime;
  48.         float accumulator = 0f;
  49.         float interval = 1f / TARGET_UPS;
  50.  
  51.         while (!window.requestClose()) {
  52.             elapsedTime = timer.getElapsedTime();
  53.             accumulator += elapsedTime;
  54.  
  55.             input();
  56.             while (accumulator >= interval){
  57.                 update(interval);
  58.                 accumulator -= interval;
  59.             }
  60.  
  61.             render();
  62.  
  63.             if(!window.isvSync()) {
  64.                 sync();
  65.             }
  66.         }
  67.     }
  68.  
  69.     private void sync() {
  70.         float loopSlot = 1f / TARGET_FPS;
  71.         double endTime = timer.getLastLoopTime() + loopSlot;
  72.  
  73.         while (timer.getTime() < endTime){
  74.             try{
  75.                 Thread.sleep(1);
  76.             }catch (InterruptedException ie){
  77.                 ie.printStackTrace();
  78.             }
  79.         }
  80.     }
  81.  
  82.     private void render() {
  83.         gameLogic.render(window);
  84.     }
  85.  
  86.     private void update(float interval) {
  87.         gameLogic.update(interval);
  88.     }
  89.  
  90.     private void input() {
  91.         gameLogic.input(window);
  92.     }
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top