From 520a610a8db7c07423fec086806b48ff2890c9c0 Mon Sep 17 00:00:00 2001 From: Charadon Date: Mon, 13 Jun 2022 19:28:37 -0400 Subject: Revert "Revert "Revert "Revert "Revert "Added liberapay link, and more fruitless work on the internal_clock""""" This reverts commit 0e1a3565cd077a297c07bad7466f317025758b31. --- README.txt | 1 - src/ball.c | 4 ++-- src/enemy.c | 4 ++-- src/main.c | 38 ++++++++++++++++++++++---------------- src/marathon.c | 3 +-- src/pong.h | 10 +--------- src/title.c | 4 ++-- src/versus.c | 8 ++++---- 8 files changed, 34 insertions(+), 38 deletions(-) diff --git a/README.txt b/README.txt index b2125bd..493360b 100644 --- a/README.txt +++ b/README.txt @@ -52,4 +52,3 @@ Build: 1. Install homebrew (https://brew.sh/) 2. Follow the instructions from the unix section. =============================================================================== -If you like my work and want to support me, consider donating to me on https://liberapay.com/Charadon/ diff --git a/src/ball.c b/src/ball.c index 20d3c09..af210bb 100644 --- a/src/ball.c +++ b/src/ball.c @@ -8,7 +8,7 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc } // Moves - int CurrentTick = Ticks; + int CurrentTick = SDL_AtomicGet(&Ticks); if(Ball->NextTick <= CurrentTick){ int RunThisManyTimes = 0; if (CurrentTick > Ball->NextTick) { @@ -61,7 +61,7 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc play_audio(SOUND_BOUNCE); } } - Ball->NextTick = Ticks+1; + Ball->NextTick = SDL_AtomicGet(&Ticks)+1; // printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick); } // printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick); diff --git a/src/enemy.c b/src/enemy.c index 6c24c8d..56c7d87 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -2,7 +2,7 @@ #include void enemy(struct Players *Enemy, struct Balls ball) { - int CurrentTick = Ticks; + int CurrentTick = SDL_AtomicGet(&Ticks); int RunThisManyTimes = 0; if (Enemy->NextTick <= CurrentTick) { if(CurrentTick > Enemy->NextTick) { @@ -43,7 +43,7 @@ void enemy(struct Players *Enemy, struct Balls ball) { Enemy->HitBox.y = Enemy->Y; Enemy->BallDetector.y = Enemy->Y+80; } - Enemy->NextTick = Ticks+1; + Enemy->NextTick = SDL_AtomicGet(&Ticks)+1; } return; } 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 +#include +#include +#include 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); diff --git a/src/marathon.c b/src/marathon.c index 7d38242..a04519a 100644 --- a/src/marathon.c +++ b/src/marathon.c @@ -85,7 +85,7 @@ void marathon_main() { Ball.Direction = LEFT; Ball.Speed = 3.0f; Ball.Angle = 0.0f; - Ball.NextTick = Ticks+1; + Ball.NextTick = SDL_AtomicGet(&Ticks)+1; // Init Player struct Players Player; @@ -110,7 +110,6 @@ void marathon_main() { MainCamera.rotation = 0; bool MarathonGoing = true; while(MarathonGoing == true && GameGoing == true) { - internal_clock(); if (WindowShouldClose()) { //Quit Game if the window is closed. GameGoing = false; } diff --git a/src/pong.h b/src/pong.h index b847a8a..eef1150 100644 --- a/src/pong.h +++ b/src/pong.h @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -51,16 +50,10 @@ extern struct Settings GlobalSettings; extern int Difficulty; extern bool GameGoing; +extern SDL_atomic_t Ticks; extern char VersionString[256]; extern SDL_atomic_t AudioInitializing; -//Clock -extern double NewTime; -extern double OldTime; -extern double Milliseconds; -extern double DeltaTime; -extern int Ticks; - void enemy(struct Players *Enemy, struct Balls ball); void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerScore, int *EnemyScore); bool play_audio(int SoundEffect); @@ -68,6 +61,5 @@ int title_screen(); void versus_main(); void marathon_main(); void set_screen_mode(); -int internal_clock(); #endif diff --git a/src/title.c b/src/title.c index 7a58fb9..c5a15ce 100644 --- a/src/title.c +++ b/src/title.c @@ -396,8 +396,8 @@ int title_screen() { DisableCursor(); while(TitleScreenGoing == true && GameGoing == true) { - internal_clock(); - printf("%lf, %d\n", GetTime(), Ticks); + + printf("%lf, %d\n", GetTime(), SDL_AtomicGet(&Ticks)); if (WindowShouldClose()) { //Quit Game if the window is closed. GameGoing = false; TitleScreenGoing = false; diff --git a/src/versus.c b/src/versus.c index 4e13f8a..34914bc 100644 --- a/src/versus.c +++ b/src/versus.c @@ -42,11 +42,11 @@ void versus_main() { MainCamera.offset = (Vector2){0, 0}; MainCamera.rotation = 0; bool VersusGoing = true; - Ball.NextTick = Ticks; - Enemy.NextTick = Ticks; + Ball.NextTick = SDL_AtomicGet(&Ticks); + Enemy.NextTick = SDL_AtomicGet(&Ticks); while(VersusGoing == true && GameGoing == true) { - internal_clock(); - if (WindowShouldClose()) { //Quit Game if the window is closed. + + if (WindowShouldClose()) { //Quit Game if the window is closed. GameGoing = false; } -- cgit 1.4.1-2-gfad0