Engine.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package me.hammerle.snuviengine.api;
  2. import org.lwjgl.glfw.*;
  3. import org.lwjgl.opengl.*;
  4. import static org.lwjgl.glfw.Callbacks.*;
  5. import static org.lwjgl.glfw.GLFW.*;
  6. import static org.lwjgl.opengl.GL11.*;
  7. import static org.lwjgl.system.MemoryUtil.*;
  8. public class Engine
  9. {
  10. private static long window;
  11. private static long nanosPerTick = 10_000_000;
  12. private static final int MAX_TICKS_PER_FRAME = 20;
  13. private static long nanosPerFrame = 1000000000l / 60l;
  14. private static Timer fpsTimer;
  15. private static Timer tpsTimer;
  16. private static Renderer renderer;
  17. private Engine()
  18. {
  19. }
  20. public static void start(IGame game)
  21. {
  22. loop(game);
  23. glfwFreeCallbacks(window);
  24. glfwDestroyWindow(window);
  25. glfwTerminate();
  26. GLFWErrorCallback error = glfwSetErrorCallback(null);
  27. if(error != null)
  28. {
  29. error.free();
  30. }
  31. }
  32. public static void stop()
  33. {
  34. glfwSetWindowShouldClose(window, true);
  35. }
  36. public static void init(String name, int width, int height)
  37. {
  38. GLFWErrorCallback.createPrint(System.err).set();
  39. if(!glfwInit())
  40. {
  41. throw new IllegalStateException("Unable to initialize GLFW");
  42. }
  43. glfwDefaultWindowHints();
  44. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  45. glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
  46. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  47. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  48. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  49. window = glfwCreateWindow(width, height, name, NULL, NULL);
  50. if(window == NULL)
  51. {
  52. throw new IllegalStateException("Failed to create the GLFW window");
  53. }
  54. glfwSetKeyCallback(window, (w, key, scancode, action, mods) ->
  55. {
  56. if(action == GLFW_RELEASE)
  57. {
  58. KeyHandler.onKeyUpEvent(key);
  59. }
  60. else if(action == GLFW_PRESS)
  61. {
  62. KeyHandler.onKeyDownEvent(key);
  63. }
  64. });
  65. glfwMakeContextCurrent(window);
  66. glfwSwapInterval(1);
  67. GL.createCapabilities();
  68. renderer = new Renderer(width, height);
  69. fpsTimer = new Timer(60);
  70. tpsTimer = new Timer(1_000_000_000l / nanosPerTick);
  71. glfwSetFramebufferSizeCallback(window, (w, fwidth, fheight) ->
  72. {
  73. glViewport(0, 0, fwidth, fheight);
  74. renderer.setViewPort(fwidth, fheight);
  75. });
  76. glfwShowWindow(window);
  77. }
  78. private static void sleep(long nanos)
  79. {
  80. if(nanos < 0)
  81. {
  82. return;
  83. }
  84. long end = System.nanoTime() + nanos - 10000;
  85. try
  86. {
  87. Thread.sleep(nanos / 1_000_000);
  88. }
  89. catch(InterruptedException ex)
  90. {
  91. }
  92. int i = 0;
  93. while(System.nanoTime() < end)
  94. {
  95. i++;
  96. }
  97. }
  98. private static void loop(IGame game)
  99. {
  100. long newTime = glfwGetTimerValue();
  101. long oldTime;
  102. long lag = 0;
  103. long frameLag = 0;
  104. long lastFrame = 0;
  105. while(!glfwWindowShouldClose(window))
  106. {
  107. oldTime = newTime;
  108. newTime = glfwGetTimerValue();
  109. lag += newTime - oldTime;
  110. frameLag += newTime - oldTime;
  111. if(lag >= nanosPerTick || frameLag >= nanosPerFrame)
  112. {
  113. int ticksPerFrame = 0;
  114. while(lag >= nanosPerTick)
  115. {
  116. lag -= nanosPerTick;
  117. tpsTimer.update();
  118. KeyHandler.tick();
  119. game.tick();
  120. ticksPerFrame++;
  121. if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
  122. {
  123. long skip = lag / nanosPerTick;
  124. lag -= skip * nanosPerTick;
  125. if(skip > 0)
  126. {
  127. System.out.println("skipped " + skip + " game ticks " + lag);
  128. }
  129. break;
  130. }
  131. }
  132. if(frameLag >= nanosPerFrame)
  133. {
  134. frameLag -= nanosPerFrame;
  135. // make sure no frames are rendered immediately after each other
  136. // this happens if the game tick takes too long
  137. if(lastFrame + nanosPerFrame - 1000000 < glfwGetTimerValue())
  138. {
  139. lastFrame = glfwGetTimerValue();
  140. game.renderTick(renderer, (float) lag / nanosPerTick);
  141. tpsTimer.draw(renderer);
  142. fpsTimer.draw(renderer);
  143. fpsTimer.update();
  144. glfwSwapBuffers(window);
  145. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  146. }
  147. }
  148. glfwPollEvents();
  149. }
  150. else
  151. {
  152. // wait until next frame
  153. long waitingTime = Math.min(nanosPerFrame - frameLag, nanosPerTick - lag);
  154. sleep(waitingTime);
  155. }
  156. }
  157. game.onStop();
  158. }
  159. public static void setNanosPerTick(long nanos)
  160. {
  161. nanosPerTick = nanos;
  162. tpsTimer.setExpectedValue(1_000_000_000l / nanos);
  163. }
  164. public static long getNanosPerTick()
  165. {
  166. return nanosPerTick;
  167. }
  168. public static double getTicksPerSecond()
  169. {
  170. return tpsTimer.getCallsPerSecond();
  171. }
  172. public static void setRenderTicksPerSecond(boolean active)
  173. {
  174. tpsTimer.setActive(active);
  175. }
  176. public static double getFramesPerSecond()
  177. {
  178. return fpsTimer.getCallsPerSecond();
  179. }
  180. public static void setMaxFramesPerSecond(int max)
  181. {
  182. nanosPerFrame = 1_000_000_000 / max;
  183. }
  184. public static void setRenderFramesPerSecond(boolean active)
  185. {
  186. fpsTimer.setActive(active);
  187. }
  188. }