dbf-halloween2015

view src/audio/audio.cc @ 3:c37fe5d8a4ed

windows port
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:04:28 +0200
parents
children
line source
1 #include <stdio.h>
3 #include "openal.h"
4 #include "audio.h"
6 static ALCdevice *dev;
7 static ALCcontext *ctx;
9 bool init_audio()
10 {
11 if (dev) {
12 // Already initiated.
13 return true;
14 }
15 else if (!(dev = alcOpenDevice(0))) {
16 fprintf(stderr, "failed to open OpenAL device\n");
17 return false;
18 }
20 if (ctx)
21 {
22 return true;
23 }
24 else if (!(ctx = alcCreateContext(dev, 0)))
25 {
26 fprintf(stderr, "failed to create context\n");
27 alcCloseDevice(dev);
28 return false;
29 }
31 alcMakeContextCurrent(ctx);
33 // Clear error state.
34 alGetError();
36 return true;
37 }
39 void destroy_audio()
40 {
41 alcMakeContextCurrent(0);
43 if (ctx) {
44 alcDestroyContext(ctx);
45 }
47 if (dev) {
48 alcCloseDevice(dev);
49 }
50 }
52 void set_audio_listener(const Matrix4x4 &xform)
53 {
54 float pos[3], orient[6];
56 pos[0] = xform[0][3];
57 pos[1] = xform[1][3];
58 pos[2] = xform[2][3];
60 orient[0] = xform[0][2];
61 orient[1] = xform[1][2];
62 orient[2] = -xform[2][2];
64 orient[3] = xform[0][1];
65 orient[4] = xform[1][1];
66 orient[5] = xform[2][1];
68 alListenerfv(AL_POSITION, pos);
69 alListenerfv(AL_ORIENTATION, orient);
70 }