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