Replaced asserts with proper runtime exceptions

This commit is contained in:
Elias Naur 2004-03-27 11:55:07 +00:00
parent afb8491cfa
commit c093f3ace1
11 changed files with 1058 additions and 1013 deletions

View File

@ -196,8 +196,10 @@ public final class Display {
* @param contrast The contrast, larger than 0.0.
*/
public static void setDisplayConfiguration(float gamma, float brightness, float contrast) throws Exception {
assert brightness >= -1.0f && brightness <= 1.0f;
assert contrast >= 0.0f;
if (brightness < -1.0f || brightness > 1.0f)
throw new IllegalArgumentException("Invalid brightness value");
if (contrast < 0.0f)
throw new IllegalArgumentException("Invalid contrast value");
int rampSize = getGammaRampLength();
if (rampSize == 0) {
throw new Exception("Display configuration not supported");

View File

@ -164,9 +164,8 @@ public class Controller {
* @throws Exception if the controller could not be created for any reason
*/
public static void create() throws Exception {
assert Window.isCreated() : "Window must be created prior to creating controller";
if (!Window.isCreated())
throw new IllegalStateException("Window must be created before you can create Controller");
if (!initialized) {
initialize();
}
@ -203,7 +202,8 @@ public class Controller {
* Polls the controller.
*/
public static void poll() {
assert created : "The controller has not been created.";
if (!created)
throw new IllegalStateException("Controller must be created before you can poll the device");
nPoll();
}
@ -215,7 +215,8 @@ public class Controller {
* @see #buttonCount
*/
public static boolean isButtonDown(int button) {
assert created : "The controller has not been created.";
if (!created)
throw new IllegalStateException("Controller must be created before you can query button state");
return buttons[button];
}
@ -231,7 +232,7 @@ public class Controller {
* it always throws a RuntimeException.
*/
public static void read() {
throw new RuntimeException("Buffering is not implemented for Controllers.");
throw new UnsupportedOperationException("Buffering is not implemented for Controllers.");
}
/**

View File

@ -75,11 +75,16 @@ public class Cursor {
* @throws Exception if the cursor could not be created for any reason
*/
public Cursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws Exception {
assert Mouse.isCreated();
assert width*height*numImages <= images.remaining() : "width*height*numImages > images.remaining()";
assert delays == null || numImages <= delays.remaining() : "delays != null && numImages > delays.remaining()";
assert xHotspot < width && xHotspot >= 0 : "xHotspot > width || xHotspot < 0";
assert yHotspot < height && yHotspot >= 0 : "yHotspot > height || yHotspot < 0";
if (!Mouse.isCreated())
throw new IllegalStateException("Mouse must be created before creating cursor objects");
if (width*height*numImages > images.remaining())
throw new IllegalArgumentException("width*height*numImages > images.remaining()");
if (delays != null && numImages > delays.remaining())
throw new IllegalArgumentException("delays != null && numImages > delays.remaining()");
if (xHotspot >= width || xHotspot < 0)
throw new IllegalArgumentException("xHotspot > width || xHotspot < 0");
if (yHotspot >= height || yHotspot < 0)
throw new IllegalArgumentException("yHotspot > height || yHotspot < 0");
// initialize
if (!initialized) {

View File

@ -285,7 +285,8 @@ public class Keyboard {
* @throws Exception if the keyboard could not be created for any reason
*/
public static void create() throws Exception {
assert Window.isCreated() : "Window must be created prior to creating keyboard";
if (!Window.isCreated())
throw new IllegalStateException("Window must be created before you can create Keyboard");
if (!initialized)
initialize();
if (created)
@ -334,7 +335,8 @@ public class Keyboard {
* @see org.lwjgl.input.Keyboard#read()
*/
public static void poll() {
assert created : "The keyboard has not been created.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can poll the device");
nPoll(keyDownBuffer);
}
@ -361,8 +363,10 @@ public class Keyboard {
* @see org.lwjgl.input.Keyboard#getEventCharacter()
*/
public static void read() {
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can read events");
if (readBuffer == null)
throw new IllegalStateException("Event buffering must be enabled before you can read events");
readBuffer.compact();
int numEvents = nRead(readBuffer, readBuffer.position());
if (translationEnabled)
@ -383,8 +387,10 @@ public class Keyboard {
* and keyboard buffering must be enabled.
*/
public static void enableTranslation() throws Exception {
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can read events");
if (readBuffer == null)
throw new IllegalStateException("Event buffering must be enabled before you can read events");
nEnableTranslation();
translationEnabled = true;
}
@ -398,7 +404,8 @@ public class Keyboard {
* Enable keyboard buffering. Must be called after the keyboard is created.
*/
public static void enableBuffer() throws Exception {
assert created : "The keyboard has not been created.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can enable buffering");
readBuffer = BufferUtils.createByteBuffer(4*BUFFER_SIZE);
readBuffer.limit(0);
nEnableBuffer();
@ -417,7 +424,8 @@ public class Keyboard {
* @return true if the key is down according to the last poll()
*/
public static boolean isKeyDown(int key) {
assert created : "The keyboard has not been created.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can query key state");
return keyDownBuffer.get(key) != 0;
}
@ -442,7 +450,8 @@ public class Keyboard {
* @return STATE_ON if on, STATE_OFF if off and STATE_UNKNOWN if the state is unknown
*/
public static int isStateKeySet(int key) {
assert created : "The keyboard has not been created.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can query key state");
return nisStateKeySet(key);
}
private static native int nisStateKeySet(int key);
@ -473,7 +482,8 @@ public class Keyboard {
* @return the number of keyboard events
*/
public static int getNumKeyboardEvents() {
assert created : "The keyboard has not been created.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can read events");
if (translationEnabled)
return readBuffer.remaining()/4;
else
@ -492,8 +502,10 @@ public class Keyboard {
* @return true if a keyboard event was read, false otherwise
*/
public static boolean next() {
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
if (!created)
throw new IllegalStateException("Keyboard must be created before you can read events");
if (readBuffer == null)
throw new IllegalStateException("Event buffering must be enabled before you can read events");
if (readBuffer.hasRemaining()) {
eventKey = readBuffer.get() & 0xFF;

View File

@ -167,7 +167,10 @@ public class Mouse {
* @throws Exception if the cursor could not be set for any reason
*/
public static Cursor setNativeCursor(Cursor cursor) throws Exception {
assert created && ((getNativeCursorCaps() | CURSOR_ONE_BIT_TRANSPARENCY) != 0);
if (!created)
throw new IllegalStateException("Create the Mouse before setting the native cursor");
if ((getNativeCursorCaps() & CURSOR_ONE_BIT_TRANSPARENCY) == 0)
throw new IllegalStateException("Mouse doesn't support native cursors");
Cursor oldCursor = currentCursor;
currentCursor = cursor;
if (currentCursor != null) {
@ -239,7 +242,8 @@ public class Mouse {
*/
public static void create() throws Exception {
assert Window.isCreated() : "Window must be created prior to creating mouse";
if (!Window.isCreated())
throw new IllegalStateException("Window must be created prior to creating mouse");
if (!initialized) {
initialize();
@ -327,7 +331,8 @@ public class Mouse {
* @see org.lwjgl.input.Mouse#read()
*/
public static void poll() {
assert created : "The mouse has not been created.";
if (!created)
throw new IllegalStateException("Mouse must be created before you can poll it");
nPoll();
// set absolute position
@ -363,7 +368,8 @@ public class Mouse {
* @return true if the specified button is down
*/
public static boolean isButtonDown(int button) {
assert created : "The mouse has not been created.";
if (!created)
throw new IllegalStateException("Mouse must be created before you can poll the button state");
if (button >= buttonCount || button < 0)
return false;
else
@ -398,7 +404,8 @@ public class Mouse {
* Enable mouse button buffering. Must be called after the mouse is created.
*/
public static void enableBuffer() throws Exception {
assert created : "The mouse has not been created.";
if (!created)
throw new IllegalStateException("Mouse must be created before you can enable buffering");
readBuffer = BufferUtils.createByteBuffer(2*BUFFER_SIZE);
readBuffer.limit(0);
nEnableBuffer();
@ -424,8 +431,10 @@ public class Mouse {
* @see org.lwjgl.input.Mouse#getEventButtonState()
*/
public static void read() {
assert created : "The mouse has not been created.";
assert readBuffer != null : "Mouse buffering has not been enabled.";
if (!created)
throw new IllegalStateException("Mouse must be created before you can read events");
if (readBuffer == null)
throw new IllegalStateException("Event buffering must be enabled before you can read events");
readBuffer.compact();
int numEvents = nRead(readBuffer, readBuffer.position());
readBuffer.position(readBuffer.position() + numEvents*2);
@ -447,8 +456,10 @@ public class Mouse {
* @return true if a mouse event was read, false otherwise
*/
public static boolean next() {
assert created : "The mouse has not been created.";
assert readBuffer != null : "Mouse buffering has not been enabled.";
if (!created)
throw new IllegalStateException("Mouse must be created before you can read events");
if (readBuffer == null)
throw new IllegalStateException("Event buffering must be enabled before you can read events");
if (readBuffer.hasRemaining()) {
eventButton = readBuffer.get() & 0xFF;

View File

@ -458,7 +458,8 @@ public final class AL10 {
* @param data Buffer to place the integers in
*/
public static void alGetInteger(int pname, IntBuffer data) {
assert data.remaining() > 0;
if (data.remaining() <= 0)
throw new IllegalArgumentException("data.remaining() <= 0");
nalGetIntegerv(pname, data, data.position());
}
private static native void nalGetIntegerv(int pname, IntBuffer data, int offset);
@ -478,7 +479,8 @@ public final class AL10 {
* @param data Buffer to place the floats in
*/
public static void alGetFloat(int pname, FloatBuffer data) {
assert data.remaining() > 0;
if (data.remaining() <= 0)
throw new IllegalArgumentException("data.remaining() <= 0");
nalGetFloatv(pname, data, data.position());
}
private static native void nalGetFloatv(int pname, FloatBuffer data, int position);
@ -786,7 +788,8 @@ public final class AL10 {
* @param floatdata Buffer to write floats to
*/
public static void alGetSource(int source, int pname, FloatBuffer floatdata) {
assert floatdata.remaining() > 0;
if (floatdata.remaining() <= 0)
throw new IllegalArgumentException("floatdata.remaining() <= 0");
nalGetSourcefv(source, pname, floatdata, floatdata.position());
}
private static native void nalGetSourcefv(int source, int pname, FloatBuffer floatdata, int position);

View File

@ -89,7 +89,7 @@ public final class ARBVertexBufferObject {
case GL_ARRAY_BUFFER_ARB:
VBOTracker.getVBOArrayStack().setState(buffer);
break;
default: assert false: "Unsupported VBO target " + target;
default: throw new IllegalArgumentException("Unsupported VBO target " + target);
}
nglBindBufferARB(target, buffer);
}

View File

@ -67,7 +67,7 @@ public final class ARBVertexShader {
// ---------------------------
public static void glBindAttribLocationARB(int programObj, int index, ByteBuffer name) {
if ( name.get(name.limit() - 1) != 0 ) {
throw new RuntimeException("<name> must be a null-terminated string.");
throw new IllegalArgumentException("<name> must be a null-terminated string.");
}
nglBindAttribLocationARB(programObj, index, name, name.position());
}
@ -99,7 +99,7 @@ public final class ARBVertexShader {
// ---------------------------
public static int glGetAttribLocationARB(int programObj, ByteBuffer name) {
if ( name.get(name.limit() - 1) != 0 ) {
throw new RuntimeException("<name> must be a null-terminated string.");
throw new IllegalArgumentException("<name> must be a null-terminated string.");
}
return nglGetAttribLocationARB(programObj, name, name.position());
}

View File

@ -68,7 +68,7 @@ public final class ATIDrawBuffers {
// ---------------------------
public static void glDrawBuffersATI(IntBuffer buffers) {
if (buffers.remaining() == 0) {
throw new RuntimeException("<buffers> must have at least 1 integer available.");
throw new IllegalArgumentException("<buffers> must have at least 1 integer available.");
}
nglDrawBuffersATI(buffers.remaining(), buffers, buffers.position());
}

View File

@ -87,7 +87,7 @@ public final class GL15 {
VBOTracker.getVBOArrayStack().setState(buffer);
break;
default:
assert false: "Unsupported VBO target " + target;
throw new IllegalArgumentException("Unsupported VBO target " + target);
}
nglBindBuffer(target, buffer);
}

View File

@ -108,7 +108,8 @@ public final class Window {
* @return the width of the window
*/
public static int getWidth() {
assert isCreated() : "Cannot get width on uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot get width on uncreated window");
return width;
}
@ -116,7 +117,8 @@ public final class Window {
* @return the height of the window
*/
public static int getHeight() {
assert isCreated() : "Cannot get height on uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot get height on uncreated window");
return height;
}
@ -124,7 +126,8 @@ public final class Window {
* @return the title of the window
*/
public static String getTitle() {
assert isCreated() : "Cannot get title on uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot get title on uncreated window");
return title;
}
@ -132,7 +135,8 @@ public final class Window {
* @return whether this window is in fullscreen mode
*/
public static boolean isFullscreen() {
assert isCreated() : "Cannot determine state of uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot determine fullscreen state of uncreated window");
return fullscreen;
}
@ -141,7 +145,8 @@ public final class Window {
* @param newTitle The new window title
*/
public static void setTitle(String newTitle) {
assert isCreated() : "Cannot set title of uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot set title on uncreated window");
title = newTitle;
nSetTitle(title);
}
@ -156,7 +161,8 @@ public final class Window {
* @return true if the user or operating system has asked the window to close
*/
public static boolean isCloseRequested() {
assert isCreated() : "Cannot determine state of uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot determine close requested state of uncreated window");
return nIsCloseRequested();
}
@ -166,7 +172,8 @@ public final class Window {
* @return true if the window is minimized or otherwise not visible
*/
public static boolean isMinimized() {
assert isCreated() : "Cannot determine state of uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot determine minimized state of uncreated window");
return nIsMinimized();
}
@ -176,7 +183,8 @@ public final class Window {
* @return true if window is focused
*/
public static boolean isFocused() {
assert isCreated() : "Cannot determine state of uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot determine focused state of uncreated window");
return nIsFocused();
}
@ -210,7 +218,8 @@ public final class Window {
* and needs to repaint itself
*/
public static boolean isDirty() {
assert isCreated() : "Cannot determine state of uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot determine dirty state of uncreated window");
return nIsDirty();
}
@ -222,7 +231,8 @@ public final class Window {
* @throws OpenGLException if an OpenGL error has occured since the last call to GL11.glGetError()
*/
public static void update() {
assert isCreated() : "Cannot paint uncreated window";
if (!isCreated())
throw new IllegalStateException("Cannot determine update uncreated window");
nUpdate();
if ((isDirty() && !isMinimized()) || (isFocused() && !isMinimized())) {
Util.checkGLError();
@ -259,7 +269,8 @@ public final class Window {
* Make the Window the current rendering context for GL calls.
*/
public static synchronized void makeCurrent() {
assert isCreated() : "No window has been created.";
if (!isCreated())
throw new IllegalStateException("No window created to make current");
nMakeCurrent();
GLContext.useContext(context);
}
@ -315,7 +326,7 @@ public final class Window {
*/
public static void create(String title, int bpp, int alpha, int depth, int stencil, int samples) throws Exception {
if (isCreated())
throw new Exception("Only one LWJGL window may be instantiated at any one time.");
throw new IllegalStateException("Only one LWJGL window may be instantiated at any one time.");
Window.fullscreen = true;
Window.x = 0;
Window.y = 0;
@ -371,7 +382,7 @@ public final class Window {
public static void create(String title, int x, int y, int width, int height, int bpp, int alpha, int depth, int stencil, int samples)
throws Exception {
if (isCreated())
throw new Exception("Only one LWJGL window may be instantiated at any one time.");
throw new IllegalStateException("Only one LWJGL window may be instantiated at any one time.");
Window.fullscreen = false;
Window.x = x;
Window.y = y;
@ -518,7 +529,8 @@ public final class Window {
* @return boolean
*/
public static boolean isVSyncEnabled() {
assert isCreated() : "Cannot determine sync of uncreated window";
if (isCreated())
throw new IllegalStateException("Cannot determine vsync state of uncreated window");
return nIsVSyncEnabled();
}
@ -531,11 +543,10 @@ public final class Window {
* @param sync true to synchronize; false to ignore synchronization
*/
public static void setVSyncEnabled(boolean sync) {
assert isCreated() : "Cannot set sync of uncreated window";
if (isCreated())
throw new IllegalStateException("Cannot set vsync state of uncreated window");
nSetVSyncEnabled(sync);
}
private static native void nSetVSyncEnabled(boolean sync);
}