]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
fix skybox loading in q3 shaders, needed to append a _ to the end of the skybox name
[divverent/darkplaces.git] / cd_shared.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
21 // rights reserved.
22
23 #include "quakedef.h"
24
25 // Prototypes of the system dependent functions
26 extern void CDAudio_SysEject (void);
27 extern void CDAudio_SysCloseDoor (void);
28 extern int CDAudio_SysGetAudioDiskInfo (void);
29 extern float CDAudio_SysGetVolume (void);
30 extern void CDAudio_SysSetVolume (float volume);
31 extern int CDAudio_SysPlay (qbyte track);
32 extern int CDAudio_SysStop (void);
33 extern int CDAudio_SysPause (void);
34 extern int CDAudio_SysResume (void);
35 extern int CDAudio_SysUpdate (void);
36 extern void CDAudio_SysInit (void);
37 extern int CDAudio_SysStartup (void);
38 extern void CDAudio_SysShutdown (void);
39
40 // used by menu to ghost CD audio slider
41 cvar_t  cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0"};
42
43 static qboolean wasPlaying = false;
44 static qboolean initialized = false;
45 static qboolean enabled = false;
46 static float cdvolume;
47 static qbyte remap[100];
48 static qbyte maxTrack;
49 static int faketrack = -1;
50
51 static float saved_vol = 1.0f;
52
53 // exported variables
54 qboolean cdValid = false;
55 qboolean cdPlaying = false;
56 qboolean cdPlayLooping = false;
57 qbyte cdPlayTrack;
58
59
60 static void CDAudio_Eject (void)
61 {
62         if (!enabled)
63                 return;
64
65         CDAudio_SysEject();
66 }
67
68
69 static void CDAudio_CloseDoor (void)
70 {
71         if (!enabled)
72                 return;
73
74         CDAudio_SysCloseDoor();
75 }
76
77 static int CDAudio_GetAudioDiskInfo (void)
78 {
79         int ret;
80
81         cdValid = false;
82
83         ret = CDAudio_SysGetAudioDiskInfo();
84         if (ret < 1)
85                 return -1;
86
87         cdValid = true;
88         maxTrack = ret;
89
90         return 0;
91 }
92
93
94 void CDAudio_Play (qbyte track, qboolean looping)
95 {
96         sfx_t* sfx;
97
98         if (!enabled)
99                 return;
100
101         track = remap[track];
102         if (track < 1)
103         {
104                 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
105                 return;
106         }
107
108         if (cdPlaying && cdPlayTrack == track)
109                 return;
110         CDAudio_Stop ();
111
112         // Try playing a fake track (sound file) first
113         sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false);
114         if (sfx != NULL)
115         {
116                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
117                 if (faketrack != -1)
118                 {
119                         if (looping)
120                         S_LoopChannel (faketrack, true);
121                         Con_DPrintf ("Fake CD track %u playing...\n", track);
122                 }
123         }
124
125         // If we can't play a fake CD track, try the real one
126         if (faketrack == -1)
127         {
128                 if (!cdValid)
129                 {
130                         CDAudio_GetAudioDiskInfo();
131                         if (!cdValid)
132                         {
133                                 Con_Print ("No CD in player.\n");
134                                 return;
135                         }
136                 }
137
138                 if (track > maxTrack)
139                 {
140                         Con_Printf("CDAudio: Bad track number %u.\n", track);
141                         return;
142                 }
143
144                 if (CDAudio_SysPlay(track) == -1)
145                         return;
146         }
147
148         cdPlayLooping = looping;
149         cdPlayTrack = track;
150         cdPlaying = true;
151
152         if (cdvolume == 0.0)
153                 CDAudio_Pause ();
154 }
155
156
157 void CDAudio_Stop (void)
158 {
159         if (!enabled || !cdPlaying)
160                 return;
161
162         if (faketrack != -1)
163         {
164                 S_StopChannel (faketrack);
165                 faketrack = -1;
166         }
167         else if (CDAudio_SysStop() == -1)
168                 return;
169
170         wasPlaying = false;
171         cdPlaying = false;
172 }
173
174 void CDAudio_Pause (void)
175 {
176         if (!enabled || !cdPlaying)
177                 return;
178
179         if (faketrack != -1)
180                 S_PauseChannel (faketrack, true);
181         else if (CDAudio_SysPause() == -1)
182                 return;
183
184         wasPlaying = cdPlaying;
185         cdPlaying = false;
186 }
187
188
189 void CDAudio_Resume (void)
190 {
191         if (!enabled || cdPlaying || !wasPlaying)
192                 return;
193
194         if (faketrack != -1)
195                 S_PauseChannel (faketrack, false);
196         else if (CDAudio_SysResume() == -1)
197                 return;
198         cdPlaying = true;
199 }
200
201 static void CD_f (void)
202 {
203         const char *command;
204         int ret;
205         int n;
206
207         if (Cmd_Argc() < 2)
208                 return;
209
210         command = Cmd_Argv (1);
211
212         if (strcasecmp(command, "on") == 0)
213         {
214                 enabled = true;
215                 return;
216         }
217
218         if (strcasecmp(command, "off") == 0)
219         {
220                 if (cdPlaying)
221                         CDAudio_Stop();
222                 enabled = false;
223                 return;
224         }
225
226         if (strcasecmp(command, "reset") == 0)
227         {
228                 enabled = true;
229                 if (cdPlaying)
230                         CDAudio_Stop();
231                 for (n = 0; n < 100; n++)
232                         remap[n] = n;
233                 CDAudio_GetAudioDiskInfo();
234                 return;
235         }
236
237         if (strcasecmp(command, "remap") == 0)
238         {
239                 ret = Cmd_Argc() - 2;
240                 if (ret <= 0)
241                 {
242                         for (n = 1; n < 100; n++)
243                                 if (remap[n] != n)
244                                         Con_Printf("  %u -> %u\n", n, remap[n]);
245                         return;
246                 }
247                 for (n = 1; n <= ret; n++)
248                         remap[n] = atoi(Cmd_Argv (n+1));
249                 return;
250         }
251
252         if (strcasecmp(command, "close") == 0)
253         {
254                 CDAudio_CloseDoor();
255                 return;
256         }
257
258         if (strcasecmp(command, "play") == 0)
259         {
260                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
261                 return;
262         }
263
264         if (strcasecmp(command, "loop") == 0)
265         {
266                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
267                 return;
268         }
269
270         if (strcasecmp(command, "stop") == 0)
271         {
272                 CDAudio_Stop();
273                 return;
274         }
275
276         if (strcasecmp(command, "pause") == 0)
277         {
278                 CDAudio_Pause();
279                 return;
280         }
281
282         if (strcasecmp(command, "resume") == 0)
283         {
284                 CDAudio_Resume();
285                 return;
286         }
287
288         if (strcasecmp(command, "eject") == 0)
289         {
290                 if (cdPlaying && faketrack == -1)
291                         CDAudio_Stop();
292                 CDAudio_Eject();
293                 cdValid = false;
294                 return;
295         }
296
297         if (strcasecmp(command, "info") == 0)
298         {
299                 CDAudio_GetAudioDiskInfo ();
300                 if (cdValid)
301                         Con_Printf("%u tracks on CD.\n", maxTrack);
302                 else
303                         Con_Print ("No CD in player.\n");
304                 if (cdPlaying)
305                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
306                 else if (wasPlaying)
307                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
308                 Con_Printf("Volume is %f\n", cdvolume);
309                 return;
310         }
311 }
312
313 void CDAudio_SetVolume (float newvol)
314 {
315         // If the volume hasn't changed
316         if (newvol == cdvolume)
317                 return;
318
319         // If the CD has been muted
320         if (newvol == 0.0f)
321                 CDAudio_Pause ();
322         else
323         {
324                 // If the CD has been unmuted
325                 if (cdvolume == 0.0f)
326                         CDAudio_Resume ();
327
328                 if (faketrack != -1)
329                         S_SetChannelVolume (faketrack, newvol);
330                 CDAudio_SysSetVolume (newvol);
331         }
332
333         cdvolume = newvol;
334 }
335
336 void CDAudio_Update (void)
337 {
338         if (!enabled)
339                 return;
340
341         CDAudio_SetVolume (bgmvolume.value);
342
343         if (faketrack == -1)
344                 CDAudio_SysUpdate();
345 }
346
347 int CDAudio_Init (void)
348 {
349         int i;
350
351         if (cls.state == ca_dedicated)
352                 return -1;
353
354         if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
355                 return -1;
356
357         CDAudio_SysInit();
358
359         for (i = 0; i < 100; i++)
360                 remap[i] = i;
361
362         Cvar_RegisterVariable(&cdaudioinitialized);
363         Cvar_SetValueQuick(&cdaudioinitialized, true);
364         enabled = true;
365
366         Cmd_AddCommand("cd", CD_f);
367
368         return 0;
369 }
370
371 int CDAudio_Startup (void)
372 {
373         CDAudio_SysStartup ();
374
375         if (CDAudio_GetAudioDiskInfo())
376         {
377                 Con_DPrint("CDAudio_Init: No CD in player.\n");
378                 cdValid = false;
379         }
380
381         saved_vol = CDAudio_SysGetVolume ();
382         if (saved_vol < 0.0f)
383         {
384                 Con_DPrint ("Can't get initial CD volume\n");
385                 saved_vol = 1.0f;
386         }
387         else
388                 Con_DPrintf ("Initial CD volume: %g\n", saved_vol);
389
390         initialized = true;
391
392         Con_DPrint("CD Audio Initialized\n");
393
394         return 0;
395 }
396
397 void CDAudio_Shutdown (void)
398 {
399         if (!initialized)
400                 return;
401
402         CDAudio_SysSetVolume (saved_vol);
403
404         CDAudio_Stop();
405         CDAudio_SysShutdown();
406         initialized = false;
407 }