lwjgl/src/java/org/lwjgl/util/Display.java

249 lines
8.4 KiB
Java
Raw Normal View History

/*
2008-04-07 14:36:09 -04:00
* Copyright (c) 2002-2008 LWJGL Project
2004-04-21 14:11:00 -04:00
* All rights reserved.
*
2004-04-21 14:11:00 -04:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
2004-04-21 14:11:00 -04:00
* met:
*
* * Redistributions of source code must retain the above copyright
2004-04-21 14:11:00 -04:00
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
2004-06-12 16:28:34 -04:00
* from this software without specific prior written permission.
*
2004-04-21 14:11:00 -04:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2004-04-21 14:11:00 -04:00
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2004-04-21 14:11:00 -04:00
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util;
import java.util.ArrayList;
2004-07-03 09:13:54 -04:00
import java.util.Arrays;
2004-04-21 14:11:00 -04:00
import java.util.Comparator;
2022-08-16 14:27:14 -04:00
import java.util.function.ToIntFunction;
2004-04-21 14:11:00 -04:00
import org.lwjgl.LWJGLException;
2005-05-04 16:59:44 -04:00
import org.lwjgl.LWJGLUtil;
2004-07-03 09:13:54 -04:00
import org.lwjgl.opengl.DisplayMode;
2004-04-21 14:11:00 -04:00
/**
* Display initialization utility, that can be used to find display modes and pick
* one for you based on your criteria.
* @author $Author$
* @version $Revision$
2006-03-23 14:32:21 -05:00
* $Id$
2004-04-21 14:11:00 -04:00
*/
public final class Display {
/**
* Determine the available display modes that match the specified minimum and maximum criteria.
* If any given criterium is specified as -1 then it is ignored.
*
2004-04-21 14:11:00 -04:00
* @param minWidth the minimum display resolution in pixels
* @param minHeight the minimum display resolution in pixels
* @param maxWidth the maximum display resolution in pixels
* @param maxHeight the maximum display resolution in pixels
* @param minBPP the minimum bit depth per pixel
* @param maxBPP the maximum bit depth per pixel
* @param minFreq the minimum display frequency in Hz
* @param maxFreq the maximum display frequency in Hz
* @return an array of matching display modes
*/
public static DisplayMode[] getAvailableDisplayModes(int minWidth, int minHeight, int maxWidth, int maxHeight, int minBPP, int maxBPP,
int minFreq, int maxFreq) throws LWJGLException
2004-04-21 14:11:00 -04:00
{
// First get the available display modes
2004-07-03 09:07:02 -04:00
DisplayMode[] modes = org.lwjgl.opengl.Display.getAvailableDisplayModes();
2022-08-16 14:27:14 -04:00
if (LWJGLUtil.DEBUG) {
LWJGLUtil.logger().log(() -> {
StringBuilder sb = new StringBuilder("Available display modes:");
for (DisplayMode mode : modes) {
sb.append('\n').append(mode);
}
return sb.toString();
});
2004-04-21 14:11:00 -04:00
}
2022-08-16 14:27:14 -04:00
DisplayMode[] matches = new DisplayMode[modes.length];
2004-04-21 14:11:00 -04:00
2022-08-16 14:27:14 -04:00
int j = 0;
2004-04-21 14:11:00 -04:00
for (int i = 0; i < modes.length; i ++) {
assert modes[i] != null : ""+i+" "+modes.length;
2004-07-03 09:07:02 -04:00
if (minWidth != -1 && modes[i].getWidth() < minWidth)
2004-04-21 14:11:00 -04:00
continue;
2004-07-03 09:07:02 -04:00
if (maxWidth != -1 && modes[i].getWidth() > maxWidth)
2004-04-21 14:11:00 -04:00
continue;
2004-07-03 09:07:02 -04:00
if (minHeight != -1 && modes[i].getHeight() < minHeight)
2004-04-21 14:11:00 -04:00
continue;
2004-07-03 09:07:02 -04:00
if (maxHeight != -1 && modes[i].getHeight() > maxHeight)
2004-04-21 14:11:00 -04:00
continue;
2004-07-03 09:07:02 -04:00
if (minBPP != -1 && modes[i].getBitsPerPixel() < minBPP)
2004-04-21 14:11:00 -04:00
continue;
2004-07-03 09:07:02 -04:00
if (maxBPP != -1 && modes[i].getBitsPerPixel() > maxBPP)
2004-04-21 14:11:00 -04:00
continue;
//if (modes[i].bpp == 24)
// continue;
2004-07-03 09:07:02 -04:00
if (modes[i].getFrequency() != 0) {
if (minFreq != -1 && modes[i].getFrequency() < minFreq)
2004-04-21 14:11:00 -04:00
continue;
2004-07-03 09:07:02 -04:00
if (maxFreq != -1 && modes[i].getFrequency() > maxFreq)
2004-04-21 14:11:00 -04:00
continue;
}
2022-08-16 14:27:14 -04:00
matches[j++] = modes[i];
2004-04-21 14:11:00 -04:00
}
2022-08-16 14:27:14 -04:00
DisplayMode[] ret = Arrays.copyOf(matches, j);
if (LWJGLUtil.DEBUG) {
LWJGLUtil.logger().log(() -> {
StringBuilder sb = new StringBuilder("Filtered display modes:");
for (DisplayMode mode : ret) {
sb.append('\n').append(mode);
}
return sb.toString();
});
2004-04-21 14:11:00 -04:00
}
2004-04-21 14:11:00 -04:00
return ret;
}
2022-08-16 14:27:14 -04:00
private static final class FieldAccessor {
final ToIntFunction<DisplayMode> func;
final int order;
final int preferred;
final boolean usePreferred;
FieldAccessor(ToIntFunction<DisplayMode> func, int order, int preferred, boolean usePreferred) {
this.func = func;
this.order = order;
this.preferred = preferred;
this.usePreferred = usePreferred;
}
static ToIntFunction<DisplayMode> funcByName(String fieldName) {
switch (fieldName) {
case "width":
return DisplayMode::getWidth;
case "height":
return DisplayMode::getHeight;
case "freq":
return DisplayMode::getFrequency;
case "bpp":
return DisplayMode::getBitsPerPixel;
default:
throw new IllegalArgumentException("Unknown field " + fieldName);
}
}
}
2004-04-21 14:11:00 -04:00
/**
* Create the display by choosing from a list of display modes based on an order of preference.
* You must supply a list of allowable display modes, probably by calling getAvailableDisplayModes(),
* and an array with the order in which you would like them sorted in descending order.
* This method attempts to create the topmost display mode; if that fails, it will try the next one,
* and so on, until there are no modes left. If no mode is set at the end, an exception is thrown.
2004-05-14 13:20:17 -04:00
* @param dm a list of display modes to choose from
* @param param the names of the DisplayMode fields in the order in which you would like them sorted.
2004-04-21 14:11:00 -04:00
* @return the chosen display mode
* @throws NoSuchFieldException if one of the params is not a field in DisplayMode
* @throws Exception if no display mode could be set
2008-01-21 17:20:25 -05:00
* @see org.lwjgl.opengl.DisplayMode
2004-04-21 14:11:00 -04:00
*/
public static DisplayMode setDisplayMode(DisplayMode[] dm, final String[] param) throws Exception {
class Sorter implements Comparator<DisplayMode> {
final FieldAccessor[] accessors;
Sorter() {
accessors = new FieldAccessor[param.length];
for (int i = 0; i < accessors.length; i ++) {
2004-04-21 14:11:00 -04:00
int idx = param[i].indexOf('=');
2022-08-16 14:27:14 -04:00
ToIntFunction<DisplayMode> func;
2004-04-21 14:11:00 -04:00
if (idx > 0) {
2022-08-16 14:27:14 -04:00
accessors[i] = new FieldAccessor(FieldAccessor.funcByName(param[i].substring(0, idx)), 0, Integer.parseInt(param[i].substring(idx + 1, param[i].length())), true);
2004-04-21 14:11:00 -04:00
} else if (param[i].charAt(0) == '-') {
2022-08-16 14:27:14 -04:00
accessors[i] = new FieldAccessor(FieldAccessor.funcByName(param[i].substring(1)), -1, 0, false);
2004-04-21 14:11:00 -04:00
} else {
2022-08-16 14:27:14 -04:00
accessors[i] = new FieldAccessor(FieldAccessor.funcByName(param[i]), 1, 0, false);
2004-04-21 14:11:00 -04:00
}
}
}
2004-04-21 14:11:00 -04:00
/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(DisplayMode dm1, DisplayMode dm2) {
2022-08-16 14:27:14 -04:00
for (FieldAccessor accessor : accessors) {
int f1 = accessor.func.applyAsInt(dm1);
int f2 = accessor.func.applyAsInt(dm2);
if ( accessor.usePreferred && f1 != f2 ) {
if ( f1 == accessor.preferred )
2005-08-20 06:33:28 -04:00
return -1;
else if ( f2 == accessor.preferred )
2005-08-20 06:33:28 -04:00
return 1;
else {
// Score according to the difference between the values
int absf1 = Math.abs(f1 - accessor.preferred);
int absf2 = Math.abs(f2 - accessor.preferred);
if ( absf1 < absf2 )
2004-04-21 14:11:00 -04:00
return -1;
else if ( absf1 > absf2 )
2004-04-21 14:11:00 -04:00
return 1;
2005-08-20 06:33:28 -04:00
else
continue;
}
} else if ( f1 < f2 )
return accessor.order;
else if ( f1 == f2 )
2005-08-20 06:33:28 -04:00
continue;
else
return -accessor.order;
2004-04-21 14:11:00 -04:00
}
2004-04-21 14:11:00 -04:00
return 0;
}
}
2004-04-21 14:11:00 -04:00
// Sort the display modes
Arrays.sort(dm, new Sorter());
2004-04-21 14:11:00 -04:00
// Try them out in the appropriate order
2022-08-16 14:27:14 -04:00
if (LWJGLUtil.DEBUG) {
LWJGLUtil.logger().log(() -> {
StringBuilder sb = new StringBuilder("Sorted display modes:");
for (DisplayMode mode : dm) {
sb.append('\n').append(mode);
}
return sb.toString();
});
2004-04-21 14:11:00 -04:00
}
2022-08-16 14:27:14 -04:00
for (DisplayMode aDm : dm) {
2004-04-21 14:11:00 -04:00
try {
2022-08-16 14:27:14 -04:00
if (LWJGLUtil.DEBUG) {
LWJGLUtil.logger().log(() -> "Attempting to set display mode: " + aDm);
}
org.lwjgl.opengl.Display.setDisplayMode(aDm);
return aDm;
2004-04-21 14:11:00 -04:00
} catch (Exception e) {
2022-08-16 14:27:14 -04:00
LWJGLUtil.logger().log(() -> "Failed to set display mode to " + aDm, e);
2004-04-21 14:11:00 -04:00
}
}
2004-04-21 14:11:00 -04:00
throw new Exception("Failed to set display mode.");
}
2004-04-21 14:11:00 -04:00
}