[14] | 1 | #include "alutInternal.h" |
---|
| 2 | |
---|
| 3 | static enum |
---|
| 4 | { |
---|
| 5 | Unintialized, /* ALUT has not been initialized yet or has been de-initialised */ |
---|
| 6 | ALUTDeviceAndContext, /* alutInit has been called successfully */ |
---|
| 7 | ExternalDeviceAndContext /* alutInitWithoutContext has been called */ |
---|
| 8 | } initialisationState = Unintialized; |
---|
| 9 | |
---|
| 10 | /* |
---|
| 11 | * Note: alutContext contains something valid only when initialisationState |
---|
| 12 | * contains ALUTDeviceAndContext. |
---|
| 13 | */ |
---|
| 14 | static ALCcontext *alutContext; |
---|
| 15 | |
---|
| 16 | ALboolean |
---|
| 17 | _alutSanityCheck (void) |
---|
| 18 | { |
---|
| 19 | ALCcontext *context; |
---|
| 20 | |
---|
| 21 | if (initialisationState == Unintialized) |
---|
| 22 | { |
---|
| 23 | _alutSetError (ALUT_ERROR_INVALID_OPERATION); |
---|
| 24 | return AL_FALSE; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | context = alcGetCurrentContext (); |
---|
| 28 | if (context == NULL) |
---|
| 29 | { |
---|
| 30 | _alutSetError (ALUT_ERROR_NO_CURRENT_CONTEXT); |
---|
| 31 | return AL_FALSE; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | if (alGetError () != AL_NO_ERROR) |
---|
| 35 | { |
---|
| 36 | _alutSetError (ALUT_ERROR_AL_ERROR_ON_ENTRY); |
---|
| 37 | return AL_FALSE; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | if (alcGetError (alcGetContextsDevice (context)) != ALC_NO_ERROR) |
---|
| 41 | { |
---|
| 42 | _alutSetError (ALUT_ERROR_ALC_ERROR_ON_ENTRY); |
---|
| 43 | return AL_FALSE; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | return AL_TRUE; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | ALboolean |
---|
| 50 | alutInit (int *argcp, char **argv) |
---|
| 51 | { |
---|
| 52 | ALCdevice *device; |
---|
| 53 | ALCcontext *context; |
---|
| 54 | |
---|
| 55 | if (initialisationState != Unintialized) |
---|
| 56 | { |
---|
| 57 | _alutSetError (ALUT_ERROR_INVALID_OPERATION); |
---|
| 58 | return AL_FALSE; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if ((argcp == NULL) != (argv == NULL)) |
---|
| 62 | { |
---|
| 63 | _alutSetError (ALUT_ERROR_INVALID_VALUE); |
---|
| 64 | return AL_FALSE; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | device = alcOpenDevice (NULL); |
---|
| 68 | if (device == NULL) |
---|
| 69 | { |
---|
| 70 | _alutSetError (ALUT_ERROR_OPEN_DEVICE); |
---|
| 71 | return AL_FALSE; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | context = alcCreateContext (device, NULL); |
---|
| 75 | if (context == NULL) |
---|
| 76 | { |
---|
| 77 | alcCloseDevice (device); |
---|
| 78 | _alutSetError (ALUT_ERROR_CREATE_CONTEXT); |
---|
| 79 | return AL_FALSE; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if (!alcMakeContextCurrent (context)) |
---|
| 83 | { |
---|
| 84 | alcDestroyContext (context); |
---|
| 85 | alcCloseDevice (device); |
---|
| 86 | _alutSetError (ALUT_ERROR_MAKE_CONTEXT_CURRENT); |
---|
| 87 | return AL_FALSE; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | initialisationState = ALUTDeviceAndContext; |
---|
| 91 | alutContext = context; |
---|
| 92 | return AL_TRUE; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | ALboolean |
---|
| 96 | alutInitWithoutContext (int *argcp, char **argv) |
---|
| 97 | { |
---|
| 98 | if (initialisationState != Unintialized) |
---|
| 99 | { |
---|
| 100 | _alutSetError (ALUT_ERROR_INVALID_OPERATION); |
---|
| 101 | return AL_FALSE; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if ((argcp == NULL) != (argv == NULL)) |
---|
| 105 | { |
---|
| 106 | _alutSetError (ALUT_ERROR_INVALID_VALUE); |
---|
| 107 | return AL_FALSE; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | initialisationState = ExternalDeviceAndContext; |
---|
| 111 | return AL_TRUE; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | ALboolean |
---|
| 115 | alutExit (void) |
---|
| 116 | { |
---|
| 117 | ALCdevice *device; |
---|
| 118 | |
---|
| 119 | if (initialisationState == Unintialized) |
---|
| 120 | { |
---|
| 121 | _alutSetError (ALUT_ERROR_INVALID_OPERATION); |
---|
| 122 | return AL_FALSE; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (initialisationState == ExternalDeviceAndContext) |
---|
| 126 | { |
---|
| 127 | initialisationState = Unintialized; |
---|
| 128 | return AL_TRUE; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if (!_alutSanityCheck ()) |
---|
| 132 | { |
---|
| 133 | return AL_FALSE; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (!alcMakeContextCurrent (NULL)) |
---|
| 137 | { |
---|
| 138 | _alutSetError (ALUT_ERROR_MAKE_CONTEXT_CURRENT); |
---|
| 139 | return AL_FALSE; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | device = alcGetContextsDevice (alutContext); |
---|
| 143 | alcDestroyContext (alutContext); |
---|
| 144 | if (alcGetError (device) != ALC_NO_ERROR) |
---|
| 145 | { |
---|
| 146 | _alutSetError (ALUT_ERROR_DESTROY_CONTEXT); |
---|
| 147 | return AL_FALSE; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | if (!alcCloseDevice (device)) |
---|
| 151 | { |
---|
| 152 | _alutSetError (ALUT_ERROR_CLOSE_DEVICE); |
---|
| 153 | return AL_FALSE; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | initialisationState = Unintialized; |
---|
| 157 | return AL_TRUE; |
---|
| 158 | } |
---|