DisplayManager.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package eu.tankernn.gameEngine.renderEngine;
  2. import java.awt.Dimension;
  3. import java.awt.Toolkit;
  4. import org.lwjgl.input.Keyboard;
  5. import org.lwjgl.opengl.GL11;
  6. import org.lwjgl.LWJGLException;
  7. import org.lwjgl.Sys;
  8. import org.lwjgl.opengl.ContextAttribs;
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.DisplayMode;
  11. import org.lwjgl.opengl.GL13;
  12. import org.lwjgl.opengl.PixelFormat;
  13. import eu.tankernn.gameEngine.settings.Settings;
  14. /**
  15. * Handles the OpenGL display.
  16. *
  17. * @author Frans
  18. */
  19. public class DisplayManager {
  20. private static final int WIDTH = 1600;
  21. private static final int HEIGHT = 900;
  22. private static final int FPS_CAP = 60;
  23. private static final int MULTISAMPLING = 8;
  24. private static long lastFrameTime;
  25. private static float delta;
  26. private static boolean fullscreen = false;
  27. /**
  28. * Creates a new display.
  29. */
  30. public static void createDisplay() {
  31. ContextAttribs attribs = new ContextAttribs(3, 3)
  32. .withForwardCompatible(true)
  33. .withProfileCore(true);
  34. try {
  35. setDisplayMode(WIDTH, HEIGHT, fullscreen);
  36. Display.setResizable(true);
  37. Display.create(new PixelFormat().withSamples(MULTISAMPLING).withDepthBits(24), attribs);
  38. GL11.glEnable(GL13.GL_MULTISAMPLE);
  39. } catch (LWJGLException e) {
  40. e.printStackTrace();
  41. }
  42. GL11.glViewport(0, 0, WIDTH, HEIGHT);
  43. lastFrameTime = getCurrentTime();
  44. Display.setTitle(Settings.GAME_NAME);
  45. }
  46. /**
  47. * Set the display mode to be used
  48. *
  49. * @param width The width of the display required
  50. * @param height The height of the display required
  51. * @param fullscreen True if we want fullscreen mode
  52. */
  53. public static void setDisplayMode(int width, int height, boolean fullscreen) {
  54. // return if requested DisplayMode is already set
  55. if ((Display.getDisplayMode().getWidth() == width) &&
  56. (Display.getDisplayMode().getHeight() == height) &&
  57. (Display.isFullscreen() == fullscreen)) {
  58. return;
  59. }
  60. try {
  61. DisplayMode targetDisplayMode = null;
  62. if (fullscreen) {
  63. DisplayMode[] modes = Display.getAvailableDisplayModes();
  64. int freq = 0;
  65. for (int i = 0; i < modes.length; i++) {
  66. DisplayMode current = modes[i];
  67. if ((current.getWidth() == width) && (current.getHeight() == height)) {
  68. if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
  69. if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
  70. targetDisplayMode = current;
  71. freq = targetDisplayMode.getFrequency();
  72. }
  73. }
  74. // if we've found a match for bpp and frequence against the
  75. // original display mode then it's probably best to go for this one
  76. // since it's most likely compatible with the monitor
  77. if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
  78. (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
  79. targetDisplayMode = current;
  80. break;
  81. }
  82. }
  83. }
  84. } else {
  85. targetDisplayMode = new DisplayMode(width, height);
  86. }
  87. if (targetDisplayMode == null) {
  88. System.out.println("Failed to find value mode: " + width + "x" + height + " fs=" + fullscreen);
  89. return;
  90. }
  91. Display.setDisplayMode(targetDisplayMode);
  92. Display.setFullscreen(fullscreen);
  93. Display.setResizable(!fullscreen);
  94. } catch (LWJGLException e) {
  95. System.out.println("Unable to setup mode " + width + "x" + height + " fullscreen=" + fullscreen + e);
  96. }
  97. }
  98. /**
  99. * Updates the display. Should be called every frame.
  100. */
  101. public static void updateDisplay() {
  102. if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
  103. fullscreen = !fullscreen;
  104. if (fullscreen) {
  105. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  106. int width = (int) screenSize.getWidth();
  107. int height = (int) screenSize.getHeight();
  108. setDisplayMode(width, height, fullscreen);
  109. } else {
  110. setDisplayMode(WIDTH, HEIGHT, fullscreen);
  111. }
  112. }
  113. Display.sync(FPS_CAP);
  114. Display.update();
  115. long currentFrameTime = getCurrentTime();
  116. delta = (currentFrameTime - lastFrameTime) / 1000f;
  117. lastFrameTime = currentFrameTime;
  118. }
  119. /**
  120. * Get the current tick length in seconds. Used to synchronize
  121. * time-dependent actions.
  122. *
  123. * @return Current tick length in seconds
  124. */
  125. public static float getFrameTimeSeconds() {
  126. return delta;
  127. }
  128. /**
  129. * Close the display. Call when game stops.
  130. */
  131. public static void closeDisplay() {
  132. Display.destroy();
  133. }
  134. private static long getCurrentTime() {
  135. return Sys.getTime() * 1000 / Sys.getTimerResolution();
  136. }
  137. }