music.cpp

Go to the documentation of this file.
00001 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <SDL_mixer.h>
00008 
00009 #ifndef karteneditor
00010 #include "sdl/sound.h"
00011 #endif
00012 
00013 
00014 #include "basestrm.h"
00015 #include "stringtokenizer.h"
00016 #include "music.h"
00017 #include "itemrepository.h"
00018 #include "dlg_box.h"
00019 #include "textfile_evaluation.h"
00020 #include "sgstream.h"
00021 
00022 
00023 typedef deallocating_vector<MusicPlayList*> PlayLists;
00024 PlayLists playLists;
00025 
00026 void PlayListLoader::readTextFiles ( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location )
00027 {
00028    MusicPlayList* mpl = new MusicPlayList;
00029    mpl->runTextIO ( prc );
00030    
00031    mpl->filename = fileName;
00032    mpl->location = location;
00033    playLists.push_back ( mpl );
00034 }
00035 
00036 void PlayListLoader::read ( tnstream& stream )
00037 {
00038    readPointerContainer( playLists, stream );
00039 }
00040 
00041 void PlayListLoader::write ( tnstream& stream )
00042 {
00043    writePointerContainer( playLists, stream );
00044 }
00045 
00046 
00047 void MusicPlayList :: runTextIO ( PropertyContainer& pc )
00048 {
00049    pc.addString( "Name", name );
00050    pc.addStringArray( "Tracks", fileGroups );
00051 
00052    displayLogMessage ( 4, "Reading play list: " );
00053 
00054    reset();
00055 }
00056 
00057 
00058 void MusicPlayList :: read ( tnstream& stream )
00059 {
00060    int version = stream.readInt(); // version
00061    if ( version != 1 ) fatalError ("invalid version for MusicPlayList" );
00062    name = stream.readString();
00063    filename = stream.readString();
00064    location = stream.readString();
00065    readClassContainer( fileGroups, stream );
00066    reset();
00067 }
00068 
00069 void MusicPlayList :: write ( tnstream& stream ) const
00070 {
00071    stream.writeInt( 1 );
00072    stream.writeString ( name );
00073    stream.writeString ( filename );
00074    stream.writeString ( location );
00075    writeClassContainer( fileGroups, stream );
00076 }
00077 
00078 
00079 const ASCString& MusicPlayList :: getNextTrack()
00080 {
00081    if ( fileNameList.empty() ) {
00082       static ASCString emptyString;
00083       return emptyString;
00084    }
00085 
00086    if ( iter == fileNameList.end() )
00087       reset();
00088 
00089    return *(iter++);
00090 }
00091 
00092 void MusicPlayList :: reset ( )
00093 {
00094    fileNameList.clear();
00095 
00096    for ( TrackList::iterator i = fileGroups.begin(); i != fileGroups.end(); i++ ) {
00097       tfindfile ff ( *i );
00098       int loc;
00099       bool incontainer;
00100       ASCString location,name;
00101       name = ff.getnextname ( &loc, &incontainer, &location );
00102       while ( !name.empty()) {
00103          if ( !incontainer ) {
00104             ASCString filename = location + pathdelimitterstring + name;
00105             displayLogMessage ( 4, filename + ", "  );
00106             fileNameList.push_back ( filename );
00107          }
00108          name = ff.getnextname ( &loc, &incontainer, &location );
00109       };
00110    }
00111    displayLogMessage ( 4, "Finished \n" );
00112 
00113    iter = fileNameList.begin();
00114 }
00115 
00116 
00117 ASCString MusicPlayList::getDiagnosticText()
00118 {
00119    ASCString text;
00120 
00121    text += "Play List location:\n";
00122    text += location + "\n\n";
00123    
00124    text += "File Patterns:\n";
00125    if ( fileGroups.empty() )
00126       text += "-none-\n";
00127    else
00128       for ( TrackList::iterator i = fileGroups.begin(); i != fileGroups.end(); i++ ) 
00129          text += *i + "\n";
00130    text += "\n";
00131 
00132    text += "Found Files:\n";
00133    if ( fileNameList.empty() )
00134       text += "-none-\n";
00135    else
00136       for ( TrackList::iterator i = fileNameList.begin(); i != fileNameList.end(); i++ ) 
00137          text += *i + "\n";
00138    text += "\n";
00139 
00140    return text;
00141 }
00142 
00143 void startMusic ()
00144 {
00145 #ifndef karteneditor
00146   if ( !playLists.empty() )
00147      SoundSystem::getInstance()->playMusic ( playLists.front() );
00148   else
00149      displayLogMessage ( 1, "No play lists available!\n" );
00150 
00151 #endif
00152 }
00153 
00154 
00155 
00156 
00157 
00158 class   PlayListSelector : public tstringselect {
00159            public :
00160                  int lastchoice;
00161                  virtual void setup(void);
00162                  virtual void buttonpressed(int id);
00163                  virtual void run(void);
00164                  virtual void get_text( int nr);
00165               };
00166 
00167 void         PlayListSelector ::setup(void)
00168 {
00169    action = 0;
00170    title = "Select PlayList";
00171    numberoflines = playLists.size();
00172    ey = ysize - 90;
00173    startpos = lastchoice;
00174    addbutton("~O~K",20,ysize - 80,xsize/2-5,ysize - 60,0,1,12,true);
00175    addbutton("~C~ancel",  xsize/2+5,ysize - 80,xsize-20,ysize - 60,0,1,14,true);
00176    addkey ( 12, ct_enter );
00177    addkey ( 14, ct_esc );
00178 }
00179 
00180 
00181 void         PlayListSelector ::buttonpressed(int         id)
00182 {
00183    tstringselect::buttonpressed(id);
00184    switch (id) {
00185 
00186       case 14:  action = -1;
00187                 break;
00188 
00189       case 12:   if ( redline >= 0) {
00190 #ifndef karteneditor
00191                       SoundSystem::getInstance()->playMusic ( playLists[redline] );
00192                       action = 1;
00193 #endif
00194                  } else
00195                     displaymessage ( "Please select a play list", 3);
00196                  break;
00197    }
00198 }
00199 
00200 
00201 void         PlayListSelector ::get_text( int nr)
00202 {
00203    strcpy(txt,playLists[nr]->getName().c_str() );
00204 }
00205 
00206 
00207 void         PlayListSelector ::run(void)
00208 {
00209    do {
00210       tstringselect::run();
00211    }  while ( action == 0 );
00212 }
00213 
00214 
00215 void selectPlayList( )
00216 {
00217    PlayListSelector  gps;
00218 
00219    gps.init();
00220    gps.run();
00221    gps.done();
00222 }
00223 

Generated on Mon May 21 01:26:35 2012 for Advanced Strategic Command by  doxygen 1.5.1