📄 chighscoredlg.cpp
字号:
/*
Crazy Eggs Remade By Kevin Lynx
File : CHighscoreDlg.cpp
Desc : create the highscore dialog, and manage the highscore function
Date : 2007.1.25
*/
#include "CHighscoreDlg.h"
#include "../CGame.h"
#include "../resourceGen/res.h"
#include "../memleak.h"
const char *HIGH_SCORE_FILE = "properties/highscore.dat";
CHighscoreDlg::CHighscoreDlg( int id ) :
Dialog( DIALOG_SMALL, DIALOG_BTN, id, true, "Highscore", "", "OK", Dialog::BUTTONS_FOOTER )
{
//mTextAlign = 2;
SetHeaderFont( NORMAL_FONT );
( ( ImageFont* ) mHeaderFont)->SetScale( 1.5 );
SetLinesFont( NORMAL_FONT );
SetButtonFont( NORMAL_FONT );
SetColor( Dialog::COLOR_HEADER, Color( 0, 0, 255 ) );
SetColor( Dialog::COLOR_BUTTON_TEXT, Color( 255, 237, 120 ) );
SetColor( Dialog::COLOR_LINES, Color( 0, 100, 255 ) );
mContentInsets = Insets( 45, 60, 40, 50 );
mSpaceAfterHeader = 20;
Resize( ( G_WINDOW_WIDTH - 400 ) / 2, ( G_WINDOW_HEIGHT - 370 ) / 2, 400, 370 );
}
CHighscoreDlg::~CHighscoreDlg()
{
}
void CHighscoreDlg::ButtonDepress( int theId )
{
Dialog::ButtonDepress( theId );
if( mId == DLG_ID &&
theId == ID_FOOTER )
{
gSexyAppBase->KillDialog( this );
}
}
/*
===============================================================================================
*/
CHighscoreMgr::CHighscoreMgr()
{
LoadScore();
}
CHighscoreMgr::~CHighscoreMgr()
{
}
void CHighscoreMgr::LoadScore()
{
FILE *fp = fopen( HIGH_SCORE_FILE, "r" );
if( fp == NULL )
{
WriteDefaultScore();
}
fp = fopen( HIGH_SCORE_FILE, "r" );
if( fp == NULL )
return ;
for( int i = 0; i < SCORE_COUNT; ++ i )
{
fscanf( fp, "%s%d%d", m_scoreList[i].m_name, &m_scoreList[i].m_score,
&m_scoreList[i].m_maxHit );
}
fclose( fp );
m_scoreInfo = "Name Score MAX HIT\n";
for( int i = SCORE_COUNT - 1; i >= 0; -- i )
{
m_scoreInfo += StrFormat( "%s %d %d\n",
m_scoreList[i].m_name,
m_scoreList[i].m_score,
m_scoreList[i].m_maxHit );
}
}
void CHighscoreMgr::WriteDefaultScore()
{
FILE *fp = fopen( HIGH_SCORE_FILE, "w" );
for( int i = 0; i < SCORE_COUNT; ++i )
{
fprintf( fp, "kevin\t%d\t%d\n", ( i + 1 ) * 50 + Rand() % 50, ( i + 1 ) * 2 + Rand() % 5 );
}
fclose( fp );
}
void CHighscoreMgr::InsertScore( std::string name, int score, int maxHit )
{
for( int i = SCORE_COUNT - 1; i >= 0; -- i )
{
int s = m_scoreList[i].m_score + m_scoreList[i].m_maxHit * 5;
if( score + maxHit * 5 > s )
{
for( int j = 0; j < i; ++ j )
{
m_scoreList[j].m_maxHit = m_scoreList[j+1].m_maxHit ;
m_scoreList[j].m_score = m_scoreList[j+1].m_score ;
//m_scoreList[j].m_name = m_scoreList[j+1].m_name ;
strcpy( m_scoreList[j].m_name, m_scoreList[j+1].m_name );
//m_scoreList[j] = m_scoreList[j+1]; //it's a error
}
strcpy( m_scoreList[j].m_name, name.c_str() );
//m_scoreList[i].m_name = name;
m_scoreList[i].m_maxHit = maxHit;
m_scoreList[i].m_score = score;
break;
}
}
//write to file
FILE *fp = fopen( HIGH_SCORE_FILE, "w" );
for( int i = 0; i < SCORE_COUNT; ++ i )
{
fprintf( fp, "%s\t%d\t%d\n", m_scoreList[i].m_name,
m_scoreList[i].m_score,
m_scoreList[i].m_maxHit );
}
fclose( fp );
}
bool CHighscoreMgr::CanInsert( int score, int maxHit )
{
int lowestScore = m_scoreList[0].m_maxHit * 5 + m_scoreList[0].m_score ;
if( score + maxHit * 5 > lowestScore )
return true;
else
return false;
}
void CHighscoreMgr::ShowDlg()
{
//first, get the score information
LoadScore();
CHighscoreDlg *dlg = new CHighscoreDlg();
dlg->mDialogLines = m_scoreInfo;
gSexyAppBase->AddDialog( dlg );
}
std::string CHighscoreMgr::GetScoreInfo()
{
LoadScore();
return m_scoreInfo;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -