dbf-halloween2015

diff src/audio/audio.cc @ 0:50683c78264e

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:09:12 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/audio.cc	Sun Nov 01 00:09:12 2015 +0200
     1.3 @@ -0,0 +1,70 @@
     1.4 +#include <stdio.h>
     1.5 +
     1.6 +#include "openal.h"
     1.7 +#include "audio.h"
     1.8 +
     1.9 +static ALCdevice *dev;
    1.10 +static ALCcontext *ctx;
    1.11 +
    1.12 +bool init_audio()
    1.13 +{
    1.14 +	if (dev) {
    1.15 +		// Already initiated.
    1.16 +		return true;
    1.17 +	}
    1.18 +	else if (!(dev = alcOpenDevice(0))) {
    1.19 +		fprintf(stderr, "failed to open OpenAL device\n");
    1.20 +		return false;
    1.21 +	}
    1.22 +
    1.23 +	if (ctx)
    1.24 +	{
    1.25 +		return true;
    1.26 +	}
    1.27 +	else if (!(ctx = alcCreateContext(dev, 0)))
    1.28 +	{
    1.29 +		fprintf(stderr, "failed to create context\n");
    1.30 +		alcCloseDevice(dev);
    1.31 +		return false;
    1.32 +	}
    1.33 +
    1.34 +	alcMakeContextCurrent(ctx);
    1.35 +
    1.36 +	// Clear error state.
    1.37 +	alGetError();
    1.38 +
    1.39 +	return true;
    1.40 +}
    1.41 +
    1.42 +void destroy_audio()
    1.43 +{
    1.44 +	alcMakeContextCurrent(0);
    1.45 +
    1.46 +	if (ctx) {
    1.47 +		alcDestroyContext(ctx);
    1.48 +	}
    1.49 +
    1.50 +	if (dev) {
    1.51 +		alcCloseDevice(dev);
    1.52 +	}
    1.53 +}
    1.54 +
    1.55 +void set_audio_listener(const Matrix4x4 &xform)
    1.56 +{
    1.57 +	float pos[3], orient[6];
    1.58 +
    1.59 +	pos[0] = xform[0][3];
    1.60 +	pos[1] = xform[1][3];
    1.61 +	pos[2] = xform[2][3];
    1.62 +
    1.63 +	orient[0] = xform[0][2];
    1.64 +	orient[1] = xform[1][2];
    1.65 +	orient[2] = -xform[2][2];
    1.66 +
    1.67 +	orient[3] = xform[0][1];
    1.68 +	orient[4] = xform[1][1];
    1.69 +	orient[5] = xform[2][1];
    1.70 +
    1.71 +	alListenerfv(AL_POSITION, pos);
    1.72 +	alListenerfv(AL_ORIENTATION, orient);
    1.73 +}