Code cleanup

This commit is contained in:
Ioannis Tsakpinis 2004-11-25 22:31:38 +00:00
parent 3041c0494d
commit 83dd8b6bbc
11 changed files with 350 additions and 339 deletions

View File

@ -57,10 +57,10 @@ import org.lwjgl.input.Mouse;
public final class Display {
/** The display implementor */
private final static DisplayImplementation display_impl;
private static final DisplayImplementation display_impl;
/** The initial display mode */
private final static DisplayMode initial_mode;
private static final DisplayMode initial_mode;
/** The current display mode, if created */
private static DisplayMode current_mode;
@ -100,7 +100,7 @@ public final class Display {
});
}
private final static DisplayImplementation createDisplayImplementation() {
private static DisplayImplementation createDisplayImplementation() {
String class_name;
String os_name = System.getProperty("os.name");
if (os_name.startsWith("Linux")) {
@ -285,11 +285,13 @@ public final class Display {
timeThen = timeNow;
}
private static long timeLate;
/**
* Alternative sync method which works better on triple-buffered GL displays.
*
* @param fps The desired frame rate, in frames per second
*/
private static long timeLate;
public static void sync2(int fps) {
long gapTo = Sys.getTimerResolution() / fps + timeThen;
timeNow = Sys.getTime();
@ -654,7 +656,7 @@ public final class Display {
* The window is clamped to remain entirely on the screen. If you attempt
* to position the window such that it would extend off the screen, the window
* is simply placed as close to the edge as possible.
* @param x, y The new window location
* @param x , y The new window location
*/
public static void setLocation(int x, int y) {
if (fullscreen) {

View File

@ -44,16 +44,18 @@ import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
public interface DisplayImplementation {
public void createWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException;
public void destroyWindow();
public void switchDisplayMode(DisplayMode mode) throws LWJGLException;
void createWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException;
void destroyWindow();
void switchDisplayMode(DisplayMode mode) throws LWJGLException;
/**
* Reset the display mode to whatever it was when LWJGL was initialized.
* Fails silently.
*/
public void resetDisplayMode();
void resetDisplayMode();
/**
* Return the length of the gamma ramp arrays. Returns 0 if gamma settings are
@ -61,132 +63,130 @@ public interface DisplayImplementation {
*
* @return the length of each gamma ramp array, or 0 if gamma settings are unsupported.
*/
public int getGammaRampLength();
int getGammaRampLength();
/**
* Native method to set the gamma ramp.
*/
public void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException;
void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException;
/**
* Get the driver adapter string. This is a unique string describing the actual card's hardware, eg. "Geforce2", "PS2",
* "Radeon9700". If the adapter cannot be determined, this function returns null.
* @return a String
*/
public String getAdapter();
String getAdapter();
/**
* Get the driver version. This is a vendor/adapter specific version string. If the version cannot be determined,
* this function returns null.
* @return a String
*/
public String getVersion();
String getVersion();
/**
* Initialize and return the current display mode.
*/
public DisplayMode init();
DisplayMode init();
/**
* Native implementation of setTitle(). This will read the window's title member
* and stash it in the native title of the window.
*/
public void setTitle(String title);
void setTitle(String title);
public boolean isCloseRequested();
boolean isCloseRequested();
public boolean isVisible();
public boolean isActive();
boolean isVisible();
boolean isActive();
public boolean isDirty();
boolean isDirty();
/**
* Swap double buffers.
*/
public void swapBuffers();
void swapBuffers();
/**
* Make the window the current rendering context for GL calls.
*/
public void makeCurrent() throws LWJGLException;
void makeCurrent() throws LWJGLException;
/**
* Create the native OpenGL context.
* @throws LWJGLException
*/
public void createContext(PixelFormat pixel_format) throws LWJGLException;
void createContext(PixelFormat pixel_format) throws LWJGLException;
public void destroyContext();
void destroyContext();
/**
* Updates the windows internal state. This must be called at least once per video frame
* to handle window close requests, moves, paints, etc.
*/
public void update();
void update();
public void setVSyncEnabled(boolean sync);
void setVSyncEnabled(boolean sync);
public void reshape(int x, int y, int width, int height);
void reshape(int x, int y, int width, int height);
/**
* Native method for getting displaymodes
*/
public DisplayMode[] getAvailableDisplayModes();
DisplayMode[] getAvailableDisplayModes();
/*
* Mouse methods
*/
/** Native query of wheel support */
public boolean hasWheel();
boolean hasWheel();
/** Native query of button count */
public int getButtonCount();
int getButtonCount();
/**
* Native method to create the mouse.
*
* @return true if the mouse was created
*/
public void createMouse();
void createMouse();
/**
* Native method the destroy the mouse
*/
public void destroyMouse();
void destroyMouse();
/**
* Native method to poll the mouse
*/
public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons);
void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons);
/**
* Native method to enable the buffer
* @return the event buffer,
* or null if no buffer can be allocated
*/
public void enableMouseBuffer() throws LWJGLException;
void enableMouseBuffer() throws LWJGLException;
/**
* Native method to read the keyboard buffer
*
* @return the total number of events read.
*/
public int readMouse(IntBuffer buffer, int buffer_position);
int readMouse(IntBuffer buffer, int buffer_position);
public void grabMouse(boolean grab);
void grabMouse(boolean grab);
/**
* Native function to determine native cursor support
*/
public int getNativeCursorCaps();
int getNativeCursorCaps();
/** Native method to set the native cursor */
public void setNativeCursor(Object handle) throws LWJGLException;
void setNativeCursor(Object handle) throws LWJGLException;
/** Native method returning the minimum cursor size */
public int getMinCursorSize();
int getMinCursorSize();
/** Native method returning the maximum cursor size */
public int getMaxCursorSize();
int getMaxCursorSize();
/*
* Keyboard methods
@ -195,48 +195,46 @@ public interface DisplayImplementation {
/**
* Native method to create the keyboard
*/
public void createKeyboard() throws LWJGLException;
void createKeyboard() throws LWJGLException;
/**
* Native method to destroy the keyboard
*/
public void destroyKeyboard();
void destroyKeyboard();
/**
* Native method to poll the keyboard.
*
* @param keyDownBufferAddress the address of a 256-byte buffer to place
* @param keyDownBuffer the address of a 256-byte buffer to place
* key states in.
*/
public void pollKeyboard(ByteBuffer keyDownBuffer);
void pollKeyboard(ByteBuffer keyDownBuffer);
/**
* Native method to read the keyboard buffer
* @return the total number of events read.
*/
public int readKeyboard(IntBuffer buffer, int buffer_position);
int readKeyboard(IntBuffer buffer, int buffer_position);
/**
* Native method to enable the translation buffer
*/
public void enableTranslation() throws LWJGLException;
void enableTranslation() throws LWJGLException;
/**
* Native method to enable the buffer
* @return the event buffer,
* or null if no buffer can be allocated
*/
public void enableKeyboardBuffer() throws LWJGLException;
void enableKeyboardBuffer() throws LWJGLException;
public int isStateKeySet(int key);
int isStateKeySet(int key);
/** Native cursor handles */
public Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException;
Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException;
public void destroyCursor(Object cursor_handle);
void destroyCursor(Object cursor_handle);
/* Pbuffer caps */
public int getPbufferCaps();
int getPbufferCaps();
public boolean openURL(String url);
boolean openURL(String url);
}

View File

@ -128,7 +128,7 @@ public final class DisplayMode {
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
StringBuffer sb = new StringBuffer(32);
sb.append(width);
sb.append(" x ");
sb.append(height);

View File

@ -41,11 +41,14 @@ import java.nio.ByteBuffer;
import java.nio.IntBuffer;
abstract class EventQueue {
private final static int QUEUE_SIZE = 200;
private static final int QUEUE_SIZE = 200;
private final int event_size;
private final IntBuffer queue;
public EventQueue(int event_size) {
protected EventQueue(int event_size) {
this.event_size = event_size;
this.queue = ByteBuffer.allocateDirect(QUEUE_SIZE*event_size).asIntBuffer();
}

View File

@ -44,8 +44,9 @@ import java.nio.ByteBuffer;
import org.lwjgl.input.Keyboard;
final class KeyboardEventQueue extends EventQueue implements KeyListener {
private final static int[] KEY_MAP = new int[0xffff];
private final static int EVENT_SIZE = 3;
private static final int[] KEY_MAP = new int[0xffff];
private static final int EVENT_SIZE = 3;
private final byte[] key_states = new byte[Keyboard.KEYBOARD_SIZE];
@ -258,10 +259,10 @@ final class KeyboardEventQueue extends EventQueue implements KeyListener {
private synchronized void handleKey(int key_code, byte state, char character) {
int key_code_mapped = KEY_MAP[key_code];
if (character == KeyEvent.CHAR_UNDEFINED)
if ( character == KeyEvent.CHAR_UNDEFINED )
character = Keyboard.CHAR_NONE;
/* Ignore repeating presses */
if (key_states[key_code_mapped] == state)
if ( key_states[key_code_mapped] == state )
return;
key_states[key_code_mapped] = state;
int key_int_char = ((int)character) & 0xffff;

View File

@ -47,7 +47,7 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
final class LinuxDisplay implements DisplayImplementation {
private final static int CURSOR_HANDLE_SIZE = 8;
private static final int CURSOR_HANDLE_SIZE = 8;
public native void createWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException;
public native void destroyWindow();

View File

@ -51,7 +51,6 @@ import java.lang.reflect.Proxy;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -59,7 +58,7 @@ import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
final class MacOSXDisplay implements DisplayImplementation {
private final static int GAMMA_LENGTH = 256;
private static final int GAMMA_LENGTH = 256;
private MacOSXFrame frame;
private MouseEventQueue mouse_queue;
@ -69,7 +68,7 @@ final class MacOSXDisplay implements DisplayImplementation {
/* States */
private boolean close_requested;
public MacOSXDisplay() {
MacOSXDisplay() {
new MacOSXApplicationListener();
}

View File

@ -52,6 +52,7 @@ import java.lang.reflect.InvocationTargetException;
import org.lwjgl.LWJGLException;
final class MacOSXFrame extends Frame implements WindowListener, ComponentListener {
private final MacOSXGLCanvas canvas;
private boolean close_requested;
@ -62,23 +63,23 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
private boolean visible;
private boolean minimized;
public MacOSXFrame(DisplayMode mode, java.awt.DisplayMode requested_mode, boolean fullscreen, int x, int y) throws LWJGLException {
MacOSXFrame(DisplayMode mode, java.awt.DisplayMode requested_mode, boolean fullscreen, int x, int y) throws LWJGLException {
setResizable(false);
addWindowListener(this);
addComponentListener(this);
canvas = new MacOSXGLCanvas();
add(canvas, BorderLayout.CENTER);
setUndecorated(fullscreen);
if (fullscreen) {
if ( fullscreen ) {
getDevice().setFullScreenWindow(this);
getDevice().setDisplayMode(requested_mode);
java.awt.DisplayMode real_mode = getDevice().getDisplayMode();
/** For some strange reason, the display mode is sometimes silently capped even though the mode is reported as supported */
if (requested_mode.getWidth() != real_mode.getWidth() || requested_mode.getHeight() != real_mode.getHeight()) {
if ( requested_mode.getWidth() != real_mode.getWidth() || requested_mode.getHeight() != real_mode.getHeight() ) {
getDevice().setFullScreenWindow(null);
syncDispose();
throw new LWJGLException("AWT capped mode: requested mode = " + requested_mode.getWidth() + "x" + requested_mode.getHeight() +
" but got " + real_mode.getWidth() + " " + real_mode.getHeight());
" but got " + real_mode.getWidth() + " " + real_mode.getHeight());
}
}
pack();
@ -94,7 +95,7 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
}
public Rectangle syncGetBounds() {
synchronized (this) {
synchronized ( this ) {
return bounds;
}
}
@ -106,7 +107,7 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
}
private void updateBounds() {
synchronized (this) {
synchronized ( this ) {
bounds = getBounds();
}
}
@ -119,20 +120,20 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
updateBounds();
}
public final static GraphicsDevice getDevice() {
public static GraphicsDevice getDevice() {
GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = g_env.getDefaultScreenDevice();
return device;
}
public void windowIconified(WindowEvent e) {
synchronized (this) {
synchronized ( this ) {
minimized = true;
}
}
public void windowDeiconified(WindowEvent e) {
synchronized (this) {
synchronized ( this ) {
minimized = false;
}
}
@ -144,19 +145,19 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
}
public void windowClosing(WindowEvent e) {
synchronized (this) {
synchronized ( this ) {
close_requested = true;
}
}
public void windowDeactivated(WindowEvent e) {
synchronized (this) {
synchronized ( this ) {
active = false;
}
}
public void windowActivated(WindowEvent e) {
synchronized (this) {
synchronized ( this ) {
should_update = true;
active = true;
}
@ -165,16 +166,17 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
public void syncDispose() {
invokeAWT(new Runnable() {
public void run() {
if (isDisplayable())
if ( isDisplayable() )
dispose();
}
});
}
private class TitleSetter implements Runnable {
private final String title;
public TitleSetter(String title) {
TitleSetter(String title) {
this.title = title;
}
@ -189,7 +191,7 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
public boolean syncIsCloseRequested() {
boolean result;
synchronized (this) {
synchronized ( this ) {
result = close_requested;
close_requested = false;
}
@ -197,13 +199,13 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
}
public boolean syncIsVisible() {
synchronized (this) {
synchronized ( this ) {
return !minimized;
}
}
public boolean syncIsActive() {
synchronized (this) {
synchronized ( this ) {
return active;
}
}
@ -214,7 +216,7 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
public boolean syncShouldUpdateContext() {
boolean result;
synchronized (this) {
synchronized ( this ) {
result = canvas.syncShouldUpdateContext() || should_update;
should_update = false;
}
@ -222,12 +224,13 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
}
private class Reshaper implements Runnable {
private final int x;
private final int y;
private final int width;
private final int height;
public Reshaper(int x, int y, int width, int height) {
Reshaper(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
@ -255,9 +258,10 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
}
private class CursorSetter implements Runnable {
private final java.awt.Cursor awt_cursor;
public CursorSetter(java.awt.Cursor awt_cursor) {
CursorSetter(java.awt.Cursor awt_cursor) {
this.awt_cursor = awt_cursor;
}

View File

@ -44,6 +44,7 @@ import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
final class MacOSXGLCanvas extends Canvas implements ComponentListener {
private int width;
private int height;
private boolean context_update;
@ -55,9 +56,9 @@ final class MacOSXGLCanvas extends Canvas implements ComponentListener {
}
public void paint(Graphics g) {
synchronized (this) {
synchronized ( this ) {
dirty = true;
if (!canvas_created) {
if ( !canvas_created ) {
setFocusTraversalKeysEnabled(false);
/* Input methods are not enabled in fullscreen anyway, so disable always */
enableInputMethods(false);
@ -72,7 +73,7 @@ final class MacOSXGLCanvas extends Canvas implements ComponentListener {
public boolean syncIsDirty() {
boolean result;
synchronized (this) {
synchronized ( this ) {
result = dirty;
dirty = false;
}
@ -80,8 +81,8 @@ final class MacOSXGLCanvas extends Canvas implements ComponentListener {
}
public void waitForCanvasCreated() {
synchronized (this) {
while (!canvas_created) {
synchronized ( this ) {
while ( !canvas_created ) {
try {
wait();
} catch (InterruptedException e) {
@ -93,7 +94,7 @@ final class MacOSXGLCanvas extends Canvas implements ComponentListener {
public boolean syncShouldUpdateContext() {
boolean should_update;
synchronized (this) {
synchronized ( this ) {
should_update = context_update;
context_update = false;
}
@ -101,7 +102,7 @@ final class MacOSXGLCanvas extends Canvas implements ComponentListener {
}
private synchronized void setUpdate() {
synchronized (this) {
synchronized ( this ) {
width = getWidth();
height = getHeight();
context_update = true;
@ -109,13 +110,13 @@ final class MacOSXGLCanvas extends Canvas implements ComponentListener {
}
public int syncGetWidth() {
synchronized (this) {
synchronized ( this ) {
return width;
}
}
public int syncGetHeight() {
synchronized (this) {
synchronized ( this ) {
return height;
}
}

View File

@ -49,9 +49,10 @@ import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
final class MouseEventQueue extends EventQueue implements MouseListener, MouseMotionListener, MouseWheelListener {
private final static int WHEEL_SCALE = 120;
public final static int NUM_BUTTONS = 3;
private final static int EVENT_SIZE = 5;
private static final int WHEEL_SCALE = 120;
public static final int NUM_BUTTONS = 3;
private static final int EVENT_SIZE = 5;
private final int width;
private final int height;
@ -75,7 +76,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
/** Buttons array */
private final byte[] buttons = new byte[NUM_BUTTONS];
public MouseEventQueue(int width, int height) {
MouseEventQueue(int width, int height) {
super(EVENT_SIZE);
this.width = width;
this.height = height;
@ -103,7 +104,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
}
private boolean putMouseEvent(int button, int state, int dz) {
if (grabbed)
if ( grabbed )
return putMouseEventWithCoords(button, state, 0, 0, dz);
else
return putMouseEventWithCoords(button, state, last_x, last_y, dz);
@ -119,7 +120,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
}
public synchronized void poll(IntBuffer coord_buffer, ByteBuffer buttons_buffer) {
if (grabbed) {
if ( grabbed ) {
coord_buffer.put(0, accum_dx);
coord_buffer.put(1, accum_dy);
} else {
@ -135,7 +136,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
private synchronized void setCursorPos(int x, int y) {
y = transformY(y);
if (grabbed)
if ( grabbed )
return;
int dx = x - last_x;
int dy = y - last_y;
@ -157,7 +158,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
private void handleButton(MouseEvent e) {
byte button;
switch (e.getButton()) {
switch ( e.getButton() ) {
case MouseEvent.BUTTON1:
button = (byte)0;
break;
@ -171,7 +172,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
throw new IllegalArgumentException("Not a valid button: " + e.getButton());
}
byte state;
switch (e.getID()) {
switch ( e.getID() ) {
case MouseEvent.MOUSE_PRESSED:
state = 1;
break;
@ -198,7 +199,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
}
private void handleMotion(MouseEvent e) {
if (grabbed) {
if ( grabbed ) {
updateDeltas();
} else {
setCursorPos(e.getX(), e.getY());
@ -209,7 +210,7 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
handleMotion(e);
}
public void mouseMoved(MouseEvent e) {
public void mouseMoved(MouseEvent e) {
handleMotion(e);
}
@ -219,13 +220,13 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
}
private void updateDeltas() {
if (!grabbed)
if ( !grabbed )
return;
synchronized (this) {
synchronized ( this ) {
((MacOSXDisplay)Display.getImplementation()).getMouseDeltas(delta_buffer);
int dx = delta_buffer.get(0);
int dy = -delta_buffer.get(1);
if (dx != 0 || dy != 0) {
if ( dx != 0 || dy != 0 ) {
putMouseEventWithCoords(-1, 0, dx, dy, 0);
accum_dx += dx;
accum_dy += dy;
@ -233,8 +234,8 @@ final class MouseEventQueue extends EventQueue implements MouseListener, MouseMo
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
int wheel_amount = -e.getWheelRotation()*WHEEL_SCALE;
public void mouseWheelMoved(MouseWheelEvent e) {
int wheel_amount = -e.getWheelRotation() * WHEEL_SCALE;
handleWheel(wheel_amount);
}
}

View File

@ -46,7 +46,7 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
final class Win32Display implements DisplayImplementation {
private final static int CURSOR_HANDLE_SIZE = 8;
private static final int CURSOR_HANDLE_SIZE = 8;
public native void createWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException;
public native void destroyWindow();
@ -70,6 +70,7 @@ final class Win32Display implements DisplayImplementation {
public native void setVSyncEnabled(boolean sync);
public native void reshape(int x, int y, int width, int height);
public native DisplayMode[] getAvailableDisplayModes();
/* Mouse */
public native boolean hasWheel();
public native int getButtonCount();
@ -83,6 +84,7 @@ final class Win32Display implements DisplayImplementation {
public native void setNativeCursor(Object handle) throws LWJGLException;
public native int getMinCursorSize();
public native int getMaxCursorSize();
/* Keyboard */
public native void createKeyboard() throws LWJGLException;
public native void destroyKeyboard();