diff --git a/src/Audio.c b/src/Audio.c index 224d3ce..afd0590 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -5,12 +5,12 @@ #include "Dengine/Assets.h" #include "Dengine/Audio.h" -#include "Dengine/Common.h" #include "Dengine/Debug.h" #define AL_CHECK_ERROR() alCheckError_(__FILE__, __LINE__); void alCheckError_(const char *file, int line) { + // NOTE(doyle): OpenAL error stack is 1 deep ALenum errorCode; while ((errorCode = alGetError()) != AL_NO_ERROR) { @@ -40,16 +40,50 @@ void alCheckError_(const char *file, int line) } }; -const i32 audio_rendererInit(AudioRenderer *audioRenderer) +const i32 audio_init() { + /* Clear error stack */ alGetError(); - ALCdevice *deviceAL = alcOpenDevice(NULL); + + ALboolean enumerateAudioDevice = + alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"); + + const ALCchar *device = NULL; + if (enumerateAudioDevice == AL_TRUE) + { + // TODO(doyle): Actually allow users to choose device to output + /* + The OpenAL specification says that the list of devices is organized as + a string devices are separated with a NULL character and the list is + terminated by two NULL characters. + + alcGetString with NULL = get all device spcifiers not a particular one + */ + device = alcGetString(NULL, ALC_DEVICE_SPECIFIER); + const ALCchar *next = device + 1; + size_t len = 0; + + printf("Devices list:\n"); + printf("----------\n"); + while (device && *device != '\0' && next && *next != '\0') + { + printf("%s\n", device); + len = common_strlen(device); + device += (len + 1); + next += (len + 2); + } + printf("----------\n"); + } + + /* Get audio device */ + ALCdevice *deviceAL = alcOpenDevice(device); if (!deviceAL) { printf("alcOpenDevice() failed: Failed to init OpenAL device.\n"); return -1; } + /* Set device context */ ALCcontext *contextAL = alcCreateContext(deviceAL, NULL); alcMakeContextCurrent(contextAL); if (!contextAL) @@ -59,6 +93,11 @@ const i32 audio_rendererInit(AudioRenderer *audioRenderer) } AL_CHECK_ERROR(); + return 0; +} + +const i32 audio_rendererInit(AudioRenderer *audioRenderer) +{ /* Generate number of concurrent audio file listeners */ alGenSources(ARRAY_COUNT(audioRenderer->sourceId), audioRenderer->sourceId); AL_CHECK_ERROR(); diff --git a/src/dengine.c b/src/dengine.c index b2265c7..39a3a41 100644 --- a/src/dengine.c +++ b/src/dengine.c @@ -113,6 +113,14 @@ int main() * INITIALISE AUDIO ******************* */ + i32 result = audio_init(); + if (result) + { +#ifdef DENGINE_DEBUG + ASSERT(INVALID_CODE_PATH); +#endif + } + AudioRenderer audioRenderer = {0}; audio_rendererInit(&audioRenderer); @@ -128,6 +136,12 @@ int main() audio_streamVorbis(&audioRenderer, audio); + AudioRenderer overworldAudioRenderer = {0}; + audio_rendererInit(&overworldAudioRenderer); + AudioVorbis *overworldAudio = + asset_getVorbis(&worldTraveller.assetManager, audiolist_overworld); + audio_streamVorbis(&overworldAudioRenderer, overworldAudio); + /* ******************* * GAME LOOP @@ -159,6 +173,7 @@ int main() worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed); GL_CHECK_ERROR(); audio_updateAndPlay(&audioRenderer); + //audio_updateAndPlay(&overworldAudioRenderer); /* Swap the buffers */ glfwSwapBuffers(window); diff --git a/src/include/Dengine/Audio.h b/src/include/Dengine/Audio.h index e172f5f..a06e4b7 100644 --- a/src/include/Dengine/Audio.h +++ b/src/include/Dengine/Audio.h @@ -3,6 +3,8 @@ #include +#include "Dengine/Common.h" + typedef struct AudioRenderer { ALuint sourceId[1]; @@ -12,6 +14,7 @@ typedef struct AudioRenderer ALuint format; } AudioRenderer; +const i32 audio_init(); const i32 audio_rendererInit(AudioRenderer *audioRenderer); void audio_streamVorbis(AudioRenderer *audioRenderer, AudioVorbis *vorbis); void audio_updateAndPlay(AudioRenderer *audioRenderer);