aboutsummaryrefslogtreecommitdiff
path: root/backends/gdx-backend-lwjgl3/src/com/badlogic/gdx/backends/lwjgl3/Lwjgl3ApplicationConfiguration.java
blob: 08ae39898d266d9c0762a7cd7283e85605af98d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.backends.lwjgl3;

import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWVidMode.Buffer;
import org.lwjgl.opengl.GL;

import com.badlogic.gdx.Audio;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.Graphics.DisplayMode;
import com.badlogic.gdx.Graphics.Monitor;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.Lwjgl3DisplayMode;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.Lwjgl3Monitor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.HdpiUtils;

public class Lwjgl3ApplicationConfiguration {
	boolean disableAudio = false;
	int audioDeviceSimultaneousSources = 16;
	int audioDeviceBufferSize = 512;
	int audioDeviceBufferCount = 9;

	boolean useGL30 = false;
	int gles30ContextMajorVersion = 3;
	int gles30ContextMinorVersion = 2;

	int r = 8, g = 8, b = 8, a = 8;
	int depth = 16, stencil = 0;
	int samples = 0;

	int windowX = -1;
	int windowY = -1;
	int windowWidth = 640;
	int windowHeight = 480;
	int windowMinWidth = -1, windowMinHeight = -1, windowMaxWidth = -1, windowMaxHeight = -1;
	boolean windowResizable = true;
	boolean windowDecorated = true;
	Lwjgl3WindowListener windowListener;
	Lwjgl3DisplayMode fullscreenMode;

	boolean vSyncEnabled = true;
	String title = "";
	Color initialBackgroundColor = Color.BLACK;

	String preferencesDirectory = ".prefs/";
	Files.FileType preferencesFileType = FileType.External;

	HdpiMode hdpiMode = HdpiMode.Logical;
	
	static Lwjgl3ApplicationConfiguration copy(Lwjgl3ApplicationConfiguration config) {
		Lwjgl3ApplicationConfiguration copy = new Lwjgl3ApplicationConfiguration();
		copy.disableAudio = config.disableAudio;
		copy.audioDeviceSimultaneousSources = config.audioDeviceSimultaneousSources;
		copy.audioDeviceBufferSize = config.audioDeviceBufferSize;
		copy.audioDeviceBufferCount = config.audioDeviceBufferCount;
		copy.useGL30 = config.useGL30;
		copy.gles30ContextMajorVersion = config.gles30ContextMajorVersion;
		copy.gles30ContextMinorVersion = config.gles30ContextMinorVersion;
		copy.r = config.r;
		copy.g = config.g;
		copy.b = config.b;
		copy.a = config.a;
		copy.depth = config.depth;
		copy.stencil = config.stencil;
		copy.samples = config.samples;
		copy.windowX = config.windowX;
		copy.windowY = config.windowY;
		copy.windowWidth = config.windowWidth;
		copy.windowHeight = config.windowHeight;
		copy.windowMinWidth = config.windowMinWidth;
		copy.windowMinHeight = config.windowMinHeight;
		copy.windowMaxWidth = config.windowMaxWidth;
		copy.windowMaxHeight = config.windowMaxHeight;
		copy.windowResizable = config.windowResizable;
		copy.windowDecorated = config.windowDecorated;
		copy.windowListener = config.windowListener;
		copy.fullscreenMode = config.fullscreenMode;
		copy.vSyncEnabled = config.vSyncEnabled;
		copy.title = config.title;
		copy.initialBackgroundColor = config.initialBackgroundColor;
		copy.preferencesDirectory = config.preferencesDirectory;
		copy.preferencesFileType = config.preferencesFileType;
		copy.hdpiMode = config.hdpiMode;
		return copy;
	}
	

	/**
	 * Whether to disable audio or not. If set to false, the returned audio
	 * class instances like {@link Audio} or {@link Music} will be mock
	 * implementations.
	 */
	public void disableAudio(boolean disableAudio) {
		this.disableAudio = disableAudio;
	}

	/**
	 * Sets the audio device configuration.
	 * 
	 * @param simultaniousSources
	 *            the maximum number of sources that can be played
	 *            simultaniously (default 16)
	 * @param bufferSize
	 *            the audio device buffer size in samples (default 512)
	 * @param bufferCount
	 *            the audio device buffer count (default 9)
	 */
	public void setAudioConfig(int simultaniousSources, int bufferSize, int bufferCount) {
		this.audioDeviceSimultaneousSources = simultaniousSources;
		this.audioDeviceBufferSize = bufferSize;
		this.audioDeviceBufferCount = bufferCount;
	}

	/**
	 * Sets whether to use OpenGL ES 3.0 emulation. If the given major/minor
	 * version is not supported, the backend falls back to OpenGL ES 2.0
	 * emulation. The default parameters for major and minor should be 3 and 2
	 * respectively to be compatible with Mac OS X. Specifying major version 4
	 * and minor version 2 will ensure that all OpenGL ES 3.0 features are
	 * supported. Note however that Mac OS X does only support 3.2.
	 * 
	 * @see <a href=
	 *      "http://legacy.lwjgl.org/javadoc/org/lwjgl/opengl/ContextAttribs.html">
	 *      LWJGL OSX ContextAttribs note
	 * 
	 * @param useGL30
	 *            whether to use OpenGL ES 3.0
	 * @param gles3MajorVersion
	 *            OpenGL ES major version, use 3 as default
	 * @param gles3MinorVersion
	 *            OpenGL ES minor version, use 2 as default
	 */
	public void useOpenGL3(boolean useGL30, int gles3MajorVersion, int gles3MinorVersion) {
		this.useGL30 = useGL30;
		this.gles30ContextMajorVersion = gles3MajorVersion;
		this.gles30ContextMinorVersion = gles3MinorVersion;
	}

	/**
	 * Sets the bit depth of the color, depth and stencil buffer as well as
	 * multi-sampling.
	 * 
	 * @param r
	 *            red bits (default 8)
	 * @param g
	 *            green bits (default 8)
	 * @param b
	 *            blue bits (default 8)
	 * @param a
	 *            alpha bits (default 8)
	 * @param depth
	 *            depth bits (default 16)
	 * @param stencil
	 *            stencil bits (default 0)
	 * @param samples
	 *            MSAA samples (default 0)
	 */
	public void setBackBufferConfig(int r, int g, int b, int a, int depth, int stencil, int samples) {
		this.r = r;
		this.g = g;
		this.b = b;
		this.a = a;
		this.depth = depth;
		this.stencil = stencil;
		this.samples = samples;
	}

	/**
	 * Sets the app to use windowed mode.
	 * 
	 * @param width
	 *            the width of the window (default 640)
	 * @param height
	 *            the height of the window (default 480)
	 */
	public void setWindowedMode(int width, int height) {
		this.windowWidth = width;
		this.windowHeight = height;		
	}
	
	/** 
	 * @param resizable whether the windowed mode window is resizable (default true)
	 */
	public void setResizable(boolean resizable) {
		this.windowResizable = resizable;
	}
	
	/**
	 * @param decorated whether the windowed mode window is decorated, i.e. displaying the title bars (default true)
	 */
	public void setDecorated(boolean decorated) {
		this.windowDecorated = decorated;
	}
	
	/**
	 * Sets the position of the window in windowed mode on the
	 * primary monitor. Default -1 for booth coordinates for centered.
	 */
	public void setWindowPosition(int x, int y) {
		windowX = x;
		windowY = y;
	}
	
	/**
	 * Sets minimum and maximum size limits for the window. If the window is full screen or not resizable, these 
	 * limits are ignored. The default for all four parameters is -1, which means unrestricted.
	 */
	public void setWindowSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight) {
		windowMinWidth = minWidth;
		windowMinHeight = minHeight;
		windowMaxWidth = maxWidth;
		windowMaxHeight = maxHeight;
	}
	
	/**
	 * Sets the {@link Lwjgl3WindowListener} which will be informed about
	 * iconficiation, focus loss and window close events.
	 */
	public void setWindowListener(Lwjgl3WindowListener windowListener) {
		this.windowListener = windowListener;
	}

	/**
	 * Sets the app to use fullscreen mode. Use the static methods like
	 * {@link #getDisplayMode()} on this class to enumerate connected monitors
	 * and their fullscreen display modes.
	 */
	public void setFullscreenMode(DisplayMode mode) {
		this.fullscreenMode = (Lwjgl3DisplayMode)mode;
	}

	/**
	 * Sets whether to use vsync. This setting can be changed anytime at runtime
	 * via {@link Graphics#setVSync(boolean)}.
	 */
	public void useVsync(boolean vsync) {
		this.vSyncEnabled = vsync;
	}

	/**
	 * Sets the window title. Defaults to empty string.
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * Sets the initial background color. Defaults to black.
	 */
	public void setInitialBackgroundColor(Color color) {
		initialBackgroundColor = color;
	}

	/**
	 * Sets the directory where {@link Preferences} will be stored, as well as
	 * the file type to be used to store them. Defaults to "$USER_HOME/.prefs/"
	 * and {@link FileType#External}.
	 */
	public void setPreferencesConfig(String preferencesDirectory, Files.FileType preferencesFileType) {
		this.preferencesDirectory = preferencesDirectory;
		this.preferencesFileType = preferencesFileType;
	}

	/**
	 * Defines how HDPI monitors are handled. Operating systems may have a
	 * per-monitor HDPI scale setting. The operating system may report window
	 * width/height and mouse coordinates in a logical coordinate system at a
	 * lower resolution than the actual physical resolution. This setting allows
	 * you to specify whether you want to work in logical or raw pixel units.
	 * See {@link HdpiMode} for more information. Note that some OpenGL
	 * functions like {@link GL#glViewport()} and {@link GL#glScissor()} require
	 * raw pixel units. Use {@link HdpiUtils} to help with the conversion if
	 * HdpiMode is set to {@link HdpiMode#Logical}. Defaults to {@link HdpiMode#Logical}.
	 */
	public void setHdpiMode(HdpiMode mode) {
		this.hdpiMode = mode;
	}	

	/**
	 * @return the currently active {@link DisplayMode} of the primary monitor
	 */
	public static DisplayMode getDisplayMode() {
		Lwjgl3Application.initializeGlfw();
		GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
		return new Lwjgl3Graphics.Lwjgl3DisplayMode(GLFW.glfwGetPrimaryMonitor(), videoMode.width(), videoMode.height(), videoMode.refreshRate(),
				videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
	}
	
	/**
	 * @return the currently active {@link DisplayMode} of the given monitor
	 */
	public static DisplayMode getDisplayMode(Monitor monitor) {
		Lwjgl3Application.initializeGlfw();
		GLFWVidMode videoMode = GLFW.glfwGetVideoMode(((Lwjgl3Monitor)monitor).monitorHandle);
		return new Lwjgl3Graphics.Lwjgl3DisplayMode(((Lwjgl3Monitor)monitor).monitorHandle, videoMode.width(), videoMode.height(), videoMode.refreshRate(),
				videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
	}

	/**
	 * @return the available {@link DisplayMode}s of the primary monitor
	 */
	public static DisplayMode[] getDisplayModes() {
		Lwjgl3Application.initializeGlfw(); 
		Buffer videoModes = GLFW.glfwGetVideoModes(GLFW.glfwGetPrimaryMonitor());
		DisplayMode[] result = new DisplayMode[videoModes.limit()];
		for (int i = 0; i < result.length; i++) {
			GLFWVidMode videoMode = videoModes.get(i);
			result[i] = new Lwjgl3Graphics.Lwjgl3DisplayMode(GLFW.glfwGetPrimaryMonitor(), videoMode.width(), videoMode.height(),
					videoMode.refreshRate(), videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
		}
		return result;
	}

	/**
	 * @return the available {@link DisplayMode}s of the given {@link Monitor}
	 */
	public static DisplayMode[] getDisplayModes(Monitor monitor) {
		Lwjgl3Application.initializeGlfw();
		Buffer videoModes = GLFW.glfwGetVideoModes(((Lwjgl3Monitor)monitor).monitorHandle);
		DisplayMode[] result = new DisplayMode[videoModes.limit()];
		for (int i = 0; i < result.length; i++) {
			GLFWVidMode videoMode = videoModes.get(i);
			result[i] = new Lwjgl3Graphics.Lwjgl3DisplayMode(((Lwjgl3Monitor)monitor).monitorHandle, videoMode.width(), videoMode.height(),
					videoMode.refreshRate(), videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
		}
		return result;
	}

	/**
	 * @return the primary {@link Monitor}
	 */
	public static Monitor getPrimaryMonitor() {
		Lwjgl3Application.initializeGlfw();
		return toLwjgl3Monitor(GLFW.glfwGetPrimaryMonitor());
	}

	/**
	 * @return the conntected {@link Monitor}s
	 */
	public static Monitor[] getMonitors() {
		Lwjgl3Application.initializeGlfw();
		PointerBuffer glfwMonitors = GLFW.glfwGetMonitors();
		Monitor[] monitors = new Monitor[glfwMonitors.limit()];
		for (int i = 0; i < glfwMonitors.limit(); i++) {
			monitors[i] = toLwjgl3Monitor(glfwMonitors.get(i));
		}
		return monitors;
	}

	static Lwjgl3Monitor toLwjgl3Monitor(long glfwMonitor) {
		IntBuffer tmp = BufferUtils.createIntBuffer(1);
		IntBuffer tmp2 = BufferUtils.createIntBuffer(1);
		GLFW.glfwGetMonitorPos(glfwMonitor, tmp, tmp2);
		int virtualX = tmp.get(0);
		int virtualY = tmp2.get(0);
		String name = GLFW.glfwGetMonitorName(glfwMonitor);
		return new Lwjgl3Monitor(glfwMonitor, virtualX, virtualY, name);
	}

	public static enum HdpiMode {
		/**
		 * mouse coordinates, {@link Graphics#getWidth()} and
		 * {@link Graphics#getHeight()} will return logical coordinates
		 * according to the system defined HDPI scaling. Rendering will be
		 * performed to a backbuffer at raw resolution. Use {@link HdpiUtils}
		 * when calling {@link GL20#glScissor} or {@link GL20#glViewport} which
		 * expect raw coordinates.
		 */
		Logical,

		/**
		 * Mouse coordinates, {@link Graphics#getWidth()} and
		 * {@link Graphics#getHeight()} will return raw pixel coordinates
		 * irrespective of the system defined HDPI scaling.
		 */
		Pixels
	}		
}