about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/main.c20
-rw-r--r--src/marathon.c1
-rw-r--r--src/pong.h1
-rw-r--r--src/title.c21
-rw-r--r--src/versus.c2
5 files changed, 33 insertions, 12 deletions
diff --git a/src/main.c b/src/main.c
index 00e23de..8c907d0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,6 +8,7 @@ char VersionString[256];
 struct Settings GlobalSettings;
 
 SDL_mutex *AudioQueueBeingModified;
+SDL_mutex *AudioInitializing;
 
 int AudioQueue[20];
 
@@ -24,13 +25,13 @@ int internal_clock() {
 }
 
 int audio() {
+	SDL_TryLockMutex(AudioInitializing);
 	int i;
 	Mix_Init(0);
 	Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 1024);
 	Mix_AllocateChannels(64);
-	Mix_Volume(-1, 128);
 	Mix_Chunk *Bounce = Mix_LoadWAV("resources/bounce.wav");
-	Mix_Chunk *TitleScreen = Mix_LoadWAV("resources/title.wav");
+	//Mix_Chunk *TitleScreen = Mix_LoadWAV("resources/title.wav");
 	Mix_Chunk *Victory = Mix_LoadWAV("resources/victory.wav");
 	Mix_Chunk *Defeat = Mix_LoadWAV("resources/defeat.wav");
 	Mix_Chunk *PlayerScore = Mix_LoadWAV("resources/score_player.wav");
@@ -39,6 +40,7 @@ int audio() {
 		0, 20000000
 	};
 	struct timespec Remaining;
+	SDL_UnlockMutex(AudioInitializing);
 	while(GameGoing == true) {
 		for(i = 0; i < 20; i++) {
 			if(AudioQueue[i] != -1){
@@ -53,9 +55,9 @@ int audio() {
 					case 2: //Play win
 						Mix_PlayChannel(-1, Victory, 0);
 						break;
-					case 3: //Title Screen
-						Mix_PlayChannel(-1, TitleScreen, 0);
-						break;
+					//case 3: //Title Screen
+					//	Mix_PlayChannel(-1, TitleScreen, 0);
+					//	break;
 					case 4: //Player Score
 						Mix_PlayChannel(-1, PlayerScore, 0);
 						break;
@@ -118,9 +120,10 @@ int main(int argc, char *argv[]) {
 	for(unsigned int i = 0; i < 20; i++) {
 		AudioQueue[i] = -1;
 	}
-	AudioQueueBeingModified = SDL_CreateMutex();
 
-	// Init Threads
+	//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);
@@ -159,6 +162,9 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
+	// Wait for audio to finish initializing.
+	SDL_LockMutex(AudioInitializing);
+	
 	// Launch Game
 	while (GameGoing == true) {
 		switch(title_screen()) {
diff --git a/src/marathon.c b/src/marathon.c
index b5de37e..209fb52 100644
--- a/src/marathon.c
+++ b/src/marathon.c
@@ -5,6 +5,7 @@ void marathon_main() {
     // Init Music
     Mix_Music *Background = Mix_LoadMUS("resources/marathon.wav");
     Mix_PlayMusic(Background, -1);
+    Mix_VolumeMusic(GlobalSettings.MusicVolume);
 
     // Init balls lmao
     struct Balls Ball;
diff --git a/src/pong.h b/src/pong.h
index 74a208a..a9adf41 100644
--- a/src/pong.h
+++ b/src/pong.h
@@ -51,6 +51,7 @@ extern int Difficulty;
 extern bool GameGoing;
 extern SDL_atomic_t Ticks;
 extern char VersionString[256];
+extern SDL_mutex *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 3e85a96..2f8148d 100644
--- a/src/title.c
+++ b/src/title.c
@@ -13,8 +13,11 @@ int title_screen() {
     MainCamera.offset = (Vector2){0,0};
     MainCamera.target = (Vector2){0,0};
     MainCamera.rotation = 0.0f;
+
     bool TitleScreenGoing = true;
-    play_audio(MUSIC_TITLE);
+    int Choice = 0;
+
+    // Selection
     Rectangle Versus = {
         20, 150, 230, 48
     };
@@ -36,6 +39,12 @@ int title_screen() {
     Rectangle *Selected;
     Selected = &Versus;
     EnableCursor();
+
+    //Music
+    Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav");
+    Mix_PlayMusic(TitleMusic, 1);
+    Mix_VolumeMusic(GlobalSettings.MusicVolume);
+
     while(TitleScreenGoing == true && GameGoing == true) {
         
         if (WindowShouldClose()) { //Quit Game if the window is closed.
@@ -49,12 +58,14 @@ int title_screen() {
         if (CheckCollisionRecs(Mouse, Versus)) {
             Selected = &Versus;
             if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
-                return(0); 
+                Choice = 0;
+                TitleScreenGoing = false;
             }
         } else if (CheckCollisionRecs(Mouse, Marathon)) {
             Selected = &Marathon;
             if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
-                return(1);
+                Choice = 1;
+                TitleScreenGoing = false;
             }
         } else if (CheckCollisionRecs(Mouse, Settings)) {
             Selected = &Settings;
@@ -81,5 +92,7 @@ int title_screen() {
             DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN);
         EndDrawing();
     }
-    return -1;
+    Mix_HaltMusic();
+    Mix_FreeMusic(TitleMusic);
+    return Choice;
 }
diff --git a/src/versus.c b/src/versus.c
index eb5ebf7..71e3689 100644
--- a/src/versus.c
+++ b/src/versus.c
@@ -23,7 +23,7 @@ void versus_main() {
 	// Init music
 	Mix_Music *Background = Mix_LoadMUS("resources/versus.wav");
 	Mix_PlayMusic(Background, -1);
-	Mix_VolumeMusic(MIX_MAX_VOLUME);
+	Mix_VolumeMusic(GlobalSettings.MusicVolume);
     
     // Set Sprites
 	Texture2D PaddleSprite = LoadTexture("resources/paddle.png");