diff options
author | Charadon <dev@iotib.net> | 2022-06-10 23:38:23 -0400 |
---|---|---|
committer | Charadon <dev@iotib.net> | 2022-06-10 23:38:23 -0400 |
commit | a35948788efff45c976e799c5913746d9b55b81a (patch) | |
tree | 3e06764b1052c57cbb8ae2007a7f58efbdd0802f | |
parent | ea25821768c7ac5dc7f7c0617689f6a67a88bb2a (diff) | |
download | Pong-C-a35948788efff45c976e799c5913746d9b55b81a.tar.gz |
Holy shit I finally figured out malloc, holy balls
-rw-r--r-- | src/title.c | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/title.c b/src/title.c index 501b3ad..73efbbe 100644 --- a/src/title.c +++ b/src/title.c @@ -1,43 +1,47 @@ #include "pong.h" -static struct { +struct LeaderboardEntries { int Score; - char *Name; -}Leaderboard[10]; + char Name[16]; +}; -static void order_leaderboard() { - int *Score; - bool Parsing = true; +struct LeaderboardEntries Top10[10]; + +static int compare_int(const void *Score1, const void *Score2) { + const struct LeaderboardEntries *Entry1 = *(struct LeaderboardEntries**) Score1; + const struct LeaderboardEntries *Entry2 = *(struct LeaderboardEntries**) Score2; + if (Entry1->Score > Entry2->Score) { + return -1; + } + return +1; +} - //Load leaderboard file. +static void order_leaderboard() { 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((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { + return; //If it doesn't exist yet, we don't need to care. } - int Count = 0; - for(int i = 0; feof(LeaderboardFile); i++) { - Count++; - Score = malloc(Count*sizeof(int)); - if(Score == NULL) { - fprintf(stderr, "Failed to load data from leaderboards.txt\n"); - free(Score); - return; - } - fscanf(LeaderboardFile, "%d", &Score[i]); + struct LeaderboardEntries *TmpStore[999]; + for (int size = 0; size < 999; size++) + TmpStore[size] = malloc(sizeof(struct LeaderboardEntries)); //If there's 131,000 entries, I dunno what to tell you. You really like pong... + + if(*TmpStore == NULL) { + exit(1); + } + // Load Scores and Names + int size = 0; + for (int a = 0; !feof(LeaderboardFile); a++, size++) { + fscanf(LeaderboardFile, "%s %d\n", TmpStore[a]->Name, &TmpStore[a]->Score); + } + + qsort(TmpStore, size, sizeof(struct LeaderboardEntries *), compare_int); + for(int i = 0; i < 10; i++) { + strcpy(Top10[i].Name, TmpStore[i]->Name); + Top10[i].Score = TmpStore[i]->Score; } - int TopSpot = 0; - //int Result = 0; - //char *ResultName = NULL; - //int OldResults[sizeof(Score];) - //while(Parsing == true) { - for(int a = 0; a <= Count; a++) { - - } - TopSpot += 1; - //} return; } @@ -177,9 +181,9 @@ int title_screen() { MainCamera.offset = (Vector2){0,0}; MainCamera.target = (Vector2){0,0}; MainCamera.rotation = 0.0f; - + struct LeaderboardEntries *Top10[10]; // Load leaderboard - //order_leaderboard(); + order_leaderboard(); bool TitleScreenGoing = true; int Choice = 0; |