about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-06-08 07:21:16 -0400
committerCharadon <dev@iotib.net>2022-06-08 07:21:16 -0400
commitf0592f80f01b52652dfda9dec8096af4767e39d7 (patch)
treed57c2a9ed5a66d2ea7ad16ec07f19040b0f50ee0
parent9cb1d207cbc896494b60068aa61598502675e4b7 (diff)
downloadPong-C-f0592f80f01b52652dfda9dec8096af4767e39d7.tar.gz
Fixed audio initializing wait not working
-rw-r--r--src/main.c26
-rw-r--r--src/pong.h2
-rw-r--r--src/title.c58
3 files changed, 72 insertions, 14 deletions
diff --git a/src/main.c b/src/main.c
index 8c907d0..7d760b6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,7 +8,7 @@ char VersionString[256];
 struct Settings GlobalSettings;
 
 SDL_mutex *AudioQueueBeingModified;
-SDL_mutex *AudioInitializing;
+SDL_atomic_t AudioInitializing;
 
 int AudioQueue[20];
 
@@ -25,7 +25,6 @@ int internal_clock() {
 }
 
 int audio() {
-	SDL_TryLockMutex(AudioInitializing);
 	int i;
 	Mix_Init(0);
 	Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 1024);
@@ -40,7 +39,7 @@ int audio() {
 		0, 20000000
 	};
 	struct timespec Remaining;
-	SDL_UnlockMutex(AudioInitializing);
+	SDL_AtomicSet(&AudioInitializing, 1);
 	while(GameGoing == true) {
 		for(i = 0; i < 20; i++) {
 			if(AudioQueue[i] != -1){
@@ -114,7 +113,7 @@ int main(int argc, char *argv[]) {
 	SDL_Init(SDL_INIT_AUDIO);
 
 	//Init Variables
-	strncpy(VersionString, "Version 0.2 - AEOLUS", sizeof(VersionString));
+	strncpy(VersionString, "Version 0.3 - Charon", sizeof(VersionString));
 	
 	//Populate Audio Queue
 	for(unsigned int i = 0; i < 20; i++) {
@@ -123,17 +122,18 @@ int main(int argc, char *argv[]) {
 
 	//Threading
 	AudioQueueBeingModified = SDL_CreateMutex();
-	AudioInitializing = SDL_CreateMutex();
 	SDL_AtomicSet(&Ticks, 0);
 	SDL_Thread *InternalClock = SDL_CreateThread(internal_clock, "Internal Clock", NULL);
 	SDL_Thread *AudioThread = SDL_CreateThread(audio, "Audio", NULL);
+	SDL_AtomicSet(&AudioInitializing, 0);
 
 	//Load Settings
-	char *SettingsDirectory = SDL_GetPrefPath("iotib.net", "Pong");
+	char *SettingsDirectory = SDL_GetPrefPath("iotib", "Pong");
 	char SettingsFilePath[8192];
 	snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory);
 	FILE *SettingsFile;
 	reopen:
+	//Create settings file if it doesn't exist.
 	if ((SettingsFile = fopen(SettingsFilePath, "r")) == NULL) {
 		if(SettingsFile != NULL) {
 			fclose(SettingsFile);
@@ -146,9 +146,10 @@ int main(int argc, char *argv[]) {
 		fprintf(SettingsFile, "music_volume 100\n");
 		fprintf(SettingsFile, "fullscreen 0");
 		fclose(SettingsFile);
-		goto reopen;
+		goto reopen; //Try opening again.
 	}
 	
+	// Parse the settings file.
 	char Option[2048];
 	int Value;
 	while(!feof(SettingsFile)) {
@@ -161,10 +162,17 @@ int main(int argc, char *argv[]) {
 			GlobalSettings.Fullscreen = Value;
 		}
 	}
+	fclose(SettingsFile);
 
 	// Wait for audio to finish initializing.
-	SDL_LockMutex(AudioInitializing);
-	
+	struct timespec a = {
+		0, 5000000
+	};
+	struct timespec b;
+	while(SDL_AtomicGet(&AudioInitializing) == 0) {
+		nanosleep(&a, &b); //Prevent heavy cpu usage
+	}
+
 	// Launch Game
 	while (GameGoing == true) {
 		switch(title_screen()) {
diff --git a/src/pong.h b/src/pong.h
index a9adf41..64e3f26 100644
--- a/src/pong.h
+++ b/src/pong.h
@@ -51,7 +51,7 @@ extern int Difficulty;
 extern bool GameGoing;
 extern SDL_atomic_t Ticks;
 extern char VersionString[256];
-extern SDL_mutex *AudioInitializing;
+extern SDL_atomic_t AudioInitializing;
 
 void enemy(struct Players *Enemy, struct Balls ball);
 void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerScore, int *EnemyScore);
diff --git a/src/title.c b/src/title.c
index 2f8148d..6066797 100644
--- a/src/title.c
+++ b/src/title.c
@@ -1,9 +1,56 @@
 #include "pong.h"
-#include "raylib.h"
 
+static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) {
+    bool SettingsGoing = true;
+    int MusicBarY = 50;
 
+    Rectangle MouseCursor = {
+        0,0,1,1
+    };
 
-void help_text() {
+    Rectangle MusicBar[10] = {
+        {50,MusicBarY,50,50},
+        {105,MusicBarY,50,50},
+        {160,MusicBarY,50,50},
+        {215,MusicBarY,50,50},
+        {270,MusicBarY,50,50},
+        {325,MusicBarY,50,50},
+        {380,MusicBarY,50,50},
+        {435,MusicBarY,50,50},
+        {490,MusicBarY,50,50},
+        {545,MusicBarY,50,50},
+    };
+    while(SettingsGoing == true && GameGoing == true) {
+        MouseCursor.x = GetMouseX();
+        MouseCursor.y = GetMouseY();
+        Mix_VolumeMusic(GlobalSettings.MusicVolume);
+        BeginDrawing();
+        ClearBackground(BLACK);
+        BeginMode2D(*MainCamera);
+
+            // Music
+            DrawText("Music Volume:", 50, 10, 42, WHITE);
+            DrawText("<", 0,0,128,WHITE);
+            for(int i = 0; i < 10; i++) {
+                if (CheckCollisionRecs(MouseCursor, MusicBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
+                    GlobalSettings.MusicVolume = i*10;
+                }
+                if(i <= (GlobalSettings.MusicVolume/10)) {
+                    DrawRectangleRec(MusicBar[i], RED);
+                }
+            }
+
+            // Sound
+
+            // Fullscreen
+        EndMode2D();
+        EndDrawing();
+    }
+    // Put save to file here.
+    return;
+}
+
+static void help_text() {
 
 }
 
@@ -42,7 +89,7 @@ int title_screen() {
 
     //Music
     Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav");
-    Mix_PlayMusic(TitleMusic, 1);
+    Mix_PlayMusic(TitleMusic, -1);
     Mix_VolumeMusic(GlobalSettings.MusicVolume);
 
     while(TitleScreenGoing == true && GameGoing == true) {
@@ -69,13 +116,16 @@ int title_screen() {
             }
         } else if (CheckCollisionRecs(Mouse, Settings)) {
             Selected = &Settings;
+            if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
+                settings(&MainCamera, TitleMusic);
+            }
         } else if (CheckCollisionRecs(Mouse, Help)) {
             Selected = &Help;
         } else if (CheckCollisionRecs(Mouse, Exit)) {
             Selected = &Exit;
             if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                 GameGoing = false;
-                return -1;
+                Choice = -1;
             }
         }
         BeginDrawing();