]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cd_sdl.c
Fix MSVC++ 2015 warnings about variable scope and some narrowing conversions without...
[xonotic/darkplaces.git] / cd_sdl.c
index 4ec4f95805573e7cdd33fcc5ae45ca4ae42fa75f..410dd3903a79d12bd198fe82be28eb01ed57c640 100644 (file)
--- a/cd_sdl.c
+++ b/cd_sdl.c
@@ -20,24 +20,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
 #include "quakedef.h"
-#include <SDL.h>
+#include "cdaudio.h"
 
-/*IMPORTANT: 
-SDL 1.2.7 and older seems to have a strange bug regarding CDPause and CDResume under WIN32.
-If CDResume is called, it plays to end of the CD regardless what values for lasttrack and lastframe
-were passed to CDPlayTracks.
-*/
+#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
+// SDL 1.2 has CD audio
+
+#include <SDL.h>
+#include <time.h>
 
 // If one of the functions fails, it returns -1, if not 0
 
 // SDL supports multiple cd devices - so we are going to support this, too.
 static void CDAudio_SDL_CDDrive_f( void );
 
-// we only support playing on CD at a time
+// we only support playing one CD at a time
 static SDL_CD *cd;
-static int drive;
-static double pauseoffset;
-static double endtime;
 
 static int ValidateDrive( void )
 {
@@ -58,7 +55,6 @@ void CDAudio_SysCloseDoor (void)
        //NO SDL FUNCTION
 }
 
-
 int CDAudio_SysGetAudioDiskInfo (void)
 {
        if( ValidateDrive() ) // everything > 0 is ok, 0 is trayempty and -1 is error
@@ -66,72 +62,64 @@ int CDAudio_SysGetAudioDiskInfo (void)
        return -1;
 }
 
-
 float CDAudio_SysGetVolume (void)
 {
        return -1.0f;
 }
 
-
 void CDAudio_SysSetVolume (float volume)
 {
        //NO SDL FUNCTION
 }
 
-
-int CDAudio_SysPlay (qbyte track)
+int CDAudio_SysPlay (int track)
 {
-       SDL_CDStop( cd );
-       endtime = realtime + (float) cd->track[ track - 1 ].length / CD_FPS;
-       return SDL_CDPlayTracks( cd, track - 1, 0, track, 1 ); //FIXME: shall we play the whole cd or only the track?
+       return SDL_CDPlayTracks(cd, track-1, 0, 1, 0);
 }
 
-
 int CDAudio_SysStop (void)
 {
-       endtime = -1.0;
        return SDL_CDStop( cd );
 }
 
-
 int CDAudio_SysPause (void)
 {
-       SDL_CDStatus( cd );
-       pauseoffset = cd->cur_frame;
        return SDL_CDPause( cd );
 }
 
 int CDAudio_SysResume (void)
 {
-       SDL_CDResume( cd );
-       endtime = realtime + (cd->track[ cdPlayTrack - 1 ].length - pauseoffset) / CD_FPS;
-       return SDL_CDPlayTracks( cd, cdPlayTrack - 1, pauseoffset, cdPlayTrack, 0 );
+       return SDL_CDResume( cd );
 }
 
 int CDAudio_SysUpdate (void)
 {
-       if( !cd || cd->status <= 0 ) {
-               cdValid = false;
-               return -1;
-       }
-       if( endtime > 0.0 && realtime >= endtime )
-               if( SDL_CDStatus( cd ) == CD_STOPPED ){
-                       endtime = -1.0;
+       static time_t lastchk = 0;
+
+       if (cdPlaying && lastchk < time(NULL))
+       {
+               lastchk = time(NULL) + 2; //two seconds between chks
+               if( !cd || cd->status <= 0 ) {
+                       cdValid = false;
+                       return -1;
+               }
+               if (SDL_CDStatus( cd ) == CD_STOPPED)
+               {
                        if( cdPlayLooping )
                                CDAudio_SysPlay( cdPlayTrack );
                        else
                                cdPlaying = false;
                }
+       }
        return 0;
 }
 
-
 void CDAudio_SysInit (void)
 {
-       if( SDL_InitSubSystem( SDL_INIT_CDROM ) == -1 ) 
-               Con_SafePrint( "Failed to init the CDROM SDL subsystem!\n" );
+       if( SDL_InitSubSystem( SDL_INIT_CDROM ) == -1 )
+               Con_Print( "Failed to init the CDROM SDL subsystem!\n" );
 
-       Cmd_AddCommand( "cddrive", CDAudio_SDL_CDDrive_f );
+       Cmd_AddCommand( "cddrive", CDAudio_SDL_CDDrive_f, "select an SDL-detected CD drive by number" );
 }
 
 static int IsAudioCD( void )
@@ -147,17 +135,17 @@ int CDAudio_SysStartup (void)
 {
        int i;
        int numdrives;
-    
+
        numdrives = SDL_CDNumDrives();
        if( numdrives == -1 ) // was the CDROM system initialized correctly?
                return -1;
 
-       Con_DPrintf( "Found %i cdrom drives.\n", numdrives );
+       Con_Printf( "Found %i cdrom drives.\n", numdrives );
 
        for( i = 0 ; i < numdrives ; i++, cd = NULL ) {
                cd = SDL_CDOpen( i );
                if( !cd ) {
-                       Con_DPrintf( "CD drive %i is invalid.\n", i );
+                       Con_Printf( "CD drive %i is invalid.\n", i );
                        continue;
                }
 
@@ -165,9 +153,9 @@ int CDAudio_SysStartup (void)
                        if( IsAudioCD() )
                                break;
                        else
-                               Con_DPrintf( "The CD in drive %i is not an audio cd.\n", i );
+                               Con_Printf( "The CD in drive %i is not an audio cd.\n", i );
                else
-                       Con_DPrintf( "No CD in drive %i.\n", i );
+                       Con_Printf( "No CD in drive %i.\n", i );
 
                SDL_CDClose( cd );
        }
@@ -175,8 +163,6 @@ int CDAudio_SysStartup (void)
        if( i == numdrives && !cd )
                return -1;
 
-       drive = i;
-
        return 0;
 }
 
@@ -209,18 +195,89 @@ void CDAudio_SDL_CDDrive_f( void )
        if( !cd ) {
                Con_Printf( "Couldn't open drive %i.\n", i );
                return;
-       } 
-       
+       }
+
        if( !CD_INDRIVE( SDL_CDStatus( cd ) ) )
                Con_Printf( "No cd in drive %i.\n", i );
        else if( !IsAudioCD() )
                Con_Printf( "The CD in drive %i is not an audio CD.\n", i );
 
-       drive = i;
        ValidateDrive();
 }
 
 
 
 
-               
+
+#else
+// SDL 1.3 does not have CD audio
+
+void CDAudio_SysEject (void)
+{
+}
+
+
+void CDAudio_SysCloseDoor (void)
+{
+}
+
+
+int CDAudio_SysGetAudioDiskInfo (void)
+{
+       return -1;
+}
+
+
+float CDAudio_SysGetVolume (void)
+{
+       return -1.0f;
+}
+
+
+void CDAudio_SysSetVolume (float fvolume)
+{
+}
+
+
+int CDAudio_SysPlay (int track)
+{
+       return -1;
+}
+
+
+int CDAudio_SysStop (void)
+{
+       return -1;
+}
+
+
+int CDAudio_SysPause (void)
+{
+       return -1;
+}
+
+int CDAudio_SysResume (void)
+{
+       return -1;
+}
+
+int CDAudio_SysUpdate (void)
+{
+       return -1;
+}
+
+
+void CDAudio_SysInit (void)
+{
+}
+
+int CDAudio_SysStartup (void)
+{
+       return -1;
+}
+
+void CDAudio_SysShutdown (void)
+{
+}
+#endif
+