Added more error checking in OpenAL. Unload native stubs on AL destroy.

This commit is contained in:
Elias Naur 2004-07-04 13:58:11 +00:00
parent 53bd9b660d
commit 0ae705b7b0
6 changed files with 74 additions and 78 deletions

View File

@ -125,15 +125,7 @@ public abstract class AL {
create(); create();
} }
/** private static String[] getOALPaths() throws LWJGLException {
* Creates an OpenAL instance. The empty create will cause OpenAL to
* open the default device, and create a context using default values.
*/
public static void create() throws LWJGLException {
if(created) {
return;
}
// need to pass path of possible locations of OAL to native side // need to pass path of possible locations of OAL to native side
String libpath = System.getProperty("java.library.path"); String libpath = System.getProperty("java.library.path");
String seperator = System.getProperty("path.separator"); String seperator = System.getProperty("path.separator");
@ -168,43 +160,58 @@ public abstract class AL {
//add cwd path //add cwd path
oalPaths[oalPaths.length - 1] = ""; oalPaths[oalPaths.length - 1] = "";
return oalPaths;
}
/**
* Creates an OpenAL instance. The empty create will cause OpenAL to
* open the default device, and create a context using default values.
*/
public static void create() throws LWJGLException {
if (created) {
return;
}
String[] oalPaths = getOALPaths();
nCreate(oalPaths); nCreate(oalPaths);
AL10.initNativeStubs();
ALC.create();
device = ALC.alcOpenDevice(deviceArguments);
if (device == null) {
ALC.destroy();
throw new LWJGLException("Could not open ALC device");
}
//check if doing default values or not
if (contextFrequency == -1) {
context = ALC.alcCreateContext(device.device, null);
} else {
context = ALC.alcCreateContext(device.device,
ALCcontext.createAttributeList(contextFrequency, contextRefresh, contextSynchronized));
}
ALC.alcMakeContextCurrent(context.context);
created = true; created = true;
try {
AL10.initNativeStubs();
ALC.initNativeStubs();
device = ALC.alcOpenDevice(deviceArguments);
if (device == null)
throw new LWJGLException("Could not open ALC device");
//check if doing default values or not
if (contextFrequency == -1) {
context = ALC.alcCreateContext(device.device, null);
} else {
context = ALC.alcCreateContext(device.device,
ALCcontext.createAttributeList(contextFrequency, contextRefresh, contextSynchronized));
}
if (ALC.alcMakeContextCurrent(context.context) != 0)
throw new LWJGLException("Could not make ALC context current");
created = true;
} catch (LWJGLException e) {
destroy();
throw e;
}
} }
/** /**
* Exit cleanly by calling destroy. * Exit cleanly by calling destroy.
*/ */
public static void destroy() { public static void destroy() {
if(!created) { if (context != null) {
return; ALC.alcDestroyContext(context.context);
context = null;
} }
if (device != null) {
ALC.alcDestroyContext(context.context); ALC.alcCloseDevice(device.device);
ALC.alcCloseDevice(device.device); device = null;
ALC.destroy(); }
resetNativeStubs(AL10.class);
device = null; resetNativeStubs(ALC.class);
context = null;
deviceArguments = null; deviceArguments = null;
@ -212,8 +219,9 @@ public abstract class AL {
contextRefresh = -1; contextRefresh = -1;
contextSynchronized = ALC.ALC_FALSE; contextSynchronized = ALC.ALC_FALSE;
if (created)
nDestroy();
created = false; created = false;
nDestroy();
} }
/** /**
@ -242,5 +250,7 @@ public abstract class AL {
Sys.log("Failure locating OpenAL using classloader:" + e); Sys.log("Failure locating OpenAL using classloader:" + e);
} }
return null; return null;
} }
private static native void resetNativeStubs(Class clazz);
} }

View File

@ -67,9 +67,6 @@ import org.lwjgl.LWJGLException;
* @version $Revision$ * @version $Revision$
*/ */
public class ALC { public class ALC {
/** Has the ALC object been created? */
protected static boolean created;
/** Bad value */ /** Bad value */
public static final int ALC_INVALID = -1; public static final int ALC_INVALID = -1;
@ -146,41 +143,13 @@ public class ALC {
Sys.initialize(); Sys.initialize();
} }
/** Creates a new instance of ALC */
protected ALC() {
}
/** /**
* Override to provide any initialization code after creation. * Override to provide any initialization code after creation.
*/ */
protected static void init() { protected static void init() {
} }
/** static native void initNativeStubs() throws LWJGLException;
* Creates the ALC instance
*
* @throws LWJGLException if a failiure occured in the ALC creation process
*/
protected static void create() throws LWJGLException {
if (created) {
return;
}
initNativeStubs();
init();
created = true;
}
private static native void initNativeStubs() throws LWJGLException;
/**
* Calls whatever destruction rutines that are needed
*/
protected static void destroy() {
if (!created) {
return;
}
created = false;
}
/** /**
* The application can obtain certain strings from ALC. * The application can obtain certain strings from ALC.
@ -284,7 +253,7 @@ public class ALC {
* @param context address of context to make current * @param context address of context to make current
* @return true if successfull, false if not * @return true if successfull, false if not
*/ */
native static boolean alcMakeContextCurrent(int context); native static int alcMakeContextCurrent(int context);
/** /**
* The current context is the only context accessible to state changes by AL commands * The current context is the only context accessible to state changes by AL commands

View File

@ -176,7 +176,7 @@ void ext_InitializeClass(JNIEnv *env, jclass clazz, ExtGetProcAddressPROC gpa, i
method->signature = function->signature; method->signature = function->signature;
method->fnPtr = function->method_pointer; method->fnPtr = function->method_pointer;
} }
jint result = env->RegisterNatives(clazz, methods, num_functions); env->RegisterNatives(clazz, methods, num_functions);
free(methods); free(methods);
} }

View File

@ -43,3 +43,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nCreate (JNIEnv *env, jclass cla
JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nDestroy(JNIEnv *env, jclass clazz) { JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nDestroy(JNIEnv *env, jclass clazz) {
DeInitializeOpenAL(); DeInitializeOpenAL();
} }
JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_resetNativeStubs(JNIEnv *env, jclass clazz, jclass al_class) {
env->UnregisterNatives(al_class);
}

View File

@ -15,6 +15,8 @@ extern "C" {
/* Inaccessible static: contextRefresh */ /* Inaccessible static: contextRefresh */
/* Inaccessible static: contextSynchronized */ /* Inaccessible static: contextSynchronized */
/* Inaccessible static: created */ /* Inaccessible static: created */
/* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024AL10 */
/* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024ALC */
/* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024AL */ /* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024AL */
/* Inaccessible static: class_00024java_00024lang_00024String */ /* Inaccessible static: class_00024java_00024lang_00024String */
/* /*
@ -33,6 +35,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nCreate
JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nDestroy JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nDestroy
(JNIEnv *, jclass); (JNIEnv *, jclass);
/*
* Class: org_lwjgl_openal_AL
* Method: resetNativeStubs
* Signature: (Ljava/lang/Class;)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_resetNativeStubs
(JNIEnv *, jclass, jclass);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -49,7 +49,7 @@ typedef ALCvoid (ALCAPIENTRY *alcGetIntegervPROC)(ALCdevice *device,ALCenum
typedef ALCdevice* (ALCAPIENTRY *alcOpenDevicePROC)(ALCubyte *deviceName); typedef ALCdevice* (ALCAPIENTRY *alcOpenDevicePROC)(ALCubyte *deviceName);
typedef ALCvoid (ALCAPIENTRY *alcCloseDevicePROC)(ALCdevice *device); typedef ALCvoid (ALCAPIENTRY *alcCloseDevicePROC)(ALCdevice *device);
typedef ALCcontext* (ALCAPIENTRY *alcCreateContextPROC)(ALCdevice *device,ALCint *attrList); typedef ALCcontext* (ALCAPIENTRY *alcCreateContextPROC)(ALCdevice *device,ALCint *attrList);
typedef ALCboolean (ALCAPIENTRY *alcMakeContextCurrentPROC)(ALCcontext *context); typedef ALCenum (ALCAPIENTRY *alcMakeContextCurrentPROC)(ALCcontext *context);
typedef ALCvoid (ALCAPIENTRY *alcProcessContextPROC)(ALCcontext *context); typedef ALCvoid (ALCAPIENTRY *alcProcessContextPROC)(ALCcontext *context);
typedef ALCdevice* (ALCAPIENTRY *alcGetContextsDevicePROC)(ALCcontext *context); typedef ALCdevice* (ALCAPIENTRY *alcGetContextsDevicePROC)(ALCcontext *context);
typedef ALCvoid (ALCAPIENTRY *alcSuspendContextPROC)(ALCcontext *context); typedef ALCvoid (ALCAPIENTRY *alcSuspendContextPROC)(ALCcontext *context);
@ -205,12 +205,15 @@ static jobject JNICALL Java_org_lwjgl_openal_ALC_alcCreateContext (JNIEnv *env,
* C Specification: * C Specification:
* ALCboolean alcMakeContextCurrent(ALCcontext *context); * ALCboolean alcMakeContextCurrent(ALCcontext *context);
*/ */
static jboolean JNICALL Java_org_lwjgl_openal_ALC_alcMakeContextCurrent (JNIEnv *env, jclass clazz, jint contextaddress) { static jint JNICALL Java_org_lwjgl_openal_ALC_alcMakeContextCurrent (JNIEnv *env, jclass clazz, jint contextaddress) {
ALCcontext* context = (ALCcontext*) contextaddress; ALCcontext* context = (ALCcontext*) contextaddress;
ALCenum result;
if(context == NULL) { if(context == NULL) {
return alcMakeContextCurrent(NULL); result = alcMakeContextCurrent(NULL);
} else {
result = alcMakeContextCurrent(context);
} }
return alcMakeContextCurrent(context); return result;
} }
/** /**
@ -360,7 +363,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_openal_ALC_initNativeStubs(JNIEnv *env, jc
{"alcOpenDevice", "(Ljava/lang/String;)Lorg/lwjgl/openal/ALCdevice;", (void*)&Java_org_lwjgl_openal_ALC_alcOpenDevice, "alcOpenDevice", (void**)&alcOpenDevice}, {"alcOpenDevice", "(Ljava/lang/String;)Lorg/lwjgl/openal/ALCdevice;", (void*)&Java_org_lwjgl_openal_ALC_alcOpenDevice, "alcOpenDevice", (void**)&alcOpenDevice},
{"alcCloseDevice", "(I)V", (void*)&Java_org_lwjgl_openal_ALC_alcCloseDevice, "alcCloseDevice", (void**)&alcCloseDevice}, {"alcCloseDevice", "(I)V", (void*)&Java_org_lwjgl_openal_ALC_alcCloseDevice, "alcCloseDevice", (void**)&alcCloseDevice},
{"alcCreateContext", "(ILjava/nio/IntBuffer;)Lorg/lwjgl/openal/ALCcontext;", (void*)&Java_org_lwjgl_openal_ALC_alcCreateContext, "alcCreateContext", (void**)&alcCreateContext}, {"alcCreateContext", "(ILjava/nio/IntBuffer;)Lorg/lwjgl/openal/ALCcontext;", (void*)&Java_org_lwjgl_openal_ALC_alcCreateContext, "alcCreateContext", (void**)&alcCreateContext},
{"alcMakeContextCurrent", "(I)Z", (void*)&Java_org_lwjgl_openal_ALC_alcMakeContextCurrent, "alcMakeContextCurrent", (void**)&alcMakeContextCurrent}, {"alcMakeContextCurrent", "(I)I", (void*)&Java_org_lwjgl_openal_ALC_alcMakeContextCurrent, "alcMakeContextCurrent", (void**)&alcMakeContextCurrent},
{"nalcProcessContext", "(I)V", (void*)&Java_org_lwjgl_openal_ALC_nalcProcessContext, "alcProcessContext", (void**)&alcProcessContext}, {"nalcProcessContext", "(I)V", (void*)&Java_org_lwjgl_openal_ALC_nalcProcessContext, "alcProcessContext", (void**)&alcProcessContext},
{"alcGetCurrentContext", "()Lorg/lwjgl/openal/ALCcontext;", (void*)&Java_org_lwjgl_openal_ALC_alcGetCurrentContext, "alcGetCurrentContext", (void**)&alcGetCurrentContext}, {"alcGetCurrentContext", "()Lorg/lwjgl/openal/ALCcontext;", (void*)&Java_org_lwjgl_openal_ALC_alcGetCurrentContext, "alcGetCurrentContext", (void**)&alcGetCurrentContext},
{"alcGetContextsDevice", "(I)Lorg/lwjgl/openal/ALCdevice;", (void*)&Java_org_lwjgl_openal_ALC_alcGetContextsDevice, "alcGetContextsDevice", (void**)&alcGetContextsDevice}, {"alcGetContextsDevice", "(I)Lorg/lwjgl/openal/ALCdevice;", (void*)&Java_org_lwjgl_openal_ALC_alcGetContextsDevice, "alcGetContextsDevice", (void**)&alcGetContextsDevice},