about summary refs log tree commit diff stats
path: root/src/title.c
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-06-12 03:01:12 -0400
committerCharadon <dev@iotib.net>2022-06-12 03:01:12 -0400
commitbe121c63b03cf4b36793351ae0d733866512bd4c (patch)
treeed2b4a199e47c0635c975012a2d25035b1e2f864 /src/title.c
parent05b4d914d92738a1ade55f24ac900c07f5a9c336 (diff)
downloadPong-C-be121c63b03cf4b36793351ae0d733866512bd4c.tar.gz
More work on score screen
Diffstat (limited to 'src/title.c')
-rw-r--r--src/title.c71
1 files changed, 69 insertions, 2 deletions
diff --git a/src/title.c b/src/title.c
index b2ce12b..01cc508 100644
--- a/src/title.c
+++ b/src/title.c
@@ -68,7 +68,7 @@ static void order_leaderboard() {
     return;
 }
 
-static void score_screen() {
+static void score_screen(Camera2D *MainCamera) {
     // Find leaderboard file, and open it.
     char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong");
     char LeaderboardFilePath[8192];
@@ -81,10 +81,13 @@ static void score_screen() {
     // 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...
-        Scores[size] = malloc(sizeof(struct LeaderboardEntries)); 
+        Scores[size] = malloc(sizeof(struct LeaderboardEntries));
     if(*Scores == NULL) { //Check if memory got assigned correctly.
         exit(1);
     }
+    for (int i = 0; i < (UINT16_MAX*2); i++) {
+        strcpy(Scores[i]->Name, " ");
+    }
 
     // Load Scores and Names
     int size = 0;
@@ -92,6 +95,67 @@ static void score_screen() {
         fscanf(LeaderboardFile, "%s %d\n", Scores[a]->Name, &Scores[a]->Score);
     }
     qsort(Scores, size, sizeof(struct LeaderboardEntries *), compare_int);
+
+    //Begin drawing scores.
+
+    //Mouse
+    bool LookingAtScores = true;
+    bool MouseCursorIn = true;
+    Vector2 OldPosition = GetMousePosition();
+    Vector2 NewPosition = GetMousePosition();
+    Rectangle MouseCursor = {0,0,1,1};
+    Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png");
+
+    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};
+
+        //Mouse
+        if (MouseCursorIn == true) {
+            OldPosition = NewPosition;
+            NewPosition = GetMousePosition();
+            MouseCursor.y -= OldPosition.y-NewPosition.y;
+            MouseCursor.x -= OldPosition.x-NewPosition.x;
+            if (MouseCursor.y >= 720 || MouseCursor.y <= 0) {
+                MouseCursor.y += OldPosition.y-NewPosition.y;
+            }
+            if (MouseCursor.x >= 1280 || MouseCursor.x <= 0) {
+                MouseCursor.x += OldPosition.x-NewPosition.x;
+            }
+        }
+
+        if (IsKeyPressed(KEY_ESCAPE)) {
+            EnableCursor();
+            MouseCursorIn = false;
+        } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
+            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++) {
+                    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);
+                    } else {
+                        EndOfPages = true;
+                    }
+                }
+                DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE);
+            EndMode2D();
+        EndDrawing();
+        if(IsKeyPressed(KEY_Q)) {
+            return;
+        }
+    }
     
 }
 
@@ -395,6 +459,9 @@ int title_screen() {
             EndMode2D();
             DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN);
         EndDrawing();
+        if (IsKeyPressed(KEY_D)) {
+            score_screen(&MainCamera);
+        }
     }
     Mix_HaltMusic();
     Mix_FreeMusic(TitleMusic);