diff options
author | Charadon <dev@iotib.net> | 2022-06-07 17:49:55 -0400 |
---|---|---|
committer | Charadon <dev@iotib.net> | 2022-06-07 17:49:55 -0400 |
commit | b8dda4d4a5385772c3ee997ae3948d52026a06e2 (patch) | |
tree | 9535de8a53084a45ea1bd209ed435a5217966920 | |
parent | 528f41896e4d5a0a52c080302b75c143bb2c9b05 (diff) | |
download | Pong-C-b8dda4d4a5385772c3ee997ae3948d52026a06e2.tar.gz |
Moved to SDL threads
-rw-r--r-- | src/enemy.c | 3 | ||||
-rw-r--r-- | src/main.c | 49 | ||||
-rw-r--r-- | src/marathon.c | 2 | ||||
-rw-r--r-- | src/pong.h | 11 | ||||
-rw-r--r-- | src/versus.c | 3 |
5 files changed, 33 insertions, 35 deletions
diff --git a/src/enemy.c b/src/enemy.c index 5350770..5a7e99b 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -1,8 +1,7 @@ -#include "raylib.h" #include "pong.h" void enemy(struct Players *Enemy, struct Balls ball) { - if (atomic_load(&Ticks) % 1 == 0) { + if (SDL_AtomicGet(&Ticks) % 1 == 0) { if (!CheckCollisionRecs(ball.HitBox, Enemy->BallDetector)) { if (Enemy->Y+120 > ball.Y) { Enemy->Direction = 0; 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); } diff --git a/src/marathon.c b/src/marathon.c index 02c5b3f..b5de37e 100644 --- a/src/marathon.c +++ b/src/marathon.c @@ -1,6 +1,4 @@ #include "pong.h" -#include "raylib.h" -#include <SDL2/SDL_mixer.h> void marathon_main() { diff --git a/src/pong.h b/src/pong.h index 76fa05d..15ba223 100644 --- a/src/pong.h +++ b/src/pong.h @@ -1,9 +1,14 @@ #ifndef PONG_H #define PONG_H +#include <SDL2/SDL_atomic.h> #define MOUSE_LEFT_BUTTON MOUSE_BUTTON_LEFT #include "raylib.h" -#include "SDL2/SDL.h" -#include "SDL2/SDL_mixer.h" +#include <SDL2/SDL.h> +#include <SDL2/SDL_atomic.h> +#include <SDL2/SDL_audio.h> +#include <SDL2/SDL_mixer.h> +#include <SDL2/SDL_mutex.h> +#include <SDL2/SDL_thread.h> #include "sounds.h" #include <stdatomic.h> #include <stdio.h> @@ -37,7 +42,7 @@ struct Balls { extern int Difficulty; extern bool GameGoing; -extern atomic_int Ticks; +extern SDL_atomic_t Ticks; extern char VersionString[256]; void enemy(struct Players *Enemy, struct Balls ball); diff --git a/src/versus.c b/src/versus.c index 233b238..eb5ebf7 100644 --- a/src/versus.c +++ b/src/versus.c @@ -1,7 +1,4 @@ #include "pong.h" -#include "raylib.h" -#include "sounds.h" -#include <SDL2/SDL_mixer.h> void versus_main() { // Init Player Variables |