From fe79ab351ab6faa585d88de3cce6f5b806b276ed Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 18 May 2008 18:40:53 -0700 Subject: [PATCH] Add a simple example that prints out some OpenAL info --- CMakeLists.txt | 9 ++- examples/openal-info.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 examples/openal-info.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 363c7974..09ba20d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,9 @@ OPTION(WINMM "Check for Windows Multimedia backend" ON) OPTION(DLOPEN "Check for the dlopen API for loading optional libs" ON) -OPTION(WERROR "Treat compile warnings as errors" OFF) +OPTION(WERROR "Treat compile warnings as errors" OFF) + +OPTION(EXAMPLES "Build example programs" ON) SET(LIB_MAJOR_VERSION "1") @@ -316,6 +318,11 @@ INSTALL(FILES include/AL/al.h DESTINATION include/AL ) +IF(EXAMPLES) + ADD_EXECUTABLE(openal-info examples/openal-info.c) + TARGET_LINK_LIBRARIES(openal-info ${LIBNAME}) +ENDIF() + MESSAGE(STATUS "") MESSAGE(STATUS "Building OpenAL with support for the following backends:") MESSAGE(STATUS " ${BACKENDS}") diff --git a/examples/openal-info.c b/examples/openal-info.c new file mode 100644 index 00000000..1693e899 --- /dev/null +++ b/examples/openal-info.c @@ -0,0 +1,170 @@ +/* + * openal-info: Display information about ALC and AL. + * + * Idea based on glxinfo for OpenGL. + * Initial OpenAL version by Erik Hofman . + * Further hacked by Sven Panne . + * + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +#include "AL/alc.h" +#include "AL/al.h" +#include "AL/alext.h" + + +static const int indentation = 4; +static const int maxmimumWidth = 79; + +static void printChar(int c, int *width) +{ + putchar(c); + *width = ((c == '\n') ? 0 : ((*width) + 1)); +} + +static void indent(int *width) +{ + int i; + for(i = 0; i < indentation; i++) + printChar(' ', width); +} + +static void printExtensions(const char *header, char separator, const char *extensions) +{ + int width = 0, start = 0, end = 0; + + printf("%s:\n", header); + if(extensions == NULL || extensions[0] == '\0') + return; + + indent(&width); + while (1) + { + if(extensions[end] == separator || extensions[end] == '\0') + { + if(width + end - start + 2 > maxmimumWidth) + { + printChar('\n', &width); + indent(&width); + } + while(start < end) + { + printChar(extensions[start], &width); + start++; + } + if(extensions[end] == '\0') + break; + start++; + end++; + if(extensions[end] == '\0') + break; + printChar(',', &width); + printChar(' ', &width); + } + end++; + } + printChar('\n', &width); +} + +static void die(const char *kind, const char *description) +{ + fprintf(stderr, "%s error %s occured\n", kind, description); + exit(EXIT_FAILURE); +} + +static void checkForErrors(void) +{ + { + ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext()); + ALCenum error = alcGetError(device); + if(error != ALC_NO_ERROR) + die("ALC", (const char*)alcGetString(device, error)); + } + { + ALenum error = alGetError(); + if(error != AL_NO_ERROR) + die("AL", (const char*)alGetString(error)); + } +} + +static void printDevices(ALCenum which, const char *kind) +{ + const char *s = alcGetString(NULL, which); + checkForErrors(); + + printf("Available %sdevices:\n", kind); + while(*s != '\0') + { + printf(" %s\n", s); + while(*s++ != '\0') + ; + } +} + +static void printALCInfo (void) +{ + ALCint major, minor; + ALCdevice *device; + + if(alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE) + { + if(alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATE_ALL_EXT") == AL_TRUE) + printDevices(ALC_ALL_DEVICES_SPECIFIER, "playback "); + else + printDevices(ALC_DEVICE_SPECIFIER, "playback "); + printDevices(ALC_CAPTURE_DEVICE_SPECIFIER, "capture "); + } + else + printf("No device enumeration available\n"); + + device = alcGetContextsDevice(alcGetCurrentContext()); + checkForErrors(); + + printf("Default device: %s\n", + alcGetString(device, ALC_DEFAULT_DEVICE_SPECIFIER)); + + printf("Default capture device: %s\n", + alcGetString(device, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER)); + + alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major); + alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &minor); + checkForErrors(); + printf("ALC version: %d.%d\n", (int)major, (int)minor); + + printExtensions("ALC extensions", ' ', + alcGetString(device, ALC_EXTENSIONS)); + checkForErrors(); +} + +static void printALInfo(void) +{ + printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR)); + printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER)); + printf("OpenAL version string: %s\n", alGetString(AL_VERSION)); + printExtensions("OpenAL extensions", ' ', alGetString(AL_EXTENSIONS)); + checkForErrors(); +} + +int main() +{ + ALCdevice *device = alcOpenDevice(NULL); + ALCcontext *context = alcCreateContext(device, NULL); + alcMakeContextCurrent(context); + checkForErrors(); + + printALCInfo(); + printALInfo(); + checkForErrors(); + + alcMakeContextCurrent(NULL); + alcDestroyContext(context); + alcCloseDevice(device); + + return EXIT_SUCCESS; +} -- 2.11.4.GIT