OSDN Git Service

Eh, guess we'll roll with the Toolkit PPI. :/
[mikumikustudio/libgdx-mikumikustudio.git] / backends / gdx-backend-jglfw / src / com / badlogic / gdx / backends / jglfw / JglfwGraphics.java
1
2 package com.badlogic.gdx.backends.jglfw;
3
4 import static com.badlogic.jglfw.Glfw.*;
5
6 import com.badlogic.gdx.ApplicationListener;
7 import com.badlogic.gdx.Gdx;
8 import com.badlogic.gdx.Graphics;
9 import com.badlogic.gdx.graphics.Color;
10 import com.badlogic.gdx.graphics.GL10;
11 import com.badlogic.gdx.graphics.GL11;
12 import com.badlogic.gdx.graphics.GL20;
13 import com.badlogic.gdx.graphics.GLCommon;
14 import com.badlogic.gdx.utils.Array;
15 import com.badlogic.gdx.utils.GdxRuntimeException;
16 import com.badlogic.jglfw.GlfwVideoMode;
17 import com.badlogic.jglfw.gl.GL;
18
19 import java.awt.Toolkit;
20
21 /** An implementation of the {@link Graphics} interface based on GLFW.
22  * @author Nathan Sweet */
23 public class JglfwGraphics implements Graphics {
24         static int glMajorVersion, glMinorVersion;
25
26         long window;
27         private boolean fullscreen;
28         private long fullscreenMonitor;
29         private String title;
30         private boolean resizable, undecorated;
31         private BufferFormat bufferFormat;
32         private boolean vSync;
33         int x, y;
34         private boolean visible;
35         private Color initialBackgroundColor;
36         private volatile boolean isContinuous = true;
37         private volatile boolean renderRequested;
38
39         private float deltaTime;
40         private long frameStart, lastTime = -1;
41         private int frames, fps;
42
43         private GLCommon gl;
44         private JglfwGL10 gl10;
45         private JglfwGL11 gl11;
46         private JglfwGL20 gl20;
47
48         public JglfwGraphics (JglfwApplicationConfiguration config) {
49                 // Store values from config.
50                 bufferFormat = new BufferFormat(config.r, config.g, config.b, config.a, config.depth, config.stencil, config.samples, false);
51                 title = config.title;
52                 resizable = config.resizable;
53                 undecorated = config.undecorated;
54                 x = config.x;
55                 y = config.y;
56                 vSync = config.vSync;
57                 initialBackgroundColor = config.initialBackgroundColor;
58                 if (config.fullscreenMonitorIndex != -1) { // Use monitor specified in config if it is valid.
59                         long[] monitors = glfwGetMonitors();
60                         if (config.fullscreenMonitorIndex < monitors.length) fullscreenMonitor = monitors[config.fullscreenMonitorIndex];
61                 }
62
63                 // Create window.
64                 if (!createWindow(config.width, config.height, config.fullscreen)) {
65                         throw new GdxRuntimeException("Unable to create window: " + config.width + "x" + config.height + ", fullscreen: "
66                                 + config.fullscreen);
67                 }
68
69                 // Create GL.
70                 String version = GL.glGetString(GL11.GL_VERSION);
71                 glMajorVersion = Integer.parseInt("" + version.charAt(0));
72                 glMinorVersion = Integer.parseInt("" + version.charAt(2));
73                 if (config.useGL20 && (glMajorVersion >= 2 || version.contains("2.1"))) { // special case for MESA, wtf...
74                         gl20 = new JglfwGL20();
75                         gl = gl20;
76                 } else {
77                         gl20 = null;
78                         if (glMajorVersion == 1 && glMinorVersion < 5)
79                                 gl10 = new JglfwGL10();
80                         else {
81                                 gl11 = new JglfwGL11();
82                                 gl10 = gl11;
83                         }
84                         gl = gl10;
85                 }
86                 Gdx.gl = gl;
87                 Gdx.gl10 = gl10;
88                 Gdx.gl11 = gl11;
89                 Gdx.gl20 = gl20;
90
91                 if (!config.hidden) show();
92         }
93
94         private boolean createWindow (int width, int height, boolean fullscreen) {
95                 if (fullscreen && fullscreenMonitor == 0) fullscreenMonitor = getWindowMonitor();
96
97                 glfwWindowHint(GLFW_VISIBLE, 0);
98                 glfwWindowHint(GLFW_RESIZABLE, resizable ? 1 : 0);
99                 glfwWindowHint(GLFW_UNDECORATED, undecorated ? 1 : 0);
100                 glfwWindowHint(GLFW_RED_BITS, bufferFormat.r);
101                 glfwWindowHint(GLFW_GREEN_BITS, bufferFormat.g);
102                 glfwWindowHint(GLFW_BLUE_BITS, bufferFormat.b);
103                 glfwWindowHint(GLFW_ALPHA_BITS, bufferFormat.a);
104                 glfwWindowHint(GLFW_DEPTH_BITS, bufferFormat.depth);
105                 glfwWindowHint(GLFW_STENCIL_BITS, bufferFormat.stencil);
106                 glfwWindowHint(GLFW_SAMPLES, bufferFormat.samples);
107
108                 boolean mouseCaptured = window != 0 && glfwGetInputMode(window, GLFW_CURSOR_MODE) == GLFW_CURSOR_CAPTURED;
109
110                 long oldWindow = window;
111                 long newWindow = glfwCreateWindow(width, height, title, fullscreen ? fullscreenMonitor : 0, oldWindow);
112                 if (newWindow == 0) return false;
113                 if (oldWindow != 0) glfwDestroyWindow(oldWindow);
114                 window = newWindow;
115
116                 this.fullscreen = fullscreen;
117                 if (!fullscreen) {
118                         if (x != -1 && y != -1)
119                                 glfwSetWindowPos(window, x, y);
120                         else {
121                                 DisplayMode mode = getDesktopDisplayMode();
122                                 glfwSetWindowPos(window, (mode.width - width) / 2, (mode.height - height) / 2);
123                         }
124                 }
125
126                 if (!mouseCaptured) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL); // Prevent fullscreen from taking mouse.
127
128                 glfwMakeContextCurrent(newWindow);
129                 setVSync(vSync);
130                 if (visible) glfwShowWindow(window);
131
132                 return true;
133         }
134
135         void frameStart () {
136                 long time = System.nanoTime();
137                 if (lastTime == -1) lastTime = time;
138                 deltaTime = (time - lastTime) / 1000000000.0f;
139                 lastTime = time;
140
141                 if (time - frameStart >= 1000000000) {
142                         fps = frames;
143                         frames = 0;
144                         frameStart = time;
145                 }
146                 frames++;
147         }
148
149         void resized (int width, int height) {
150                 Gdx.gl.glViewport(0, 0, width, height);
151                 ApplicationListener listener = Gdx.app.getApplicationListener();
152                 if (listener != null) listener.resize(width, height);
153                 requestRendering();
154         }
155
156         public boolean isGL11Available () {
157                 return gl11 != null;
158         }
159
160         public boolean isGL20Available () {
161                 return gl20 != null;
162         }
163
164         public GLCommon getGLCommon () {
165                 return gl;
166         }
167
168         public GL10 getGL10 () {
169                 return gl10;
170         }
171
172         public GL11 getGL11 () {
173                 return gl11;
174         }
175
176         public GL20 getGL20 () {
177                 return gl20;
178         }
179
180         public int getWidth () {
181                 return glfwGetWindowWidth(window);
182         }
183
184         public int getHeight () {
185                 return glfwGetWindowHeight(window);
186         }
187
188         public float getDeltaTime () {
189                 return deltaTime;
190         }
191
192         public float getRawDeltaTime () {
193                 return deltaTime;
194         }
195
196         public int getFramesPerSecond () {
197                 return fps;
198         }
199
200         public GraphicsType getType () {
201                 return GraphicsType.JGLFW;
202         }
203
204         public float getPpiX () {
205                 // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) * 0.03937f);
206                 return Toolkit.getDefaultToolkit().getScreenResolution();
207         }
208
209         public float getPpiY () {
210                 // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) * 0.03937f);
211                 return Toolkit.getDefaultToolkit().getScreenResolution();
212         }
213
214         public float getPpcX () {
215                 // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) / 10);
216                 return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
217         }
218
219         public float getPpcY () {
220                 // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) / 10);
221                 return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
222         }
223
224         public float getDensity () {
225                 long monitor = getWindowMonitor();
226                 float mmWidth = glfwGetMonitorPhysicalWidth(monitor);
227                 float mmHeight = glfwGetMonitorPhysicalHeight(monitor);
228                 float inches = (float)Math.sqrt(mmWidth * mmWidth + mmHeight * mmHeight) * 0.03937f;
229                 float pixelWidth = getWidth();
230                 float pixelHeight = getHeight();
231                 float pixels = (float)Math.sqrt(pixelWidth * pixelWidth + pixelHeight * pixelHeight);
232                 float diagonalPpi = pixels / inches;
233                 return diagonalPpi / 160f;
234         }
235
236         public boolean supportsDisplayModeChange () {
237                 return true;
238         }
239
240         private long getWindowMonitor () {
241                 if (window != 0) {
242                         long monitor = glfwGetWindowMonitor(window);
243                         if (monitor != 0) return monitor;
244                 }
245                 return glfwGetPrimaryMonitor();
246         }
247
248         public DisplayMode[] getDisplayModes () {
249                 Array<DisplayMode> modes = new Array();
250                 for (GlfwVideoMode mode : glfwGetVideoModes(getWindowMonitor()))
251                         modes.add(new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits));
252                 return modes.toArray(DisplayMode.class);
253         }
254
255         public DisplayMode getDesktopDisplayMode () {
256                 GlfwVideoMode mode = glfwGetVideoMode(getWindowMonitor());
257                 return new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits);
258         }
259
260         public boolean setDisplayMode (DisplayMode displayMode) {
261                 bufferFormat = new BufferFormat( //
262                         displayMode.bitsPerPixel == 16 ? 5 : 8, //
263                         displayMode.bitsPerPixel == 16 ? 6 : 8, //
264                         displayMode.bitsPerPixel == 16 ? 6 : 8, //
265                         bufferFormat.a, bufferFormat.depth, bufferFormat.stencil, bufferFormat.samples, false);
266                 boolean success = createWindow(displayMode.width, displayMode.height, fullscreen);
267                 if (success && fullscreen) resized(displayMode.width, displayMode.height);
268                 return success;
269         }
270
271         public boolean setDisplayMode (int width, int height, boolean fullscreen) {
272                 if (fullscreen || this.fullscreen) {
273                         boolean success = createWindow(width, height, fullscreen);
274                         if (success && fullscreen) resized(width, height);
275                         return success;
276                 }
277
278                 glfwSetWindowSize(window, width, height);
279                 return true;
280         }
281
282         public void setTitle (String title) {
283                 if (title == null) title = "";
284                 glfwSetWindowTitle(window, title);
285                 this.title = title;
286         }
287
288         public void setVSync (boolean vsync) {
289                 this.vSync = vsync;
290                 glfwSwapInterval(vsync ? 1 : 0);
291         }
292
293         public BufferFormat getBufferFormat () {
294                 return bufferFormat;
295         }
296
297         public boolean supportsExtension (String extension) {
298                 return glfwExtensionSupported(extension);
299         }
300
301         public void setContinuousRendering (boolean isContinuous) {
302                 this.isContinuous = isContinuous;
303         }
304
305         public boolean isContinuousRendering () {
306                 return isContinuous;
307         }
308
309         public void requestRendering () {
310                 renderRequested = true;
311         }
312
313         public boolean isFullscreen () {
314                 return fullscreen;
315         }
316
317         /** Returns the JGLFW window handle. Note this should not be stored externally as it may change if the window is recreated to
318          * enter/exit fullscreen. */
319         public long getWindow () {
320                 return window;
321         }
322
323         public int getX () {
324                 return x;
325         }
326
327         public int getY () {
328                 return y;
329         }
330
331         public void hide () {
332                 visible = false;
333                 glfwHideWindow(window);
334         }
335
336         public void show () {
337                 visible = true;
338                 glfwShowWindow(window);
339
340                 Gdx.gl.glClearColor(initialBackgroundColor.r, initialBackgroundColor.g, initialBackgroundColor.b, initialBackgroundColor.a);
341                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
342                 glfwSwapBuffers(window);
343         }
344
345         boolean shouldRender () {
346                 try {
347                         return renderRequested || isContinuous;
348                 } finally {
349                         renderRequested = false;
350                 }
351         }
352
353         static class JglfwDisplayMode extends DisplayMode {
354                 protected JglfwDisplayMode (int width, int height, int refreshRate, int bitsPerPixel) {
355                         super(width, height, refreshRate, bitsPerPixel);
356                 }
357         }
358 }