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.c38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/main.c b/src/main.c
index 4e5d412..d15877e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,11 @@
 #include "pong.h"
+#include <SDL2/SDL_atomic.h>
+#include <SDL2/SDL_thread.h>
+#include <SDL2/SDL_timer.h>
+#include <raylib.h>
 
 int Difficulty = 1;
+SDL_atomic_t Ticks;
 bool GameGoing = true;
 char VersionString[256];
 
@@ -19,12 +24,11 @@ void set_screen_mode() {
 	switch(GlobalSettings.Fullscreen) {
 		case 1: //Real Fullscreen is fickle as fuck. So it needs a timeout.
 			SetWindowSize(GetMonitorWidth(GetCurrentMonitor()), GetMonitorHeight(GetCurrentMonitor()));
-			int Timeout = Ticks+300; //Set the timeout for 5 seconds.
+			int Timeout = SDL_AtomicGet(&Ticks)+300; //Set the timeout for 5 seconds.
 			while(GetScreenHeight() != GetMonitorHeight(GetCurrentMonitor())) {
-				internal_clock();
 				BeginDrawing();
 				EndDrawing();
-				if(Ticks >= Timeout) { //Default to windowed if fullscreen fails for whatever reason.
+				if(SDL_AtomicGet(&Ticks) >= Timeout) { //Default to windowed if fullscreen fails for whatever reason.
 					goto FullScreenFailed;
 				}
 			}
@@ -47,19 +51,20 @@ void set_screen_mode() {
 	return;
 }
 
-double NewTime = 0;
-double OldTime = 0;
-double Milliseconds = (1000.0/60)/1000.0;
-double DeltaTime = 0;
-int Ticks = 0;
-
-int internal_clock() {
-	NewTime = SDL_GetTicks()/1000.0f;
-	DeltaTime += (NewTime-OldTime)/Milliseconds;
-	OldTime = NewTime;
-	while(DeltaTime >= 1.0) {
-		Ticks++;
-		DeltaTime--;
+static int internal_clock() {
+	SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
+	double NewTime = 0;
+	double OldTime = 0;
+	double Milliseconds = (1000.0/60)/1000.0;
+	double DeltaTime = 0;
+	while(GameGoing == true) {
+		NewTime = SDL_GetTicks()/1000.0f;
+		DeltaTime += (NewTime-OldTime)/Milliseconds;
+		OldTime = NewTime;
+		while(DeltaTime >= 1.0) {
+			SDL_AtomicAdd(&Ticks, 1);
+			DeltaTime--;
+		}
 	}
 	return(0);
 }
@@ -167,6 +172,7 @@ int main(int argc, char *argv[]) {
 
 	//Threading
 	AudioQueueBeingModified = 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);