The code below works fine if you are using ALSA sound. If you are using OSS
or PulsaAudio
it does not work. I have tried
putenv(“SDL_AUDIODRIVER=dsp”); with dma, pulse, audio, oss, and others. Got
any ideals.
/////////////////////// CODE //////////////////////////
/*
- Gary Russell (ISSW)
- June 18 2009
- Using SDL libuary.
- Music CD Player.*
- When program starts - the cd plays.
- When exit program - cd stops.
- “Esc” or Click on the window “X” to exit.
- “Space Bar” or Click on active window will
- pause or resume.
- “Tab” stops and ejects, but does not exit program.
- “P” or “p” will play the cd. If it is not playing.
*/
#include
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_cdrom.h>
using namespace std;
#define Screen_Width 100
#define Screen_Height 100
//If Bits_color is 0, it is treated as the current display bpp.
#define Bits_color 0
#define Video_flages SDL_ANYFORMAT
//#define Video_flages SDL_DOUBLEBUF | SDL_HWSURFACE //|
SDL_FULLSCREEN
SDL_Surface *screen = NULL;
SDL_Surface *icon = NULL;
SDL_Event event;
SDL_CD *cdrom = NULL;
char driver[] = “oss”;
/////////////////////// FUNCTIONS //////////////////////////
void clean_up(void) {
SDL_CDClose(cdrom);
SDL_Quit();
}
//////////////////////////////
//Setup SDL and anything else needing setup.
bool setup(void) {
//use to center the video box. call befor SDL_SetVideoMode().
// putenv(“SDL_VIDEO_WINDOW_POS”);
// putenv(“SDL_VIDEO_CENTERED=1”);
//This will put it at the uper left corner.
putenv(“SDL_VIDEO_WINDOW_POS=0,0”);
//putenv(“SDL_AUDIODRIVER=dsp”);
//putenv(“SDL_DISKAUDIOFILE=/home/admin/Temp/music.raw”);
// system(“export SDL_AUDIODRIVER”);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
cerr << "UNABLE TO INITIALIZE VIDEO: " << SDL_GetError() << endl;
exit(1); }
icon = SDL_LoadBMP(“icon.bmp”);
SDL_WM_SetCaption(“Press (Esc) key to exit!”, “Press (Esc) key to exit!”);
SDL_WM_SetIcon(icon,NULL);
screen =
SDL_SetVideoMode(Screen_Width,Screen_Height,Bits_color,Video_flages);
if (screen == NULL) {
cerr << "Unable to set video mode: " << SDL_GetError() << endl;
exit(1); }
if(SDL_Init(SDL_INIT_CDROM) < 0 ) {
cerr << "Couldn’t initialize SDL: " << SDL_GetError() << endl;
exit(1); }
return true;
}
//////////////////////////////
//Opening the default drive
bool open_def_cdrom(void) {
bool error = false;
CDstatus status;
char *status_str;
cdrom = SDL_CDOpen(0);
if(cdrom == NULL) {
fprintf(stderr, “Couldn’t open default CD-ROM drive: %s\n”,
SDL_GetError());
exit(2); }
status = SDL_CDStatus(cdrom);
switch (status) {
case CD_TRAYEMPTY:
status_str = “tray empty”;
error = true;
break;
case CD_STOPPED:
status_str = “stopped”;
break;
case CD_PLAYING:
status_str = “playing”;
break;
case CD_PAUSED:
status_str = “paused”;
break;
case CD_ERROR:int trk = 2;
status_str = “error state”;
error = true;
break;
} //end switch().
printf(“Drive status: %s\n”, status_str);
if(status >= CD_PLAYING) {
int m, s, f;
FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
printf(“Currently playing track %d, %d:%2.2d\n”,
cdrom->track[cdrom->cur_track].id, m, s);
}//end if().
return error;
}
//////////////////////////////
//Plays the whole cd.
bool PlayCD(void) {
//Change SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); to
//SDL_CDPlayTracks(cdrom, 0, 0, 1, 0); will play only
//the first track.
if(CD_INDRIVE(SDL_CDStatus(cdrom))) {
SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); }
return true;
}
//////////////////////////////
////////////////////////////// MAIN ////////////////////////
int main() {
bool ret,kstate;
int quit = false;
CDstatus status;
ret = setup();
if(ret == true) { }
kstate = false;
open_def_cdrom();
PlayCD(); cout << “PLAY CD \n”; SDL_Delay(100);
//main loop
while(quit == false) {
if(SDL_WaitEvent(&event) == 0) {
cerr << "ERROR IN SDL_WaitEvent(): " << SDL_GetError() << endl;
exit(1); }
switch(event.type) {
case SDL_ACTIVEEVENT: { }break;
case SDL_KEYDOWN: {
if(event.key.keysym.sym == SDLK_ESCAPE) {
SDL_CDStop(cdrom); quit = true; }
else if(event.key.keysym.sym == SDLK_SPACE) {
if(kstate == false) {
SDL_CDPause(cdrom); kstate = true; }
else { SDL_CDResume(cdrom); kstate = false; }
}
else if(event.key.keysym.sym == SDLK_TAB) {
SDL_CDStop(cdrom);
SDL_CDEject(cdrom); }
else if(event.key.keysym.sym == SDLK_p) {
status = SDL_CDStatus(cdrom);
if(status == CD_STOPPED) {
PlayCD(); cout << "PLAY CD \n"; SDL_Delay(100); }
}
}//end SDL_KEYDOWN.
case SDL_KEYUP: { }//end SDL_KEYUP.
case SDL_MOUSEMOTION: { }break;
case SDL_MOUSEBUTTONDOWN: {
//if(event.key.keysym.sym == SDLK_SPACE) {
if(kstate == false) { SDL_CDPause(cdrom); kstate = true; }
else { SDL_CDResume(cdrom); kstate = false; }
}break;
case SDL_MOUSEBUTTONUP: { }break;
case SDL_VIDEOEXPOSE: { }break;
case SDL_VIDEORESIZE: { }break;
case SDL_QUIT: { SDL_CDStop(cdrom); quit = true;}
default: { }break;
}//end switch().
};//end main loop.
clean_up(); //clean up.
return 0;
}
–
View this message in context: http://www.nabble.com/SDL-CDROM-tp24158611p24158611.html
Sent from the SDL mailing list archive at Nabble.com.