OSDN Git Service

ran gdx-tools HeaderFixer tool
[mikumikustudio/libgdx-mikumikustudio.git] / backends / gdx-backend-jglfw / src / com / badlogic / gdx / backends / jglfw / JglfwGraphics.java
index 18255f2..56117e3 100644 (file)
@@ -1,10 +1,27 @@
+/*******************************************************************************\r
+ * Copyright 2011 See AUTHORS file.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *   http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ ******************************************************************************/
 
 package com.badlogic.gdx.backends.jglfw;
 
 import static com.badlogic.jglfw.Glfw.*;
 
+import com.badlogic.gdx.ApplicationListener;
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Graphics;
+import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.GL10;
 import com.badlogic.gdx.graphics.GL11;
 import com.badlogic.gdx.graphics.GL20;
@@ -22,23 +39,26 @@ public class JglfwGraphics implements Graphics {
        static int glMajorVersion, glMinorVersion;
 
        long window;
-       boolean fullscreen;
-       long fullscreenMonitor;
-       String title;
-       boolean resizable, undecorated;
-       BufferFormat bufferFormat;
-       boolean sync;
-       volatile boolean isContinuous = true;
-       volatile boolean requestRendering;
-
-       float deltaTime;
-       long frameStart, lastTime;
-       int frames, fps;
-
-       GLCommon gl;
-       JglfwGL10 gl10;
-       JglfwGL11 gl11;
-       JglfwGL20 gl20;
+       private boolean fullscreen;
+       private long fullscreenMonitor;
+       private String title;
+       private boolean resizable, undecorated;
+       private BufferFormat bufferFormat;
+       private boolean vSync;
+       private int x, y, width, height;
+       private boolean visible;
+       private Color initialBackgroundColor;
+       private volatile boolean isContinuous = true, renderRequested;
+       volatile boolean foreground, minimized;
+
+       private float deltaTime;
+       private long frameStart, lastTime = -1;
+       private int frames, fps;
+
+       private GLCommon gl;
+       private JglfwGL10 gl10;
+       private JglfwGL11 gl11;
+       private JglfwGL20 gl20;
 
        public JglfwGraphics (JglfwApplicationConfiguration config) {
                // Store values from config.
@@ -46,6 +66,10 @@ public class JglfwGraphics implements Graphics {
                title = config.title;
                resizable = config.resizable;
                undecorated = config.undecorated;
+               x = config.x;
+               y = config.y;
+               vSync = config.vSync;
+               initialBackgroundColor = config.initialBackgroundColor;
                if (config.fullscreenMonitorIndex != -1) { // Use monitor specified in config if it is valid.
                        long[] monitors = glfwGetMonitors();
                        if (config.fullscreenMonitorIndex < monitors.length) fullscreenMonitor = monitors[config.fullscreenMonitorIndex];
@@ -56,8 +80,6 @@ public class JglfwGraphics implements Graphics {
                        throw new GdxRuntimeException("Unable to create window: " + config.width + "x" + config.height + ", fullscreen: "
                                + config.fullscreen);
                }
-               if (config.x != -1 && config.y != -1) glfwSetWindowPos(window, config.x, config.y);
-               setVSync(config.vSync);
 
                // Create GL.
                String version = GL.glGetString(GL11.GL_VERSION);
@@ -68,9 +90,9 @@ public class JglfwGraphics implements Graphics {
                        gl = gl20;
                } else {
                        gl20 = null;
-                       if (glMajorVersion == 1 && glMinorVersion < 5) {
+                       if (glMajorVersion == 1 && glMinorVersion < 5)
                                gl10 = new JglfwGL10();
-                       else {
+                       else {
                                gl11 = new JglfwGL11();
                                gl10 = gl11;
                        }
@@ -80,6 +102,81 @@ public class JglfwGraphics implements Graphics {
                Gdx.gl10 = gl10;
                Gdx.gl11 = gl11;
                Gdx.gl20 = gl20;
+
+               if (!config.hidden) show();
+       }
+
+       private boolean createWindow (int width, int height, boolean fullscreen) {
+               if (fullscreen && fullscreenMonitor == 0) fullscreenMonitor = getWindowMonitor();
+
+               glfwWindowHint(GLFW_VISIBLE, 0);
+               glfwWindowHint(GLFW_RESIZABLE, resizable ? 1 : 0);
+               glfwWindowHint(GLFW_UNDECORATED, undecorated ? 1 : 0);
+               glfwWindowHint(GLFW_RED_BITS, bufferFormat.r);
+               glfwWindowHint(GLFW_GREEN_BITS, bufferFormat.g);
+               glfwWindowHint(GLFW_BLUE_BITS, bufferFormat.b);
+               glfwWindowHint(GLFW_ALPHA_BITS, bufferFormat.a);
+               glfwWindowHint(GLFW_DEPTH_BITS, bufferFormat.depth);
+               glfwWindowHint(GLFW_STENCIL_BITS, bufferFormat.stencil);
+               glfwWindowHint(GLFW_SAMPLES, bufferFormat.samples);
+
+               boolean mouseCaptured = window != 0 && glfwGetInputMode(window, GLFW_CURSOR_MODE) == GLFW_CURSOR_CAPTURED;
+
+               long oldWindow = window;
+               long newWindow = glfwCreateWindow(width, height, title, fullscreen ? fullscreenMonitor : 0, oldWindow);
+               if (newWindow == 0) return false;
+               if (oldWindow != 0) glfwDestroyWindow(oldWindow);
+               window = newWindow;
+               this.width = Math.max(1, width);
+               this.height = Math.max(1, height);
+
+               this.fullscreen = fullscreen;
+               if (!fullscreen) {
+                       if (x == -1 || y == -1) {
+                               DisplayMode mode = getDesktopDisplayMode();
+                               x = (mode.width - width) / 2;
+                               y = (mode.height - height) / 2;
+                       }
+                       glfwSetWindowPos(window, x, y);
+               }
+
+               if (!mouseCaptured) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL); // Prevent fullscreen from taking mouse.
+
+               glfwMakeContextCurrent(newWindow);
+               setVSync(vSync);
+               if (visible) glfwShowWindow(window);
+
+               return true;
+       }
+
+       void frameStart (long time) {
+               if (lastTime == -1) lastTime = time;
+               deltaTime = (time - lastTime) / 1000000000.0f;
+               lastTime = time;
+
+               if (time - frameStart >= 1000000000) {
+                       fps = frames;
+                       frames = 0;
+                       frameStart = time;
+               }
+               frames++;
+       }
+
+       void sizeChanged (int width, int height) {
+               glfwShowWindow(window); // This is required to refresh the NSOpenGLContext on OSX!
+               width = Math.max(1, width);
+               height = Math.max(1, height);
+               this.width = width;
+               this.height = height;
+               Gdx.gl.glViewport(0, 0, width, height);
+               ApplicationListener listener = Gdx.app.getApplicationListener();
+               if (listener != null) listener.resize(width, height);
+               requestRendering();
+       }
+
+       void positionChanged (int x, int y) {
+               this.x = x;
+               this.y = y;
        }
 
        public boolean isGL11Available () {
@@ -107,24 +204,11 @@ public class JglfwGraphics implements Graphics {
        }
 
        public int getWidth () {
-               return glfwGetWindowWidth(window);
+               return width;
        }
 
        public int getHeight () {
-               return glfwGetWindowHeight(window);
-       }
-
-       void updateTime () {
-               long time = System.nanoTime();
-               deltaTime = (time - lastTime) / 1000000000.0f;
-               lastTime = time;
-
-               if (time - frameStart >= 1000000000) {
-                       fps = frames;
-                       frames = 0;
-                       frameStart = time;
-               }
-               frames++;
+               return height;
        }
 
        public float getDeltaTime () {
@@ -144,22 +228,35 @@ public class JglfwGraphics implements Graphics {
        }
 
        public float getPpiX () {
+               // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) * 0.03937f); // mm to inches
                return Toolkit.getDefaultToolkit().getScreenResolution();
        }
 
        public float getPpiY () {
+               // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) * 0.03937f); // mm to inches
                return Toolkit.getDefaultToolkit().getScreenResolution();
        }
 
        public float getPpcX () {
+               // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) / 10); // mm to cm
                return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
        }
 
        public float getPpcY () {
+               // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) / 10); // mm to cm
                return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
        }
 
        public float getDensity () {
+               // long monitor = getWindowMonitor();
+               // float mmWidth = glfwGetMonitorPhysicalWidth(monitor);
+               // float mmHeight = glfwGetMonitorPhysicalHeight(monitor);
+               // float inches = (float)Math.sqrt(mmWidth * mmWidth + mmHeight * mmHeight) * 0.03937f; // mm to inches
+               // float pixelWidth = getWidth();
+               // float pixelHeight = getHeight();
+               // float pixels = (float)Math.sqrt(pixelWidth * pixelWidth + pixelHeight * pixelHeight);
+               // float diagonalPpi = pixels / inches;
+               // return diagonalPpi / 160f;
                return Toolkit.getDefaultToolkit().getScreenResolution() / 160f;
        }
 
@@ -167,15 +264,23 @@ public class JglfwGraphics implements Graphics {
                return true;
        }
 
+       private long getWindowMonitor () {
+               if (window != 0) {
+                       long monitor = glfwGetWindowMonitor(window);
+                       if (monitor != 0) return monitor;
+               }
+               return glfwGetPrimaryMonitor();
+       }
+
        public DisplayMode[] getDisplayModes () {
                Array<DisplayMode> modes = new Array();
-               for (GlfwVideoMode mode : glfwGetVideoModes(glfwGetWindowMonitor(window)))
+               for (GlfwVideoMode mode : glfwGetVideoModes(getWindowMonitor()))
                        modes.add(new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits));
                return modes.toArray(DisplayMode.class);
        }
 
        public DisplayMode getDesktopDisplayMode () {
-               GlfwVideoMode mode = glfwGetVideoMode(glfwGetWindowMonitor(window));
+               GlfwVideoMode mode = glfwGetVideoMode(getWindowMonitor());
                return new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits);
        }
 
@@ -185,44 +290,22 @@ public class JglfwGraphics implements Graphics {
                        displayMode.bitsPerPixel == 16 ? 6 : 8, //
                        displayMode.bitsPerPixel == 16 ? 6 : 8, //
                        bufferFormat.a, bufferFormat.depth, bufferFormat.stencil, bufferFormat.samples, false);
-               return createWindow(displayMode.width, displayMode.height, fullscreen);
+               boolean success = createWindow(displayMode.width, displayMode.height, fullscreen);
+               if (success && fullscreen) sizeChanged(displayMode.width, displayMode.height);
+               return success;
        }
 
        public boolean setDisplayMode (int width, int height, boolean fullscreen) {
-               if (window == 0 || fullscreen != this.fullscreen || this.fullscreen) return createWindow(width, height, fullscreen);
+               if (fullscreen || this.fullscreen) {
+                       boolean success = createWindow(width, height, fullscreen);
+                       if (success && fullscreen) sizeChanged(width, height);
+                       return success;
+               }
 
                glfwSetWindowSize(window, width, height);
                return true;
        }
 
-       private boolean createWindow (int width, int height, boolean fullscreen) {
-               if (fullscreenMonitor == 0) fullscreenMonitor = glfwGetPrimaryMonitor();
-
-               glfwWindowHint(GLFW_RESIZABLE, resizable ? 1 : 0);
-               glfwWindowHint(GLFW_UNDECORATED, undecorated ? 1 : 0);
-               glfwWindowHint(GLFW_RED_BITS, bufferFormat.r);
-               glfwWindowHint(GLFW_GREEN_BITS, bufferFormat.g);
-               glfwWindowHint(GLFW_BLUE_BITS, bufferFormat.b);
-               glfwWindowHint(GLFW_ALPHA_BITS, bufferFormat.a);
-               glfwWindowHint(GLFW_DEPTH_BITS, bufferFormat.depth);
-               glfwWindowHint(GLFW_STENCIL_BITS, bufferFormat.stencil);
-               glfwWindowHint(GLFW_SAMPLES, bufferFormat.samples);
-
-               boolean mouseCaptured = window != 0 && glfwGetInputMode(window, GLFW_CURSOR_MODE) == GLFW_CURSOR_CAPTURED;
-
-               long oldWindow = window;
-               long newWindow = glfwCreateWindow(width, height, title, fullscreen ? fullscreenMonitor : 0, oldWindow);
-               if (newWindow == 0) return false;
-               if (oldWindow != 0) glfwDestroyWindow(oldWindow);
-               glfwMakeContextCurrent(newWindow);
-               window = newWindow;
-               this.fullscreen = fullscreen;
-
-               if (!mouseCaptured) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL); // Prevent fullscreen from taking mouse.
-
-               return true;
-       }
-
        public void setTitle (String title) {
                if (title == null) title = "";
                glfwSetWindowTitle(window, title);
@@ -230,7 +313,7 @@ public class JglfwGraphics implements Graphics {
        }
 
        public void setVSync (boolean vsync) {
-               this.sync = vsync;
+               this.vSync = vsync;
                glfwSwapInterval(vsync ? 1 : 0);
        }
 
@@ -251,9 +334,7 @@ public class JglfwGraphics implements Graphics {
        }
 
        public void requestRendering () {
-               synchronized (this) {
-                       requestRendering = true;
-               }
+               renderRequested = true;
        }
 
        public boolean isFullscreen () {
@@ -266,11 +347,57 @@ public class JglfwGraphics implements Graphics {
                return window;
        }
 
+       public int getX () {
+               return x;
+       }
+
+       public int getY () {
+               return y;
+       }
+
+       public void setPosition (int x, int y) {
+               glfwSetWindowPos(window, x, y);
+       }
+
+       public void hide () {
+               visible = false;
+               glfwHideWindow(window);
+       }
+
+       public void show () {
+               visible = true;
+               glfwShowWindow(window);
+
+               Gdx.gl.glClearColor(initialBackgroundColor.r, initialBackgroundColor.g, initialBackgroundColor.b, initialBackgroundColor.a);
+               Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
+               glfwSwapBuffers(window);
+       }
+
+       public boolean isHidden () {
+               return !visible;
+       }
+
+       public boolean isMinimized () {
+               return minimized;
+       }
+
+       public boolean isForeground () {
+               return foreground;
+       }
+
+       public void minimize () {
+               glfwIconifyWindow(window);
+       }
+
+       public void restore () {
+               glfwRestoreWindow(window);
+       }
+
        boolean shouldRender () {
-               synchronized (this) {
-                       boolean requestRendering = this.requestRendering;
-                       this.requestRendering = false;
-                       return requestRendering || isContinuous;
+               try {
+                       return renderRequested || isContinuous;
+               } finally {
+                       renderRequested = false;
                }
        }
 
@@ -279,4 +406,4 @@ public class JglfwGraphics implements Graphics {
                        super(width, height, refreshRate, bitsPerPixel);
                }
        }
-}
+}
\ No newline at end of file