Using SDL: the CD-ROM

[Prev] Table Of Contents [Next] # Using the Simple DirectMedia Layer API ## CD-ROM audio
Opening a CD-ROM drive for use You can find out how many CD-ROM drives are on the system with the SDL_CDNumDrives() function, and then pick which one to use with SDL_CDOpen(). The system default CD-ROM is always drive 0. The CD-ROM drive may be opened for use even if there is no disk in the drive. You should use the SDL_CDStatus() function to determine the state of the drive. After you are done using the CD-ROM drive, close it with the SDL_CDClose() function. | Tip: You can get the system-dependent name for a CD-ROM drive using the SDL_CDName() function.
Example:
    {
        SDL_CD *cdrom;

        if ( SDL_CDNumDrives() > 0 ) {
            cdrom = SDL_CDOpen(0);
            if ( cdrom == NULL ) {
                fprintf(stderr, "Couldn't open default CD-ROM: %s\n" SDL_GetError());
                return;
            }

            ...

            SDL_CDClose(cdrom);
        }
    }
Playing the CD-ROM CD-ROM drives specify time either in MSF format (mins/secs/frames) or directly in frames. A frame is a standard unit of time on the CD, corresponding to 1/75 of a second. SDL uses frames instead of the MSF format when specifying track lengths and offsets, but you can convert between them using the FRAMES_TO_MSF() and MSF_TO_FRAMES() macros. SDL doesn't update the track information in the SDL_CD structure until you call SDL_CDStatus(), so you should always use SDL_CDStatus() to make sure there is a CD in the drive and determine what tracks are available before playing the CD. Note that track indexes start at 0 for the first track. SDL has two functions for playing the CD-ROM. You can either play specific tracks on the CD using SDL_CDPlayTracks(), or you can play absolute frame offsets using SDL_CDPlay(). SDL doesn't provide automatic notification of CD insertion or play completion. To detect these conditions, you need to periodically poll the status of the drive with SDL_CDStatus(). Since this call reads the table of contents for the CD, it should not be called continuously in a tight loop. | Tip: You can determine which tracks are audio tracks and which are data tracks by looking at the cdrom->tracks[track].type, and comparing it to SDL_AUDIO_TRACK and SDL_DATA_TRACK.
Example:
    void PlayTrack(SDL_CD *cdrom, int track)
    {
        if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) {
            SDL_CDPlayTracks(cdrom, track, 0, track+1, 0);
        }
        while ( SDL_CDStatus(cdrom) == CD_PLAYING ) {
            SDL_Delay(1000);
        }
    }
[Prev] Table Of Contents [Next]