From c020a309846d9d20be1badd28e592da161d1761f Mon Sep 17 00:00:00 2001 From: Charadon Date: Thu, 23 Jun 2022 19:08:04 -0400 Subject: Migrated code to ANSI C --- Pong.make | 7 +++- premake5.lua | 1 + src/ball.c | 15 ++++--- src/controls.c | 5 +++ src/enemy.c | 11 ++--- src/main.c | 57 ++++++++++++------------- src/marathon.c | 39 +++++++++--------- src/pause.c | 6 +-- src/title.c | 128 ++++++++++++++++++++++++++++++--------------------------- src/versus.c | 33 ++++++++------- 10 files changed, 159 insertions(+), 143 deletions(-) create mode 100644 src/controls.c diff --git a/Pong.make b/Pong.make index 1257627..50abfbf 100644 --- a/Pong.make +++ b/Pong.make @@ -26,7 +26,7 @@ DEFINES += INCLUDES += FORCE_INCLUDE += ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES) -ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -Og -g -ggdb `pkg-config --cflags raylib` `pkg-config --cflags glfw3` `pkg-config --cflags gl` `pkg-config --cflags sdl2` `pkg-config --cflags SDL2_mixer` `pkg-config --cflags glu` +ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -Og -std=c89 -g -ggdb `pkg-config --cflags raylib` `pkg-config --cflags glfw3` `pkg-config --cflags gl` `pkg-config --cflags sdl2` `pkg-config --cflags SDL2_mixer` `pkg-config --cflags glu` ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -Og -g -ggdb `pkg-config --cflags raylib` `pkg-config --cflags glfw3` `pkg-config --cflags gl` `pkg-config --cflags sdl2` `pkg-config --cflags SDL2_mixer` `pkg-config --cflags glu` ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES) LIBS += @@ -51,6 +51,7 @@ GENERATED := OBJECTS := GENERATED += $(OBJDIR)/ball.o +GENERATED += $(OBJDIR)/controls.o GENERATED += $(OBJDIR)/enemy.o GENERATED += $(OBJDIR)/main.o GENERATED += $(OBJDIR)/marathon.o @@ -58,6 +59,7 @@ GENERATED += $(OBJDIR)/pause.o GENERATED += $(OBJDIR)/title.o GENERATED += $(OBJDIR)/versus.o OBJECTS += $(OBJDIR)/ball.o +OBJECTS += $(OBJDIR)/controls.o OBJECTS += $(OBJDIR)/enemy.o OBJECTS += $(OBJDIR)/main.o OBJECTS += $(OBJDIR)/marathon.o @@ -130,6 +132,9 @@ endif $(OBJDIR)/ball.o: src/ball.c @echo $(notdir $<) $(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" +$(OBJDIR)/controls.o: src/controls.c + @echo $(notdir $<) + $(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/enemy.o: src/enemy.c @echo $(notdir $<) $(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" diff --git a/premake5.lua b/premake5.lua index 7b8f642..6105513 100644 --- a/premake5.lua +++ b/premake5.lua @@ -100,6 +100,7 @@ project("Pong") kind("WindowedApp") optimize("Debug") language("C") + cdialect("C89") files {"src/*.c", "src/*.h", } --libdirs {"/usr/local/lib", "/usr/pkg/lib", "/usr/X11R7/include"} diff --git a/src/ball.c b/src/ball.c index af210bb..0e6b62f 100644 --- a/src/ball.c +++ b/src/ball.c @@ -7,14 +7,15 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc NoEnemy = true; } - // Moves + /* Moves */ int CurrentTick = SDL_AtomicGet(&Ticks); if(Ball->NextTick <= CurrentTick){ int RunThisManyTimes = 0; if (CurrentTick > Ball->NextTick) { RunThisManyTimes = (CurrentTick - Ball->NextTick); } - for (int i = 0; i <= RunThisManyTimes; i++) { + int i = 0; + for (i = 0; i <= RunThisManyTimes; i++) { Ball->Y += Ball->Angle; if (Ball->Direction == LEFT) { Ball->X -= Ball->Speed; @@ -22,11 +23,11 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc Ball->X += Ball->Speed; } - // Moves hitbox with ball. + /* Moves hitbox with ball. */ Ball->HitBox.x = Ball->X; Ball->HitBox.y = Ball->Y; - // Check collisions against players. + /* Check collisions against players. */ if (CheckCollisionRecs(*Player, Ball->HitBox) && Ball->Direction == LEFT) { Ball->Direction = RIGHT; Ball->Speed *= 1.5f; @@ -55,18 +56,16 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc play_audio(SOUND_BOUNCE); } } - // Bounce ball if touches top or bottom of screen. + /* Bounce ball if touches top or bottom of screen. */ if ( (Ball->Y+32 >= 720 && Ball->Angle >=1) || (Ball->Y <= 0 && Ball->Angle <= -1) ) { Ball->Angle *= -1; play_audio(SOUND_BOUNCE); } } Ball->NextTick = SDL_AtomicGet(&Ticks)+1; -// printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick); } -// printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick); - // Calculates score and resets ball. + /* Calculates score and resets ball. */ bool Scored = false; if (Ball->X < 0 && NoEnemy == false) { *EnemyScore += 1; diff --git a/src/controls.c b/src/controls.c new file mode 100644 index 0000000..ac5aa3d --- /dev/null +++ b/src/controls.c @@ -0,0 +1,5 @@ +#include "pong.h" + +int player_controls() { + return(0); +} diff --git a/src/enemy.c b/src/enemy.c index 56c7d87..b46ce15 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -8,8 +8,9 @@ void enemy(struct Players *Enemy, struct Balls ball) { if(CurrentTick > Enemy->NextTick) { RunThisManyTimes = (CurrentTick - Enemy->NextTick); } - for (int i = 0; i <= RunThisManyTimes; i++) { - // Changes direction + int i = 0; + for (i = 0; i <= RunThisManyTimes; i++) { + /* Changes direction */ if (!CheckCollisionRecs(ball.HitBox, Enemy->BallDetector)) { if (Enemy->Y+120 > ball.Y) { Enemy->Direction = 0; @@ -20,7 +21,7 @@ void enemy(struct Players *Enemy, struct Balls ball) { Enemy->Direction = 3; } - // Moves + /* Moves */ switch(Enemy->Direction) { case 0: Enemy->Y -= 15; @@ -32,14 +33,14 @@ void enemy(struct Players *Enemy, struct Balls ball) { break; } - // Prevents from going off screen. + /* Prevents from going off screen. */ if ( Enemy->Y > 480 ) { Enemy->Y = 480; } else if (Enemy->Y < 0) { Enemy->Y = 0; } - // Updates hitbox and detector + /* Updates hitbox and detector */ Enemy->HitBox.y = Enemy->Y; Enemy->BallDetector.y = Enemy->Y+80; } diff --git a/src/main.c b/src/main.c index 4a383f4..54f5b1b 100644 --- a/src/main.c +++ b/src/main.c @@ -18,13 +18,13 @@ void set_screen_mode() { ClearWindowState(FLAG_WINDOW_UNDECORATED); ClearWindowState(FLAG_FULLSCREEN_MODE); switch(GlobalSettings.Fullscreen) { - case 1: //Real Fullscreen is fickle as fuck. So it needs a timeout. + case 1: /* Real Fullscreen is fickle as fuck. So it needs a timeout. */ SetWindowSize(GetMonitorWidth(GetCurrentMonitor()), GetMonitorHeight(GetCurrentMonitor())); - int Timeout = SDL_AtomicGet(&Ticks)+300; //Set the timeout for 5 seconds. + int Timeout = SDL_AtomicGet(&Ticks)+300; /* Set the timeout for 5 seconds. */ while(GetScreenHeight() != GetMonitorHeight(GetCurrentMonitor())) { BeginDrawing(); EndDrawing(); - if(SDL_AtomicGet(&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; } } @@ -58,7 +58,7 @@ static int internal_clock() { 0, 99999 }; while(GameGoing == true) { - nanosleep(&SleepFor, NULL); //Reduces CPU usage + nanosleep(&SleepFor, NULL); /* Reduces CPU usage */ NewTime = GetTime(); DeltaTime += (NewTime - OldTime)/Framerate; OldTime = NewTime; @@ -77,7 +77,6 @@ static int audio() { Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 1024); Mix_AllocateChannels(64); Mix_Chunk *Bounce = Mix_LoadWAV("resources/bounce.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"); @@ -92,25 +91,22 @@ static int audio() { if(AudioQueue[i] != -1){ Mix_Volume(-1, GlobalSettings.SoundVolume); switch(AudioQueue[i]) { - case 0: //Play bounce sound. + case 0: /* Play bounce sound. */ Mix_PlayChannel(-1, Bounce, 0); break; - case 1: //Play game over + case 1: /* Play game over */ Mix_PlayChannel(-1, Defeat, 0); break; - case 2: //Play win + case 2: /* Play win */ Mix_PlayChannel(-1, Victory, 0); break; - //case 3: //Title Screen - // Mix_PlayChannel(-1, TitleScreen, 0); - // break; - case 4: //Player Score + case 4: /* Player Score */ Mix_PlayChannel(-1, PlayerScore, 0); break; - case 5: //Enemy Score + case 5: /* Enemy Score */ Mix_PlayChannel(-1, EnemyScore, 0); break; - case 99: //Stop All Sounds + case 99: /* Stop All Sounds */ Mix_HaltChannel(-1); break; default: @@ -146,7 +142,7 @@ bool play_audio(int SoundEffect) { } int main(int argc, char *argv[]) { - //Raylib Init + /* Raylib Init */ InitWindow(1280, 720, "Pong"); SDL_Init(SDL_INIT_AUDIO); SDL_AtomicSet(&Ticks, 0); @@ -159,31 +155,32 @@ int main(int argc, char *argv[]) { SetWindowState(FLAG_WINDOW_RESIZABLE); SetWindowMinSize(1280, 720); - //Settings declaration to prevent undefined behavior. + /* Settings declaration to prevent undefined behavior. */ GlobalSettings.Fullscreen = 0; GlobalSettings.SoundVolume = 0; GlobalSettings.MusicVolume = 0; - //Init Variables + /* Init Variables */ strncpy(VersionString, "Version 0.4 - APOLLO", sizeof(VersionString)); - //Populate Audio Queue - for(unsigned int i = 0; i < 20; i++) { + /* Populate Audio Queue */ + unsigned int i = 0; + for(i = 0; i < 20; i++) { AudioQueue[i] = -1; } - //Threading + /* Threading */ AudioQueueBeingModified = SDL_CreateMutex(); SDL_Thread *AudioThread = SDL_CreateThread(audio, "Audio", NULL); SDL_AtomicSet(&AudioInitializing, 0); - //Load Settings + /* Load Settings */ char *SettingsDirectory = SDL_GetPrefPath("iotib", "Pong"); char SettingsFilePath[8192]; snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory); FILE *SettingsFile; reopen: - //Create settings file if it doesn't exist. + /* Create settings file if it doesn't exist. */ if ((SettingsFile = fopen(SettingsFilePath, "r")) == NULL) { if(SettingsFile != NULL) { fclose(SettingsFile); @@ -196,10 +193,10 @@ int main(int argc, char *argv[]) { fprintf(SettingsFile, "music_volume 100\n"); fprintf(SettingsFile, "fullscreen 0"); fclose(SettingsFile); - goto reopen; //Try opening again. + goto reopen; /* Try opening again. */ } - // Parse the settings file. + /* Parse the settings file. */ char Option[2048]; int Value; while(!feof(SettingsFile)) { @@ -216,23 +213,23 @@ int main(int argc, char *argv[]) { set_screen_mode(); - // Wait for audio to finish initializing. + /* Wait for audio to finish initializing. */ struct timespec a = { 0, 5000000 }; struct timespec b; while(SDL_AtomicGet(&AudioInitializing) == 0) { - nanosleep(&a, &b); //Prevent heavy cpu usage + nanosleep(&a, &b); /* Prevent heavy cpu usage */ } - // Launch Game + /* Launch Game */ while (GameGoing == true) { switch(title_screen()) { - case 0: //Versus + case 0: /* Versus */ play_audio(STOP_ALL_SOUNDS); versus_main(); break; - case 1: //Marathon + case 1: /* Marathon */ play_audio(STOP_ALL_SOUNDS); marathon_main(); break; @@ -241,7 +238,7 @@ int main(int argc, char *argv[]) { } } - GameGoing = false; // Make sure the game is going to end. + GameGoing = false; /* Make sure the game is going to end. */ SDL_WaitThread(InternalClock, NULL); SDL_WaitThread(AudioThread, NULL); SDL_Quit(); diff --git a/src/marathon.c b/src/marathon.c index 07a9fe3..2140b08 100644 --- a/src/marathon.c +++ b/src/marathon.c @@ -3,7 +3,7 @@ void leaderboard_record(int Score) { bool LeaderBoardGoing = true; - // Create Leaderboard struct + /* Create Leaderboard struct */ #define MAX_CHARACTERS 16 struct { char Name[MAX_CHARACTERS]; @@ -11,16 +11,17 @@ void leaderboard_record(int Score) { } LeaderboardEntry; LeaderboardEntry.Score = Score; - // Init strings + /* Init strings */ int16_t CharacterNumber = 0; int Key = 0; - for(int i = 0; i < sizeof(LeaderboardEntry.Name); i++) { + int i = 0; + for(i = 0; i < sizeof(LeaderboardEntry.Name); i++) { LeaderboardEntry.Name[i] = '\0'; } char Scored[8192]; snprintf(Scored, sizeof(Scored), "You Scored: %d", LeaderboardEntry.Score); - // Init Camera + /* Init Camera */ Camera2D MainCamera; MainCamera.target = (Vector2){0, 0}; MainCamera.offset = (Vector2){0, 0}; @@ -61,7 +62,7 @@ void leaderboard_record(int Score) { char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; - // Put save to file here. + /* Put save to file here. */ if ((LeaderboardFile = fopen(LeaderboardFilePath, "a")) == NULL) { fprintf(stderr, "Unable to create leaderboard file.\n"); return; @@ -73,12 +74,12 @@ void leaderboard_record(int Score) { void marathon_main() { - // Init Music + /* Init Music */ Mix_Music *Background = Mix_LoadMUS("resources/marathon.wav"); Mix_PlayMusic(Background, -1); Mix_VolumeMusic(GlobalSettings.MusicVolume); - // Init balls lmao + /* Init balls lmao */ struct Balls Ball; Ball.X = 1280/2.0f; Ball.Y = 720/2.0f; @@ -87,23 +88,23 @@ void marathon_main() { Ball.Angle = 0.0f; Ball.NextTick = SDL_AtomicGet(&Ticks)+1; - // Init Player + /* Init Player */ struct Players Player; Player.Y = 0; Player.Direction = 0; Player.Score = 0; - // Init sprites + /* Init sprites */ Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); Texture2D BallSprite = LoadTexture("resources/ball.png"); - char PlayerScore[50]; // Used later to display score on screen. + char PlayerScore[50]; /* Used later to display score on screen. */ - // Set Collision Boxes + /* Set Collision Boxes */ Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height}; Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height}; - // Init Camera + /* Init Camera */ Camera2D MainCamera; MainCamera.target = (Vector2){0, 0}; MainCamera.offset = (Vector2){0, 0}; @@ -111,11 +112,11 @@ void marathon_main() { bool MarathonGoing = true; bool HasScored = false; while(MarathonGoing == true && GameGoing == true) { - if (WindowShouldClose()) { //Quit Game if the window is closed. + if (WindowShouldClose()) { /* Quit Game if the window is closed. */ GameGoing = false; } - //Prevents multi-scoring in some cases. + /* Prevents multi-scoring in some cases. */ if (Ball.Direction == LEFT) { HasScored = true; } else { @@ -127,7 +128,7 @@ void marathon_main() { MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; - //Controls + /* Controls */ if(IsKeyDown(KEY_UP)) { Player.Y -= 10; } else if (IsKeyDown(KEY_DOWN)) { @@ -150,19 +151,19 @@ void marathon_main() { SetMousePosition(0, 720); } - //Check if players are off-screen + /* Check if players are off-screen */ if (Player.Y < 0) { Player.Y = 0; } else if (Player.Y > 480) { Player.Y = 480; } - // Collision + /* Collision */ ball(&Player.HitBox, NULL, &Ball, &Player.Score, NULL); - //Updates hitbox with player's position. + /* Updates hitbox with player's position. */ Player.HitBox.y = Player.Y; - // End Game + /* End Game */ if(Ball.X < 0) { MarathonGoing = false; } diff --git a/src/pause.c b/src/pause.c index 57d5df5..c785c66 100644 --- a/src/pause.c +++ b/src/pause.c @@ -4,7 +4,7 @@ bool pause_screen(Camera2D *MainCamera) { bool PauseScreenGoing = true; - // Mouse + /* Mouse */ Rectangle Mouse = { 1280.0f/2, 720.0f/2, 10, 10 }; @@ -15,11 +15,11 @@ bool pause_screen(Camera2D *MainCamera) { DisableCursor(); while(PauseScreenGoing == true && GameGoing == true) { - // Update Camera + /* Update Camera */ MainCamera->zoom = GetScreenHeight()/720.0f; MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); diff --git a/src/title.c b/src/title.c index f44e71f..43b2eed 100644 --- a/src/title.c +++ b/src/title.c @@ -1,5 +1,4 @@ #include "pong.h" -#include struct LeaderboardEntries { int Score; @@ -18,44 +17,47 @@ static int compare_int(const void *Score1, const void *Score2) { } static int order_leaderboard() { - // Find leaderboard file, and open it. + /* Find leaderboard file, and open it. */ char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; if((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { - return(1); //If it doesn't exist yet, we don't need to care. + return(1); /* If it doesn't exist yet, we don't need to care. */ } - // Check if file is empty. + /* Check if file is empty. */ fscanf(LeaderboardFile, "\n"); if(feof(LeaderboardFile)) { return(1); } - rewind(LeaderboardFile); //If not , rewind the stream. - // Init a temporary pointer to store scores and names into. This will be organized into Top10. + rewind(LeaderboardFile); /* If not , rewind the stream. */ + /* Init a temporary pointer to store scores and names into. This will be organized into Top10. */ struct LeaderboardEntries *TmpStore[UINT16_MAX*2]; - for (int size = 0; size < (UINT16_MAX*2); size++) //If there's 131,000 entries, I dunno what to tell you. You really like pong... + int size = 0; + for (size = 0; size < (UINT16_MAX*2); size++) /* If there's 131,000 entries, I dunno what to tell you. You really like pong... */ TmpStore[size] = malloc(sizeof(struct LeaderboardEntries)); - if(*TmpStore == NULL) { //Check if memory got assigned correctly. + if(*TmpStore == NULL) { /* Check if memory got assigned correctly. */ exit(1); } - // Load Scores and Names - int size = 0; - for (int a = 0; !feof(LeaderboardFile); a++, size++) { //Load data from file into array. + /* Load Scores and Names */ + size = 0; + int a = 0; + for (a = 0; !feof(LeaderboardFile); a++, size++) { /* Load data from file into array. */ fscanf(LeaderboardFile, "%s %d\n", TmpStore[a]->Name, &TmpStore[a]->Score); } qsort(TmpStore, size, sizeof(struct LeaderboardEntries *), compare_int); - - for(int i = 0; i < size; i++) { //Copy first 10 elements into top10 + + int i = 0; + for(i = 0; i < size; i++) { /* Copy first 10 elements into top10. */ strcpy(Top10[i].Name, TmpStore[i]->Name); Top10[i].Score = TmpStore[i]->Score; } - // Cleanup - // Free TmpStore - free(*TmpStore); //Truth be told, I dunno if this even works, since the amount of memory used is kilobytes... - // Close leaderboard file. + /* Cleanup */ + /* Free TmpStore */ + free(*TmpStore); /* Truth be told, I dunno if this even works, since the amount of memory used is kilobytes... */ + /* Close leaderboard file. */ if (LeaderboardFile != NULL) { fclose(LeaderboardFile); } @@ -63,29 +65,32 @@ static int order_leaderboard() { } static void score_screen(Camera2D *MainCamera) { - // Find leaderboard file, and open it. + /* Find leaderboard file, and open it. */ char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; if((LeaderboardFile = fopen(LeaderboardFilePath, "r")) == NULL) { - return; //If it doesn't exist yet, we don't need to care. + return; /* If it doesn't exist yet, we don't need to care. */ } - // Init a temporary pointer to store scores and names into. This will be organized into Top10. + /* Init a temporary pointer to store scores and names into. This will be organized into Top10. */ struct LeaderboardEntries *Scores[UINT16_MAX*2]; - for (int size = 0; size < (UINT16_MAX*2); size++) //If there's 131,000 entries, I dunno what to tell you. You really like pong... + int size = 0; + for (size = 0; size < (UINT16_MAX*2); size++) /* If there's 131,000 entries, I dunno what to tell you. You really like pong... */ Scores[size] = malloc(sizeof(struct LeaderboardEntries)); - if(*Scores == NULL) { //Check if memory got assigned correctly. + if(*Scores == NULL) { /* Check if memory got assigned correctly. */ exit(1); } - for (int i = 0; i < (UINT16_MAX*2); i++) { + int i = 0; + for (i = 0; i < (UINT16_MAX*2); i++) { strcpy(Scores[i]->Name, " "); } - // Load Scores and Names - int size = 0; - for (int a = 0; !feof(LeaderboardFile); a++, size++) { //Load data from file into array. + /* Load Scores and Names. */ + size = 0; + int a = 0; + for (a = 0; !feof(LeaderboardFile); a++, size++) { /* Load data from file into array. */ fscanf(LeaderboardFile, "%s %d", Scores[a]->Name, &Scores[a]->Score); if(feof(LeaderboardFile)) { break; @@ -93,8 +98,8 @@ static void score_screen(Camera2D *MainCamera) { } qsort(Scores, size, sizeof(struct LeaderboardEntries *), compare_int); - //Begin drawing scores. - //Mouse + /* Begin drawing scores. */ + /* Mouse */ bool LookingAtScores = true; bool MouseCursorIn = true; SetMousePosition(1280/2, 720/2); @@ -103,19 +108,19 @@ static void score_screen(Camera2D *MainCamera) { Rectangle MouseCursor = {1280/2.0f,720/2.0f,1,1}; Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); - //Page Buttons Rectangles + /* Page Buttons Rectangles. */ Rectangle PrevPage = {5, 720-50, 70, 45}; Rectangle NextPage = {1280-70, 720-50, 70, 45}; int Page = 0; bool EndOfPages = false; while(LookingAtScores == true && GameGoing == true) { - // Update Camera + /* Update Camera */ MainCamera->zoom = GetScreenHeight()/720.0f; MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); @@ -141,16 +146,17 @@ static void score_screen(Camera2D *MainCamera) { ClearBackground(BLACK); BeginMode2D(*MainCamera); DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - // Scores + /* Scores */ int a = 0; - for (int i = 1+(10*Page); i <= 10+(10*Page); i++, a++) { + int i = 0; + for (i = 1+(10*Page); i <= 10+(10*Page); i++, a++) { if(Scores[i-1]->Name[0] != ' ') { DrawText(TextFormat("%d. %s: %d", i, Scores[i-1]->Name, Scores[i-1]->Score), 460, 60+(50*a), 48, WHITE); } else { EndOfPages = true; } } - // Page Buttons + /* Page Buttons */ if(CheckCollisionRecs(MouseCursor, PrevPage) && Page > 0) { DrawRectangleRec(PrevPage, RED); if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { @@ -168,7 +174,7 @@ static void score_screen(Camera2D *MainCamera) { } DrawText("<--", 5, 720-50, 48, WHITE); DrawText("-->", 1280-70, 720-50, 48, WHITE); - //Exit Button + /* Exit Button */ if(CheckCollisionRecs(MouseCursor, (Rectangle){0,0,42,120})) { DrawRectangle(0, 0, 42, 120, RED); if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { @@ -176,7 +182,7 @@ static void score_screen(Camera2D *MainCamera) { } } DrawText("<", 0, 0, 128, WHITE); - // Cursor + /* Cursor */ DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); EndMode2D(); EndDrawing(); @@ -215,12 +221,12 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { {545,SoundBarY,50,50}, }; - // Back Selection + /* Back Selection */ Rectangle BackButton = { 0,0,42,120 }; - //Screen Buttons + /* Screen Buttons */ int ScreenButtonLength = 250; Rectangle ScreenButtons[3] = { {95, 242, ScreenButtonLength, 42}, @@ -228,7 +234,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { {95, 326, ScreenButtonLength, 42} }; SetMousePosition(GetScreenWidth()/2, GetScreenHeight()/2); - // Mouse + /* Mouse */ Rectangle MouseCursor = { 1280/2.0f,720/2.0f,1,1 }; @@ -239,9 +245,9 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { while(SettingsGoing == true && GameGoing == true) { MainCamera->zoom = GetScreenHeight()/720.0f; MainCamera->offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; + MainCamera->target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); @@ -268,7 +274,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { ClearBackground(BLACK); BeginMode2D(*MainCamera); DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - // Back Button + /* Back Button */ if (CheckCollisionRecs(MouseCursor, BackButton)) { DrawRectangleRec(BackButton, RED); if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { @@ -276,11 +282,12 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { } } - // Music + /* Music */ DrawText("Music Volume:", 50, 10, 42, WHITE); DrawText("<", 0,0,128,WHITE); DrawRectangle(45, MusicBarY-5, 555, 60, DARKGRAY); - for(int i = 0; i < 10; i++) { + int i = 0; + for(i = 0; i < 10; i++) { if (CheckCollisionRecs(MouseCursor, MusicBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { GlobalSettings.MusicVolume = i*10; } @@ -289,11 +296,11 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { } } - // Sound + /* Sound */ DrawText("Sound Volume:", 50, 108, 42, WHITE); DrawRectangle(45, SoundBarY-5, 555, 60, DARKGRAY); DrawText("<", 0,0,128,WHITE); - for(int i = 0; i < 10; i++) { + for(i = 0; i < 10; i++) { if (CheckCollisionRecs(MouseCursor, SoundBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { play_audio(SOUND_BOUNCE); GlobalSettings.SoundVolume = i*10; @@ -302,10 +309,10 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { DrawRectangleRec(SoundBar[i], RED); } } - // Fullscreen + /* Fullscreen */ DrawRectangle(95, 243, 250, 125, DARKGRAY); bool MouseHovering = false; - for(int i = 0; i < 3; i++) { + for(i = 0; i < 3; i++) { if(CheckCollisionRecs(MouseCursor, ScreenButtons[i])) { DrawRectangleRec(ScreenButtons[i], RED); MouseHovering = true; @@ -330,7 +337,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { char SettingsFilePath[8192]; snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory); FILE *SettingsFile; - // Put save to file here. + /* Put save to file here. */ if ((SettingsFile = fopen(SettingsFilePath, "w")) == NULL) { fprintf(stderr, "Unable to create settings file.\n"); exit(1); @@ -347,19 +354,19 @@ static void help_text() { } int title_screen() { - // Init Camera + /* Init Camera */ Camera2D MainCamera; MainCamera.offset = (Vector2){0,0}; MainCamera.target = (Vector2){0,0}; MainCamera.rotation = 0.0f; - // Load leaderboard + /* Load leaderboard */ int NoScores = order_leaderboard(); bool TitleScreenGoing = true; int Choice = 0; - // Selection + /* Selection */ Rectangle Versus = { 20, 150, 230, 48 }; @@ -384,12 +391,12 @@ int title_screen() { Rectangle *Selected; Selected = &Versus; - //Music + /* Music */ Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav"); Mix_PlayMusic(TitleMusic, -1); Mix_VolumeMusic(GlobalSettings.MusicVolume); - // Mouse + /* Mouse */ Vector2 OldPosition = GetMousePosition(); Vector2 NewPosition = GetMousePosition(); Texture2D MouseCursor = LoadTexture("resources/cursor.png"); @@ -398,17 +405,17 @@ int title_screen() { while(TitleScreenGoing == true && GameGoing == true) { - if (WindowShouldClose()) { //Quit Game if the window is closed. + if (WindowShouldClose()) { /* Quit Game if the window is closed. */ GameGoing = false; TitleScreenGoing = false; } - // Update Camera + /* Update Camera */ MainCamera.zoom = GetScreenHeight()/720.0f; MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; - //Mouse + /* Mouse */ if (MouseCursorIn == true) { OldPosition = NewPosition; NewPosition = GetMousePosition(); @@ -422,7 +429,7 @@ int title_screen() { } } - //Gamepad & Keyboard + /* Gamepad & Keyboard */ if(IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN) || IsKeyPressed(KEY_S)) { Choice++; } else if(IsGamepadButtonPressed(0, GAMEPAD_BUTTON_LEFT_FACE_UP) || IsKeyPressed(KEY_W)) { @@ -492,8 +499,9 @@ int title_screen() { goto skip; } char LeaderboardText[1024]; - for (int i = 1; i <= 10; i++) { - if(Top10[i-1].Name[0] != '\0') { //If name is blank, that means we're at the end of the list. + int i = 1; + for (i = 1; i <= 10; i++) { + if(Top10[i-1].Name[0] != '\0') { /* If name is blank, that means we're at the end of the list. */ snprintf(LeaderboardText, sizeof(LeaderboardText), "%d: %s : %d", i, Top10[i-1].Name, Top10[i-1].Score); DrawText(LeaderboardText, 600, 50*i, 48, WHITE); } diff --git a/src/versus.c b/src/versus.c index 7b140ae..30bccd1 100644 --- a/src/versus.c +++ b/src/versus.c @@ -1,18 +1,18 @@ #include "pong.h" void versus_main() { - // Init Player Variables + /* Init Player Variables */ struct Players Player; Player.Y = 0; Player.Score = 0; - // Init Enemy Variables + /* Init Enemy Variables */ struct Players Enemy; Enemy.Y = 0; Enemy.Score = 0; Enemy.Direction = 0; - // Init Ball Variables + /* Init Ball Variables */ struct Balls Ball; Ball.X = 1280/2.0f; Ball.Y = 720/2.0f; @@ -20,23 +20,23 @@ void versus_main() { Ball.Speed = 3.0f; Ball.Angle = 0.0f; - // Init music + /* Init music */ Mix_Music *Background = Mix_LoadMUS("resources/versus.wav"); Mix_PlayMusic(Background, -1); Mix_VolumeMusic(GlobalSettings.MusicVolume); - // Set Sprites + /* Set Sprites */ Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); Texture2D BallSprite = LoadTexture("resources/ball.png"); - // Set Collision Boxes + /* Set Collision Boxes */ Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height}; Enemy.HitBox = (Rectangle){1200, Enemy.Y, 5, PaddleSprite.height}; Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height}; Enemy.BallDetector = (Rectangle){0, Enemy.Y+120, 1280, PaddleSprite.height/5.0f}; char EnemyScore[50]; - char PlayerScore[50]; // Used later to display score on screen. - // Init Camera + char PlayerScore[50]; /* Used later to display score on screen. */ + /* Init Camera */ Camera2D MainCamera; MainCamera.target = (Vector2){0, 0}; MainCamera.offset = (Vector2){0, 0}; @@ -47,7 +47,7 @@ void versus_main() { Enemy.NextTick = SDL_AtomicGet(&Ticks); while(VersusGoing == true && GameGoing == true) { - if (WindowShouldClose()) { //Quit Game if the window is closed. + if (WindowShouldClose()) { /* Quit Game if the window is closed. */ GameGoing = false; } @@ -55,7 +55,7 @@ void versus_main() { MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; - // Who won? + /* Who won? */ if (Enemy.Score >= 5) { Mix_HaltMusic(); Mix_FreeMusic(Background); @@ -86,7 +86,7 @@ void versus_main() { return; } - //Controls + /* Controls */ if(IsKeyDown(KEY_UP)) { Player.Y -= 10; } else if (IsKeyDown(KEY_DOWN)) { @@ -104,7 +104,7 @@ void versus_main() { DisableCursor(); } - // Leave Game + /* Leave Game */ if(IsKeyPressed(KEY_Q)) { VersusGoing = false; } @@ -115,7 +115,7 @@ void versus_main() { SetMousePosition(0, 720); } - //Check if players are off-screen + /* Check if players are off-screen */ if (Player.Y < 0) { Player.Y = 0; } else if (Player.Y > 480) { @@ -124,16 +124,15 @@ void versus_main() { enemy(&Enemy, Ball); - // Collision + /* Collision */ ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score); - //Updates hitbox with player's position. + /* Updates hitbox with player's position. */ Player.HitBox.y = Player.Y; - //Turn Scores into strings. + /* Turn Scores into strings. */ snprintf(PlayerScore, 50, "Player: %d", Player.Score); snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score); - //fprintf(stdout, "%d\n", GetFPS()); BeginDrawing(); ClearBackground(BLACK); BeginMode2D(MainCamera); -- cgit 1.4.1-2-gfad0