lwjgl2-arm64/src/java/org/lwjgl/Display.java

277 lines
8.1 KiB
Java
Raw Normal View History

2002-08-15 17:46:18 +02:00
/*
2002-12-22 20:52:15 +01:00
* Copyright (c) 2002 Light Weight Java Game Library Project
2002-08-15 17:46:18 +02:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
2002-08-09 12:56:30 +02:00
*
2002-08-15 17:46:18 +02:00
* * Redistributions of source code must retain the above copyright
* 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 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2002-08-09 12:56:30 +02:00
*/
2002-08-15 17:46:18 +02:00
2002-08-09 12:56:30 +02:00
package org.lwjgl;
import java.util.HashSet;
import java.util.Arrays;
2002-08-09 12:56:30 +02:00
/**
2002-08-15 17:46:18 +02:00
* $Id$
*
2002-08-09 12:56:30 +02:00
* Encapsulates everything you need for game display.
* It must be created before any input devices are created.
* The game display has NO mouse cursor or any other window decorations.
2002-08-09 12:56:30 +02:00
*
2002-08-15 17:46:18 +02:00
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
2002-08-09 12:56:30 +02:00
*/
2002-08-15 17:46:18 +02:00
2002-08-09 12:56:30 +02:00
public final class Display {
static {
2002-08-19 16:01:23 +02:00
System.loadLibrary(Sys.getLibraryName());
2002-08-09 12:56:30 +02:00
}
/** Has the display been created? */
private static boolean created;
/** The current display mode, if created */
private static DisplayMode mode;
2003-02-11 00:09:54 +01:00
2003-02-07 22:54:31 +01:00
/** A pointer to the native display window. On Windows this will be an hWnd. */
2002-08-09 12:56:30 +02:00
private static int handle;
2003-02-11 00:09:54 +01:00
/** Whether or not the display has been requested to shutdown by the user */
private static boolean closeRequested = false;
2002-08-09 12:56:30 +02:00
/**
* No construction allowed.
*/
private Display() {
super();
}
/**
* Returns the entire list of display modes as an array, in no
* particular order. Any given mode is not guaranteed to be available and
* the only certain way to check is to call create() and make sure it works.
* Only non-palette-indexed modes are returned (ie. bpp will be 16, 24, or 32).
*
* @return an array of all display modes the system reckons it can handle.
*/
public static DisplayMode[] getAvailableDisplayModes() {
DisplayMode[] unfilteredModes = nGetAvailableDisplayModes();
2003-03-03 22:58:17 +01:00
if (unfilteredModes == null) {
return new DisplayMode[0];
}
2003-03-03 22:58:17 +01:00
// We'll use a HashSet to filter out the duplicated modes
HashSet modes = new HashSet(unfilteredModes.length);
modes.addAll(Arrays.asList(unfilteredModes));
DisplayMode[] filteredModes = new DisplayMode[modes.size()];
modes.toArray(filteredModes);
if(Sys.DEBUG) {
System.out.println("Removed " + (unfilteredModes.length - filteredModes.length) + " duplicate displaymodes");
}
return filteredModes;
}
/**
* Native method for getting displaymodes
*/
public static native DisplayMode[] nGetAvailableDisplayModes();
2002-08-09 12:56:30 +02:00
/**
* Create a display with the specified display mode. If the display is
* already created then no action is taken - the display must first be
* destroyed.
*
2003-02-06 19:26:04 +01:00
* @param displayMode A display mode to choose
* @param fullscreen Whether to create the display fullscreen
* @param title The title for the application
2002-08-09 12:56:30 +02:00
* @throws Exception if the display mode could not be set
2002-08-23 18:14:38 +02:00
* @see #destroy()
2002-08-09 12:56:30 +02:00
*/
public static void create(
DisplayMode displayMode,
2003-02-06 19:26:04 +01:00
boolean fullscreen,
String title)
2002-08-09 12:56:30 +02:00
throws Exception {
2003-02-11 00:09:54 +01:00
if (created) {
2002-08-09 12:56:30 +02:00
return;
2003-02-11 00:09:54 +01:00
}
if (!nCreate(displayMode.width,
displayMode.height,
displayMode.bpp,
displayMode.freq,
2002-12-22 20:52:15 +01:00
displayMode.alpha,
displayMode.depth,
displayMode.stencil,
2003-02-06 19:26:04 +01:00
fullscreen,
2003-02-11 00:09:54 +01:00
title)) {
throw new Exception("Failed to set display mode to " + displayMode);
2003-02-11 00:09:54 +01:00
}
2002-08-09 12:56:30 +02:00
created = true;
mode = displayMode;
}
/**
* Native method to create the display. This will set the handle if it is
* successful.
* @return true if the display was successfully created
2002-08-23 18:14:38 +02:00
* @see #create(org.lwjgl.DisplayMode, boolean)
2002-08-09 12:56:30 +02:00
*/
private static native boolean nCreate(
int width,
int height,
int bpp,
int freq,
int alpha_bits,
int depth_bits,
int stencil_bits,
2003-02-06 19:26:04 +01:00
boolean fullscreen,
String title);
2002-08-09 12:56:30 +02:00
/**
* Destroy the display and return it to normal. If the display has not yet
* been created no action is taken.
*/
public static void destroy() {
2003-02-11 00:09:54 +01:00
if (!created) {
2002-08-09 12:56:30 +02:00
return;
2003-02-11 00:09:54 +01:00
}
2002-08-09 12:56:30 +02:00
nDestroy();
created = false;
mode = null;
}
2003-02-11 00:09:54 +01:00
2002-08-09 12:56:30 +02:00
/**
* Native method to destroy the display. This will reset the handle.
*/
private static native void nDestroy();
/**
2003-02-11 00:09:54 +01:00
* Retrieves the width of the created display
*
2002-08-09 12:56:30 +02:00
* @return the current display width.
* @throws AssertionError if the display has not been created yet.
2002-08-09 12:56:30 +02:00
*/
public static int getWidth() {
assert created : "The display has not been created yet.";
2002-08-09 12:56:30 +02:00
return mode.width;
}
/**
2003-02-11 00:09:54 +01:00
* Retrieves the height of the created display
*
2002-08-09 12:56:30 +02:00
* @return the current display height.
* @throws AssertionError if the display has not been created yet.
2002-08-09 12:56:30 +02:00
*/
public static int getHeight() {
assert created : "The display has not been created yet.";
2002-08-09 12:56:30 +02:00
return mode.height;
}
/**
2003-02-11 00:09:54 +01:00
* Retrieves the current display depth of the created display
*
2002-08-09 12:56:30 +02:00
* @return the current display depth.
* @throws AssertionError if the display has not been created yet.
2002-08-09 12:56:30 +02:00
*/
public static int getDepth() {
assert created : "The display has not been created yet.";
2002-08-09 12:56:30 +02:00
return mode.bpp;
}
/**
2003-02-11 00:09:54 +01:00
* Retrieves the current display frequency of the created display
*
2002-08-09 12:56:30 +02:00
* @return the current display frequency.
* @throws AssertionError if the display has not been created yet.
2002-08-09 12:56:30 +02:00
*/
public static int getFrequency() {
assert created : "The display has not been created yet.";
2002-08-09 12:56:30 +02:00
return mode.freq;
}
2003-02-11 00:09:54 +01:00
2002-08-09 12:56:30 +02:00
/**
2003-02-11 00:09:54 +01:00
* Retrieves the <code>DisplayMode</code> that the display has currently been
* set to.
*
2002-08-09 12:56:30 +02:00
* @return the current display mode, or null if the display is not yet created
* @throws AssertionError if the display has not been created yet.
2002-08-09 12:56:30 +02:00
*/
public static DisplayMode getDisplayMode() {
assert created : "The display has not been created yet.";
2002-08-09 12:56:30 +02:00
return mode;
}
2003-02-11 00:09:54 +01:00
2002-08-09 12:56:30 +02:00
/**
2003-02-11 00:09:54 +01:00
* Retrieves the native handle to the created window
*
2002-08-09 12:56:30 +02:00
* @return the native handle
* @throws AssertionError if the display has not been created yet.
2002-08-09 12:56:30 +02:00
*/
public static int getHandle() {
assert created : "The display has not been created yet.";
2002-08-09 12:56:30 +02:00
return handle;
}
2003-02-11 00:09:54 +01:00
2002-08-09 12:56:30 +02:00
/**
2003-02-11 00:09:54 +01:00
* Tests whether or not the display has been created
*
2002-08-09 12:56:30 +02:00
* @return true if the display has been created
*/
public static boolean isCreated() {
return created;
}
2003-02-07 22:54:31 +01:00
/**
* Determines if the display is minimized. When the display is minimized it is
* effectively invisible, and you need perform no rendering in your game loop.
* On the native side, when the application is switched to some other application,
* the display window will minimize; when focus is regained, it will maximize and
* automatically gain focus and become the foreground window again.
* @return true if the display is minimized
*/
public static native boolean isMinimized();
2003-02-11 00:09:54 +01:00
/**
* Determines if the user has requested that the application should close.
* When a user has requested that the application should shutdown, it is up to
* the application to perform the actual shutdown and cleanup of any allocated
* resources.
*
* @return true if the user has requested that the application should close
*/
public static boolean isCloseRequested() {
return closeRequested;
}
2003-02-09 00:01:58 +01:00
}