about summary refs log tree commit diff stats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c49
1 files changed, 24 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index fcdd3b3..aa47996 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,16 +1,14 @@
 #include "pong.h"
 #include "sounds.h"
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_audio.h>
-#include <SDL2/SDL_mixer.h>
+
 
 
 int Difficulty = 1;
-atomic_int Ticks = 0;
+SDL_atomic_t Ticks;
 bool GameGoing = true;
 char VersionString[256];
 
-mtx_t AudioQueueBeingModified;
+SDL_mutex *AudioQueueBeingModified;
 
 int AudioQueue[20];
 
@@ -20,15 +18,14 @@ int internal_clock() {
 	};
 	struct timespec Remaining;
 	while(GameGoing == true) {
-		atomic_fetch_add(&Ticks, 1);
-		thrd_sleep(&Delay, &Remaining);
+		SDL_AtomicAdd(&Ticks, 1);
+		nanosleep(&Delay, &Remaining);
 	}
-	thrd_exit(0);
+	return(0);
 }
 
 int audio() {
 	int i;
-	SDL_Init(SDL_INIT_AUDIO);
 	Mix_Init(0);
 	Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 1024);
 	Mix_AllocateChannels(64);
@@ -71,20 +68,19 @@ int audio() {
 					default:
 						break;
 				}
-				while(mtx_trylock(&AudioQueueBeingModified) == thrd_busy) thrd_sleep(&Delay, &Remaining);
+				SDL_LockMutex(AudioQueueBeingModified);
 				AudioQueue[i] = -1;
-				mtx_unlock(&AudioQueueBeingModified);
+				SDL_UnlockMutex(AudioQueueBeingModified);
 			}
 		}
-		thrd_sleep(&Delay, &Remaining);
+		nanosleep(&Delay, &Remaining);
 	}
-	SDL_Quit();
-	thrd_exit(0);
+	return(0);
 }
 
 bool play_audio(int SoundEffect) {
 	unsigned int i;
-	while(mtx_trylock(&AudioQueueBeingModified) == thrd_busy);
+	SDL_LockMutex(AudioQueueBeingModified);
 	for(i = 1; i != 20; i++) {
 		if (AudioQueue[i-1] == -1) {
 			AudioQueue[i-1] = AudioQueue[i];
@@ -92,12 +88,12 @@ bool play_audio(int SoundEffect) {
 	}
 	for(i = 0; AudioQueue[i] != -1; i++) {
 		if(i > sizeof(AudioQueue)) {
-			mtx_unlock(&AudioQueueBeingModified);
+			SDL_UnlockMutex(AudioQueueBeingModified);
 			return false;
 		}
 	}
 	AudioQueue[i] = SoundEffect;
-	mtx_unlock(&AudioQueueBeingModified);
+	SDL_UnlockMutex(AudioQueueBeingModified);
 	return true;
 }
 
@@ -112,6 +108,9 @@ int main() {
 	SetWindowState(FLAG_WINDOW_RESIZABLE);
 	SetWindowMinSize(1280, 720);
 
+	//SDL Init
+	SDL_Init(SDL_INIT_AUDIO);
+
 	//Init Variables
 	strncpy(VersionString, "Version 0.2 - AEOLUS", sizeof(VersionString));
 	
@@ -119,13 +118,12 @@ int main() {
 	for(unsigned int i = 0; i < 20; i++) {
 		AudioQueue[i] = -1;
 	}
-	mtx_init(&AudioQueueBeingModified, mtx_plain);
+	AudioQueueBeingModified = SDL_CreateMutex();
 
-	// Initialize Internal Clock
-	thrd_t InternalClock;
-	thrd_create(&InternalClock, internal_clock, NULL);
-	thrd_t AudioThread;
-	thrd_create(&AudioThread, audio, NULL);
+	// Init Threads
+	SDL_AtomicSet(&Ticks, 0);
+	SDL_Thread *InternalClock = SDL_CreateThread(internal_clock, "Internal Clock", NULL);
+	SDL_Thread *AudioThread = SDL_CreateThread(audio, "Audio", NULL);
 	
 	// Launch Game
 	while (GameGoing == true) {
@@ -144,8 +142,9 @@ int main() {
 	}
 	
 	GameGoing = false; // Make sure the game is going to end.
-	thrd_join(AudioThread, NULL);
-	thrd_join(InternalClock, NULL);
+	SDL_WaitThread(InternalClock, NULL);
+	SDL_WaitThread(AudioThread, NULL);
+	SDL_Quit();
 	CloseWindow();
 	return(0);
 }