2 package com.badlogic.gdx.backends.jglfw;
4 import static com.badlogic.jglfw.Glfw.*;
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;
19 import java.awt.Toolkit;
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;
27 private boolean fullscreen;
28 private long fullscreenMonitor;
30 private boolean resizable, undecorated;
31 private BufferFormat bufferFormat;
32 private boolean vSync;
33 private int x, y, width, height;
34 private boolean visible;
35 private Color initialBackgroundColor;
36 private volatile boolean isContinuous = true;
37 private volatile boolean renderRequested;
39 private float deltaTime;
40 private long frameStart, lastTime = -1;
41 private int frames, fps;
44 private JglfwGL10 gl10;
45 private JglfwGL11 gl11;
46 private JglfwGL20 gl20;
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);
52 resizable = config.resizable;
53 undecorated = config.undecorated;
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];
64 if (!createWindow(config.width, config.height, config.fullscreen)) {
65 throw new GdxRuntimeException("Unable to create window: " + config.width + "x" + config.height + ", fullscreen: "
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();
78 if (glMajorVersion == 1 && glMinorVersion < 5)
79 gl10 = new JglfwGL10();
81 gl11 = new JglfwGL11();
91 if (!config.hidden) show();
94 private boolean createWindow (int width, int height, boolean fullscreen) {
95 if (fullscreen && fullscreenMonitor == 0) fullscreenMonitor = getWindowMonitor();
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);
108 boolean mouseCaptured = window != 0 && glfwGetInputMode(window, GLFW_CURSOR_MODE) == GLFW_CURSOR_CAPTURED;
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);
115 this.width = Math.max(1, width);
116 this.height = Math.max(1, height);
118 this.fullscreen = fullscreen;
120 if (x == -1 || y == -1) {
121 DisplayMode mode = getDesktopDisplayMode();
122 x = (mode.width - width) / 2;
123 y = (mode.height - height) / 2;
125 glfwSetWindowPos(window, x, y);
128 if (!mouseCaptured) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL); // Prevent fullscreen from taking mouse.
130 glfwMakeContextCurrent(newWindow);
132 if (visible) glfwShowWindow(window);
138 long time = System.nanoTime();
139 if (lastTime == -1) lastTime = time;
140 deltaTime = (time - lastTime) / 1000000000.0f;
143 if (time - frameStart >= 1000000000) {
151 void sizeChanged (int width, int height) {
152 width = Math.max(1, width);
153 height = Math.max(1, height);
155 this.height = height;
156 Gdx.gl.glViewport(0, 0, width, height);
157 ApplicationListener listener = Gdx.app.getApplicationListener();
158 if (listener != null) listener.resize(width, height);
162 void positionChanged (int x, int y) {
167 public boolean isGL11Available () {
171 public boolean isGL20Available () {
175 public GLCommon getGLCommon () {
179 public GL10 getGL10 () {
183 public GL11 getGL11 () {
187 public GL20 getGL20 () {
191 public int getWidth () {
195 public int getHeight () {
199 public float getDeltaTime () {
203 public float getRawDeltaTime () {
207 public int getFramesPerSecond () {
211 public GraphicsType getType () {
212 return GraphicsType.JGLFW;
215 public float getPpiX () {
216 // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) * 0.03937f); // mm to inches
217 return Toolkit.getDefaultToolkit().getScreenResolution();
220 public float getPpiY () {
221 // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) * 0.03937f); // mm to inches
222 return Toolkit.getDefaultToolkit().getScreenResolution();
225 public float getPpcX () {
226 // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) / 10); // mm to cm
227 return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
230 public float getPpcY () {
231 // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) / 10); // mm to cm
232 return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
235 public float getDensity () {
236 // long monitor = getWindowMonitor();
237 // float mmWidth = glfwGetMonitorPhysicalWidth(monitor);
238 // float mmHeight = glfwGetMonitorPhysicalHeight(monitor);
239 // float inches = (float)Math.sqrt(mmWidth * mmWidth + mmHeight * mmHeight) * 0.03937f; // mm to inches
240 // float pixelWidth = getWidth();
241 // float pixelHeight = getHeight();
242 // float pixels = (float)Math.sqrt(pixelWidth * pixelWidth + pixelHeight * pixelHeight);
243 // float diagonalPpi = pixels / inches;
244 // return diagonalPpi / 160f;
245 return Toolkit.getDefaultToolkit().getScreenResolution() / 160f;
248 public boolean supportsDisplayModeChange () {
252 private long getWindowMonitor () {
254 long monitor = glfwGetWindowMonitor(window);
255 if (monitor != 0) return monitor;
257 return glfwGetPrimaryMonitor();
260 public DisplayMode[] getDisplayModes () {
261 Array<DisplayMode> modes = new Array();
262 for (GlfwVideoMode mode : glfwGetVideoModes(getWindowMonitor()))
263 modes.add(new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits));
264 return modes.toArray(DisplayMode.class);
267 public DisplayMode getDesktopDisplayMode () {
268 GlfwVideoMode mode = glfwGetVideoMode(getWindowMonitor());
269 return new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits);
272 public boolean setDisplayMode (DisplayMode displayMode) {
273 bufferFormat = new BufferFormat( //
274 displayMode.bitsPerPixel == 16 ? 5 : 8, //
275 displayMode.bitsPerPixel == 16 ? 6 : 8, //
276 displayMode.bitsPerPixel == 16 ? 6 : 8, //
277 bufferFormat.a, bufferFormat.depth, bufferFormat.stencil, bufferFormat.samples, false);
278 boolean success = createWindow(displayMode.width, displayMode.height, fullscreen);
279 if (success && fullscreen) sizeChanged(displayMode.width, displayMode.height);
283 public boolean setDisplayMode (int width, int height, boolean fullscreen) {
284 if (fullscreen || this.fullscreen) {
285 boolean success = createWindow(width, height, fullscreen);
286 if (success && fullscreen) sizeChanged(width, height);
290 glfwSetWindowSize(window, width, height);
294 public void setTitle (String title) {
295 if (title == null) title = "";
296 glfwSetWindowTitle(window, title);
300 public void setVSync (boolean vsync) {
302 glfwSwapInterval(vsync ? 1 : 0);
305 public BufferFormat getBufferFormat () {
309 public boolean supportsExtension (String extension) {
310 return glfwExtensionSupported(extension);
313 public void setContinuousRendering (boolean isContinuous) {
314 this.isContinuous = isContinuous;
317 public boolean isContinuousRendering () {
321 public void requestRendering () {
322 renderRequested = true;
325 public boolean isFullscreen () {
329 /** Returns the JGLFW window handle. Note this should not be stored externally as it may change if the window is recreated to
330 * enter/exit fullscreen. */
331 public long getWindow () {
343 public void setPosition (int x, int y) {
344 glfwSetWindowPos(window, x, y);
347 public void hide () {
349 glfwHideWindow(window);
352 public void show () {
354 glfwShowWindow(window);
356 Gdx.gl.glClearColor(initialBackgroundColor.r, initialBackgroundColor.g, initialBackgroundColor.b, initialBackgroundColor.a);
357 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
358 glfwSwapBuffers(window);
361 public boolean isMinimized () {
362 return glfwGetWindowParam(window, GLFW_ICONIFIED) == 1;
365 public void minimize () {
366 glfwIconifyWindow(window);
369 public void restore () {
370 glfwRestoreWindow(window);
373 boolean shouldRender () {
375 return renderRequested || isContinuous;
377 renderRequested = false;
381 static class JglfwDisplayMode extends DisplayMode {
382 protected JglfwDisplayMode (int width, int height, int refreshRate, int bitsPerPixel) {
383 super(width, height, refreshRate, bitsPerPixel);