fix scoreboard

This commit is contained in:
Hanse-14 2023-12-20 16:35:25 +01:00
parent 4ea40d99d2
commit 1a9a348793
4 changed files with 17 additions and 18 deletions

View File

@ -3,6 +3,7 @@
#define MAX_PRINTED_SCORES 5
int recordsInFile;
// struct that store player score at the end of the game
typedef struct playerScore_t

View File

@ -7,7 +7,7 @@
//global struct for storing all data of the 10 best players
playerScore_t topScores[];
int recordsInFile;
//==========================
//==== savePlayerScores ====
@ -52,8 +52,7 @@ void readTopScores(const char *filename)
{
FILE *filePtr;
playerScore_t tempPlayerScore;
int recordsInFile;
int highestPlayerScore;
int highestPlayerScore = 0;
int count = 0; // increase up to 'MAX_PRINTED_SCORES'
// determine the number of contents in the file
@ -70,13 +69,13 @@ void readTopScores(const char *filename)
// fail with file opening
if (filePtr == NULL)
{
LOGI("Datei: Fehler beim Öffnen der Datei für die besten 10 Ergebnisse!\n");
LOGI("Datei: Fehler beim Öffnen der Datei für die besten 10 Ergebnisse!\n");
game.gameState = EXIT;
return;
}
LOGI("Datensaetze in Datei: %d\n", recordsInFile);
LOGI("Datei: Datensaetze in Datei: %d\n", recordsInFile);
//---- search for the highest score------
@ -85,38 +84,36 @@ void readTopScores(const char *filename)
fread(&tempPlayerScore, sizeof(playerScore_t), 1, filePtr);
if(tempPlayerScore.score > highestPlayerScore)
{
highestPlayerScore == tempPlayerScore.score;
highestPlayerScore = tempPlayerScore.score;
}
}
//--- decrease highest score -----
while((count < MAX_PRINTED_SCORES) && (count < MAX_PRINTED_SCORES))
while((count < MAX_PRINTED_SCORES) && (count < recordsInFile))
{
// set file pointer to start of the file
rewind(filePtr);
// search for the highest score and then save it in topScores
for (int i = 0; i < recordsInFile; i++)
{
// read record from the file
fread(&tempPlayerScore, sizeof(playerScore_t), 1, filePtr);
// current highscore found
if(tempPlayerScore.score == highestPlayerScore)
{
topScores[count] = tempPlayerScore;
LOGI("score: %d name: %s schwierigkeit: %d map: %s\n", topScores[count].score, topScores[count].playerName, topScores[count].difficulty, topScores[count].map);
LOGI("Datei: score: %d name: %s schwierigkeit: %d map: %s\n", topScores[count].score, topScores[count].playerName, topScores[count].difficulty, topScores[count].map);
count++;
}
// leave if MAX_PRINTED_SCORES is reached
if(count >= MAX_PRINTED_SCORES)
// leave if limit is reached
if(count >= recordsInFile || count >= MAX_PRINTED_SCORES)
{
break;
}
}
highestPlayerScore--;
}
fclose(filePtr);
}

View File

@ -82,7 +82,6 @@ void showLeaderboard()
//play audio file, wait until playback is finished
//note: when displaying actual leaderboard, the second parameter should be 'false' to not block the program
renderLeaderboard();
return;

View File

@ -10,6 +10,8 @@
#include <stdio.h>
#define NUM_COLUMNS 4
void renderGame(){
SDL_SetRenderDrawColor(game.renderer, 0, 0, 0, 255);
@ -457,7 +459,6 @@ void renderInfoScreen()
//--------------------------------------------------------------
void renderLeaderboard()
{
#define NUM_COLUMNS 4
char* menuDescription[] ={"LEADERBOARD"};
char* columnDescriptions[NUM_COLUMNS] =
@ -508,7 +509,8 @@ void renderLeaderboard()
}
// rendering score data
for (int i = 0; i < MAX_PRINTED_SCORES; ++i) {
int maxCycles = (recordsInFile < MAX_PRINTED_SCORES) ? recordsInFile : MAX_PRINTED_SCORES;
for (int i = 0; i < maxCycles; ++i) {
char playerName[50]; // temporary buffer for text
char map[50]; // temporary buffer for text
strcpy(playerName, topScores[i].playerName);
@ -574,13 +576,13 @@ int CreateSDLWindow(){
// Erstelle ein SDL-Fenster
game.window = SDL_CreateWindow("Snake", 350, 50, config.windowSize, config.windowSize, SDL_WINDOW_OPENGL);
if (game.window == NULL) {
printf("Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
LOGI("SDL: Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
game.renderer = SDL_CreateRenderer(game.window, -1, SDL_RENDERER_ACCELERATED);
if (game.renderer == NULL) {
printf("Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
LOGI("SDL: Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
return 0;