|
@@ -1,5 +1,9 @@
|
|
|
package eu.tankernn.gameEngine.renderEngine;
|
|
|
|
|
|
+import java.awt.Dimension;
|
|
|
+import java.awt.Toolkit;
|
|
|
+
|
|
|
+import org.lwjgl.input.Keyboard;
|
|
|
import org.lwjgl.opengl.GL11;
|
|
|
import org.lwjgl.LWJGLException;
|
|
|
import org.lwjgl.Sys;
|
|
@@ -25,6 +29,7 @@ public class DisplayManager {
|
|
|
|
|
|
private static long lastFrameTime;
|
|
|
private static float delta;
|
|
|
+ private static boolean fullscreen = false;
|
|
|
|
|
|
/**
|
|
|
* Creates a new display.
|
|
@@ -35,7 +40,8 @@ public class DisplayManager {
|
|
|
.withProfileCore(true);
|
|
|
|
|
|
try {
|
|
|
- Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
|
|
|
+ setDisplayMode(WIDTH, HEIGHT, fullscreen);
|
|
|
+ Display.setResizable(true);
|
|
|
Display.create(new PixelFormat().withSamples(MULTISAMPLING).withDepthBits(24), attribs);
|
|
|
GL11.glEnable(GL13.GL_MULTISAMPLE);
|
|
|
} catch (LWJGLException e) {
|
|
@@ -48,10 +54,85 @@ public class DisplayManager {
|
|
|
Display.setTitle(Settings.GAME_NAME);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Set the display mode to be used
|
|
|
+ *
|
|
|
+ * @param width The width of the display required
|
|
|
+ * @param height The height of the display required
|
|
|
+ * @param fullscreen True if we want fullscreen mode
|
|
|
+ */
|
|
|
+ public static void setDisplayMode(int width, int height, boolean fullscreen) {
|
|
|
+
|
|
|
+ // return if requested DisplayMode is already set
|
|
|
+ if ((Display.getDisplayMode().getWidth() == width) &&
|
|
|
+ (Display.getDisplayMode().getHeight() == height) &&
|
|
|
+ (Display.isFullscreen() == fullscreen)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ DisplayMode targetDisplayMode = null;
|
|
|
+
|
|
|
+ if (fullscreen) {
|
|
|
+ DisplayMode[] modes = Display.getAvailableDisplayModes();
|
|
|
+ int freq = 0;
|
|
|
+
|
|
|
+ for (int i = 0; i < modes.length; i++) {
|
|
|
+ DisplayMode current = modes[i];
|
|
|
+
|
|
|
+ if ((current.getWidth() == width) && (current.getHeight() == height)) {
|
|
|
+ if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
|
|
|
+ if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
|
|
|
+ targetDisplayMode = current;
|
|
|
+ freq = targetDisplayMode.getFrequency();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // if we've found a match for bpp and frequence against the
|
|
|
+ // original display mode then it's probably best to go for this one
|
|
|
+ // since it's most likely compatible with the monitor
|
|
|
+ if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
|
|
|
+ (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
|
|
|
+ targetDisplayMode = current;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ targetDisplayMode = new DisplayMode(width, height);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (targetDisplayMode == null) {
|
|
|
+ System.out.println("Failed to find value mode: " + width + "x" + height + " fs=" + fullscreen);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Display.setDisplayMode(targetDisplayMode);
|
|
|
+ Display.setFullscreen(fullscreen);
|
|
|
+ Display.setResizable(!fullscreen);
|
|
|
+
|
|
|
+ } catch (LWJGLException e) {
|
|
|
+ System.out.println("Unable to setup mode " + width + "x" + height + " fullscreen=" + fullscreen + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Updates the display. Should be called every frame.
|
|
|
*/
|
|
|
public static void updateDisplay() {
|
|
|
+ if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
|
|
|
+ fullscreen = !fullscreen;
|
|
|
+
|
|
|
+ if (fullscreen) {
|
|
|
+ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
|
|
+ int width = (int) screenSize.getWidth();
|
|
|
+ int height = (int) screenSize.getHeight();
|
|
|
+ setDisplayMode(width, height, fullscreen);
|
|
|
+ } else {
|
|
|
+ setDisplayMode(WIDTH, HEIGHT, fullscreen);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
Display.sync(FPS_CAP);
|
|
|
Display.update();
|
|
|
long currentFrameTime = getCurrentTime();
|