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;
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);
116 this.fullscreen = fullscreen;
118 if (x != -1 && y != -1)
119 glfwSetWindowPos(window, x, y);
121 DisplayMode mode = getDesktopDisplayMode();
122 glfwSetWindowPos(window, (mode.width - width) / 2, (mode.height - height) / 2);
126 if (!mouseCaptured) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL); // Prevent fullscreen from taking mouse.
128 glfwMakeContextCurrent(newWindow);
130 if (visible) glfwShowWindow(window);
136 long time = System.nanoTime();
137 if (lastTime == -1) lastTime = time;
138 deltaTime = (time - lastTime) / 1000000000.0f;
141 if (time - frameStart >= 1000000000) {
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);
156 public boolean isGL11Available () {
160 public boolean isGL20Available () {
164 public GLCommon getGLCommon () {
168 public GL10 getGL10 () {
172 public GL11 getGL11 () {
176 public GL20 getGL20 () {
180 public int getWidth () {
181 return glfwGetWindowWidth(window);
184 public int getHeight () {
185 return glfwGetWindowHeight(window);
188 public float getDeltaTime () {
192 public float getRawDeltaTime () {
196 public int getFramesPerSecond () {
200 public GraphicsType getType () {
201 return GraphicsType.JGLFW;
204 public float getPpiX () {
205 // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) * 0.03937f);
206 return Toolkit.getDefaultToolkit().getScreenResolution();
209 public float getPpiY () {
210 // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) * 0.03937f);
211 return Toolkit.getDefaultToolkit().getScreenResolution();
214 public float getPpcX () {
215 // return getWidth() / (glfwGetMonitorPhysicalWidth(getWindowMonitor()) / 10);
216 return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
219 public float getPpcY () {
220 // return getHeight() / (glfwGetMonitorPhysicalHeight(getWindowMonitor()) / 10);
221 return Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f;
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;
236 public boolean supportsDisplayModeChange () {
240 private long getWindowMonitor () {
242 long monitor = glfwGetWindowMonitor(window);
243 if (monitor != 0) return monitor;
245 return glfwGetPrimaryMonitor();
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);
255 public DisplayMode getDesktopDisplayMode () {
256 GlfwVideoMode mode = glfwGetVideoMode(getWindowMonitor());
257 return new JglfwDisplayMode(mode.width, mode.height, 0, mode.redBits + mode.greenBits + mode.blueBits);
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);
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);
278 glfwSetWindowSize(window, width, height);
282 public void setTitle (String title) {
283 if (title == null) title = "";
284 glfwSetWindowTitle(window, title);
288 public void setVSync (boolean vsync) {
290 glfwSwapInterval(vsync ? 1 : 0);
293 public BufferFormat getBufferFormat () {
297 public boolean supportsExtension (String extension) {
298 return glfwExtensionSupported(extension);
301 public void setContinuousRendering (boolean isContinuous) {
302 this.isContinuous = isContinuous;
305 public boolean isContinuousRendering () {
309 public void requestRendering () {
310 renderRequested = true;
313 public boolean isFullscreen () {
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 () {
331 public void hide () {
333 glfwHideWindow(window);
336 public void show () {
338 glfwShowWindow(window);
340 Gdx.gl.glClearColor(initialBackgroundColor.r, initialBackgroundColor.g, initialBackgroundColor.b, initialBackgroundColor.a);
341 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
342 glfwSwapBuffers(window);
345 boolean shouldRender () {
347 return renderRequested || isContinuous;
349 renderRequested = false;
353 static class JglfwDisplayMode extends DisplayMode {
354 protected JglfwDisplayMode (int width, int height, int refreshRate, int bitsPerPixel) {
355 super(width, height, refreshRate, bitsPerPixel);