📄 chighscores.cpp
字号:
// Copyright 2002 Kenneth Guy,
//
// CHighScores.cpp
/** \file CHighScores.cpp
implementation of class CHighScores */
#include "CHighScores.h"
#include "f32file.h"
#include "s32file.h"
/** Number of entries in the table */
const TInt KHighScoreEntries=7;
/** Number of entries in the table */
TInt CHighScores::Number() {
return KHighScoreEntries;
}
/** Add a new high score.
Finds the correct position in the table. Removes the last
entry in the table. Then adds the new one.
Sets iLastAdded to the new entry so that the ui can highlite
that line in the table.
\param aName Players name
\param aScore Players score
*/
void CHighScores::AddScoreL(const TDesC& aName, TInt aScore) {
for(TInt i=0;i<KHighScoreEntries;++i) {
if(aScore > iScores[i]) {
iNames.Remove(KHighScoreEntries-1);
iScores.Remove(KHighScoreEntries-1);
HBufC* name=aName.AllocL();
TInt error=iNames.Insert(name,i);
if(error!=KErrNone) {
delete name;
User::Leave(error);
}
User::LeaveIfError(iScores.Insert(aScore,i));
iLastAdded=i;
return;
}
}
}
/** Checks to see if a score qualifies for an entry.
Resets the iLastAdded as a new game has been played */
TBool CHighScores::GoodEnough(TInt aScore) {
iLastAdded=-1;
if(aScore > iScores[KHighScoreEntries-1])
return ETrue;
else
return EFalse;
}
/** Return the score for an entry
\param aEntry entry to return aEntry should be < number returned
by Number() and >= 0
*/
TInt CHighScores::Score(TInt aEntry) {
return(iScores[aEntry]);
}
/** Return the name for an entry
\param aEntry entry to return aEntry should be < number returned
by Number() and >= 0
*/
const TDesC& CHighScores::Name(TInt aEntry) {
return(*(iNames[aEntry]));
}
/** Leave safe construction
\param aFileName full path and filename of high score file
*/
CHighScores* CHighScores::NewL(const TDesC &aFileName) {
CHighScores* self= new (ELeave) CHighScores;
CleanupStack::PushL(self);
self->ConstructL(aFileName);
CleanupStack::Pop(self);
return(self);
}
/** Last entry added.
\return last entry added, or -1 if no entry has been added recently
*/
TInt CHighScores::LastAdded() {
return iLastAdded;
}
/** 2nd Phase construction.
Load the high score table from the file, if that fails
create a blank table.
*/
void CHighScores::ConstructL(const TDesC &aFileName) {
iLastAdded=-1;
iFileName=aFileName.AllocL();
// try and load high scores, if that fails,
// create empty high score sheet.
TRAPD(error,LoadL());
if(error!=KErrNone) {
InitL();
}
}
/** Destructor.
Tries to save the high scores back to the file */
CHighScores::~CHighScores() {
// if we can't save the high scores there isn't much we can
// do about it, so just ignore the error.
TRAPD(ignore,SaveL());
delete iFileName;
TInt count=iNames.Count();
while(count--) {
delete (iNames[count]);
}
}
/** Reset the high score arrays. */
void CHighScores::Reset() {
TInt count=iNames.Count();
while(count--) {
delete (iNames[count]);
}
iNames.Reset();
iScores.Reset();
}
/** Create a blank high score table. */
void CHighScores::InitL() {
Reset();
_LIT(KEmpty,"No One");
TInt count=KHighScoreEntries;
while(count--) {
HBufC* name=KEmpty().AllocL();
TInt error=iNames.Append(name);
if(error!=KErrNone) {
delete name;
User::Leave(error);
}
User::LeaveIfError(iScores.Append(0));
}
}
/** Save a high score table. */
void CHighScores::SaveL() {
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
RFileWriteStream file;
User::LeaveIfError(file.Replace(fs,*iFileName,EFileWrite));
CleanupClosePushL(file);
for(TInt i=0;i<KHighScoreEntries;++i) {
file << *(iNames[i]);
file.WriteInt32L(iScores[i]);
}
CleanupStack::PopAndDestroy(2,&fs);
}
/** Load a high score table. */
void CHighScores::LoadL() {
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
RFileReadStream file;
User::LeaveIfError(file.Open(fs,*iFileName,EFileRead));
CleanupClosePushL(file);
for(TInt i=0;i<KHighScoreEntries;++i) {
HBufC* name=HBufC::NewL(file,KMaxTInt);
TInt error=iNames.Append(name);
if(error!=KErrNone) {
delete name;
User::Leave(error);
}
TInt score=file.ReadInt32L();
User::LeaveIfError(iScores.Append(score));
}
CleanupStack::PopAndDestroy(2,&fs);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -