From 35857f0e40cee27073690b17574b417f2a2ee4c2 Mon Sep 17 00:00:00 2001 From: Charadon Date: Sun, 12 Jun 2022 18:03:43 -0400 Subject: Finished score screen, and fixed bug where if no scores are in the file, you get garbled output. --- src/title.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/title.c b/src/title.c index 808885a..2327713 100644 --- a/src/title.c +++ b/src/title.c @@ -1,5 +1,6 @@ #include "pong.h" #include +#include struct LeaderboardEntries { int Score; @@ -17,16 +18,21 @@ static int compare_int(const void *Score1, const void *Score2) { return +1; } -static void order_leaderboard() { +static int order_leaderboard() { // Find leaderboard file, and open it. char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; if((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { - return; //If it doesn't exist yet, we don't need to care. + return(1); //If it doesn't exist yet, we don't need to care. } - + // Check if file is empty. + fscanf(LeaderboardFile, "\n"); + if(feof(LeaderboardFile)) { + return(1); + } + rewind(LeaderboardFile); //If not , rewind the stream. // Init a temporary pointer to store scores and names into. This will be organized into Top10. struct LeaderboardEntries *TmpStore[UINT16_MAX*2]; for (int size = 0; size < (UINT16_MAX*2); size++) //If there's 131,000 entries, I dunno what to tell you. You really like pong... @@ -54,7 +60,7 @@ static void order_leaderboard() { if (LeaderboardFile != NULL) { fclose(LeaderboardFile); } - return; + return(0); } static void score_screen(Camera2D *MainCamera) { @@ -66,6 +72,7 @@ static void score_screen(Camera2D *MainCamera) { if((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { return; //If it doesn't exist yet, we don't need to care. } + // Init a temporary pointer to store scores and names into. This will be organized into Top10. struct LeaderboardEntries *Scores[UINT16_MAX*2]; for (int size = 0; size < (UINT16_MAX*2); size++) //If there's 131,000 entries, I dunno what to tell you. You really like pong... @@ -91,9 +98,10 @@ static void score_screen(Camera2D *MainCamera) { //Mouse bool LookingAtScores = true; bool MouseCursorIn = true; + SetMousePosition(1280/2, 720/2); Vector2 OldPosition = GetMousePosition(); Vector2 NewPosition = GetMousePosition(); - Rectangle MouseCursor = {0,0,1,1}; + Rectangle MouseCursor = {1280/2.0f,720/2.0f,1,1}; Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); //Page Buttons Rectangles @@ -347,7 +355,7 @@ int title_screen() { MainCamera.rotation = 0.0f; // Load leaderboard - order_leaderboard(); + int NoScores = order_leaderboard(); bool TitleScreenGoing = true; int Choice = 0; @@ -371,6 +379,9 @@ int title_screen() { Rectangle Mouse = { 1280.0f/2, 720.0f/2, 10, 10 }; + Rectangle AllScores = { + 595, 720-155, 400, 55 + }; Rectangle *Selected; Selected = &Versus; @@ -461,9 +472,10 @@ int title_screen() { DrawText("Exit", 20, 350, 48, WHITE); if(Choice == 1) { DrawText("Leaderboard:", 600, 0, 48, WHITE); - if( Top10[0].Name[0] == ' ' ) { + if(NoScores == 1) { goto skip; } + printf("%c\n", Top10[0].Name[0]); char LeaderboardText[1024]; for (int i = 1; i <= 10; i++) { if(Top10[i-1].Name[0] != '\0') { //If name is blank, that means we're at the end of the list. @@ -472,14 +484,20 @@ int title_screen() { } } } - skip: - DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE); + skip: + if (Choice == 1) { + if(CheckCollisionRecs(Mouse, AllScores)) { + DrawRectangleRec(AllScores, RED); + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + score_screen(&MainCamera); + } + } + DrawText("See All Scores...", 600, 720-150, 48, WHITE); + } + DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE); EndMode2D(); DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN); EndDrawing(); - if (IsKeyPressed(KEY_D)) { - score_screen(&MainCamera); - } } Mix_HaltMusic(); Mix_FreeMusic(TitleMusic); -- cgit 1.4.1-2-gfad0