nuclear@0: #include nuclear@0: nuclear@0: #include "openal.h" nuclear@0: #include "audio.h" nuclear@0: nuclear@0: static ALCdevice *dev; nuclear@0: static ALCcontext *ctx; nuclear@0: nuclear@0: bool init_audio() nuclear@0: { nuclear@0: if (dev) { nuclear@0: // Already initiated. nuclear@0: return true; nuclear@0: } nuclear@0: else if (!(dev = alcOpenDevice(0))) { nuclear@0: fprintf(stderr, "failed to open OpenAL device\n"); nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: if (ctx) nuclear@0: { nuclear@0: return true; nuclear@0: } nuclear@0: else if (!(ctx = alcCreateContext(dev, 0))) nuclear@0: { nuclear@0: fprintf(stderr, "failed to create context\n"); nuclear@0: alcCloseDevice(dev); nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: alcMakeContextCurrent(ctx); nuclear@0: nuclear@0: // Clear error state. nuclear@0: alGetError(); nuclear@0: nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: void destroy_audio() nuclear@0: { nuclear@0: alcMakeContextCurrent(0); nuclear@0: nuclear@0: if (ctx) { nuclear@0: alcDestroyContext(ctx); nuclear@0: } nuclear@0: nuclear@0: if (dev) { nuclear@0: alcCloseDevice(dev); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void set_audio_listener(const Matrix4x4 &xform) nuclear@0: { nuclear@0: float pos[3], orient[6]; nuclear@0: nuclear@0: pos[0] = xform[0][3]; nuclear@0: pos[1] = xform[1][3]; nuclear@0: pos[2] = xform[2][3]; nuclear@0: nuclear@0: orient[0] = xform[0][2]; nuclear@0: orient[1] = xform[1][2]; nuclear@0: orient[2] = -xform[2][2]; nuclear@0: nuclear@0: orient[3] = xform[0][1]; nuclear@0: orient[4] = xform[1][1]; nuclear@0: orient[5] = xform[2][1]; nuclear@0: nuclear@0: alListenerfv(AL_POSITION, pos); nuclear@0: alListenerfv(AL_ORIENTATION, orient); nuclear@0: }