]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_sdl.c
347
[divverent/darkplaces.git] / cd_sdl.c
1 /*
2 Copyright (C) 2004 Andreas Kirsch (used cd_null.c as template)
3 Copyright (C) 1996-1997 Id Software, Inc.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "quakedef.h"
23 #include <SDL.h>
24
25 /*IMPORTANT: 
26 SDL 1.2.7 and older seems to have a strange bug regarding CDPause and CDResume under WIN32.
27 If CDResume is called, it plays to end of the CD regardless what values for lasttrack and lastframe
28 were passed to CDPlayTracks.
29 */
30
31 // If one of the functions fails, it returns -1, if not 0
32
33 // SDL supports multiple cd devices - so we are going to support this, too.
34 static void CDAudio_SDL_CDDrive_f( void );
35
36 // we only support playing on CD at a time
37 static SDL_CD *cd;
38 static int drive;
39 static double pauseoffset;
40 static double endtime;
41
42 static int ValidateDrive( void )
43 {
44         if( cd && SDL_CDStatus( cd ) > 0 )
45                 return cdValid = true;
46
47         return cdValid = false;
48 }
49
50 void CDAudio_SysEject (void)
51 {
52         SDL_CDEject( cd );
53 }
54
55
56 void CDAudio_SysCloseDoor (void)
57 {
58         //NO SDL FUNCTION
59 }
60
61
62 int CDAudio_SysGetAudioDiskInfo (void)
63 {
64         if( ValidateDrive() ) // everything > 0 is ok, 0 is trayempty and -1 is error
65                 return cd->numtracks;
66         return -1;
67 }
68
69
70 float CDAudio_SysGetVolume (void)
71 {
72         return -1.0f;
73 }
74
75
76 void CDAudio_SysSetVolume (float volume)
77 {
78         //NO SDL FUNCTION
79 }
80
81
82 int CDAudio_SysPlay (qbyte track)
83 {
84         SDL_CDStop( cd );
85         endtime = realtime + (float) cd->track[ track - 1 ].length / CD_FPS;
86         return SDL_CDPlayTracks( cd, track - 1, 0, track, 1 ); //FIXME: shall we play the whole cd or only the track?
87 }
88
89
90 int CDAudio_SysStop (void)
91 {
92         endtime = -1.0;
93         return SDL_CDStop( cd );
94 }
95
96
97 int CDAudio_SysPause (void)
98 {
99         SDL_CDStatus( cd );
100         pauseoffset = cd->cur_frame;
101         return SDL_CDPause( cd );
102 }
103
104 int CDAudio_SysResume (void)
105 {
106         SDL_CDResume( cd );
107         endtime = realtime + (cd->track[ cdPlayTrack - 1 ].length - pauseoffset) / CD_FPS;
108         return SDL_CDPlayTracks( cd, cdPlayTrack - 1, pauseoffset, cdPlayTrack, 0 );
109 }
110
111 int CDAudio_SysUpdate (void)
112 {
113         if( !cd || cd->status <= 0 ) {
114                 cdValid = false;
115                 return -1;
116         }
117         if( endtime > 0.0 && realtime >= endtime )
118                 if( SDL_CDStatus( cd ) == CD_STOPPED ){
119                         endtime = -1.0;
120                         if( cdPlayLooping )
121                                 CDAudio_SysPlay( cdPlayTrack );
122                         else
123                                 cdPlaying = false;
124                 }
125         return 0;
126 }
127
128
129 void CDAudio_SysInit (void)
130 {
131         if( SDL_InitSubSystem( SDL_INIT_CDROM ) == -1 ) 
132                 Con_SafePrint( "Failed to init the CDROM SDL subsystem!\n" );
133
134         Cmd_AddCommand( "cddrive", CDAudio_SDL_CDDrive_f );
135 }
136
137 static int IsAudioCD( void )
138 {
139         int i;
140         for( i = 0 ; i < cd->numtracks ; i++ )
141                 if( cd->track[ i ].type == SDL_AUDIO_TRACK )
142                         return true;
143         return false;
144 }
145
146 int CDAudio_SysStartup (void)
147 {
148         int i;
149         int numdrives;
150     
151         numdrives = SDL_CDNumDrives();
152         if( numdrives == -1 ) // was the CDROM system initialized correctly?
153                 return -1;
154
155         Con_DPrintf( "Found %i cdrom drives.\n", numdrives );
156
157         for( i = 0 ; i < numdrives ; i++, cd = NULL ) {
158                 cd = SDL_CDOpen( i );
159                 if( !cd ) {
160                         Con_DPrintf( "CD drive %i is invalid.\n", i );
161                         continue;
162                 }
163
164                 if( CD_INDRIVE( SDL_CDStatus( cd ) ) )
165                         if( IsAudioCD() )
166                                 break;
167                         else
168                                 Con_DPrintf( "The CD in drive %i is not an audio cd.\n", i );
169                 else
170                         Con_DPrintf( "No CD in drive %i.\n", i );
171
172                 SDL_CDClose( cd );
173         }
174
175         if( i == numdrives && !cd )
176                 return -1;
177
178         drive = i;
179
180         return 0;
181 }
182
183 void CDAudio_SysShutdown (void)
184 {
185         if( cd )
186                 SDL_CDClose( cd );
187 }
188
189 void CDAudio_SDL_CDDrive_f( void )
190 {
191         int i;
192         int numdrives = SDL_CDNumDrives();
193
194         if( Cmd_Argc() != 2 ) {
195                 Con_Print( "cddrive <drivenr>\n" );
196                 return;
197         }
198
199         i = atoi( Cmd_Argv( 1 ) );
200         if( i >= numdrives ) {
201                 Con_Printf("Only %i drives!\n", numdrives );
202                 return;
203         }
204
205         if( cd )
206                 SDL_CDClose( cd );
207
208         cd = SDL_CDOpen( i );
209         if( !cd ) {
210                 Con_Printf( "Couldn't open drive %i.\n", i );
211                 return;
212         } 
213         
214         if( !CD_INDRIVE( SDL_CDStatus( cd ) ) )
215                 Con_Printf( "No cd in drive %i.\n", i );
216         else if( !IsAudioCD() )
217                 Con_Printf( "The CD in drive %i is not an audio CD.\n", i );
218
219         drive = i;
220         ValidateDrive();
221 }
222
223
224
225
226