main.cpp

 //(C)Copyright 2002- Faisal D. Alsarraf (jaguarxjs97@hotmail.com)
// Touched up by Matriark TerVel
#include "SDL/SDL.h" //SDL Include
#include <stdio.h> // Standard I/O

SDL_Surface *screen; //Global Pointer to SDL_Surface
SDL_Event msg;
char *file = "cdplayer2.bmp"; //BMP to load

//Since int track = 0 is considered tracknumber 01 to the computer,
//the caption will show Track: 00, I made another variable that will
//show the correct track to the user.
SDL_CD *cdrom; //Global Pointer to SDL_CD

int track = 0;
int trackcaption;

//Function Prototypes
void PressArea();
void BackCD();
void ForwardCD();
void PlayCD();
void exitplayer();
void GetCaption();
void ShowBMP(char *file, SDL_Surface *screen, int x, int y);

//Update Caption
void GetCaption() {
    trackcaption = track + 1;

//If the track is 10 trackcaption will only be shown
//This way Track 10 instead of Track 010

char caption[50];

if(trackcaption >= 10)
sprintf(caption,"CD Player - Track: %d", trackcaption);

else sprintf(caption,"CD Player - Track: 0%d", trackcaption);

    if(trackcaption != 1) {
        if(cdrom->numtracks < trackcaption) {
        sprintf(caption,"CD Player - Track: --", trackcaption);
        SDL_CDStop(cdrom);
        track = 0;
        PlayCD(); }
    }

SDL_WM_SetCaption(caption, NULL);

}

void PressArea() {
if((msg.button.x > 37 && msg.button.x < 79) && (msg.button.y > 181 && msg.button.y < 194)) {
PlayCD(); }
if((msg.button.x > 100 && msg.button.x < 146) && (msg.button.y > 181 && msg.button.y < 194)) {
SDL_CDStop(cdrom); }
if((msg.button.x > 167 && msg.button.x < 217) && (msg.button.y > 181 && msg.button.y < 194)) {
SDL_CDPause(cdrom); }
if((msg.button.x > 235 && msg.button.x < 305) && (msg.button.y > 181 && msg.button.y < 194)) {
SDL_CDResume(cdrom); }
if((msg.button.x > 325 && msg.button.x < 459) && (msg.button.y > 181 && msg.button.y < 194)) {
exitplayer(); }
if((msg.button.x > 352 && msg.button.x < 405) && (msg.button.y > 18 && msg.button.y < 29)) {
ForwardCD(); }
if((msg.button.x > 431 && msg.button.x < 466) && (msg.button.y > 18 && msg.button.y < 29)) {
BackCD(); }
}

void BackCD() {
track--;
GetCaption();
if ( CD_INDRIVE(SDL_CDStatus(cdrom)) )
SDL_CDPlayTracks(cdrom, track, 0, 0, 0);
}

void ForwardCD() {
track++;
GetCaption();
if ( CD_INDRIVE(SDL_CDStatus(cdrom)) )
SDL_CDPlayTracks(cdrom, track, 0, 0, 0);
}

void PlayCD() {
GetCaption();
if ( CD_INDRIVE(SDL_CDStatus(cdrom)) )
SDL_CDPlayTracks(cdrom, track, 0, 0, 0);
}

//In anyway the user trys to close the program (esc, close, etc.)
//this function is called.
void exitplayer() {
SDL_WM_SetCaption("CD Player - Exiting...", NULL); //Tell the user that the cd player is exiting
SDL_CDStop(cdrom); //Stop player
SDL_CDClose(cdrom); //Close SDL_CD
exit(0); //Exit program
}

//Show cdplayer2.bmp
void ShowBMP(char *file, SDL_Surface *screen, int x, int y) {
    SDL_Surface *image;
    SDL_Rect dest;

    //Load the BMP file into a surface
    image = SDL_LoadBMP(file);

    //Blit onto the screen surface.
    //The surfaces should not be locked at this point.

    dest.x = x;
    dest.y = y;
    dest.w = image->w;
    dest.h = image->h;
    SDL_BlitSurface(image, NULL, screen, &dest);

    //Update the changed portion of the screen
    SDL_UpdateRects(screen, 1, &dest);

    SDL_FreeSurface(image);
}

int main( int argc, char* argv[] ) {
    screen = SDL_SetVideoMode(500, 205, 16, SDL_SWSURFACE);
SDL_WM_SetCaption("CD Player", NULL);

ShowBMP(file, screen, 0, 0);

SDL_Init(SDL_INIT_CDROM); //Initialize CDROM

//Check for CD drives
if(!SDL_CDNumDrives()) {
  /* None found */
      SDL_WM_SetCaption("Error - No CDROM devices available", NULL);
      while(msg.type != SDL_QUIT) {
  SDL_WaitEvent(&msg); };
}

    //Open Default Drive
    cdrom = SDL_CDOpen(0);

//Did it open? Check if cdrom is NULL
if(!cdrom){
      SDL_WM_SetCaption("Error - Couldn't open drive", NULL);
      while(msg.type != SDL_QUIT) {
  SDL_WaitEvent(&msg); };
}

    SDL_EnableKeyRepeat(1, 75); //Sounds like what it is

    char caption[50];
    sprintf(caption,"CD Player - Tracks %d", cdrom->numtracks);
SDL_WM_SetCaption(caption, NULL); //User knows CD Player is ready
SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL); //SetIcon

    PlayCD();

//Program Event Handler
while(msg.type != SDL_QUIT) {
SDL_WaitEvent(&msg);
    GetCaption();
switch(msg.type) {
case SDL_KEYDOWN:
switch(msg.key.keysym.sym) {
case SDLK_ESCAPE: //If Escape is pressed
exitplayer();
break;
case SDLK_p: //Play CD
                        PlayCD();
break;
case SDLK_r: //Resume after Wait or Pause
SDL_CDResume(cdrom);
break;
case SDLK_s: //Stop
SDL_CDStop(cdrom);
break;
case SDLK_e: //Eject
SDL_CDEject(cdrom);
break;
case SDLK_w: //Wait or Pause
SDL_CDPause(cdrom);
break;
case SDLK_f: //Forward
                        ForwardCD();
break;
case SDLK_b: //Back
                        BackCD();
break;
}
                break;
          case SDL_MOUSEBUTTONDOWN:
          switch(msg.button.button) {
          case SDL_BUTTON_LEFT:
               PressArea();
               break;
               }
               break;
}
}

exitplayer(); //Update caption, stop processes, close program

return(0);
}

Project Homepage: