Quake II RTX doxygen  1.0 dev
dynamic.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2010 Andrey Nazarov
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "shared/shared.h"
20 #include "system/system.h"
21 #include "common/cvar.h"
22 #include "common/common.h"
23 #include "common/files.h"
24 #include "dynamic.h"
25 #include <AL/alc.h>
26 
27 #define QALC_IMP \
28  QAL(LPALCCREATECONTEXT, alcCreateContext); \
29  QAL(LPALCMAKECONTEXTCURRENT, alcMakeContextCurrent); \
30  QAL(LPALCPROCESSCONTEXT, alcProcessContext); \
31  QAL(LPALCSUSPENDCONTEXT, alcSuspendContext); \
32  QAL(LPALCDESTROYCONTEXT, alcDestroyContext); \
33  QAL(LPALCGETCURRENTCONTEXT, alcGetCurrentContext); \
34  QAL(LPALCGETCONTEXTSDEVICE, alcGetContextsDevice); \
35  QAL(LPALCOPENDEVICE, alcOpenDevice); \
36  QAL(LPALCCLOSEDEVICE, alcCloseDevice); \
37  QAL(LPALCGETERROR, alcGetError); \
38  QAL(LPALCISEXTENSIONPRESENT, alcIsExtensionPresent); \
39  QAL(LPALCGETPROCADDRESS, alcGetProcAddress); \
40  QAL(LPALCGETENUMVALUE, alcGetEnumValue); \
41  QAL(LPALCGETSTRING, alcGetString); \
42  QAL(LPALCGETINTEGERV, alcGetIntegerv); \
43  QAL(LPALCCAPTUREOPENDEVICE, alcCaptureOpenDevice); \
44  QAL(LPALCCAPTURECLOSEDEVICE, alcCaptureCloseDevice); \
45  QAL(LPALCCAPTURESTART, alcCaptureStart); \
46  QAL(LPALCCAPTURESTOP, alcCaptureStop); \
47  QAL(LPALCCAPTURESAMPLES, alcCaptureSamples);
48 
49 static cvar_t *al_driver;
50 static cvar_t *al_device;
51 
52 static void *handle;
53 static ALCdevice *device;
54 static ALCcontext *context;
55 
56 #define QAL(type, func) static type q##func
58 #undef QAL
59 
60 #define QAL(type, func) type q##func
61 QAL_IMP
62 #undef QAL
63 
64 void QAL_Shutdown(void)
65 {
66  if (context) {
67  qalcMakeContextCurrent(NULL);
68  qalcDestroyContext(context);
69  context = NULL;
70  }
71  if (device) {
72  qalcCloseDevice(device);
73  device = NULL;
74  }
75 
76 #define QAL(type, func) q##func = NULL
77  QALC_IMP
78  QAL_IMP
79 #undef QAL
80 
81  if (handle) {
83  handle = NULL;
84  }
85 
86  if (al_driver)
87  al_driver->flags &= ~CVAR_SOUND;
88  if (al_device)
89  al_device->flags &= ~CVAR_SOUND;
90 }
91 
92 qboolean QAL_Init(void)
93 {
94  al_driver = Cvar_Get("al_driver", LIBAL, 0);
95  al_device = Cvar_Get("al_device", "", 0);
96 
97  // don't allow absolute or relative paths
99 
100  Sys_LoadLibrary(al_driver->string, NULL, &handle);
101  if (!handle) {
102  return qfalse;
103  }
104 
105 #define QAL(type, func) if ((q##func = Sys_GetProcAddress(handle, #func)) == NULL) goto fail;
106  QALC_IMP
107  QAL_IMP
108 #undef QAL
109 
110  device = qalcOpenDevice(al_device->string[0] ? al_device->string : NULL);
111  if (!device) {
112  Com_SetLastError(va("alcOpenDevice(%s) failed", al_device->string));
113  goto fail;
114  }
115 
116  context = qalcCreateContext(device, NULL);
117  if (!context) {
118  Com_SetLastError("alcCreateContext failed");
119  goto fail;
120  }
121 
122  if (!qalcMakeContextCurrent(context)) {
123  Com_SetLastError("alcMakeContextCurrent failed");
124  goto fail;
125  }
126 
127  al_driver->flags |= CVAR_SOUND;
128  al_device->flags |= CVAR_SOUND;
129 
130  return qtrue;
131 
132 fail:
133  QAL_Shutdown();
134  return qfalse;
135 }
136 
handle
static void * handle
Definition: dynamic.c:52
al_device
static cvar_t * al_device
Definition: dynamic.c:50
QAL_IMP
#define QAL_IMP
Definition: dynamic.h:27
QAL_Shutdown
QAL_IMP void QAL_Shutdown(void)
Definition: dynamic.c:64
Cvar_Get
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags)
Definition: cvar.c:257
device
static ALCdevice * device
Definition: dynamic.c:53
QALC_IMP
#define QALC_IMP
Definition: dynamic.c:27
dynamic.h
al_driver
static cvar_t * al_driver
Definition: dynamic.c:49
va
char * va(const char *format,...)
Definition: shared.c:429
QAL_Init
qboolean QAL_Init(void)
Definition: dynamic.c:92
Sys_LoadLibrary
void * Sys_LoadLibrary(const char *path, const char *sym, void **handle)
Definition: system.c:794
FS_SanitizeFilenameVariable
void FS_SanitizeFilenameVariable(cvar_t *var)
Definition: files.c:294
context
static ALCcontext * context
Definition: dynamic.c:54
Com_SetLastError
void Com_SetLastError(const char *msg)
Definition: common.c:382
Sys_FreeLibrary
void Sys_FreeLibrary(void *handle)
Definition: system.c:787