From 00616de78bbef4216e13b53bcb8ee8857fdfd734 Mon Sep 17 00:00:00 2001 From: Caspian Rychlik-Prince Date: Tue, 16 Sep 2003 15:39:46 +0000 Subject: [PATCH] Lazy library instantiation --- src/java/org/lwjgl/input/Controller.java | 12 +++++++---- src/java/org/lwjgl/input/Cursor.java | 17 ++++++++++----- src/java/org/lwjgl/input/Mouse.java | 27 +++++++++++++++--------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/java/org/lwjgl/input/Controller.java b/src/java/org/lwjgl/input/Controller.java index be70a99b..a196978e 100644 --- a/src/java/org/lwjgl/input/Controller.java +++ b/src/java/org/lwjgl/input/Controller.java @@ -132,10 +132,9 @@ public class Controller { private static String[] buttonName; private static final Map buttonMap = new HashMap(8); - static { - initialize(); - } - + /** Lazy initialization */ + private static boolean initialized; + /** * Controller cannot be constructed. */ @@ -156,6 +155,7 @@ public class Controller { buttonMap.put(buttonName[i], new Integer(i)); } + initialized = true; } /** @@ -163,6 +163,10 @@ public class Controller { * @throws Exception if the controller could not be created for any reason */ public static void create() throws Exception { + if (!initialized) { + initialize(); + } + if (created) { return; } diff --git a/src/java/org/lwjgl/input/Cursor.java b/src/java/org/lwjgl/input/Cursor.java index cd9467ed..4908ff85 100644 --- a/src/java/org/lwjgl/input/Cursor.java +++ b/src/java/org/lwjgl/input/Cursor.java @@ -50,9 +50,8 @@ import java.nio.ByteOrder; public class Cursor { - static { - System.loadLibrary(Sys.getLibraryName()); - } + /** Lazy initialization */ + private static boolean initialized = false; /** * The native handle to the cursor @@ -75,15 +74,23 @@ 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 { + if (!initialized) { + initialize(); + } 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"; + assert xHotspot < width && xHotspot >= 0: "xHotspot > width || xHotspot < 0"; + assert yHotspot < height && yHotspot >= 0: "yHotspot > height || yHotspot < 0"; IntBuffer images_copy = ByteBuffer.allocateDirect(images.remaining()*4).order(ByteOrder.nativeOrder()).asIntBuffer(); flipImages(width, height, numImages, images, images_copy); nativeHandle = nCreateCursor(width, height, xHotspot, height - yHotspot, numImages, images_copy, 0, delays, delays != null ? delays.position() : 0); } + + private static void initialize() { + System.loadLibrary(Sys.getLibraryName()); + initialized = true; + } private static void flipImages(int width, int height, int numImages, IntBuffer images, IntBuffer images_copy) { for (int i = 0; i < numImages; i++) { diff --git a/src/java/org/lwjgl/input/Mouse.java b/src/java/org/lwjgl/input/Mouse.java index 9da166e9..58f0e070 100644 --- a/src/java/org/lwjgl/input/Mouse.java +++ b/src/java/org/lwjgl/input/Mouse.java @@ -83,9 +83,8 @@ public class Mouse { private static String[] buttonName; private static final Map buttonMap = new HashMap(16); - static { - initialize(); - } + /** Lazy initialization */ + private static boolean initialized; /** * Mouse cannot be constructed. @@ -190,6 +189,8 @@ public class Mouse { buttonName[i] = "BUTTON" + i; buttonMap.put(buttonName[i], new Integer(i)); } + + initialized = true; } /** @@ -203,17 +204,20 @@ public class Mouse { * @throws Exception if the mouse could not be created for any reason */ public static void create() throws Exception { - if (created) + if (!initialized) { + initialize(); + } + if (created) { return; - if (!nCreate()) + } + if (!nCreate()) { throw new Exception("The mouse could not be created."); + } created = true; currentCursor = null; - //set mouse buttons + // set mouse buttons buttons = new boolean[buttonCount]; - - } /** @@ -271,7 +275,10 @@ public class Mouse { */ public static boolean isButtonDown(int button) { assert created : "The mouse has not been created."; - return buttons[button]; + if (button >= buttonCount) + return false; + else + return buttons[button]; } /** @@ -280,7 +287,7 @@ public class Mouse { * @return a String with the button's human readable name in it or null if the button is unnamed */ public static String getButtonName(int button) { - if (button < 0 || button >= buttonName.length) + if (button >= buttonName.length) return null; else return buttonName[button];