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