• Advertisement
Sign in to follow this  

keeping high scores

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 );

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement