]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
patch for nexuiz from m0rfar to colorize health/armor according to amount
[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 "sound.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 (unsigned char 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","indicates if CD Audio system is active"};
46
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 static unsigned char remap[MAXTRACKS];
52 static unsigned char 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 unsigned char cdPlayTrack;
62
63 cl_cdstate_t cd;
64
65 static void CDAudio_Eject (void)
66 {
67         if (!enabled)
68                 return;
69
70         CDAudio_SysEject();
71 }
72
73
74 static void CDAudio_CloseDoor (void)
75 {
76         if (!enabled)
77                 return;
78
79         CDAudio_SysCloseDoor();
80 }
81
82 static int CDAudio_GetAudioDiskInfo (void)
83 {
84         int ret;
85
86         cdValid = false;
87
88         ret = CDAudio_SysGetAudioDiskInfo();
89         if (ret < 1)
90                 return -1;
91
92         cdValid = true;
93         maxTrack = ret;
94
95         return 0;
96 }
97
98
99 void CDAudio_Play_byName (const char *trackname, qboolean looping)
100 {
101         unsigned char track;
102         sfx_t* sfx;
103
104         Host_StartVideo();
105
106         if (!enabled)
107                 return;
108
109         if(strspn(trackname, "0123456789") == strlen(trackname))
110         {
111                 track = (unsigned char) atoi(trackname);
112                 track = remap[track];
113                 if (track < 1)
114                 {
115                         Con_Printf("CDAudio: Bad track number %u.\n", track);
116                         return;
117                 }
118         }
119         else
120                 track = 0;
121
122         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
123                 return;
124         CDAudio_Stop ();
125
126         // Try playing a fake track (sound file) first
127         if(track >= 1)
128         {
129                 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
130                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
131                         sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
132         }
133         else
134         {
135                 sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
136         }
137         if (sfx != NULL)
138         {
139                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
140                 if (faketrack != -1)
141                 {
142                         if (looping)
143                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
144                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
145                         if(track >= 1)
146                                 Con_Printf ("Fake CD track %u playing...\n", track);
147                         else
148                                 Con_Printf ("BGM track %s playing...\n", trackname);
149                 }
150         }
151
152         // If we can't play a fake CD track, try the real one
153         if (faketrack == -1)
154         {
155                 if(track < 1)
156                 {
157                         Con_Print("Could not load BGM track.\n");
158                         return;
159                 }
160         
161                 if (!cdValid)
162                 {
163                         CDAudio_GetAudioDiskInfo();
164                         if (!cdValid)
165                         {
166                                 Con_Print ("No CD in player.\n");
167                                 return;
168                         }
169                 }
170
171                 if (track > maxTrack)
172                 {
173                         Con_Printf("CDAudio: Bad track number %u.\n", track);
174                         return;
175                 }
176
177                 if (CDAudio_SysPlay(track) == -1)
178                         return;
179         }
180
181         cdPlayLooping = looping;
182         cdPlayTrack = track;
183         cdPlaying = true;
184
185         if (cdvolume == 0.0)
186                 CDAudio_Pause ();
187 }
188
189 void CDAudio_Play (unsigned char track, qboolean looping)
190 {
191         char buf[20];
192         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
193         CDAudio_Play_byName(buf, looping);
194 }
195
196 void CDAudio_Stop (void)
197 {
198         if (!enabled || !cdPlaying)
199                 return;
200
201         if (faketrack != -1)
202         {
203                 S_StopChannel (faketrack);
204                 faketrack = -1;
205         }
206         else if (CDAudio_SysStop() == -1)
207                 return;
208
209         wasPlaying = false;
210         cdPlaying = false;
211 }
212
213 void CDAudio_Pause (void)
214 {
215         if (!enabled || !cdPlaying)
216                 return;
217
218         if (faketrack != -1)
219                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
220         else if (CDAudio_SysPause() == -1)
221                 return;
222
223         wasPlaying = cdPlaying;
224         cdPlaying = false;
225 }
226
227
228 void CDAudio_Resume (void)
229 {
230         if (!enabled || cdPlaying || !wasPlaying)
231                 return;
232
233         if (faketrack != -1)
234                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
235         else if (CDAudio_SysResume() == -1)
236                 return;
237         cdPlaying = true;
238 }
239
240 static void CD_f (void)
241 {
242         const char *command;
243         int ret;
244         int n;
245
246         Host_StartVideo();
247
248         if (Cmd_Argc() < 2)
249                 return;
250
251         command = Cmd_Argv (1);
252
253         if (strcasecmp(command, "on") == 0)
254         {
255                 enabled = true;
256                 return;
257         }
258
259         if (strcasecmp(command, "off") == 0)
260         {
261                 if (cdPlaying)
262                         CDAudio_Stop();
263                 enabled = false;
264                 return;
265         }
266
267         if (strcasecmp(command, "reset") == 0)
268         {
269                 enabled = true;
270                 if (cdPlaying)
271                         CDAudio_Stop();
272                 for (n = 0; n < MAXTRACKS; n++)
273                         remap[n] = n;
274                 CDAudio_GetAudioDiskInfo();
275                 return;
276         }
277
278         if (strcasecmp(command, "remap") == 0)
279         {
280                 ret = Cmd_Argc() - 2;
281                 if (ret <= 0)
282                 {
283                         for (n = 1; n < MAXTRACKS; n++)
284                                 if (remap[n] != n)
285                                         Con_Printf("  %u -> %u\n", n, remap[n]);
286                         return;
287                 }
288                 for (n = 1; n <= ret; n++)
289                         remap[n] = atoi(Cmd_Argv (n+1));
290                 return;
291         }
292
293         if (strcasecmp(command, "close") == 0)
294         {
295                 CDAudio_CloseDoor();
296                 return;
297         }
298
299         if (strcasecmp(command, "play") == 0)
300         {
301                 CDAudio_Play_byName(Cmd_Argv (2), false);
302                 return;
303         }
304
305         if (strcasecmp(command, "loop") == 0)
306         {
307                 CDAudio_Play_byName(Cmd_Argv (2), true);
308                 return;
309         }
310
311         if (strcasecmp(command, "stop") == 0)
312         {
313                 CDAudio_Stop();
314                 return;
315         }
316
317         if (strcasecmp(command, "pause") == 0)
318         {
319                 CDAudio_Pause();
320                 return;
321         }
322
323         if (strcasecmp(command, "resume") == 0)
324         {
325                 CDAudio_Resume();
326                 return;
327         }
328
329         if (strcasecmp(command, "eject") == 0)
330         {
331                 if (cdPlaying && faketrack == -1)
332                         CDAudio_Stop();
333                 CDAudio_Eject();
334                 cdValid = false;
335                 return;
336         }
337
338         if (strcasecmp(command, "info") == 0)
339         {
340                 CDAudio_GetAudioDiskInfo ();
341                 if (cdValid)
342                         Con_Printf("%u tracks on CD.\n", maxTrack);
343                 else
344                         Con_Print ("No CD in player.\n");
345                 if (cdPlaying)
346                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
347                 else if (wasPlaying)
348                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
349                 Con_Printf("Volume is %f\n", cdvolume);
350                 return;
351         }
352
353         Con_Printf("CD commands:\n");
354         Con_Printf("cd on - enables CD audio system\n");
355         Con_Printf("cd off - stops and disables CD audio system\n");
356         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
357         Con_Printf("cd remap <remap1> [remap2] [remap3] [...] - chooses (possibly emulated) CD tracks to play when a map asks for a particular track, this has many uses\n");
358         Con_Printf("cd close - closes CD tray\n");
359         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
360         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
361         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
362         Con_Printf("cd stop - stops playing current CD track\n");
363         Con_Printf("cd pause - pauses CD playback\n");
364         Con_Printf("cd resume - unpauses CD playback\n");
365         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
366 }
367
368 void CDAudio_SetVolume (float newvol)
369 {
370         // If the volume hasn't changed
371         if (newvol == cdvolume)
372                 return;
373
374         // If the CD has been muted
375         if (newvol == 0.0f)
376                 CDAudio_Pause ();
377         else
378         {
379                 // If the CD has been unmuted
380                 if (cdvolume == 0.0f)
381                         CDAudio_Resume ();
382
383                 if (faketrack != -1)
384                         S_SetChannelVolume (faketrack, newvol);
385                 CDAudio_SysSetVolume (newvol);
386         }
387
388         cdvolume = newvol;
389 }
390
391 void CDAudio_Update (void)
392 {
393         if (!enabled)
394                 return;
395
396         CDAudio_SetVolume (bgmvolume.value);
397
398         if (faketrack == -1)
399                 CDAudio_SysUpdate();
400 }
401
402 int CDAudio_Init (void)
403 {
404         int i;
405
406         if (cls.state == ca_dedicated)
407                 return -1;
408
409 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
410         if (COM_CheckParm("-nocdaudio"))
411                 return -1;
412
413         CDAudio_SysInit();
414
415         for (i = 0; i < MAXTRACKS; i++)
416                 remap[i] = i;
417
418         Cvar_RegisterVariable(&cdaudioinitialized);
419         Cvar_SetValueQuick(&cdaudioinitialized, true);
420         enabled = true;
421
422         Cmd_AddCommand("cd", CD_f, "execute a CD drive command (cd on/off/reset/remap/close/play/loop/stop/pause/resume/eject/info) - use cd by itself for usage");
423
424         return 0;
425 }
426
427 int CDAudio_Startup (void)
428 {
429         CDAudio_SysStartup ();
430
431         if (CDAudio_GetAudioDiskInfo())
432         {
433                 Con_Print("CDAudio_Init: No CD in player.\n");
434                 cdValid = false;
435         }
436
437         saved_vol = CDAudio_SysGetVolume ();
438         if (saved_vol < 0.0f)
439         {
440                 Con_Print ("Can't get initial CD volume\n");
441                 saved_vol = 1.0f;
442         }
443         else
444                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
445
446         initialized = true;
447
448         Con_Print("CD Audio Initialized\n");
449
450         return 0;
451 }
452
453 void CDAudio_Shutdown (void)
454 {
455         if (!initialized)
456                 return;
457
458         CDAudio_SysSetVolume (saved_vol);
459
460         CDAudio_Stop();
461         CDAudio_SysShutdown();
462         initialized = false;
463 }