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

CHighScores.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 // CHighScores.cpp
00009 
00014 #include "CHighScores.h"
00015 #include "f32file.h"
00016 #include "s32file.h"
00017 
00019 const TInt KHighScoreEntries=7;
00020 
00023 TInt CHighScores::Number() {
00024   return KHighScoreEntries;
00025 }
00026 
00040 void CHighScores::AddScoreL(const TDesC& aName, TInt aScore) {
00041   for(TInt i=0;i<KHighScoreEntries;++i) {
00042     if(aScore > iScores[i]) {
00043       iNames.Remove(KHighScoreEntries-1);
00044       iScores.Remove(KHighScoreEntries-1);
00045 
00046       HBufC* name=aName.AllocL();
00047       TInt error=iNames.Insert(name,i);
00048       if(error!=KErrNone) {
00049         delete name;
00050         User::Leave(error);
00051       }
00052       User::LeaveIfError(iScores.Insert(aScore,i));
00053       iLastAdded=i;
00054       return;
00055     }
00056   }
00057 }
00058 
00059 
00064 TBool CHighScores::GoodEnough(TInt aScore) {
00065   iLastAdded=-1;
00066   if(aScore > iScores[KHighScoreEntries-1]) 
00067     return ETrue;
00068   else
00069     return EFalse;
00070 }
00071 
00077 TInt CHighScores::Score(TInt aEntry) {
00078   return(iScores[aEntry]);
00079 }
00080 
00086 const TDesC& CHighScores::Name(TInt aEntry) {
00087   return(*(iNames[aEntry]));
00088 }
00089 
00094 CHighScores* CHighScores::NewL(const TDesC &aFileName) {
00095   CHighScores* self= new (ELeave) CHighScores;
00096   CleanupStack::PushL(self);
00097   self->ConstructL(aFileName);
00098   CleanupStack::Pop(self);
00099   return(self);
00100 }
00101 
00107 TInt CHighScores::LastAdded() {
00108   return iLastAdded;
00109 }
00110 
00118 void CHighScores::ConstructL(const TDesC &aFileName) {
00119   iLastAdded=-1;
00120   iFileName=aFileName.AllocL();
00121   // try and load high scores, if that fails,
00122   // create empty high score sheet.
00123   TRAPD(error,LoadL());
00124   if(error!=KErrNone) {
00125     InitL();
00126   }
00127 }
00128 
00133 CHighScores::~CHighScores() {
00134   // if we can't save the high scores there isn't much we can
00135   // do about it, so just ignore the error.
00136   TRAPD(ignore,SaveL());
00137   delete iFileName;
00138   TInt count=iNames.Count();
00139   while(count--) {
00140     delete (iNames[count]);
00141   }
00142 }
00143 
00144 
00147 void CHighScores::Reset() {
00148   TInt count=iNames.Count();
00149   while(count--) {
00150     delete (iNames[count]);
00151   }
00152   iNames.Reset();
00153   iScores.Reset();
00154 }
00155 
00156 
00159 void CHighScores::InitL() {
00160   Reset();
00161   _LIT(KEmpty,"No One");
00162   TInt count=KHighScoreEntries;
00163   while(count--) {
00164     HBufC* name=KEmpty().AllocL();
00165     TInt error=iNames.Append(name);
00166     if(error!=KErrNone) {
00167       delete name;
00168       User::Leave(error);
00169     }
00170     User::LeaveIfError(iScores.Append(0));
00171   }
00172 }
00173 
00174 
00177 void CHighScores::SaveL() {  
00178   RFs fs;
00179   User::LeaveIfError(fs.Connect());
00180   CleanupClosePushL(fs);
00181 
00182   RFileWriteStream file;
00183   User::LeaveIfError(file.Replace(fs,*iFileName,EFileWrite));
00184   CleanupClosePushL(file);
00185 
00186   for(TInt i=0;i<KHighScoreEntries;++i) {
00187     file << *(iNames[i]);
00188     file.WriteInt32L(iScores[i]);
00189   }
00190 
00191   CleanupStack::PopAndDestroy(2,&fs);      
00192 }
00193 
00194 
00197 void CHighScores::LoadL() {
00198   RFs fs;
00199   User::LeaveIfError(fs.Connect());
00200   CleanupClosePushL(fs);
00201 
00202   RFileReadStream file;
00203   User::LeaveIfError(file.Open(fs,*iFileName,EFileRead));
00204   CleanupClosePushL(file);
00205 
00206   for(TInt i=0;i<KHighScoreEntries;++i) {
00207     HBufC* name=HBufC::NewL(file,KMaxTInt);
00208     TInt error=iNames.Append(name);
00209     if(error!=KErrNone) {
00210       delete name;
00211       User::Leave(error);
00212     }
00213     TInt score=file.ReadInt32L();
00214     User::LeaveIfError(iScores.Append(score));
00215   }
00216 
00217   CleanupStack::PopAndDestroy(2,&fs);
00218 }

Documentation for Game (Beta) version 1.44.