Engine.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 Timer fpsTimer;
  14. private static Timer tpsTimer;
  15. private static Renderer renderer;
  16. private Engine()
  17. {
  18. }
  19. public static void start(IGame game)
  20. {
  21. loop(game);
  22. glfwFreeCallbacks(window);
  23. glfwDestroyWindow(window);
  24. glfwTerminate();
  25. GLFWErrorCallback error = glfwSetErrorCallback(null);
  26. if(error != null)
  27. {
  28. error.free();
  29. }
  30. }
  31. public static void stop()
  32. {
  33. glfwSetWindowShouldClose(window, true);
  34. }
  35. public static void init(String name, int width, int height)
  36. {
  37. GLFWErrorCallback.createPrint(System.err).set();
  38. if(!glfwInit())
  39. {
  40. throw new IllegalStateException("Unable to initialize GLFW");
  41. }
  42. glfwDefaultWindowHints();
  43. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  44. glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
  45. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  46. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  47. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  48. window = glfwCreateWindow(width, height, name, NULL, NULL);
  49. if(window == NULL)
  50. {
  51. throw new IllegalStateException("Failed to create the GLFW window");
  52. }
  53. glfwSetKeyCallback(window, (w, key, scancode, action, mods) ->
  54. {
  55. if(action == GLFW_RELEASE)
  56. {
  57. KeyHandler.onKeyUpEvent(key);
  58. }
  59. else if(action == GLFW_PRESS)
  60. {
  61. KeyHandler.onKeyDownEvent(key);
  62. }
  63. });
  64. glfwMakeContextCurrent(window);
  65. glfwSwapInterval(2);
  66. GL.createCapabilities();
  67. renderer = new Renderer(width, height);
  68. fpsTimer = new Timer(60, 0.0f);
  69. tpsTimer = new Timer(1_000_000_000l / nanosPerTick, height * 0.25f);
  70. glfwSetFramebufferSizeCallback(window, (w, fwidth, fheight) ->
  71. {
  72. glViewport(0, 0, fwidth, fheight);
  73. renderer.setViewPort(fwidth, fheight);
  74. });
  75. glfwShowWindow(window);
  76. }
  77. private static void loop(IGame game)
  78. {
  79. long oldTime = System.nanoTime();
  80. long lag = 0;
  81. while(!glfwWindowShouldClose(window))
  82. {
  83. long newTime = System.nanoTime();
  84. lag += newTime - oldTime;
  85. oldTime = newTime;
  86. int ticksPerFrame = 0;
  87. while(lag >= nanosPerTick)
  88. {
  89. lag -= nanosPerTick;
  90. tpsTimer.update();
  91. GamepadHandler.tick();
  92. KeyHandler.tick();
  93. game.tick();
  94. ticksPerFrame++;
  95. if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
  96. {
  97. long skip = lag / nanosPerTick;
  98. lag -= skip * nanosPerTick;
  99. if(skip > 0)
  100. {
  101. System.out.println(String.format("skipped %d game ticks %d", skip, lag));
  102. }
  103. break;
  104. }
  105. }
  106. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  107. game.renderTick(renderer, (float) lag / nanosPerTick);
  108. tpsTimer.draw(renderer);
  109. fpsTimer.draw(renderer);
  110. fpsTimer.update();
  111. glfwSwapBuffers(window);
  112. glfwPollEvents();
  113. }
  114. game.onStop();
  115. }
  116. public static void setNanosPerTick(long nanos)
  117. {
  118. nanosPerTick = nanos;
  119. tpsTimer.setExpectedValue(1_000_000_000l / nanos);
  120. }
  121. public static long getNanosPerTick()
  122. {
  123. return nanosPerTick;
  124. }
  125. public static double getTicksPerSecond()
  126. {
  127. return tpsTimer.getCallsPerSecond();
  128. }
  129. public static void setRenderTicksPerSecond(boolean active)
  130. {
  131. tpsTimer.setActive(active);
  132. }
  133. public static double getFramesPerSecond()
  134. {
  135. return fpsTimer.getCallsPerSecond();
  136. }
  137. public static void setRenderFramesPerSecond(boolean active)
  138. {
  139. fpsTimer.setActive(active);
  140. }
  141. }