Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

maingame.cpp

Go to the documentation of this file.
00001 #include "borqueror.h"
00002 
00003 #include "maincredits.h"
00004 #include "halloffame.h"
00005 #include "retire.h"
00006 #include "parameters.h"
00007 #include "continue.h"
00008 
00009 Galaxy  *MainGame::galaxy = 0;
00010 
00011 Galaxy &GetGalaxy( void )
00012 {
00013         return *MainGame::galaxy;
00014 }
00015 
00016 PlayGame *MainGame::game = 0;
00017 PlayGame &GetGame( void )
00018 {       
00019         return *MainGame::game;
00020 }
00021 
00023 Window *bqObjectCreate( const icstring &name )
00024 {
00025         Ars::Window     *ans = 0;
00026 
00027         if( name == "GameItfc" )
00028                 ans = new GameItfc();
00029         else if( name == "GalaxyScreen" )
00030                 ans = new GalaxyScreen();
00031         else if( name == "StarSystem" )
00032                 ans = new StarSystemObject();
00033         else if( name == "SunImage" )
00034                 ans = new SunImageObject();
00035         else if( name == "PlanetImage" )
00036                 ans = new PlanetImageObject();
00037         return ans;
00038 }
00039 
00041 class InitGalaxyScreen : public Thread  {
00042 public:
00043         virtual long Run( void * )
00044         {
00045                 Cfg::Handler    &h = Cfg::GetCfg( "@Generator" );
00046                 GalaxyScreen::img = Graf::NewSurface( h.GetCfgInt( "Galaxy-coord" , "playW" ) , h.GetCfgInt( "Galaxy-coord" , "playH" ) );
00047                 GalaxyScreen::bkgnd = Graf::NewSurface( h.GetCfgInt( "Galaxy-coord" , "playW" ) , h.GetCfgInt( "Galaxy-coord" , "playH" ) );
00048                 return 0;
00049         }
00050 };
00051 
00052 class InitStarInfo : public Thread      {
00053 public:
00054         virtual long Run( void *sem )
00055         {
00056                 Parameters::SetupStarInfo();
00057                 return 0;
00058         }
00059 };
00060 
00061 class InitStarName : public Thread      {
00062 public:
00063         virtual long Run( void *sem )
00064         {
00065                 Parameters::LoadStarsName();
00066                 return 0;
00067         }
00068 };
00069 
00070 class InitPlanetInfo : public Thread    {
00071 public:
00072         virtual long Run( void *sem )
00073         {
00074                 Parameters::SetupPlanetInfo();
00075                 return 0;
00076         }
00077 };
00078 
00079 
00080 class InitSpecies : public Thread       {
00081 public:
00082         virtual long Run( void *sem )
00083         {
00084                 Parameters::SetupSpecies();
00085                 return 0;
00086         }
00087 };
00088 
00089 class InitPlanetSize : public Thread    {
00090 public:
00091         virtual long Run( void *sem )
00092         {
00093                 Parameters::SetupPlanetSize();
00094                 return 0;
00095         }
00096 };
00097 
00098 class InitEcoZone : public Thread       {
00099 public:
00100         virtual long Run( void *sem )
00101         {
00102                 Parameters::SetupEcoZones();
00103                 return 0;
00104         }
00105 };
00106 
00108 MainGame::MainGame( const XRect r , const string &appName )
00109 : View( r , appName , false ) , wave( 0 )
00110 {
00111         galaxy = new Galaxy();
00112         game = new PlayGame();
00113 
00114         initTasks.push_back( new InitStarName );
00115         initTasks.push_back( new InitPlanetInfo );
00116         initTasks.push_back( new InitPlanetSize );
00117         initTasks.push_back( new InitEcoZone );
00118         initTasks.push_back( new InitSpecies );
00119         initTasks.push_back( new InitGalaxyScreen );
00120         initTasks.push_back( new InitStarInfo );
00121 }
00122 
00123 MainGame::~MainGame( void )
00124 {
00125         galaxy->Reset();
00126         Colony::ResetAllColonies();
00127         Parameters::ResetInfo();
00128 
00129         if( GalaxyScreen::img )
00130                 SDL_FreeSurface( GalaxyScreen::img );
00131         GalaxyScreen::img = 0;
00132         if( GalaxyScreen::bkgnd )
00133                 SDL_FreeSurface( GalaxyScreen::bkgnd );
00134         GalaxyScreen::bkgnd = 0;
00135 
00136         delete game;
00137         game = 0;
00138         delete galaxy;
00139         galaxy = 0;
00140 
00141         Mix_HaltMusic();
00142         Mix_HaltChannel( 0 );
00143         Mix_HaltChannel( 1 );
00144         if( wave )
00145                 Mix_FreeMusic( wave );
00146         wave = 0;
00147 
00148         Mix_CloseAudio();
00149 
00150         f->ShutdownDialog( this );
00151 
00152         for( int i = eMainDialog ; i < eLast ; i++ )
00153                 Free( i );
00154 
00155         for( vector<BqBase *>::iterator it = links.begin() ; it != links.end() ; ++it )
00156                 delete (*it);
00157         links.clear();
00158 
00159         {
00160                 for( vector<Uniform *>::iterator it = ExprParser::toDelinExprParser.begin() ; it != ExprParser::toDelinExprParser.end() ; ++it )
00161                         delete (*it);
00162                 ExprParser::toDelinExprParser.clear();
00163         }
00164 
00165         clear();
00166 }
00167 
00168 const bool MainGame::InitDone( void )
00169 {
00170         for( vector<Thread*>::iterator it = initTasks.begin() ;it != initTasks.end() ; ++it )
00171         {
00172                 (*it)->Wait();
00173                 delete *it;
00174         }
00175         initTasks.clear();
00176         return true;
00177 }
00178 
00179 const bool MainGame::SetupWindow( const Message &msg )
00180 {
00181         View::SetupWindow( msg );
00182 
00183         SetBgColor( Cfg::GetCfg().GetCfgColorHex( "Ars" , "back_color" , RGBColor( 0 , 0 , 0 ) ) );
00184 
00185         MessageServer::Instance().RegisterMessageClient( this , Message::CTRL_LCLICK );
00186         MessageServer::Instance().RegisterMessageClient( this , Message::MOUSE_BUTTONDOWN );
00187         MessageServer::Instance().RegisterMessageClient( this , Message::USER );
00188 
00189         links.push_back( new MainCredits( this ) );
00190         links.push_back( new HallOfFame( this ) );
00191         links.push_back( new Retire( this ) );
00192         links.push_back( new Parameters( this ) );
00193         links.push_back( new Continue( this ) );
00194         links.push_back( new MainLoad( this ) );
00195         links.push_back( new MainSave( this ) );
00196 
00197         EnableWindow();
00198 
00199         ArsApplication::objCreate.push_back( (cbObjCreateType)bqObjectCreate );
00200 
00201         return true;
00202 }
00203 
00204 void MainGame::EnableWindow( void )
00205 {
00206         Cfg::Handler    &h = Cfg::GetCfg();
00207         Rsrc    res( h.GetFilename() );
00208         Window  *ContinueButton = f->GetArsObject( 4 );
00209         Window  *LoadButton = f->GetArsObject( 5 );
00210         Window  *SaveButton = f->GetArsObject( 6 );
00211 
00212         ContinueButton->EnableWindow( !h.GetCfgString( "SavedGames" , "0" ).empty() );
00213         LoadButton->EnableWindow( !h.GetCfgString( "SavedGames" , "1" ).empty() );
00214         SaveButton->EnableWindow( !GetGalaxy().empty() );
00215 
00216         SaveButton->UpdateWindow();
00217         ContinueButton->UpdateWindow();
00218         LoadButton->UpdateWindow();
00219 }
00220 
00221 const bool MainGame::MainLoop( void )
00222 {
00223         Graf::ShowMouse( false );
00224 
00225         f = Load( "MainDialog" , eMainDialog );
00226 
00227         ArsSendMessage( Message( Message::SETUP_WINDOW , this ) );
00228 
00229         if( f )
00230         {
00231                 f->InitDialog( this );
00232                 f->Draw();
00233         }
00234         SetTextAlign( S_ALIGN_TOP , S_ALIGN_LEFT );
00235 
00236         AddSound( "Splash-Greeting" , "Splash" , "Greeting" ).Configure( 1 );
00237 
00238         PlayIntro();
00239         for( vector<Thread*>::iterator it = initTasks.begin() ;it != initTasks.end() ; ++it )
00240                 (*it)->Start( 0 );
00241 
00242         InitDone();
00243 
00244         Graf::ShowMouse( true );
00245 
00246         while( View::MainLoop() )       ;
00247 
00248         return false;
00249 }
00250 
00251 void MainGame::PlayIntro( void )
00252 {
00253         if( Cfg::GetCfg().GetCfgInt( "SetupScreen" , "PlayIntro" , 1 ) )
00254         {
00255                 Cfg::Handler    &h = Cfg::GetCfg( "@sounds" );
00256         
00257                 XMPEG   *mpeg = dynamic_cast<XMPEG *>( f->GetArsObject( 20 ) );
00258                 SMPEG_rewind( mpeg->GetMPEG() );
00259                 Mix_Chunk       *wave;
00260                 if( (wave = Mix_LoadWAV( h.GetCfgFile( "Splash" , "Intro" ).c_str() )) != 0 )
00261                         Mix_PlayChannel( 0 , wave , 0 );
00262                 mpeg->Play();
00263 
00264                 while( SMPEG_status( mpeg->GetMPEG() ) == SMPEG_PLAYING )
00265                         SDL_Delay( 10 );
00266                 
00267                 mpeg->EndPlay();
00268                 if( wave )
00269                         Mix_FreeChunk( wave );
00270 
00271                 mpeg->SetDirty();
00272                 UpdateWindow( true );
00273         }
00274         
00275         Cfg::Handler    &h = Cfg::GetCfg( "@sounds" );
00276         wave = Mix_LoadMUS( h.GetCfgFile( "music" , "MainScreen" ).c_str() );
00277         if( wave )
00278                 Mix_PlayMusic( wave , -1 );
00279         GetSound( "Splash-Greeting" ).Play();
00280 
00281         Preload( "SoundEvent" );
00282 }
00283 
00284 const bool MainGame::evMouseButtonDown( const MouseMessage &msg )
00285 {
00286         View::evMouseButtonDown( msg );
00287 
00288         return false;
00289 }
00290 
00291 const bool MainGame::evMessage( const UserMessage &msg )
00292 {
00293         const unsigned int i = msg.SubMessage();
00294 
00295         if( i < links.size() )
00296         {
00297                 f->ShutdownDialog( this );
00298                 
00299                 links[i]->MainLoop();
00300 
00301                 f->InitDialog( this );
00302                 f->Draw();
00303         }
00304         else if( i == 8 )
00305                 ArsPostMessage( Message( Message::APP_EXIT , 0 ) );
00306 /*
00307         else if( msg.SubMessage() == SAVE_GALAXY || msg.SubMessage() == LOAD_GALAXY )
00308                 return false;
00309         else if( msg.SubMessage() == SAVE_SPECIES )
00310         {
00311 
00312         }
00313 */
00314         return true;
00315 }
00316 
00317 const bool MainGame::evMouseLClick( const CtrlMessage &msg )
00318 {
00319         const int i = ((Window *)msg.source)->GetId();
00320         bool doredraw = false;
00321         if( i < (int)links.size() )
00322                 doredraw = links[i]->MainLoop();
00323         else if( i == 8 )
00324                 ArsPostMessage( Message( Message::APP_EXIT , 0 ) );
00325 
00326         EnableWindow();
00327         if( doredraw )
00328         {
00329                 f->GetArsObject( i )->ResetState( S_SELECTED|S_HIGHLIGHT|S_FOCUS ); 
00330                 f->Draw();
00331         }
00332         return true;
00333 }

Generated on Fri Dec 5 04:05:59 2003 for Borqueror by doxygen 1.3.3