DisplayManager.java 4.5 KB

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