diff --git a/src/java/org/lwjgl/openal/ALC.java b/src/java/org/lwjgl/openal/ALC.java index 38e8e26e..cc69a5ad 100644 --- a/src/java/org/lwjgl/openal/ALC.java +++ b/src/java/org/lwjgl/openal/ALC.java @@ -41,6 +41,8 @@ package org.lwjgl.openal; * @version $Revision$ */ public class ALC { + /** Has the ALC object been created? */ + protected static boolean created; /** Bad value */ public static final int INVALID = -1; @@ -92,19 +94,67 @@ public class ALC { */ public static final int OUT_OF_MEMORY = 0xA005; - static { - try { - System.loadLibrary(org.lwjgl.Sys.getLibraryName()); - } catch (UnsatisfiedLinkError ule) { - System.out.println("Failed to load OpenAL library: " + org.lwjgl.Sys.getLibraryName()); - ule.printStackTrace(); - } - } + static { + initialize(); + } /** Creates a new instance of ALC */ public ALC() { } + /** + * Override to provide any initialization code after creation. + */ + protected void init() { + } + + /** + * Static initialization + */ + private static void initialize() { + System.loadLibrary(org.lwjgl.Sys.getLibraryName()); + } + + /** + * Creates the ALC instance + * + * @throws Exception if a failiure occured in the ALC creation process + */ + public void create() throws Exception { + if (created) { + return; + } + + if (!nCreate()) { + throw new Exception("ALC instance could not be created."); + } + created = true; + init(); + } + + /** + * Native method to create ALC instance + * + * @return true if the ALC creation process succeeded + */ + protected native boolean nCreate(); + + /** + * Calls whatever destruction rutines that are needed + */ + public void destroy() { + if (!created) { + return; + } + created = false; + nDestroy(); + } + + /** + * Native method the destroy the ALC + */ + protected native void nDestroy(); + /** * Returns strings related to the context. * diff --git a/src/java/org/lwjgl/openal/ALUT.java b/src/java/org/lwjgl/openal/ALUT.java index d90fdfaa..e8bc77ea 100644 --- a/src/java/org/lwjgl/openal/ALUT.java +++ b/src/java/org/lwjgl/openal/ALUT.java @@ -42,19 +42,70 @@ */ public class ALUT { - static { - try { - System.loadLibrary(org.lwjgl.Sys.getLibraryName()); - } catch (UnsatisfiedLinkError ule) { - System.out.println("Failed to load OpenAL library: " + org.lwjgl.Sys.getLibraryName()); - ule.printStackTrace(); - } - } + /** Has the ALUT object been created? */ + protected static boolean created; + + static { + initialize(); + } /** Creates a new instance of ALUT */ public ALUT() { } + /** + * Override to provide any initialization code after creation. + */ + protected void init() { + } + + /** + * Static initialization + */ + private static void initialize() { + System.loadLibrary(org.lwjgl.Sys.getLibraryName()); + } + + /** + * Creates the ALUT instance + * + * @throws Exception if a failiure occured in the ALUT creation process + */ + public void create() throws Exception { + if (created) { + return; + } + + if (!nCreate()) { + throw new Exception("ALUT instance could not be created."); + } + created = true; + init(); + } + + /** + * Native method to create ALUT instance + * + * @return true if the ALUT creation process succeeded + */ + protected native boolean nCreate(); + + /** + * Calls whatever destruction rutines that are needed + */ + public void destroy() { + if (!created) { + return; + } + created = false; + nDestroy(); + } + + /** + * Native method the destroy the ALUT + */ + protected native void nDestroy(); + /** * Initializes the OpenAL engine * diff --git a/src/java/org/lwjgl/openal/BaseAL.java b/src/java/org/lwjgl/openal/BaseAL.java index da9faacd..f709db29 100644 --- a/src/java/org/lwjgl/openal/BaseAL.java +++ b/src/java/org/lwjgl/openal/BaseAL.java @@ -44,12 +44,63 @@ * @version $Revision$ */ public abstract class BaseAL { - static { - try { - System.loadLibrary(org.lwjgl.Sys.getLibraryName()); - } catch (UnsatisfiedLinkError ule) { - System.out.println("Failed to load OpenAL library: " + org.lwjgl.Sys.getLibraryName()); - ule.printStackTrace(); + /** Has the ALC object been created? */ + protected static boolean created; + + static { + initialize(); + } + + /** + * Override to provide any initialization code after creation. + */ + protected void init() { + } + + /** + * Static initialization + */ + private static void initialize() { + System.loadLibrary(org.lwjgl.Sys.getLibraryName()); + } + + /** + * Creates the AL instance + * + * @throws Exception if a failiure occured in the AL creation process + */ + public void create() throws Exception { + if (created) { + return; } - } + + if (!nCreate()) { + throw new Exception("AL instance could not be created."); + } + created = true; + init(); + } + + /** + * Native method to create AL instance + * + * @return true if the AL creation process succeeded + */ + protected native boolean nCreate(); + + /** + * Calls whatever destruction rutines that are needed + */ + public void destroy() { + if (!created) { + return; + } + created = false; + nDestroy(); + } + + /** + * Native method the destroy the AL + */ + protected native void nDestroy(); } \ No newline at end of file diff --git a/src/java/org/lwjgl/openal/test/BasicTest.java b/src/java/org/lwjgl/openal/test/BasicTest.java index a57e5961..5cca7a74 100644 --- a/src/java/org/lwjgl/openal/test/BasicTest.java +++ b/src/java/org/lwjgl/openal/test/BasicTest.java @@ -71,8 +71,28 @@ public abstract class BasicTest { */ public BasicTest() { al = new AL(); + try { + al.create(); + } catch (Exception e) { + e.printStackTrace(); + return; + } + alc = new ALC(); + try { + alc.create(); + } catch (Exception e) { + e.printStackTrace(); + return; + } + alut = new ALUT(); + try { + alut.create(); + } catch (Exception e) { + e.printStackTrace(); + return; + } } /**