Home | History | Annotate | Download | only in lwjgl
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.backends.lwjgl;
     18 
     19 import java.awt.GraphicsDevice;
     20 import java.awt.GraphicsEnvironment;
     21 import java.util.ArrayList;
     22 
     23 import com.badlogic.gdx.Application;
     24 import com.badlogic.gdx.Files;
     25 import com.badlogic.gdx.Files.FileType;
     26 import com.badlogic.gdx.Graphics;
     27 import com.badlogic.gdx.Graphics.DisplayMode;
     28 import com.badlogic.gdx.graphics.Color;
     29 import com.badlogic.gdx.utils.Array;
     30 
     31 public class LwjglApplicationConfiguration {
     32 	/** If true, OpenAL will not be used. This means {@link Application#getAudio()} returns null and the gdx-openal.jar and OpenAL
     33 	 * natives are not needed. */
     34 	static public boolean disableAudio;
     35 
     36 	/** whether to attempt use OpenGL ES 3.0. **/
     37 	public boolean useGL30 = false;
     38 	/** The OpenGL context major version (the part in front of the decimal point) used to emulate OpenGL ES 3.0, when the version is
     39 	 * not supported it will fall back to OpenGL ES 2.0 emulation. Defaults to 3.2 (major=3, minor=2). Only used when
     40 	 * {@link #useGL30} is true. OpenGL is fully compatible with OpenGL ES 3.0 since version 4.3, setting the context version to a
     41 	 * lower value might cause some features not to function properly. OSX requires 3.2 though.
     42 	 * @see <a href="http://legacy.lwjgl.org/javadoc/org/lwjgl/opengl/ContextAttribs.html">LWJGL OSX ContextAttribs note</a> */
     43 	public int gles30ContextMajorVersion = 3;
     44 	/** The OpenGL context major version (the part after the decimal point) used to emulate OpenGL ES 3.0, when the version is not
     45 	 * supported it will fall back to OpenGL ES 2.0 emulation. Defaults to 3.2 (major=3, minor=2). Only used when {@link #useGL30}
     46 	 * is true. OpenGL is fully compatible with OpenGL ES 3.0 since version 4.3, setting the context version to a lower value might
     47 	 * cause some features not to function properly. OSX requires 3.2 though.
     48 	 * @see <a href="http://legacy.lwjgl.org/javadoc/org/lwjgl/opengl/ContextAttribs.html">LWJGL OSX ContextAttribs note</a> */
     49 	public int gles30ContextMinorVersion = 2;
     50 
     51 	/** number of bits per color channel **/
     52 	public int r = 8, g = 8, b = 8, a = 8;
     53 	/** number of bits for depth and stencil buffer **/
     54 	public int depth = 16, stencil = 0;
     55 	/** number of samples for MSAA **/
     56 	public int samples = 0;
     57 	/** width & height of application window **/
     58 	public int width = 640, height = 480;
     59 	/** x & y of application window, -1 for center **/
     60 	public int x = -1, y = -1;
     61 	/** fullscreen **/
     62 	public boolean fullscreen = false;
     63 	/** used to emulate screen densities **/
     64 	public int overrideDensity = -1;
     65 	/** whether to enable vsync, can be changed at runtime via {@link Graphics#setVSync(boolean)} **/
     66 	public boolean vSyncEnabled = true;
     67 	/** title of application **/
     68 	public String title;
     69 	/** whether to call System.exit() on tear-down. Needed for Webstarts on some versions of Mac OS X it seems **/
     70 	public boolean forceExit = true;
     71 	/** whether the window is resizable **/
     72 	public boolean resizable = true;
     73 	/** the maximum number of sources that can be played simultaneously */
     74 	public int audioDeviceSimultaneousSources = 16;
     75 	/** the audio device buffer size in samples **/
     76 	public int audioDeviceBufferSize = 512;
     77 	/** the audio device buffer count **/
     78 	public int audioDeviceBufferCount = 9;
     79 	public Color initialBackgroundColor = Color.BLACK;
     80 	/** Target framerate when the window is in the foreground. The CPU sleeps as needed. Use 0 to never sleep. **/
     81 	public int foregroundFPS = 60;
     82 	/** Target framerate when the window is not in the foreground. The CPU sleeps as needed. Use 0 to never sleep, -1 to not render. **/
     83 	public int backgroundFPS = 60;
     84 	/** Allows software OpenGL rendering if hardware acceleration was not available.
     85 	 * @see LwjglGraphics#isSoftwareMode() */
     86 	public boolean allowSoftwareMode = false;
     87 	/** Preferences directory on the desktop. Default is ".prefs/". */
     88 	public String preferencesDirectory = ".prefs/";
     89 	/** Preferences file type on the desktop. Default is FileType.External */
     90 	public Files.FileType preferencesFileType = FileType.External;
     91 	/** Callback used when trying to create a display, can handle failures, default value is null (disabled) */
     92 	public LwjglGraphics.SetDisplayModeCallback setDisplayModeCallback;
     93 	/** enable HDPI mode on Mac OS X **/
     94 	public boolean useHDPI = false;
     95 
     96 	Array<String> iconPaths = new Array();
     97 	Array<FileType> iconFileTypes = new Array();
     98 
     99 	/** Adds a window icon. Icons are tried in the order added, the first one that works is used. Typically three icons should be
    100 	 * provided: 128x128 (for Mac), 32x32 (for Windows and Linux), and 16x16 (for Windows). */
    101 	public void addIcon (String path, FileType fileType) {
    102 		iconPaths.add(path);
    103 		iconFileTypes.add(fileType);
    104 	}
    105 
    106 	/** Sets the r, g, b and a bits per channel based on the given {@link DisplayMode} and sets the fullscreen flag to true.
    107 	 * @param mode */
    108 	public void setFromDisplayMode (DisplayMode mode) {
    109 		this.width = mode.width;
    110 		this.height = mode.height;
    111 		if (mode.bitsPerPixel == 16) {
    112 			this.r = 5;
    113 			this.g = 6;
    114 			this.b = 5;
    115 			this.a = 0;
    116 		}
    117 		if (mode.bitsPerPixel == 24) {
    118 			this.r = 8;
    119 			this.g = 8;
    120 			this.b = 8;
    121 			this.a = 0;
    122 		}
    123 		if (mode.bitsPerPixel == 32) {
    124 			this.r = 8;
    125 			this.g = 8;
    126 			this.b = 8;
    127 			this.a = 8;
    128 		}
    129 		this.fullscreen = true;
    130 	}
    131 
    132 	protected static class LwjglApplicationConfigurationDisplayMode extends DisplayMode {
    133 		protected LwjglApplicationConfigurationDisplayMode (int width, int height, int refreshRate, int bitsPerPixel) {
    134 			super(width, height, refreshRate, bitsPerPixel);
    135 		}
    136 	}
    137 
    138 	public static DisplayMode getDesktopDisplayMode () {
    139 		GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    140 		GraphicsDevice device = genv.getDefaultScreenDevice();
    141 		java.awt.DisplayMode mode = device.getDisplayMode();
    142 		return new LwjglApplicationConfigurationDisplayMode(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(),
    143 			mode.getBitDepth());
    144 	}
    145 
    146 	public static DisplayMode[] getDisplayModes () {
    147 		GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    148 		GraphicsDevice device = genv.getDefaultScreenDevice();
    149 		java.awt.DisplayMode desktopMode = device.getDisplayMode();
    150 		java.awt.DisplayMode[] displayModes = device.getDisplayModes();
    151 		ArrayList<DisplayMode> modes = new ArrayList<DisplayMode>();
    152 		int idx = 0;
    153 		for (java.awt.DisplayMode mode : displayModes) {
    154 			boolean duplicate = false;
    155 			for (int i = 0; i < modes.size(); i++) {
    156 				if (modes.get(i).width == mode.getWidth() && modes.get(i).height == mode.getHeight()
    157 					&& modes.get(i).bitsPerPixel == mode.getBitDepth()) {
    158 					duplicate = true;
    159 					break;
    160 				}
    161 			}
    162 			if (duplicate) continue;
    163 			if (mode.getBitDepth() != desktopMode.getBitDepth()) continue;
    164 			modes.add(new LwjglApplicationConfigurationDisplayMode(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode
    165 				.getBitDepth()));
    166 		}
    167 
    168 		return modes.toArray(new DisplayMode[modes.size()]);
    169 	}
    170 }
    171