2003-03-27 17:32:48 -05:00
|
|
|
/*
|
|
|
|
* Created on 27-Mar-2003
|
|
|
|
*
|
|
|
|
* To change this generated comment go to
|
|
|
|
* Window>Preferences>Java>Code Generation>Code Template
|
|
|
|
*/
|
|
|
|
package org.lwjgl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the abstract base class for a Window in LWJGL. LWJGL windows have some
|
|
|
|
* peculiar characteristics:
|
|
|
|
*
|
|
|
|
* - width and height are always fixed and cannot be changed
|
|
|
|
* - the position of the window may or may not be programmable but once specified
|
|
|
|
* cannot be changed
|
|
|
|
* - the window may be closeable by the user or operating system, and may be minimized
|
|
|
|
* by the user or operating system
|
2003-03-27 17:46:25 -05:00
|
|
|
* - only one window may ever be open at once
|
2003-03-27 17:32:48 -05:00
|
|
|
* - the operating system may or may not be able to do fullscreen or windowed windows.
|
|
|
|
*
|
|
|
|
* @author foo
|
|
|
|
*/
|
|
|
|
public abstract class Window {
|
|
|
|
|
|
|
|
static {
|
|
|
|
System.loadLibrary(Sys.getLibraryName());
|
|
|
|
}
|
2003-03-27 17:46:25 -05:00
|
|
|
|
2003-03-28 14:02:24 -05:00
|
|
|
/** The currently created window */
|
2003-03-27 18:01:14 -05:00
|
|
|
private static Window currentWindow;
|
2003-03-28 14:02:24 -05:00
|
|
|
|
|
|
|
/** Whether the window is currently created, ie. has a native peer */
|
|
|
|
private boolean created;
|
2003-03-27 17:32:48 -05:00
|
|
|
|
|
|
|
/** The window's native data structure. On Win32 this is an HWND. */
|
|
|
|
private int handle;
|
|
|
|
|
|
|
|
/** Whether the window is currently minimized */
|
|
|
|
private boolean minimized;
|
2003-07-16 17:02:48 -04:00
|
|
|
|
2003-07-28 06:09:58 -04:00
|
|
|
/** Whether the window has focus */
|
|
|
|
private boolean focused = true;
|
2003-03-27 17:32:48 -05:00
|
|
|
|
|
|
|
/** Whether the window has been asked to close by the user or underlying OS */
|
|
|
|
private boolean closeRequested;
|
|
|
|
|
|
|
|
/** Whether the window is dirty, ie. needs painting */
|
|
|
|
private boolean dirty;
|
|
|
|
|
|
|
|
/** X coordinate of the window */
|
|
|
|
private int x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 int y;
|
|
|
|
|
|
|
|
/** Width of the window */
|
2003-06-04 17:49:46 -04:00
|
|
|
private int width;
|
2003-03-27 17:32:48 -05:00
|
|
|
|
|
|
|
/** Height of the window */
|
2003-06-04 17:49:46 -04:00
|
|
|
private int height;
|
2003-03-27 17:32:48 -05:00
|
|
|
|
|
|
|
/** Title of the window */
|
|
|
|
private String title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a Window. Some OSs may not support non-fullscreen windows; in
|
|
|
|
* which case the window will be fullscreen regardless.
|
|
|
|
*
|
2003-06-04 17:49:46 -04:00
|
|
|
* A fullscreen window MUST track changes to the display settings and change its
|
|
|
|
* width and height accordingly.
|
|
|
|
*
|
2003-03-27 17:32:48 -05:00
|
|
|
* In this abstract base class, no actual window peer is constructed. This should be
|
|
|
|
* done in specialised derived classes.
|
2003-03-27 17:46:25 -05:00
|
|
|
*
|
2003-03-28 14:02:24 -05:00
|
|
|
* Only one Window can be created() at a time; to create another Window you must
|
2003-03-27 17:46:25 -05:00
|
|
|
* first destroy() the first window.
|
2003-05-20 08:20:13 -04:00
|
|
|
*
|
|
|
|
* The dimensions may be ignored if the window cannot be made non-
|
|
|
|
* fullscreen. The position may be ignored in either case.
|
2003-03-27 17:32:48 -05:00
|
|
|
*
|
|
|
|
* @param title The window's title
|
2003-05-03 17:20:23 -04:00
|
|
|
* @param x Position on x axis of top left corner of window.
|
2003-05-20 08:20:13 -04:00
|
|
|
* @param y Position on y axis of top left corner of window.
|
|
|
|
* @param width Width of window
|
|
|
|
* @param height Height of window
|
2003-03-27 17:46:25 -05:00
|
|
|
* @throws RuntimeException if you attempt to create more than one window at the same time
|
2003-03-27 17:32:48 -05:00
|
|
|
*/
|
|
|
|
protected Window(String title, int x, int y, int width, int height) {
|
|
|
|
this.title = title;
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2003-03-27 17:46:25 -05:00
|
|
|
|
2003-03-27 17:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the width of the window
|
|
|
|
*/
|
|
|
|
public final int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the height of the window
|
|
|
|
*/
|
|
|
|
public final int getHeight() {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the title of the window
|
|
|
|
*/
|
|
|
|
public final String getTitle() {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the title of the window. This may be ignored by the underlying OS.
|
|
|
|
* @param newTitle The new window title
|
|
|
|
*/
|
|
|
|
public final void setTitle(String newTitle) {
|
2003-06-07 08:47:35 -04:00
|
|
|
assert isCreated();
|
2003-03-27 17:32:48 -05:00
|
|
|
title = newTitle;
|
2003-03-30 14:26:39 -05:00
|
|
|
nSetTitle(title);
|
2003-03-27 17:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Native implementation of setTitle(). This will read the window's title member
|
|
|
|
* and stash it in the native title of the window.
|
|
|
|
*/
|
2003-03-30 14:26:39 -05:00
|
|
|
private native void nSetTitle(String title);
|
2003-03-27 17:32:48 -05:00
|
|
|
|
2003-07-28 06:09:58 -04:00
|
|
|
/**
|
|
|
|
* @return true if the user or operating system has asked the window to close
|
|
|
|
*/
|
|
|
|
public final boolean isCloseRequested() {
|
|
|
|
assert isCreated();
|
|
|
|
|
|
|
|
boolean currentValue = closeRequested;
|
|
|
|
closeRequested = false;
|
|
|
|
return currentValue;
|
|
|
|
}
|
2003-03-27 17:32:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if the window is minimized or otherwise not visible
|
|
|
|
*/
|
|
|
|
public final boolean isMinimized() {
|
2003-06-07 08:47:35 -04:00
|
|
|
assert isCreated();
|
2003-03-27 17:32:48 -05:00
|
|
|
return minimized;
|
|
|
|
}
|
2003-07-16 17:02:48 -04:00
|
|
|
|
2003-07-28 06:09:58 -04:00
|
|
|
/**
|
|
|
|
* @return true if window is focused
|
|
|
|
*/
|
|
|
|
public final boolean isFocused() {
|
|
|
|
assert isCreated();
|
|
|
|
return focused;
|
|
|
|
}
|
2003-03-27 17:32:48 -05:00
|
|
|
|
2003-06-04 17:49:46 -04:00
|
|
|
/**
|
|
|
|
* Minimize the game and allow the operating system's default display to become
|
2003-06-12 10:08:10 -04:00
|
|
|
* visible. It is the responsibility of LWJGL's native code to restore the display
|
2003-06-04 17:49:46 -04:00
|
|
|
* to its normal display settings.
|
|
|
|
*
|
|
|
|
* If the display is already minimized then this is a no-op.
|
|
|
|
*/
|
|
|
|
public final native void minimize();
|
|
|
|
|
|
|
|
/**
|
2003-06-12 10:08:10 -04:00
|
|
|
* Restore the game and hide the operating system away. It is the responsibility of
|
2003-06-04 17:49:46 -04:00
|
|
|
* LWJGL's native code to restore the display to its game display settings.
|
|
|
|
*
|
|
|
|
* If the display is not minimized then this is a no-op/
|
|
|
|
*/
|
|
|
|
public final native void restore();
|
|
|
|
|
2003-03-27 17:32:48 -05:00
|
|
|
/**
|
|
|
|
* 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 you call paint().
|
|
|
|
*
|
|
|
|
* @return true if the window has been damaged by external changes
|
|
|
|
* and needs to repaint itself
|
|
|
|
*/
|
|
|
|
public final boolean isDirty() {
|
2003-06-07 08:47:35 -04:00
|
|
|
assert isCreated();
|
2003-03-27 17:32:48 -05:00
|
|
|
return dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Paint the window. This clears the dirty flag and swaps the buffers.
|
|
|
|
*/
|
|
|
|
public final void paint() {
|
2003-06-07 08:47:35 -04:00
|
|
|
assert isCreated();
|
2003-03-27 17:32:48 -05:00
|
|
|
dirty = false;
|
2003-03-30 14:26:39 -05:00
|
|
|
doPaint();
|
2003-03-27 17:32:48 -05:00
|
|
|
}
|
2003-03-30 14:26:39 -05:00
|
|
|
|
|
|
|
protected abstract void doPaint();
|
2003-03-27 17:32:48 -05:00
|
|
|
|
2003-03-28 14:02:24 -05:00
|
|
|
/**
|
|
|
|
* Create the window.
|
|
|
|
*/
|
|
|
|
public final void create() throws Exception {
|
|
|
|
if (currentWindow != null)
|
|
|
|
throw new RuntimeException("Only one LWJGL window may be instantiated at any one time.");
|
|
|
|
doCreate();
|
|
|
|
currentWindow = this;
|
|
|
|
created = true;
|
2003-06-07 08:47:35 -04:00
|
|
|
|
|
|
|
}
|
2003-03-28 14:02:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the window (derived classes).
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected abstract void doCreate() throws Exception;
|
|
|
|
|
2003-03-27 17:32:48 -05:00
|
|
|
/**
|
|
|
|
* Destroy the window.
|
|
|
|
*/
|
2003-06-07 08:47:35 -04:00
|
|
|
public final void destroy() {
|
2003-03-28 14:02:24 -05:00
|
|
|
if (!created)
|
|
|
|
return;
|
|
|
|
doDestroy();
|
|
|
|
currentWindow = null;
|
|
|
|
created = false;
|
2003-03-27 17:46:25 -05:00
|
|
|
}
|
|
|
|
|
2003-03-28 14:02:24 -05:00
|
|
|
/**
|
|
|
|
* Destroy the window (derived classes)
|
|
|
|
*/
|
|
|
|
protected abstract void doDestroy();
|
|
|
|
|
2003-03-27 17:32:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the native window handle
|
|
|
|
*/
|
|
|
|
public final int getHandle() {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2003-03-27 18:01:14 -05:00
|
|
|
/**
|
2003-03-28 14:02:24 -05:00
|
|
|
* @return true if the window's native peer has been created
|
2003-03-27 18:01:14 -05:00
|
|
|
*/
|
2003-03-28 14:02:24 -05:00
|
|
|
public final boolean isCreated() {
|
|
|
|
return created;
|
2003-03-27 18:01:14 -05:00
|
|
|
}
|
|
|
|
|
2003-03-27 17:32:48 -05:00
|
|
|
/**
|
|
|
|
* 'Tick' the window. This must be called at least once per video frame
|
|
|
|
* to handle window close requests, moves, paints, etc.
|
|
|
|
*/
|
2003-06-07 08:47:35 -04:00
|
|
|
public native void tick();
|
2003-03-28 14:02:24 -05:00
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see java.lang.Object#toString()
|
|
|
|
*/
|
|
|
|
public String toString() {
|
|
|
|
return "Window["+title+"]";
|
|
|
|
}
|
|
|
|
|
2003-03-28 18:16:15 -05:00
|
|
|
/**
|
|
|
|
* @return the current window, or null, if there is no current window
|
|
|
|
*/
|
|
|
|
public static Window getCurrentWindow() {
|
|
|
|
return currentWindow;
|
|
|
|
}
|
2003-03-27 17:32:48 -05:00
|
|
|
}
|