From 2fef65fbe758daffab758960719dbb088d264651 Mon Sep 17 00:00:00 2001 From: Charadon Date: Sun, 12 Jun 2022 17:18:40 -0400 Subject: Fixed odd memory corruption bug, and more work on All Scores Screen --- src/title.c | 69 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/title.c b/src/title.c index 01cc508..25e0112 100644 --- a/src/title.c +++ b/src/title.c @@ -6,18 +6,7 @@ struct LeaderboardEntries { char Name[16]; }; -struct LeaderboardEntries Top10[10] = { - 0, " ", - 0, " ", - 0, " ", - 0, " ", - 0, " ", - 0, " ", - 0, " ", - 0, " ", - 0, " ", - 0, " " -}; +struct LeaderboardEntries Top10[10]; static int compare_int(const void *Score1, const void *Score2) { const struct LeaderboardEntries *Entry1 = *(struct LeaderboardEntries**) Score1; @@ -37,7 +26,7 @@ static void order_leaderboard() { 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 *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... @@ -52,15 +41,15 @@ static void order_leaderboard() { fscanf(LeaderboardFile, "%s %d\n", TmpStore[a]->Name, &TmpStore[a]->Score); } qsort(TmpStore, size, sizeof(struct LeaderboardEntries *), compare_int); + for(int i = 0; i < size; i++) { //Copy first 10 elements into top10 - strcpy(Top10[i].Name, TmpStore[i]->Name); - Top10[i].Score = TmpStore[i]->Score; + strcpy(Top10[i].Name, TmpStore[i]->Name); + Top10[i].Score = TmpStore[i]->Score; } // Cleanup // Free TmpStore - for(int i = 0; i < (UINT16_MAX*2); i++) - free(TmpStore[i]); //Truth be told, I dunno if this even works, since the amount of memory used is kilobytes... + free(*TmpStore); //Truth be told, I dunno if this even works, since the amount of memory used is kilobytes... // Close leaderboard file. if (LeaderboardFile != NULL) { fclose(LeaderboardFile); @@ -77,7 +66,6 @@ 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... @@ -92,12 +80,14 @@ static void score_screen(Camera2D *MainCamera) { // Load Scores and Names int size = 0; for (int a = 0; !feof(LeaderboardFile); a++, size++) { //Load data from file into array. - fscanf(LeaderboardFile, "%s %d\n", Scores[a]->Name, &Scores[a]->Score); + fscanf(LeaderboardFile, "%s %d", Scores[a]->Name, &Scores[a]->Score); + if(feof(LeaderboardFile)) { + break; + } } qsort(Scores, size, sizeof(struct LeaderboardEntries *), compare_int); //Begin drawing scores. - //Mouse bool LookingAtScores = true; bool MouseCursorIn = true; @@ -106,13 +96,17 @@ static void score_screen(Camera2D *MainCamera) { Rectangle MouseCursor = {0,0,1,1}; Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); + //Page Buttons Rectangles + Rectangle PrevPage = {5, 720-50, 70, 45}; + Rectangle NextPage = {1280-70, 720-50, 70, 45}; + int Page = 0; bool EndOfPages = false; while(LookingAtScores == true && GameGoing == true) { // Update Camera MainCamera->zoom = GetScreenHeight()/720.0f; - MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; + MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; + MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; //Mouse if (MouseCursorIn == true) { @@ -127,7 +121,6 @@ static void score_screen(Camera2D *MainCamera) { MouseCursor.x += OldPosition.x-NewPosition.x; } } - if (IsKeyPressed(KEY_ESCAPE)) { EnableCursor(); MouseCursorIn = false; @@ -135,20 +128,40 @@ static void score_screen(Camera2D *MainCamera) { DisableCursor(); MouseCursorIn = true; } + EndOfPages = false; BeginDrawing(); ClearBackground(BLACK); BeginMode2D(*MainCamera); DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - DrawText("<--", 5, 720-50, 48, WHITE); - DrawText("-->", 1280-70, 720-50, 48, WHITE); - for (int i = 1; i <= 10; i++) { + // Scores + int a = 0; + for (int i = 1+(10*Page); i <= 10+(10*Page); i++, a++) { if(Scores[i-1]->Name[0] != ' ') { - DrawText(TextFormat("%d. %s: %d", i, Scores[i-1]->Name, Scores[i-1]->Score), 500, 50*i, 48, WHITE); + DrawText(TextFormat("%d. %s: %d", i, Scores[i-1]->Name, Scores[i-1]->Score), 460, 60+(50*a), 48, WHITE); } else { EndOfPages = true; } } + // Page Buttons + if(CheckCollisionRecs(MouseCursor, PrevPage)) { + DrawRectangleRec(PrevPage, RED); + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if(Page > 0) { + Page--; + } + } + } else if(CheckCollisionRecs(MouseCursor, NextPage) && EndOfPages == false) { + DrawRectangleRec(NextPage, RED); + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if(EndOfPages == false) { + Page++; + } + } + } + DrawText("<--", 5, 720-50, 48, WHITE); + DrawText("-->", 1280-70, 720-50, 48, WHITE); + // Cursor DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); EndMode2D(); EndDrawing(); @@ -297,7 +310,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { DrawText("Windowed", 100, 242, 42, WHITE); DrawText("Fullscreen", 100, 284, 42, WHITE); DrawText("Borderless", 100, 326, 42, WHITE); - DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); + DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); EndMode2D(); EndDrawing(); } -- cgit 1.4.1-2-gfad0 From 75186deaa18aab61a4d4236c6e4a20b79707de0e Mon Sep 17 00:00:00 2001 From: Charadon Date: Sun, 12 Jun 2022 17:25:43 -0400 Subject: Finished All Scores Screen --- src/title.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/title.c b/src/title.c index 25e0112..808885a 100644 --- a/src/title.c +++ b/src/title.c @@ -144,7 +144,7 @@ static void score_screen(Camera2D *MainCamera) { } } // Page Buttons - if(CheckCollisionRecs(MouseCursor, PrevPage)) { + if(CheckCollisionRecs(MouseCursor, PrevPage) && Page > 0) { DrawRectangleRec(PrevPage, RED); if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { if(Page > 0) { @@ -161,15 +161,20 @@ static void score_screen(Camera2D *MainCamera) { } DrawText("<--", 5, 720-50, 48, WHITE); DrawText("-->", 1280-70, 720-50, 48, WHITE); + //Exit Button + if(CheckCollisionRecs(MouseCursor, (Rectangle){0,0,42,120})) { + DrawRectangle(0, 0, 42, 120, RED); + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + LookingAtScores = false; + } + } + DrawText("<", 0, 0, 128, WHITE); // Cursor DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); EndMode2D(); EndDrawing(); - if(IsKeyPressed(KEY_Q)) { - return; - } } - + return; } static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { @@ -461,7 +466,7 @@ int title_screen() { } char LeaderboardText[1024]; for (int i = 1; i <= 10; i++) { - if(Top10[i-1].Name[0] != ' ') { + if(Top10[i-1].Name[0] != '\0') { //If name is blank, that means we're at the end of the list. snprintf(LeaderboardText, sizeof(LeaderboardText), "%d: %s : %d", i, Top10[i-1].Name, Top10[i-1].Score); DrawText(LeaderboardText, 600, 50*i, 48, WHITE); } -- cgit 1.4.1-2-gfad0 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(-) (limited to 'src') 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