Skip to content

Commit

Permalink
OpenAL: Output errors to std::cerr with DENG_DSOPENAL_DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed May 13, 2012
1 parent 8463b14 commit 4e744ec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
2 changes: 2 additions & 0 deletions doomsday/plugins/openal/openal.pro
Expand Up @@ -11,6 +11,8 @@ win32|macx: TARGET = dsOpenAL

VERSION = $$OPENAL_VERSION

#DEFINES += DENG_DSOPENAL_DEBUG

INCLUDEPATH += include

HEADERS += include/version.h
Expand Down
95 changes: 57 additions & 38 deletions doomsday/plugins/openal/src/driver_openal.cpp
Expand Up @@ -44,12 +44,15 @@
#include <openal/al.h>
#include <openal/alc.h>
#endif
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <cassert>
#include <iostream>
#include <cstring>
#include <cmath>

#include "doomsday.h"
#include "sys_audiod.h"
#include "sys_audiod_sfx.h"
#include "doomsday.h"

#define PI 3.141592654

Expand Down Expand Up @@ -92,65 +95,74 @@ struct _GUID DSPROPSETID_EAX20_BufferProperties = {
#endif

boolean initOk = false, hasEAX = false;
int verbose;
float unitsPerMeter = 1;
float headYaw, headPitch; // In radians.
ALCdevice* device = 0;
ALCcontext* context = 0;

static int error(const char* what, const char* msg)
{
ALenum code = alGetError();
if(code == AL_NO_ERROR) return false;
#ifdef DENG_DSOPENAL_DEBUG
# define DSOPENAL_TRACE(args) std::cerr << "[dsOpenAL] " << args << std::endl;
#else
# define DSOPENAL_TRACE(args)
#endif

Con_Message("DS_%s(OpenAL): %s [%s]\n", what, msg, alGetString(code));
#define DSOPENAL_ERRCHECK(errorcode) \
error(errorcode, __FILE__, __LINE__)

static int error(ALenum errorCode, const char* file, int line)
{
if(errorCode == AL_NO_ERROR) return false;
std::cerr << "[dsOpenAL] Error at " << file << ", line " << line
<< ": (" << (int)errorCode << ") " << (const char*)alGetString(errorCode);
return true;
}

int DS_Init(void)
static void loadExtensions(void)
{
if(initOk) return true;

// Are we in verbose mode?
if((verbose = ArgExists("-verbose")))
Con_Message("DS_Init(OpenAL): Starting OpenAL...\n");

// Open device.
if(!(device = alcOpenDevice((ALCchar*) "DirectSound3D")))
{
Con_Message("Failed to initialize OpenAL (DS3D).\n");
return false;
}
// Create a context.
alcMakeContextCurrent(context = alcCreateContext(device, NULL));

// Clear error message.
alGetError();

#ifdef WIN32
// Check for EAX 2.0.
hasEAX = alIsExtensionPresent((ALchar*) "EAX2.0");
if(hasEAX)
{
EAXGet = (ALenum (*)(const struct _GUID*, ALuint, ALuint, ALvoid*, ALuint))alGetProcAddress("EAXGet");
EAXSet = (ALenum (*)(const struct _GUID*, ALuint, ALuint, ALvoid*, ALuint))alGetProcAddress("EAXSet");

if(!EAXGet || !EAXSet)
hasEAX = false;
}

if(hasEAX && verbose)
Con_Message("DS_Init(OpenAL): EAX 2.0 available.\n");
#else
hasEAX = false;
#endif
}

int DS_Init(void)
{
// Already initialized?
if(initOk) return true;

// Open a playback device.
/// @todo Shouldn't we use the system default device?
device = alcOpenDevice((ALCchar*) "DirectSound3D");
if(!device)
{
Con_Message("OpenAL init failed (device: DirectSound3D).\n");
return false;
}

// Create and make current a new context.
alcMakeContextCurrent(context = alcCreateContext(device, NULL));
DSOPENAL_ERRCHECK(alGetError());

// Attempt to load and configure the EAX extensions.
loadExtensions();

// Configure the listener and global OpenAL properties/state.
alListenerf(AL_GAIN, 1);
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
headYaw = headPitch = 0;
unitsPerMeter = 36;

// Everything is OK.
DSOPENAL_TRACE("DS_Init: OpenAL initialized%s." << hasEAX? " (EAX 2.0 available)" : "");
initOk = true;
return true;
}
Expand Down Expand Up @@ -185,19 +197,24 @@ sfxbuffer_t* DS_SFX_CreateBuffer(int flags, int bits, int rate)

// Create a new buffer and a new source.
alGenBuffers(1, &bufName);
if(error("CreateBuffer", "GenBuffers"))
if(DSOPENAL_ERRCHECK(alGetError()))
return NULL;

alGenSources(1, &srcName);
if(error("CreateBuffer", "GenSources"))
if(DSOPENAL_ERRCHECK(alGetError()))
{
alDeleteBuffers(1, &bufName);
return NULL;
}

// Attach the buffer to the source.
alSourcei(srcName, AL_BUFFER, bufName);
error("CreateBuffer", "Source BUFFER");
if(DSOPENAL_ERRCHECK(alGetError()))
{
alDeleteSources(1, &srcName);
alDeleteBuffers(1, &bufName);
return NULL;
}

if(!(flags & SFXBF_3D))
{
Expand Down Expand Up @@ -250,7 +267,10 @@ void DS_SFX_Load(sfxbuffer_t* buf, struct sfxsample_s* sample)
sample->bytesPer == 1 ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16,
sample->data, sample->size, sample->rate);

error("Load", "BufferData");
if(DSOPENAL_ERRCHECK(alGetError()))
{
/// @fixme What to do?
}
buf->sample = sample;
}

Expand Down Expand Up @@ -284,8 +304,7 @@ void DS_SFX_Play(sfxbuffer_t* buf)
alSourcei(source, AL_BUFFER, BUF(buf));
alSourcei(source, AL_LOOPING, (buf->flags & SFXBF_REPEAT) != 0);
alSourcePlay(source);
error("Play", "SourcePlay");
error("Play", "Get state");
DSOPENAL_ERRCHECK(alGetError());

// The buffer is now playing.
buf->flags |= SFXBF_PLAYING;
Expand Down

0 comments on commit 4e744ec

Please sign in to comment.