summaryrefslogtreecommitdiff
path: root/apps/plugins/sdl/progs/quake/cd_sdl.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/sdl/progs/quake/cd_sdl.c')
-rw-r--r--apps/plugins/sdl/progs/quake/cd_sdl.c223
1 files changed, 223 insertions, 0 deletions
diff --git a/apps/plugins/sdl/progs/quake/cd_sdl.c b/apps/plugins/sdl/progs/quake/cd_sdl.c
new file mode 100644
index 0000000000..46b2aa2835
--- /dev/null
+++ b/apps/plugins/sdl/progs/quake/cd_sdl.c
@@ -0,0 +1,223 @@
1/*
2 Some of this may not work. I'm not overly familiar with SDL, I just sort
3 of podged this together from the SDL headers, and the other cd-rom code.
4
5 Mark Baker <homer1@together.net>
6*/
7
8#include <SDL.h>
9
10#include "quakedef.h"
11
12static qboolean cdValid = false;
13static qboolean initialized = false;
14static qboolean enabled = true;
15static qboolean playLooping = false;
16static SDL_CD *cd_id;
17static float cdvolume = 1.0;
18
19static void CD_f();
20
21static void CDAudio_Eject()
22{
23 if(!cd_id || !enabled) return;
24
25 if(SDL_CDEject(cd_id))
26 Con_DPrintf("Unable to eject CD-ROM tray.\n");
27}
28
29void CDAudio_Play(byte track, qboolean looping)
30{
31 CDstatus cd_stat;
32 if(!cd_id || !enabled) return;
33
34 if(!cdValid)
35 {
36 if(!CD_INDRIVE(cd_stat=SDL_CDStatus(cd_id)) ||(!cd_id->numtracks)) return;
37 cdValid = true;
38 }
39
40 if((track < 1) || (track >= cd_id->numtracks))
41 {
42 Con_DPrintf("CDAudio: Bad track number: %d\n",track);
43 return;
44 }
45 track--; /* Convert track from person to SDL value */
46 if(cd_stat == CD_PLAYING)
47 {
48 if(cd_id->cur_track == track) return;
49 CDAudio_Stop();
50 }
51
52 if(SDL_CDPlay(cd_id,cd_id->track[track].offset,
53 cd_id->track[track].length))
54 {
55 Con_DPrintf("CDAudio_Play: Unable to play track: %d\n",track+1);
56 return;
57 }
58 playLooping = looping;
59}
60
61
62void CDAudio_Stop()
63{
64 int cdstate;
65 if(!cd_id || !enabled) return;
66 cdstate = SDL_CDStatus(cd_id);
67 if((cdstate != CD_PLAYING) && (cdstate != CD_PAUSED)) return;
68
69 if(SDL_CDStop(cd_id))
70 Con_DPrintf("CDAudio_Stop: Failed to stop track.\n");
71}
72
73void CDAudio_Pause()
74{
75 if(!cd_id || !enabled) return;
76 if(SDL_CDStatus(cd_id) != CD_PLAYING) return;
77
78 if(SDL_CDPause(cd_id))
79 Con_DPrintf("CDAudio_Pause: Failed to pause track.\n");
80}
81
82
83void CDAudio_Resume()
84{
85 if(!cd_id || !enabled) return;
86 if(SDL_CDStatus(cd_id) != CD_PAUSED) return;
87
88 if(SDL_CDResume(cd_id))
89 Con_DPrintf("CDAudio_Resume: Failed tp resume track.\n");
90}
91
92void CDAudio_Update()
93{
94 if(!cd_id || !enabled) return;
95 if(bgmvolume.value != cdvolume)
96 {
97 if(cdvolume)
98 {
99 Cvar_SetValue("bgmvolume",0.0);
100 CDAudio_Pause();
101 }
102 else
103 {
104 Cvar_SetValue("bgmvolume",1.0);
105 CDAudio_Resume();
106 }
107 cdvolume = bgmvolume.value;
108 return;
109 }
110 if(playLooping && (SDL_CDStatus(cd_id) != CD_PLAYING)
111 && (SDL_CDStatus(cd_id) != CD_PAUSED))
112 CDAudio_Play(cd_id->cur_track+1,true);
113}
114
115int CDAudio_Init()
116{
117 if((cls.state == ca_dedicated) || COM_CheckParm("-nocdaudio"))
118 return -1;
119
120 cd_id = SDL_CDOpen(0);
121 if(!cd_id)
122 {
123 Con_Printf("CDAudio_Init: Unable to open default CD-ROM drive: %s\n",
124 SDL_GetError());
125 return -1;
126 }
127
128 initialized = true;
129 enabled = true;
130 cdValid = true;
131
132 if(!CD_INDRIVE(SDL_CDStatus(cd_id)))
133 {
134 Con_Printf("CDAudio_Init: No CD in drive.\n");
135 cdValid = false;
136 }
137 if(!cd_id->numtracks)
138 {
139 Con_Printf("CDAudio_Init: CD contains no audio tracks.\n");
140 cdValid = false;
141 }
142 Cmd_AddCommand("cd",CD_f);
143 Con_Printf("CD Audio Initialized.\n");
144 return 0;
145}
146
147
148void CDAudio_Shutdown()
149{
150 if(!cd_id) return;
151 CDAudio_Stop();
152 SDL_CDClose(cd_id);
153 cd_id = NULL;
154}
155
156static void CD_f()
157{
158 char *command;
159 int cdstate;
160 if(Cmd_Argc() < 2) return;
161
162 command = Cmd_Argv(1);
163 if(!Q_strcasecmp(command,"on"))
164 {
165 enabled = true;
166 }
167 if(!Q_strcasecmp(command,"off"))
168 {
169 if(!cd_id) return;
170 cdstate = SDL_CDStatus(cd_id);
171 if((cdstate == CD_PLAYING) || (cdstate == CD_PAUSED))
172 CDAudio_Stop();
173 enabled = false;
174 return;
175 }
176 if(!Q_strcasecmp(command,"play"))
177 {
178 CDAudio_Play(Q_atoi(Cmd_Argv(2)),false);
179 return;
180 }
181 if(!Q_strcasecmp(command,"loop"))
182 {
183 CDAudio_Play(Q_atoi(Cmd_Argv(2)),true);
184 return;
185 }
186 if(!Q_strcasecmp(command,"stop"))
187 {
188 CDAudio_Stop();
189 return;
190 }
191 if(!Q_strcasecmp(command,"pause"))
192 {
193 CDAudio_Pause();
194 return;
195 }
196 if(!Q_strcasecmp(command,"resume"))
197 {
198 CDAudio_Resume();
199 return;
200 }
201 if(!Q_strcasecmp(command,"eject"))
202 {
203 CDAudio_Eject();
204 return;
205 }
206 if(!Q_strcasecmp(command,"info"))
207 {
208 if(!cd_id) return;
209 cdstate = SDL_CDStatus(cd_id);
210 Con_Printf("%d tracks\n",cd_id->numtracks);
211 if(cdstate == CD_PLAYING)
212 Con_Printf("Currently %s track %d\n",
213 playLooping ? "looping" : "playing",
214 cd_id->cur_track+1);
215 else
216 if(cdstate == CD_PAUSED)
217 Con_Printf("Paused %s track %d\n",
218 playLooping ? "looping" : "playing",
219 cd_id->cur_track+1);
220 return;
221 }
222}
223