keeping high scores
Does anybody have any suggestions on how to save the scores after one has completed a game level? I want to save them so i can display them in an opengl window for the user to view, any ideas or code would be appreciated, thanks
go hard or go home :)
April 25, 2005 09:20 AM
Write them out to a binary file
Assuming in a simple example your high scores are an array of integers
int g_nHighScores[MAX_HIGHSCORES];
Open the binary file:
FILE* f = fopen( "Highscores.dat", "w+b" );
if( !f )
return SOME_ERROR;
Write the data:
fwrite( g_nHighScores, sizeof( int ) * MAX_HIGHSCORES, 1, f );
Close the file:
fclose( f );
Now, to load them back in do:
FILE* f = fopen( "Highscores.dat", "r+b" );
if( !f )
return SOME_ERROR; //No highscores.dat file found....
Read the data:
fread( g_nHighScores, sizeof( int ) * MAX_HIGHSCORES, 1, f );
close the file:
fclose( f );
Assuming in a simple example your high scores are an array of integers
int g_nHighScores[MAX_HIGHSCORES];
Open the binary file:
FILE* f = fopen( "Highscores.dat", "w+b" );
if( !f )
return SOME_ERROR;
Write the data:
fwrite( g_nHighScores, sizeof( int ) * MAX_HIGHSCORES, 1, f );
Close the file:
fclose( f );
Now, to load them back in do:
FILE* f = fopen( "Highscores.dat", "r+b" );
if( !f )
return SOME_ERROR; //No highscores.dat file found....
Read the data:
fread( g_nHighScores, sizeof( int ) * MAX_HIGHSCORES, 1, f );
close the file:
fclose( f );
Yep binary file
------------------------------
void LoadScores()
{
FILE *f=fopen( "hscores.dat", "rb" );
int i;
if( f!=NULL )
{
for( i=0; i<10; i++ )
{
fread( &highscores, sizeof(Highscore), 1, f );
}
fclose(f);
}
else
{
for( i=0; i<10; i++ )
{
wsprintf( highscores.name, "%s", "my name" );
highscores.score=0;
}
}
}
void SaveScores()
{
FILE *f=fopen( "hscores.dat", "wb" );
int i;
if( f!=NULL )
{
for( i=0; i<10; i++ )
{
fwrite( &highscores, sizeof(Highscore), 1, f );
}
fclose(f);
}
}
int CheckScore()
{
for( int i=0; i<10; i++ )
{
if( player.score>highscores.score )
{
for( int j=9; j>i; j-- )
{
highscores[j]=highscores[j-1];
}
highscores.score=player.score;
return i;
}
}
return -1;
}
------------------------------
void LoadScores()
{
FILE *f=fopen( "hscores.dat", "rb" );
int i;
if( f!=NULL )
{
for( i=0; i<10; i++ )
{
fread( &highscores, sizeof(Highscore), 1, f );
}
fclose(f);
}
else
{
for( i=0; i<10; i++ )
{
wsprintf( highscores.name, "%s", "my name" );
highscores.score=0;
}
}
}
void SaveScores()
{
FILE *f=fopen( "hscores.dat", "wb" );
int i;
if( f!=NULL )
{
for( i=0; i<10; i++ )
{
fwrite( &highscores, sizeof(Highscore), 1, f );
}
fclose(f);
}
}
int CheckScore()
{
for( int i=0; i<10; i++ )
{
if( player.score>highscores.score )
{
for( int j=9; j>i; j-- )
{
highscores[j]=highscores[j-1];
}
highscores.score=player.score;
return i;
}
}
return -1;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement