about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-06-11 20:50:11 -0400
committerCharadon <dev@iotib.net>2022-06-11 20:50:11 -0400
commit9aeea3ebd946f8803fe880c1c6fc98483a002444 (patch)
tree00f490b0ea9e79005f6e73d2ffc107c0190aa39b
parentd3bd312749481a660556de4c14b47ffaa26c8374 (diff)
downloadPong-C-9aeea3ebd946f8803fe880c1c6fc98483a002444.tar.gz
Switching to software mouse rather than use system mouse because it's a pain in the ass
-rw-r--r--resources/cursor.pngbin0 -> 172 bytes
-rw-r--r--src/marathon.c2
-rw-r--r--src/title.c81
3 files changed, 64 insertions, 19 deletions
diff --git a/resources/cursor.png b/resources/cursor.png
new file mode 100644
index 0000000..292608e
--- /dev/null
+++ b/resources/cursor.png
Binary files differdiff --git a/src/marathon.c b/src/marathon.c
index 6e871fa..135c533 100644
--- a/src/marathon.c
+++ b/src/marathon.c
@@ -145,7 +145,7 @@ void marathon_main() {
 	}
 
         // Collision
-        ball(&Player.HitBox, NULL, &Ball, &Player.Score, NULL);
+       	ball(&Player.HitBox, NULL, &Ball, &Player.Score, NULL);
         //Updates hitbox with player's position.
 	Player.HitBox.y = Player.Y;
 
diff --git a/src/title.c b/src/title.c
index 71bf04a..73101dd 100644
--- a/src/title.c
+++ b/src/title.c
@@ -1,4 +1,5 @@
 #include "pong.h"
+#include <raylib.h>
 
 struct LeaderboardEntries {
     int Score;
@@ -28,35 +29,38 @@ static int compare_int(const void *Score1, const void *Score2) {
 }
 
 static void 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;
+    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.
     }
+    
+    // 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...
         TmpStore[size] = malloc(sizeof(struct LeaderboardEntries)); 
-    
-    if(*TmpStore == NULL) {
+    if(*TmpStore == NULL) { //Check if memory got assigned correctly.
         exit(1);
     }
+
     // Load Scores and Names
     int size = 0;
-    for (int a = 0; !feof(LeaderboardFile); a++, size++) {
+    for (int a = 0; !feof(LeaderboardFile); a++, size++) { //Load data from file into array.
         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++) {
+    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;
     }
+
+    // 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...
-    
     // Close leaderboard file.
     if (LeaderboardFile != NULL) {
 	fclose(LeaderboardFile);
@@ -69,9 +73,13 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) {
     int MusicBarY = 50;
     int SoundBarY = 150;
 
+    // Mouse
     Rectangle MouseCursor = {
         0,0,1,1
     };
+    Vector2 OldPosition = GetMousePosition();
+    Vector2 NewPosition = GetMousePosition();
+    Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png");
 
     Rectangle MusicBar[10] = {
         {50,MusicBarY,50,50},
@@ -114,8 +122,20 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) {
     SetMousePosition(GetScreenWidth()/2, GetScreenHeight()/2);
     while(SettingsGoing == true && GameGoing == true) {
         MainCamera->zoom = GetScreenHeight()/720.0f;
-        MouseCursor.x = GetMouseX()/MainCamera->zoom;
-        MouseCursor.y = GetMouseY()/MainCamera->zoom;
+	
+	//Mouse
+	DisableCursor();
+	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;
+	}
+
         Mix_VolumeMusic(GlobalSettings.MusicVolume);
         BeginDrawing();
         ClearBackground(BLACK);
@@ -171,13 +191,14 @@ 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);
         EndMode2D();
         EndDrawing();
     }
     char *SettingsDirectory = SDL_GetPrefPath("iotib", "Pong");
-	char SettingsFilePath[8192];
-	snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory);
-	FILE *SettingsFile;
+    char SettingsFilePath[8192];
+    snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory);
+    FILE *SettingsFile;
     // Put save to file here.
     if ((SettingsFile = fopen(SettingsFilePath, "w")) == NULL) {
         fprintf(stderr, "Unable to create settings file.\n");
@@ -224,7 +245,7 @@ int title_screen() {
         20, 350, 230, 48
     };
     Rectangle Mouse = {
-        0, 0, 10, 10
+        1280.0f/2, 720.0f/2, 10, 10
     };
     Rectangle *Selected;
     Selected = &Versus;
@@ -235,16 +256,37 @@ int title_screen() {
     Mix_PlayMusic(TitleMusic, -1);
     Mix_VolumeMusic(GlobalSettings.MusicVolume);
 
+    // Mouse
+    Vector2 OldPosition = GetMousePosition();
+    Vector2 NewPosition = GetMousePosition();
+    Texture2D MouseCursor = LoadTexture("resources/cursor.png");
+
     while(TitleScreenGoing == true && GameGoing == true) {
         
         if (WindowShouldClose()) { //Quit Game if the window is closed.
             GameGoing = false;
             TitleScreenGoing = false;
         }
-
+	
+	// Update Camera
         MainCamera.zoom = GetScreenHeight()/720.0f;
-        Mouse.x = GetMouseX()/MainCamera.zoom;
-        Mouse.y = GetMouseY()/MainCamera.zoom;
+	MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f};
+	MainCamera.target = (Vector2){1280/2.0f, 720/2.0f};
+
+	//Mouse
+	DisableCursor();
+	OldPosition = NewPosition;
+	NewPosition = GetMousePosition();
+	Mouse.y -= OldPosition.y-NewPosition.y;
+	Mouse.x -= OldPosition.x-NewPosition.x;
+	if (Mouse.y >= 720 || Mouse.y <= 0) {
+		Mouse.y += OldPosition.y-NewPosition.y;
+	}
+	if (Mouse.x >= 1280 || Mouse.x <= 0) {
+		Mouse.x += OldPosition.x-NewPosition.x;
+	}
+	printf("%f,%f\n", Mouse.x, Mouse.y);
+
         if (CheckCollisionRecs(Mouse, Versus)) {
             Selected = &Versus;
             Choice = 0;
@@ -275,6 +317,8 @@ int title_screen() {
         BeginDrawing();
             ClearBackground(BLACK);
             BeginMode2D(MainCamera);
+		DrawRectangleRec(Mouse, YELLOW);
+		DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255});
                 DrawRectangleRec(*Selected, RED);
                 DrawText("PONG", 0, 0, 128, WHITE);
                 DrawText("Versus", 20, 150, 48, WHITE);
@@ -296,6 +340,7 @@ int title_screen() {
                     }
                 }
                 skip:
+	    	DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE);
             EndMode2D();
             DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN);
         EndDrawing();
the previous revision' href='/akkartik/mu/blame/045closure_name.cc?h=main&id=94fa5c95ad9c8beead183bb7c4b88c7c2c7ca6ec'>^
ac0e9db5 ^
363be37f ^


0487a30e ^
82ac0b7e ^

0487a30e ^
82ac0b7e ^

ac0e9db5 ^
0487a30e ^
82ac0b7e ^
363be37f ^
82ac0b7e ^



363be37f ^
82ac0b7e ^



82ac0b7e ^


363be37f ^

82ac0b7e ^






ac0e9db5 ^
0f125d5f ^
82ac0b7e ^
363be37f ^


82ac0b7e ^




363be37f ^
82ac0b7e ^


ac0e9db5 ^
0487a30e ^
82ac0b7e ^
ac0e9db5 ^
82ac0b7e ^








363be37f ^
82ac0b7e ^









ac0e9db5 ^

82ac0b7e ^

ac0e9db5 ^
0487a30e ^
82ac0b7e ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133