about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-06-09 20:21:26 -0400
committerCharadon <dev@iotib.net>2022-06-09 20:21:26 -0400
commitea25821768c7ac5dc7f7c0617689f6a67a88bb2a (patch)
treeafb98f6c7f5585b143c8f9c9853c37518550ff95 /src
parent2d032601da11595cbb2c1fab34371d10cd184eb6 (diff)
downloadPong-C-ea25821768c7ac5dc7f7c0617689f6a67a88bb2a.tar.gz
Begun work on organizing leaderboard
Diffstat (limited to 'src')
-rw-r--r--src/marathon.c79
-rw-r--r--src/title.c44
2 files changed, 92 insertions, 31 deletions
diff --git a/src/marathon.c b/src/marathon.c
index 904c645..064f873 100644
--- a/src/marathon.c
+++ b/src/marathon.c
@@ -1,39 +1,60 @@
 #include "pong.h"
-#include <raylib.h>
 
 void leaderboard_record(int Score) {
     bool LeaderBoardGoing = true;
+    
+    // Create Leaderboard struct
+    #define MAX_CHARACTERS 16
     struct {
-        char Name[256];
+        char Name[MAX_CHARACTERS];
         int Score;
     } LeaderboardEntry;
     LeaderboardEntry.Score = Score;
-    strcpy(LeaderboardEntry.Name, "\0");
-    uint16_t CharacterNumber = 0;
+
+    // Init strings
+    int16_t CharacterNumber = 0;
     int Key = 0;
+    for(int i = 0; i < sizeof(LeaderboardEntry.Name); i++) {
+        LeaderboardEntry.Name[i] = '\0';
+    }
+    char Scored[8192];
+    snprintf(Scored, sizeof(Scored), "You Scored: %d", LeaderboardEntry.Score);
+
+    // Init Camera
+    Camera2D MainCamera;
+        MainCamera.target = (Vector2){0, 0};
+        MainCamera.offset = (Vector2){0, 0};
+        MainCamera.rotation = 0;
     while(LeaderBoardGoing == true) {
+        MainCamera.zoom = GetScreenHeight()/720.0f;
+		MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f};
+		MainCamera.target = (Vector2){1280/2.0f, 720/2.0f};
+
         BeginDrawing();
-        Key = GetKeyPressed();
-        switch(Key) {
-            case KEY_BACKSPACE:
-                if(CharacterNumber != 0) {
-                    LeaderboardEntry.Name[CharacterNumber] = '\0';
-                    CharacterNumber -= 1;
-                }
-                break;
-            case KEY_ENTER:
-                if(CharacterNumber != 0) {
-                    LeaderBoardGoing = false;
-                }
-                break;
-            default:
-                if (Key >= 33 && Key <= 126) {
-                    LeaderboardEntry.Name[CharacterNumber] = '\0'+Key;
-                    CharacterNumber += 1;
-                }
-                break;
-        }
-        printf("%d,%s\n", CharacterNumber, LeaderboardEntry.Name);
+            DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255});
+            Key = GetKeyPressed();
+            switch(Key) {
+                case KEY_BACKSPACE:
+                    if(CharacterNumber != 0) {
+                        LeaderboardEntry.Name[CharacterNumber] = '\0';
+                        CharacterNumber -= 1;
+                    }
+                    break;
+                case KEY_ENTER:
+                    if(CharacterNumber != 0) {
+                        LeaderBoardGoing = false;
+                    }
+                    break;
+                default:
+                    if (Key >= 33 && Key <= 126 && CharacterNumber < MAX_CHARACTERS) {
+                        LeaderboardEntry.Name[CharacterNumber] = GetCharPressed();
+                        CharacterNumber += 1;
+                    }
+                    break;
+            }
+            DrawText(Scored, 5, 0, 48, BLUE);
+            DrawText("Enter Name:", 5, 50, 48, WHITE);
+            DrawText(LeaderboardEntry.Name, 300, 50, 48, WHITE);
         EndDrawing();
     }
     char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong");
@@ -64,6 +85,7 @@ void marathon_main() {
 	Ball.Direction = LEFT;
 	Ball.Speed = 3.0f;
 	Ball.Angle = 0.0f;
+    Ball.NextTick = SDL_AtomicGet(&Ticks)+1;
 
     // Init Player
     struct Players Player;
@@ -108,11 +130,6 @@ void marathon_main() {
 			Player.Y = GetMouseY()-PaddleSprite.height/2.0f;
 			DisableCursor();
 		}
-        
-        // Leave game
-        if(IsKeyPressed(KEY_Q)) {
-            MarathonGoing = false;
-        }
 		
 		if(GetMouseY() < 0) {
 			SetMousePosition(0, 0);
@@ -133,7 +150,7 @@ void marathon_main() {
 		Player.HitBox.y = Player.Y;
 
         // End Game
-        if(Ball.X < 0) {
+        if(Ball.X < 0 || IsKeyPressed(KEY_Q)) {
             leaderboard_record(Player.Score);
             MarathonGoing = false;
         }
diff --git a/src/title.c b/src/title.c
index 04cf0ca..501b3ad 100644
--- a/src/title.c
+++ b/src/title.c
@@ -1,5 +1,46 @@
 #include "pong.h"
 
+static struct {
+    int Score;
+    char *Name;
+}Leaderboard[10];
+
+static void order_leaderboard() {
+    int *Score;
+    bool Parsing = true;
+
+    //Load leaderboard file.
+    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;
+    }
+    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]);
+    }
+    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;
+}
+
 static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) {
     bool SettingsGoing = true;
     int MusicBarY = 50;
@@ -137,6 +178,9 @@ int title_screen() {
     MainCamera.target = (Vector2){0,0};
     MainCamera.rotation = 0.0f;
 
+    // Load leaderboard
+    //order_leaderboard();
+
     bool TitleScreenGoing = true;
     int Choice = 0;