/* * Copyright (c) 2002-2008 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * 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 'LWJGL' 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. */ package org.lwjgl.opengl; /** * This is the abstract class for a Display in LWJGL. LWJGL displays have some * peculiar characteristics: * * - the display may be closeable by the user or operating system, and may be minimized * by the user or operating system * - only one display may ever be open at once * - the operating system may or may not be able to do fullscreen or windowed displays. * * @author foo */ import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLUtil; import org.lwjgl.Sys; import org.lwjgl.input.Controllers; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Arrays; import java.util.HashSet; public final class Display { private static final Thread shutdown_hook = new Thread() { public void run() { reset(); } }; /** The display implementor */ private static final DisplayImplementation display_impl; /** The initial display mode */ private static final DisplayMode initial_mode; /** The parent, if any */ private static Canvas parent; /** The current display mode, if created */ private static DisplayMode current_mode; /** X coordinate of the window */ private static int x = -1; /** Cached window icons, for when Display is recreated */ private static ByteBuffer[] cached_icons; /** * Y coordinate of the window. Y in window coordinates is from the top of the display down, * unlike GL, where it is typically at the bottom of the display. */ private static int y = -1; /** the width of the Display window */ private static int width = 0; /** the height of the Display window */ private static int height = 0; /** Title of the window (never null) */ private static String title = "Game"; /** Fullscreen */ private static boolean fullscreen; /** Swap interval */ private static int swap_interval; /** The Drawable instance that tracks the current Display context */ private static DrawableLWJGL drawable; private static boolean window_created; private static boolean parent_resized; private static boolean window_resized; private static boolean window_resizable; /** Initial Background Color of Display */ private static float r, g, b; private static final ComponentListener component_listener = new ComponentAdapter() { public void componentResized(ComponentEvent e) { synchronized ( GlobalLock.lock ) { parent_resized = true; } } }; static { Sys.initialize(); display_impl = createDisplayImplementation(); try { current_mode = initial_mode = display_impl.init(); LWJGLUtil.log("Initial mode: " + initial_mode); } catch (LWJGLException e) { throw new RuntimeException(e); }; } /** * Fetch the Drawable from the Display. * * @return the Drawable corresponding to the Display context */ public static Drawable getDrawable() { return drawable; } private static DisplayImplementation createDisplayImplementation() { switch ( LWJGLUtil.getPlatform() ) { case LWJGLUtil.PLATFORM_LINUX: return new LinuxDisplay(); case LWJGLUtil.PLATFORM_WINDOWS: return new WindowsDisplay(); case LWJGLUtil.PLATFORM_MACOSX: return new MacOSXDisplay(); default: throw new IllegalStateException("Unsupported platform"); } } /** Only constructed by ourselves */ private Display() { } /** * Returns the entire list of possible fullscreen display modes as an array, in no * particular order. Although best attempts to filter out invalid modes are done, any * given mode is not guaranteed to be available nor is it guaranteed to be within the * current monitor specs (this is especially a problem with the frequency parameter). * Furthermore, it is not guaranteed that create() will detect an illegal display mode. *
* 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). * Only DisplayModes from this call can be used when the Display is in fullscreen * mode. * * @return an array of all display modes the system reckons it can handle. */ public static DisplayMode[] getAvailableDisplayModes() throws LWJGLException { synchronized ( GlobalLock.lock ) { DisplayMode[] unfilteredModes = display_impl.getAvailableDisplayModes(); if ( unfilteredModes == null ) { return new DisplayMode[0]; } // We'll use a HashSet to filter out the duplicated modes HashSet* It is not advisable to call this method from an AWT thread, since the context will be made current on the thread * and it is difficult to predict which AWT thread will process any given AWT event.
* While the Display is in fullscreen mode, the current parent will be ignored. Additionally, when a non null parent is specified, * the Dispaly will inherit the size of the parent, disregarding the currently set display mode.
*/ public static void setParent(Canvas parent) throws LWJGLException { synchronized ( GlobalLock.lock ) { if ( Display.parent != parent ) { Display.parent = parent; if ( !isCreated() ) return; destroyWindow(); try { if ( isFullscreen() ) { switchDisplayMode(); } else { display_impl.resetDisplayMode(); } createWindow(); makeCurrentAndSetSwapInterval(); } catch (LWJGLException e) { drawable.destroy(); display_impl.resetDisplayMode(); throw e; } } } } /** * Set the fullscreen mode of the context. If no context has been created through create(), * the mode will apply when create() is called. If fullscreen is true, the context will become * a fullscreen context and the display mode is switched to the mode given by getDisplayMode(). If * fullscreen is false, the context will become a windowed context with the dimensions given in the * mode returned by getDisplayMode(). The native cursor position is also reset. * * @param fullscreen Specify the fullscreen mode of the context. * * @throws LWJGLException If fullscreen is true, and the current DisplayMode instance is not * from getAvailableDisplayModes() or if the mode switch fails. */ public static void setFullscreen(boolean fullscreen) throws LWJGLException { setDisplayModeAndFullscreenInternal(fullscreen, current_mode); } /** * Set the mode of the context. If no context has been created through create(), * the mode will apply when create() is called. If mode.isFullscreenCapable() is true, the context will become * a fullscreen context and the display mode is switched to the mode given by getDisplayMode(). If * mode.isFullscreenCapable() is false, the context will become a windowed context with the dimensions given in the * mode returned by getDisplayMode(). The native cursor position is also reset. * * @param mode The new display mode to set. Must be non-null. * * @throws LWJGLException If the mode switch fails. */ public static void setDisplayModeAndFullscreen(DisplayMode mode) throws LWJGLException { setDisplayModeAndFullscreenInternal(mode.isFullscreenCapable(), mode); } private static void setDisplayModeAndFullscreenInternal(boolean fullscreen, DisplayMode mode) throws LWJGLException { synchronized ( GlobalLock.lock ) { if ( mode == null ) throw new NullPointerException("mode must be non-null"); DisplayMode old_mode = current_mode; current_mode = mode; boolean was_fullscreen = isFullscreen(); Display.fullscreen = fullscreen; if ( was_fullscreen != isFullscreen() || !mode.equals(old_mode) ) { if ( !isCreated() ) return; destroyWindow(); try { if ( isFullscreen() ) { switchDisplayMode(); } else { display_impl.resetDisplayMode(); } createWindow(); makeCurrentAndSetSwapInterval(); } catch (LWJGLException e) { drawable.destroy(); display_impl.resetDisplayMode(); throw e; } } } } /** @return whether the Display is in fullscreen mode */ public static boolean isFullscreen() { synchronized ( GlobalLock.lock ) { return fullscreen && current_mode.isFullscreenCapable(); } } /** * Set the title of the window. This may be ignored by the underlying OS. * * @param newTitle The new window title */ public static void setTitle(String newTitle) { synchronized ( GlobalLock.lock ) { if ( newTitle == null ) { newTitle = ""; } title = newTitle; if ( isCreated() ) display_impl.setTitle(title); } } /** @return true if the user or operating system has asked the window to close */ public static boolean isCloseRequested() { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Cannot determine close requested state of uncreated window"); return display_impl.isCloseRequested(); } } /** @return true if the window is visible, false if not */ public static boolean isVisible() { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Cannot determine minimized state of uncreated window"); return display_impl.isVisible(); } } /** @return true if window is active, that is, the foreground display of the operating system. */ public static boolean isActive() { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Cannot determine focused state of uncreated window"); return display_impl.isActive(); } } /** * Determine if the window's contents have been damaged by external events. * If you are writing a straightforward game rendering loop and simply paint * every frame regardless, you can ignore this flag altogether. If you are * trying to be kind to other processes you can check this flag and only * redraw when it returns true. The flag is cleared when update() or isDirty() is called. * * @return true if the window has been damaged by external changes * and needs to repaint itself */ public static boolean isDirty() { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Cannot determine dirty state of uncreated window"); return display_impl.isDirty(); } } /** * Process operating system events. Call this to update the Display's state and to receive new * input device events. This method is called from update(), so it is not necessary to call * this method if update() is called periodically. */ public static void processMessages() { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Display not created"); display_impl.update(); } pollDevices(); } /** * Swap the display buffers. This method is called from update(), and should normally not be called by * the application. * * @throws OpenGLException if an OpenGL error has occured since the last call to glGetError() */ public static void swapBuffers() throws LWJGLException { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Display not created"); if ( LWJGLUtil.DEBUG ) drawable.checkGLError(); drawable.swapBuffers(); } } /** * Update the window. If the window is visible clears * the dirty flag and calls swapBuffers() and finally * polls the input devices. */ public static void update() { update(true); } /** * Update the window. If the window is visible clears * the dirty flag and calls swapBuffers() and finally * polls the input devices if processMessages is true. * * @param processMessages Poll input devices if true */ public static void update(boolean processMessages) { synchronized ( GlobalLock.lock ) { if ( !isCreated() ) throw new IllegalStateException("Display not created"); // We paint only when the window is visible or dirty if ( display_impl.isVisible() || display_impl.isDirty() ) { try { swapBuffers(); } catch (LWJGLException e) { throw new RuntimeException(e); } } window_resized = !isFullscreen() && parent == null && display_impl.wasResized(); if ( window_resized ) { width = display_impl.getWidth(); height = display_impl.getHeight(); } if ( parent_resized ) { reshape(); parent_resized = false; window_resized = true; } if ( processMessages ) processMessages(); } } static void pollDevices() { // Poll the input devices while we're here if ( Mouse.isCreated() ) { Mouse.poll(); Mouse.updateCursor(); } if ( Keyboard.isCreated() ) { Keyboard.poll(); } if ( Controllers.isCreated() ) { Controllers.poll(); } } /** * Release the Display context. * * @throws LWJGLException If the context could not be released */ public static void releaseContext() throws LWJGLException { drawable.releaseContext(); } /** Returns true if the Display's context is current in the current thread. */ public static boolean isCurrent() throws LWJGLException { return drawable.isCurrent(); } /** * Make the Display the current rendering context for GL calls. * * @throws LWJGLException If the context could not be made current */ public static void makeCurrent() throws LWJGLException { drawable.makeCurrent(); } private static void removeShutdownHook() { AccessController.doPrivileged(new PrivilegedAction