Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

CGameAppUi.cpp

Go to the documentation of this file.
00001 // Copyright 2002 Kenneth Guy,
00002 // 
00003 // You are free to take the source and do as you wish with it.
00004 // However it would be nice if you let me know if the code was useful
00005 // to you and give me an acknowledgement if you use a significant portion
00006 // of the code in an application.
00007 // 
00008 // CGameAppUi.cpp
00009 
00013 //
00014 
00015 #include <eikappui.h>
00016 #include <eikapp.h>
00017 #include <eikdoc.h>
00018 #include <eikenv.h>
00019 #include <uikon.hrh>
00020 #include <eikmfne.h>
00021 #include <eikdialg.h>
00022 #include <eikon.hrh>
00023 #include <eikmenup.h>
00024 #include <ckninfo.h>
00025 
00026 #include "CGameAppUi.h"
00027 #include "CGameAppView.h"
00028 #include "CGameApplication.h"
00029 #include "CGameDocument.h"
00030 #include "CGameState.h"
00031 #include "CScoreDialog.h"
00032 #include "CHighScores.h"
00033 
00034 
00039 void CGameAppUi::ConstructL() {    
00040   BaseConstructL();
00041   iGameState=((CGameDocument*)iDocument)->GameState();
00042   iAppView=new(ELeave) CGameAppView;
00043   iAppView->ConstructL(ClientRect(), iGameState);
00044   iGame= CGameFramework::NewL();
00045   iFrameworkState=CGameFramework::EDied;
00046   iLevel=0;
00047   iMapNo=0;
00048   iGameState->SetState(CGameState::ENoGame);
00049 }
00050 
00053 CGameAppUi::~CGameAppUi() {     
00054   delete iAppView;
00055   delete iGame;
00056 }
00057 
00066 void CGameAppUi::HandleCommandL(TInt aCommand) {
00067   switch (aCommand) {
00068     
00069   case ECmdStartGame:
00070   case ECmdContinueGame:
00071     {
00072       if(iGameState->State()==CGameState::ENoGame ||
00073          iGameState->State()==CGameState::ENextLevel) {
00074         LoadLevelL();
00075       }    
00076       
00077       // play a game
00078       // reset if we aren't starting the next level or have a paused game
00079       CGameFramework::TGameState state=
00080         iGame->RunL(iGameState->State()==CGameState::ENoGame ? ETrue : EFalse);
00081       
00082       switch(state) {
00083       case CGameFramework::EPaused:
00084         iGameState->SetState(CGameState::EPaused);
00085         break;
00086       case CGameFramework::ELevelCompleted:
00087         if(NextLevelL()==EFalse) {
00088           // congrats dialog
00089           CEikDialog* dialog = new (ELeave) CEikDialog();
00090           dialog->ExecuteLD(R_FINISHED_DIALOG);
00091           iGameState->SetState(CGameState::ENoGame);
00092         } else {
00093           iGameState->SetState(CGameState::ENextLevel);
00094         }
00095         break;
00096       case CGameFramework::EDied:
00097         iGameState->SetState(CGameState::ENoGame);
00098         break;      
00099       }
00100       
00101       // either died, or completed the game
00102       if(iGameState->State()==CGameState::ENoGame) {
00103         iMapNo=0;
00104         iLevel=0;
00105         iGameState->SetState(CGameState::ENoGame);
00106         if(iGameState->HighScores().GoodEnough(iGame->Score())) {
00107           CScoreDialog::RunDlgLD(iGameState->HighScores(),iGame->Score());
00108         }
00109       }
00110     }
00111     break;
00112   
00113   case ECmdCancelGame:
00114     switch(iGameState->State()) {
00115     case CGameState::ENoGame:
00116       break;
00117     case CGameState::EPaused:  // paused so we need to let the thread end
00118       iGame->CancelL();
00119       iMapNo=0;
00120       iLevel=0;
00121       iGameState->SetState(CGameState::ENoGame);
00122       iFrameworkState=CGameFramework::EDied;
00123       break;
00124     case CGameState::ENextLevel: // then set level to start
00125       iMapNo=0;
00126       iLevel=0;
00127       iGameState->SetState(CGameState::ENoGame);
00128       iFrameworkState=CGameFramework::EDied;
00129       iAppView->DrawNow();
00130       break;
00131     }
00132     
00133     break;
00134 
00135 
00136   case ECmdHelp:
00137     HelpDialogL();
00138     break;
00139 
00140   case ECmdAbout:
00141     AboutDialogL();
00142     break;
00143 
00144 
00145   case EEikCmdExit:
00146     Exit();
00147     break;
00148   }
00149 }
00150 
00161 void CGameAppUi::DynInitMenuPaneL(TInt aMenuId,CEikMenuPane* aMenuPane) {
00162   if (aMenuId == R_GAME_GAME_MENU) {    
00163     switch(iGameState->State()) {
00164     case CGameState::ENoGame:
00165       aMenuPane->SetItemDimmed(ECmdCancelGame,ETrue);        
00166       aMenuPane->SetItemDimmed(ECmdContinueGame,ETrue);
00167       aMenuPane->SetItemDimmed(ECmdStartGame,EFalse);
00168       break;
00169     case CGameState::EPaused:
00170     case CGameState::ENextLevel:
00171       aMenuPane->SetItemDimmed(ECmdCancelGame,EFalse);        
00172       aMenuPane->SetItemDimmed(ECmdContinueGame,EFalse);
00173       aMenuPane->SetItemDimmed(ECmdStartGame,ETrue);
00174       break;
00175     }
00176   }
00177 }
00178 
00182 const TUid KLevelTypeUid={1};
00187 const TUid KMapTypeUid={2};
00188 
00189 
00198 TBool CGameAppUi::NextLevelL() {
00199   TBool anotherLevel=EFalse;
00200   TParse parse;
00201   parse.Set(Application()->DllName(),NULL,NULL);
00202 
00203   RFs fs;
00204   User::LeaveIfError(fs.Connect());
00205   CleanupClosePushL(fs);
00206 
00207 
00208   // get a list of all the level files, ie files with first uid
00209   // KUidExampleGame and second uid KLevelTypeUid,
00210   TUidType levelType(KUidExampleGame,KLevelTypeUid);
00211   CDir* dir=NULL;
00212   User::LeaveIfError(fs.GetDir(parse.DriveAndPath(),levelType,ESortByUid | EAscending,dir));
00213   CleanupStack::PushL(dir);
00214 
00215   while(iLevel < dir->Count() && anotherLevel==EFalse) {
00216     TUid levelId=(*dir)[iLevel].MostDerivedUid();
00217     CDir* mapDir=NULL;
00218 
00219     // get a list of all map files for this level, ie with first uid
00220     // KUidExampleGame, second uid KMapTypeUid, and third ui levelId
00221     TUidType mapType(KUidExampleGame,KMapTypeUid,levelId);
00222     User::LeaveIfError(fs.GetDir(parse.DriveAndPath(),mapType,ESortByName | EAscending,mapDir));
00223 
00224     // if we have another map for this level use it,
00225     if(iMapNo+1<mapDir->Count()) {
00226       iMapNo++;
00227       anotherLevel=ETrue;
00228     } else {
00229       // else try the next level
00230       iMapNo=-1;
00231       iLevel++;
00232     }
00233     delete mapDir;
00234   }
00235 
00236   CleanupStack::PopAndDestroy(2,&fs); // dir
00237 
00238   if(anotherLevel==EFalse) {
00239     // back to first level
00240     iLevel=0;
00241     iMapNo=0;
00242   }
00243   return anotherLevel;
00244 }
00245 
00246 
00253 void CGameAppUi::LoadLevelL() {
00254   TParse parse;
00255   parse.Set(Application()->DllName(),NULL,NULL);
00256   RFs fs;
00257   User::LeaveIfError(fs.Connect());
00258   CleanupClosePushL(fs);
00259 
00260   // get a list of all the level files, ie files with first uid
00261   // KUidExampleGame and second uid KLevelTypeUid,
00262   TUidType levelType(KUidExampleGame,KLevelTypeUid);
00263   CDir* dir=NULL;
00264   User::LeaveIfError(fs.GetDir(parse.DriveAndPath(),levelType,ESortByUid | EAscending,dir));
00265   CleanupStack::PushL(dir);
00266 
00267   if(iLevel >= dir->Count()) {
00268     iLevel=0;
00269     User::Leave(KErrNotFound);
00270   }
00271 
00272   TUid levelId=(*dir)[iLevel].MostDerivedUid();
00273   CDir* mapDir=NULL;
00274   // get a list of all map files for this level, ie with first uid
00275   // KUidExampleGame, second uid KMapTypeUid, and third ui levelId
00276   TUidType mapType(KUidExampleGame,KMapTypeUid,levelId);
00277   User::LeaveIfError(fs.GetDir(parse.DriveAndPath(),mapType,ESortByName | EAscending,mapDir));
00278   CleanupStack::PushL(mapDir);
00279 
00280   if(iMapNo >= mapDir->Count()) {
00281     iMapNo=0;
00282     User::Leave(KErrNotFound);
00283   }
00284 
00285   // get the file entries for the level and map files
00286   const TEntry& levelEntry=(*dir)[iLevel];
00287   const TEntry& mapEntry=(*mapDir)[iMapNo];
00288 
00289   // get the full path and file names of the level and map files
00290   HBufC* levelFileName= HBufC::NewLC(levelEntry.iName.Length()+
00291                                      parse.DriveAndPath().Length());
00292   levelFileName->Des().Append(parse.DriveAndPath());
00293   levelFileName->Des().Append(levelEntry.iName);
00294 
00295   HBufC* mapFileName= HBufC::NewLC(mapEntry.iName.Length()+
00296                                    parse.DriveAndPath().Length());
00297   mapFileName->Des().Append(parse.DriveAndPath());
00298   mapFileName->Des().Append(mapEntry.iName);  
00299 
00300 
00301   // pass the filenames to CGameFramework::LoadLevelL()
00302   // reset the ship if it is a new game.
00303   if(iGameState->State()==CGameState::ENoGame)
00304     iGame->LoadLevelL(*levelFileName,*mapFileName,ETrue);
00305   else //iGameState->State()==CGameState::ENextLevel
00306     iGame->LoadLevelL(*levelFileName,*mapFileName,EFalse);
00307     
00308 
00309 
00310 
00311   CleanupStack::PopAndDestroy(5,&fs); // dir, mapDir,levelFileName,mapFileName
00312 }
00313 
00314 
00317 void CGameAppUi::HelpDialogL() {
00318   CEikonEnv* eikonEnv=CEikonEnv::Static();
00319   HBufC *title= eikonEnv->AllocReadResourceL(R_HELP_DIALOG_TITLE);
00320   CleanupStack::PushL(title);
00321   HBufC *body= eikonEnv->AllocReadResourceL(R_HELP_DIALOG_BODY);
00322   CleanupStack::PushL(body);
00323   CCknInfoDialog::RunDlgLD(*title,*body);
00324   CleanupStack::PopAndDestroy(2,title); // body
00325 }
00326 
00327 
00330 void CGameAppUi::AboutDialogL() {
00331   CEikonEnv* eikonEnv=CEikonEnv::Static();
00332   HBufC *title= eikonEnv->AllocReadResourceL(R_ABOUT_DIALOG_TITLE);
00333   CleanupStack::PushL(title);
00334   HBufC *body= eikonEnv->AllocReadResourceL(R_ABOUT_DIALOG_BODY);
00335   CleanupStack::PushL(body);
00336   CCknInfoDialog::RunDlgLD(*title,*body);
00337   CleanupStack::PopAndDestroy(2,title); // body
00338 }

Documentation for Game (Beta) version 1.44.