diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ball.c | 17 | ||||
-rw-r--r-- | src/controls.c | 43 | ||||
-rw-r--r-- | src/enemy.c | 17 | ||||
-rw-r--r-- | src/main.c | 79 | ||||
-rw-r--r-- | src/marathon.c | 281 | ||||
-rw-r--r-- | src/pause.c | 112 | ||||
-rw-r--r-- | src/title.c | 1054 | ||||
-rw-r--r-- | src/versus.c | 118 |
8 files changed, 851 insertions, 870 deletions
diff --git a/src/ball.c b/src/ball.c index 0e6b62f..f6f5362 100644 --- a/src/ball.c +++ b/src/ball.c @@ -1,7 +1,6 @@ #include "pong.h" void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerScore, int *EnemyScore) { - bool NoEnemy = false; if (Enemy == NULL) { NoEnemy = true; @@ -9,7 +8,7 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc /* Moves */ int CurrentTick = SDL_AtomicGet(&Ticks); - if(Ball->NextTick <= CurrentTick){ + if (Ball->NextTick <= CurrentTick) { int RunThisManyTimes = 0; if (CurrentTick > Ball->NextTick) { RunThisManyTimes = (CurrentTick - Ball->NextTick); @@ -57,12 +56,12 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc } } /* Bounce ball if touches top or bottom of screen. */ - if ( (Ball->Y+32 >= 720 && Ball->Angle >=1) || (Ball->Y <= 0 && Ball->Angle <= -1) ) { + 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; + Ball->NextTick = SDL_AtomicGet(&Ticks) + 1; } /* Calculates score and resets ball. */ @@ -70,18 +69,18 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc if (Ball->X < 0 && NoEnemy == false) { *EnemyScore += 1; Scored = true; - } else if (Ball ->X > 1280 && NoEnemy == false) { + } else if (Ball->X > 1280 && NoEnemy == false) { *PlayerScore += 1; Scored = true; - } else if (Ball->X > (1280-32) && NoEnemy == true) { + } else if (Ball->X > (1280 - 32) && NoEnemy == true) { *PlayerScore += 1; Scored = false; Ball->Direction = LEFT; play_audio(SOUND_PLAYER_SCORE); - } + } if (Scored == true) { - Ball->X = 1280/2.0f; - Ball->Y = 720/2.0f; + Ball->X = 1280 / 2.0f; + Ball->Y = 720 / 2.0f; Ball->Speed = 3.0f; Ball->Angle = 0; } diff --git a/src/controls.c b/src/controls.c index a3cefa1..de2520d 100644 --- a/src/controls.c +++ b/src/controls.c @@ -1,41 +1,42 @@ -#include "pong.h" #include <raylib.h> +#include "pong.h" + /* Checks what button the player is pressing amongst all control inputs. */ int player_controls() { - if( IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsKeyDown(KEY_SPACE) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN) ) { - return(CONTROLLER_ACTIVATE); - } else if( IsKeyDown(KEY_ESCAPE) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_MIDDLE_RIGHT) ) { - return(CONTROLLER_PAUSE); - } else if( IsKeyDown(KEY_A) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_LEFT) ) { - return(CONTROLLER_LEFT); - } else if( IsKeyDown(KEY_D) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT) ) { - return(CONTROLLER_RIGHT); - } else if( IsKeyDown(KEY_W) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_UP) ) { - return(CONTROLLER_UP); - } else if( IsKeyDown(KEY_S) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_DOWN) ) { - return(CONTROLLER_DOWN); + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsKeyDown(KEY_SPACE) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) { + return (CONTROLLER_ACTIVATE); + } else if (IsKeyDown(KEY_ESCAPE) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_MIDDLE_RIGHT)) { + return (CONTROLLER_PAUSE); + } else if (IsKeyDown(KEY_A) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) { + return (CONTROLLER_LEFT); + } else if (IsKeyDown(KEY_D) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) { + return (CONTROLLER_RIGHT); + } else if (IsKeyDown(KEY_W) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_UP)) { + return (CONTROLLER_UP); + } else if (IsKeyDown(KEY_S) || IsGamepadButtonDown(1, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) { + return (CONTROLLER_DOWN); } - return(-1); + return (-1); } /* Same as player_controls() but check if the button was pressed, rather than held down. */ int player_controls_pressed() { int ReturnVal = -1; - if( IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsKeyPressed(KEY_SPACE) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN) ) { + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsKeyPressed(KEY_SPACE) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) { ReturnVal = CONTROLLER_ACTIVATE; - } else if( IsKeyPressed(KEY_ESCAPE) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_MIDDLE_RIGHT) ) { + } else if (IsKeyPressed(KEY_ESCAPE) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_MIDDLE_RIGHT)) { ReturnVal = CONTROLLER_PAUSE; - } else if( IsKeyPressed(KEY_A) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_LEFT) ) { + } else if (IsKeyPressed(KEY_A) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) { ReturnVal = CONTROLLER_LEFT; - } else if( IsKeyPressed(KEY_D) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT) ) { + } else if (IsKeyPressed(KEY_D) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) { ReturnVal = CONTROLLER_RIGHT; - } else if( IsKeyPressed(KEY_W) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_UP) ) { + } else if (IsKeyPressed(KEY_W) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_UP)) { ReturnVal = CONTROLLER_UP; - } else if( IsKeyPressed(KEY_S) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_DOWN) ) { + } else if (IsKeyPressed(KEY_S) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) { ReturnVal = CONTROLLER_DOWN; } - return(ReturnVal); + return (ReturnVal); } /* Calls BeginDrawing() and EndDrawing() to clear input buffer. */ diff --git a/src/enemy.c b/src/enemy.c index b46ce15..80ef7f5 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -1,18 +1,19 @@ -#include "pong.h" #include <SDL2/SDL_atomic.h> +#include "pong.h" + void enemy(struct Players *Enemy, struct Balls ball) { int CurrentTick = SDL_AtomicGet(&Ticks); int RunThisManyTimes = 0; if (Enemy->NextTick <= CurrentTick) { - if(CurrentTick > Enemy->NextTick) { + if (CurrentTick > Enemy->NextTick) { RunThisManyTimes = (CurrentTick - Enemy->NextTick); } int i = 0; for (i = 0; i <= RunThisManyTimes; i++) { /* Changes direction */ if (!CheckCollisionRecs(ball.HitBox, Enemy->BallDetector)) { - if (Enemy->Y+120 > ball.Y) { + if (Enemy->Y + 120 > ball.Y) { Enemy->Direction = 0; } else if (Enemy->Y < ball.Y) { Enemy->Direction = 1; @@ -22,7 +23,7 @@ void enemy(struct Players *Enemy, struct Balls ball) { } /* Moves */ - switch(Enemy->Direction) { + switch (Enemy->Direction) { case 0: Enemy->Y -= 15; break; @@ -34,17 +35,17 @@ void enemy(struct Players *Enemy, struct Balls ball) { } /* Prevents from going off screen. */ - if ( Enemy->Y > 480 ) { + if (Enemy->Y > 480) { Enemy->Y = 480; } else if (Enemy->Y < 0) { Enemy->Y = 0; } - + /* Updates hitbox and detector */ Enemy->HitBox.y = Enemy->Y; - Enemy->BallDetector.y = Enemy->Y+80; + Enemy->BallDetector.y = Enemy->Y + 80; } - Enemy->NextTick = SDL_AtomicGet(&Ticks)+1; + Enemy->NextTick = SDL_AtomicGet(&Ticks) + 1; } return; } diff --git a/src/main.c b/src/main.c index 4091596..9f91def 100644 --- a/src/main.c +++ b/src/main.c @@ -17,14 +17,14 @@ void set_screen_mode() { ClearWindowState(FLAG_WINDOW_TOPMOST); ClearWindowState(FLAG_WINDOW_UNDECORATED); ClearWindowState(FLAG_FULLSCREEN_MODE); - switch(GlobalSettings.Fullscreen) { + switch (GlobalSettings.Fullscreen) { 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. */ - while(GetScreenHeight() != GetMonitorHeight(GetCurrentMonitor())) { + 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; } } @@ -39,9 +39,9 @@ void set_screen_mode() { SetWindowSize(GetMonitorWidth(GetCurrentMonitor()), GetMonitorHeight(GetCurrentMonitor())); break; default: - FullScreenFailed: + FullScreenFailed: SetWindowSize(1280, 720); - SetWindowPosition(GetMonitorHeight(GetCurrentMonitor())/5, GetMonitorHeight(GetCurrentMonitor())/5); + SetWindowPosition(GetMonitorHeight(GetCurrentMonitor()) / 5, GetMonitorHeight(GetCurrentMonitor()) / 5); break; } return; @@ -49,20 +49,19 @@ void set_screen_mode() { double Started; static int internal_clock() { - static const double Framerate = 1.0/60.0f; + static const double Framerate = 1.0 / 60.0f; double OldTime = GetTime(); double DeltaTime = 0; double NewTime = 0; Started = GetTime(); struct timespec SleepFor = { - 0, 99999 - }; - while(GameGoing == true) { + 0, 99999}; + while (GameGoing == true) { nanosleep(&SleepFor, NULL); /* Reduces CPU usage */ NewTime = GetTime(); - DeltaTime += (NewTime - OldTime)/Framerate; + DeltaTime += (NewTime - OldTime) / Framerate; OldTime = NewTime; - while(DeltaTime >= 1.0) { + while (DeltaTime >= 1.0) { SDL_AtomicAdd(&Ticks, 1); DeltaTime--; } @@ -72,7 +71,7 @@ static int internal_clock() { } } - return(0); + return (0); } static int audio() { @@ -86,15 +85,14 @@ static int audio() { Mix_Chunk *PlayerScore = Mix_LoadWAV("resources/score_player.wav"); Mix_Chunk *EnemyScore = Mix_LoadWAV("resources/score_enemy.wav"); const struct timespec Delay = { - 0, 20000000 - }; + 0, 20000000}; struct timespec Remaining; SDL_AtomicSet(&AudioInitializing, 1); - while(GameGoing == true) { - for(i = 0; i < 20; i++) { - if(AudioQueue[i] != -1){ + while (GameGoing == true) { + for (i = 0; i < 20; i++) { + if (AudioQueue[i] != -1) { Mix_Volume(-1, GlobalSettings.SoundVolume); - switch(AudioQueue[i]) { + switch (AudioQueue[i]) { case 0: /* Play bounce sound. */ Mix_PlayChannel(-1, Bounce, 0); break; @@ -123,19 +121,19 @@ static int audio() { } nanosleep(&Delay, &Remaining); } - return(0); + return (0); } bool play_audio(int SoundEffect) { unsigned int i; SDL_LockMutex(AudioQueueBeingModified); - for(i = 1; i != 20; i++) { - if (AudioQueue[i-1] == -1) { - AudioQueue[i-1] = AudioQueue[i]; + for (i = 1; i != 20; i++) { + if (AudioQueue[i - 1] == -1) { + AudioQueue[i - 1] = AudioQueue[i]; } } - for(i = 0; AudioQueue[i] != -1; i++) { - if(i > sizeof(AudioQueue)) { + for (i = 0; AudioQueue[i] != -1; i++) { + if (i > sizeof(AudioQueue)) { SDL_UnlockMutex(AudioQueueBeingModified); return false; } @@ -168,10 +166,10 @@ int main(int argc, char *argv[]) { /* Init Variables */ strncpy(VersionString, "Version 0.4 - APOLLO", sizeof(VersionString)); - + /* Populate Audio Queue */ unsigned int i = 0; - for(i = 0; i < 20; i++) { + for (i = 0; i < 20; i++) { AudioQueue[i] = -1; } @@ -185,10 +183,10 @@ int main(int argc, char *argv[]) { char SettingsFilePath[8192]; snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory); FILE *SettingsFile; - reopen: +reopen: /* Create settings file if it doesn't exist. */ if ((SettingsFile = fopen(SettingsFilePath, "r")) == NULL) { - if(SettingsFile != NULL) { + if (SettingsFile != NULL) { fclose(SettingsFile); } if ((SettingsFile = fopen(SettingsFilePath, "w")) == NULL) { @@ -201,36 +199,35 @@ int main(int argc, char *argv[]) { fclose(SettingsFile); goto reopen; /* Try opening again. */ } - + /* Parse the settings file. */ char Option[2048]; int Value; - while(!feof(SettingsFile)) { + while (!feof(SettingsFile)) { fscanf(SettingsFile, "%s %i", Option, &Value); - if(strcmp(Option, "sound_volume") == 0) { + if (strcmp(Option, "sound_volume") == 0) { GlobalSettings.SoundVolume = Value; - } else if(strcmp(Option, "music_volume") == 0) { + } else if (strcmp(Option, "music_volume") == 0) { GlobalSettings.MusicVolume = Value; - } else if(strcmp(Option, "fullscreen") == 0) { + } else if (strcmp(Option, "fullscreen") == 0) { GlobalSettings.Fullscreen = Value; } } fclose(SettingsFile); - + set_screen_mode(); /* Wait for audio to finish initializing. */ struct timespec a = { - 0, 5000000 - }; + 0, 5000000}; struct timespec b; - while(SDL_AtomicGet(&AudioInitializing) == 0) { + while (SDL_AtomicGet(&AudioInitializing) == 0) { nanosleep(&a, &b); /* Prevent heavy cpu usage */ } /* Launch Game */ while (GameGoing == true) { - switch(title_screen()) { + switch (title_screen()) { case 0: /* Versus */ play_audio(STOP_ALL_SOUNDS); versus_main(); @@ -243,11 +240,11 @@ int main(int argc, char *argv[]) { break; } } - + GameGoing = false; /* Make sure the game is going to end. */ SDL_WaitThread(InternalClock, NULL); SDL_WaitThread(AudioThread, NULL); SDL_Quit(); CloseWindow(); - return(0); + return (0); } diff --git a/src/marathon.c b/src/marathon.c index 70484b6..2807ee1 100644 --- a/src/marathon.c +++ b/src/marathon.c @@ -1,123 +1,122 @@ #include "pong.h" void leaderboard_record(int Score) { - bool LeaderBoardGoing = true; - - /* Create Leaderboard struct */ - #define MAX_CHARACTERS 16 - struct { - char Name[MAX_CHARACTERS]; - int Score; - } LeaderboardEntry; - LeaderboardEntry.Score = Score; - - /* Init strings */ - int16_t CharacterNumber = 0; - int Key = 0; + bool LeaderBoardGoing = true; + +/* Create Leaderboard struct */ +#define MAX_CHARACTERS 16 + struct { + char Name[MAX_CHARACTERS]; + int Score; + } LeaderboardEntry; + LeaderboardEntry.Score = Score; + + /* Init strings */ + int16_t CharacterNumber = 0; + int Key = 0; 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 */ - Camera2D MainCamera; - (void)MainCamera; /* Works-around unused variable compiler warning. */ - MainCamera.target = (Vector2){0, 0}; - MainCamera.offset = (Vector2){0, 0}; - MainCamera.rotation = 0; - while(LeaderBoardGoing == true) { - MainCamera.zoom = GetScreenHeight()/720.0f; - MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; - - BeginDrawing(); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - Key = GetKeyPressed(); - switch(Key) { - case KEY_BACKSPACE: - if(CharacterNumber != 0) { - LeaderboardEntry.Name[CharacterNumber] = '\0'; - CharacterNumber -= 1; - } - break; - case KEY_ENTER: - if(CharacterNumber != 0) { - LeaderBoardGoing = false; - } - break; - default: - if (Key >= 33 && Key <= 126 && CharacterNumber < MAX_CHARACTERS) { - LeaderboardEntry.Name[CharacterNumber] = GetCharPressed(); - CharacterNumber += 1; - } - break; - } - DrawText(Scored, 5, 0, 48, BLUE); - DrawText("Enter Name:", 5, 50, 48, WHITE); - DrawText(LeaderboardEntry.Name, 300, 50, 48, WHITE); - EndDrawing(); - } - char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); + 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 */ + Camera2D MainCamera; + (void)MainCamera; /* Works-around unused variable compiler warning. */ + MainCamera.target = (Vector2){0, 0}; + MainCamera.offset = (Vector2){0, 0}; + MainCamera.rotation = 0; + while (LeaderBoardGoing == true) { + MainCamera.zoom = GetScreenHeight() / 720.0f; + MainCamera.offset = (Vector2){GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}; + MainCamera.target = (Vector2){1280 / 2.0f, 720 / 2.0f}; + + BeginDrawing(); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + Key = GetKeyPressed(); + switch (Key) { + case KEY_BACKSPACE: + if (CharacterNumber != 0) { + LeaderboardEntry.Name[CharacterNumber] = '\0'; + CharacterNumber -= 1; + } + break; + case KEY_ENTER: + if (CharacterNumber != 0) { + LeaderBoardGoing = false; + } + break; + default: + if (Key >= 33 && Key <= 126 && CharacterNumber < MAX_CHARACTERS) { + LeaderboardEntry.Name[CharacterNumber] = GetCharPressed(); + CharacterNumber += 1; + } + break; + } + DrawText(Scored, 5, 0, 48, BLUE); + DrawText("Enter Name:", 5, 50, 48, WHITE); + DrawText(LeaderboardEntry.Name, 300, 50, 48, WHITE); + EndDrawing(); + } + char *LeaderboardDirectory = SDL_GetPrefPath("iotib", "Pong"); char LeaderboardFilePath[8192]; snprintf(LeaderboardFilePath, sizeof(LeaderboardFilePath), "%s/leaderboard.txt", LeaderboardDirectory); FILE *LeaderboardFile; - /* Put save to file here. */ - if ((LeaderboardFile = fopen(LeaderboardFilePath, "a")) == NULL) { - fprintf(stderr, "Unable to create leaderboard file.\n"); - return; - } - fprintf(LeaderboardFile, "%s %d\n", LeaderboardEntry.Name, Score); - fclose(LeaderboardFile); - return; + /* Put save to file here. */ + if ((LeaderboardFile = fopen(LeaderboardFilePath, "a")) == NULL) { + fprintf(stderr, "Unable to create leaderboard file.\n"); + return; + } + fprintf(LeaderboardFile, "%s %d\n", LeaderboardEntry.Name, Score); + fclose(LeaderboardFile); + return; } void marathon_main() { - - /* Init Music */ - Mix_Music *Background = Mix_LoadMUS("resources/marathon.wav"); - Mix_PlayMusic(Background, -1); - Mix_VolumeMusic(GlobalSettings.MusicVolume); - - /* Init balls lmao */ - struct Balls Ball; - Ball.X = 1280/2.0f; - Ball.Y = 720/2.0f; + /* Init Music */ + Mix_Music *Background = Mix_LoadMUS("resources/marathon.wav"); + Mix_PlayMusic(Background, -1); + Mix_VolumeMusic(GlobalSettings.MusicVolume); + + /* Init balls lmao */ + struct Balls Ball; + Ball.X = 1280 / 2.0f; + Ball.Y = 720 / 2.0f; Ball.Direction = LEFT; Ball.Speed = 3.0f; Ball.Angle = 0.0f; - Ball.NextTick = SDL_AtomicGet(&Ticks)+1; + Ball.NextTick = SDL_AtomicGet(&Ticks) + 1; - /* Init Player */ - struct Players Player; - Player.Y = 0; - Player.Direction = 0; - Player.Score = 0; + /* Init Player */ + struct Players Player; + Player.Y = 0; + Player.Direction = 0; + Player.Score = 0; - /* Init sprites */ - Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); + /* 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 */ - Camera2D MainCamera; - MainCamera.target = (Vector2){0, 0}; - MainCamera.offset = (Vector2){0, 0}; - MainCamera.rotation = 0; - bool MarathonGoing = true; + /* Init Camera */ + Camera2D MainCamera; + MainCamera.target = (Vector2){0, 0}; + MainCamera.offset = (Vector2){0, 0}; + MainCamera.rotation = 0; + bool MarathonGoing = true; bool HasScored = false; (void)HasScored; Vector2 MouseCurrentPosition = GetMousePosition(); - while(MarathonGoing == true && GameGoing == true) { - if (WindowShouldClose()) { /* Quit Game if the window is closed. */ - GameGoing = false; - } + while (MarathonGoing == true && GameGoing == true) { + if (WindowShouldClose()) { /* Quit Game if the window is closed. */ + GameGoing = false; + } /* Prevents multi-scoring in some cases. */ if (Ball.Direction == LEFT) { @@ -126,13 +125,13 @@ void marathon_main() { HasScored = false; } - snprintf(PlayerScore, 50, "Player: %d", Player.Score); - MainCamera.zoom = GetScreenHeight()/720.0f; - MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; + snprintf(PlayerScore, 50, "Player: %d", Player.Score); + MainCamera.zoom = GetScreenHeight() / 720.0f; + MainCamera.offset = (Vector2){GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}; + MainCamera.target = (Vector2){1280 / 2.0f, 720 / 2.0f}; - /* Controls */ - switch(player_controls()) { + /* Controls */ + switch (player_controls()) { case CONTROLLER_UP: Player.Y -= 20; break; @@ -145,55 +144,55 @@ void marathon_main() { EndDrawing(); MarathonGoing = pause_screen(&MainCamera); Mix_ResumeMusic(); - Ball.NextTick = SDL_AtomicGet(&Ticks)+1; + Ball.NextTick = SDL_AtomicGet(&Ticks) + 1; break; default: break; } /* Mouse Controls */ - if(MouseCurrentPosition.y != GetMousePosition().y) { - Player.Y = GetMouseY()-PaddleSprite.height/2.0f; - - if(GetMouseY() < 0) { - SetMousePosition(0, 0); - } else if(GetMouseY() > 720) { - SetMousePosition(0, 720); - } + if (MouseCurrentPosition.y != GetMousePosition().y) { + Player.Y = GetMouseY() - PaddleSprite.height / 2.0f; + + if (GetMouseY() < 0) { + SetMousePosition(0, 0); + } else if (GetMouseY() > 720) { + SetMousePosition(0, 720); + } MouseCurrentPosition = GetMousePosition(); } - /* Check if players are off-screen */ - if (Player.Y < 0) { - Player.Y = 0; - } else if (Player.Y > 480) { - Player.Y = 480; - } - - /* Collision */ - ball(&Player.HitBox, NULL, &Ball, &Player.Score, NULL); - /* Updates hitbox with player's position. */ - Player.HitBox.y = Player.Y; - - /* End Game */ - if(Ball.X < 0) { - MarathonGoing = false; - } - - BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(MainCamera); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - DrawTexture(PaddleSprite, 0, Player.Y, WHITE); - DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); - DrawText(PlayerScore, 0, 0, 32, BLUE); - EndMode2D(); + /* Check if players are off-screen */ + if (Player.Y < 0) { + Player.Y = 0; + } else if (Player.Y > 480) { + Player.Y = 480; + } + + /* Collision */ + ball(&Player.HitBox, NULL, &Ball, &Player.Score, NULL); + /* Updates hitbox with player's position. */ + Player.HitBox.y = Player.Y; + + /* End Game */ + if (Ball.X < 0) { + MarathonGoing = false; + } + + BeginDrawing(); + ClearBackground(BLACK); + BeginMode2D(MainCamera); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + DrawTexture(PaddleSprite, 0, Player.Y, WHITE); + DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); + DrawText(PlayerScore, 0, 0, 32, BLUE); + EndMode2D(); EndDrawing(); - } - Mix_HaltMusic(); + } + Mix_HaltMusic(); if (Player.Score > 0) { - leaderboard_record(Player.Score); + leaderboard_record(Player.Score); } - Mix_FreeMusic(Background); - return; + Mix_FreeMusic(Background); + return; } diff --git a/src/pause.c b/src/pause.c index c3264ec..fa5e97a 100644 --- a/src/pause.c +++ b/src/pause.c @@ -1,51 +1,51 @@ -#include "pong.h" #include <raylib.h> +#include "pong.h" + bool pause_screen(Camera2D *MainCamera) { bool PauseScreenGoing = true; - + /* Mouse */ Rectangle Mouse = { - 1280.0f/2, 720.0f/2, 10, 10 - }; + 1280.0f / 2, 720.0f / 2, 10, 10}; Vector2 OldPosition = GetMousePosition(); Vector2 NewPosition = GetMousePosition(); Texture2D MouseCursor = LoadTexture("resources/cursor.png"); bool MouseCursorIn = false; EnableCursor(); int Choice = 0; - while(PauseScreenGoing == true && GameGoing == true) { + while (PauseScreenGoing == true && GameGoing == true) { /* 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}; + MainCamera->zoom = GetScreenHeight() / 720.0f; + MainCamera->offset = (Vector2){GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}; + MainCamera->target = (Vector2){1280 / 2.0f, 720 / 2.0f}; /* Mouse */ - if (MouseCursorIn == true) { - OldPosition = NewPosition; - NewPosition = GetMousePosition(); - Mouse.y -= OldPosition.y-NewPosition.y; - Mouse.x -= OldPosition.x-NewPosition.x; - if (Mouse.y >= 720 || Mouse.y <= 0) { - Mouse.y += OldPosition.y-NewPosition.y; - } - if (Mouse.x >= 1280 || Mouse.x <= 0) { - Mouse.x += OldPosition.x-NewPosition.x; - } - } + if (MouseCursorIn == true) { + OldPosition = NewPosition; + NewPosition = GetMousePosition(); + Mouse.y -= OldPosition.y - NewPosition.y; + Mouse.x -= OldPosition.x - NewPosition.x; + if (Mouse.y >= 720 || Mouse.y <= 0) { + Mouse.y += OldPosition.y - NewPosition.y; + } + if (Mouse.x >= 1280 || Mouse.x <= 0) { + Mouse.x += OldPosition.x - NewPosition.x; + } + } - if(IsKeyPressed(KEY_Y)) { + if (IsKeyPressed(KEY_Y)) { return false; - } else if(IsKeyPressed(KEY_N)) { + } else if (IsKeyPressed(KEY_N)) { return true; } - + if (IsKeyPressed(KEY_ESCAPE)) { EnableCursor(); MouseCursorIn = false; - } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { DisableCursor(); - MouseCursorIn = true; - } + MouseCursorIn = true; + } if (player_controls() == CONTROLLER_LEFT) { Choice--; } else if (player_controls() == CONTROLLER_RIGHT) { @@ -59,36 +59,36 @@ bool pause_screen(Camera2D *MainCamera) { } BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(*MainCamera); - DrawRectangle(0, 0, 1280, 720, (Color){20,20,20,255}); - if(CheckCollisionRecs(Mouse, (Rectangle){1280/3.0f, (720/3.0f)+50, 100, 50})) { - Choice = 0; - } else if(CheckCollisionRecs(Mouse, (Rectangle){(1280/3.0f)+150, (720/3.0f)+50, 70, 50})) { - Choice = 1; - } - switch(Choice) { - case 0: - DrawRectangle((1280/3.0f)-5, (720/3.0f)+50, 104, 50, RED); - break; - default: - DrawRectangle((1280/3.0f)+150,(720/3.0f)+50, 70, 50, RED); - break; - } - if ( player_controls() == CONTROLLER_ACTIVATE ) { - switch(Choice) { - case 0: - return false; - break; - default: - return true; - break; - } - } - DrawText("Paused. Exit?", 1280/3, 720/3, 48, WHITE); - DrawText("YES NO", 1280/3, (720/3)+50, 48, WHITE); - DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE); - EndMode2D(); + ClearBackground(BLACK); + BeginMode2D(*MainCamera); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + if (CheckCollisionRecs(Mouse, (Rectangle){1280 / 3.0f, (720 / 3.0f) + 50, 100, 50})) { + Choice = 0; + } else if (CheckCollisionRecs(Mouse, (Rectangle){(1280 / 3.0f) + 150, (720 / 3.0f) + 50, 70, 50})) { + Choice = 1; + } + switch (Choice) { + case 0: + DrawRectangle((1280 / 3.0f) - 5, (720 / 3.0f) + 50, 104, 50, RED); + break; + default: + DrawRectangle((1280 / 3.0f) + 150, (720 / 3.0f) + 50, 70, 50, RED); + break; + } + if (player_controls() == CONTROLLER_ACTIVATE) { + switch (Choice) { + case 0: + return false; + break; + default: + return true; + break; + } + } + DrawText("Paused. Exit?", 1280 / 3, 720 / 3, 48, WHITE); + DrawText("YES NO", 1280 / 3, (720 / 3) + 50, 48, WHITE); + DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE); + EndMode2D(); EndDrawing(); } return true; diff --git a/src/title.c b/src/title.c index cacd2b0..154700e 100644 --- a/src/title.c +++ b/src/title.c @@ -1,10 +1,11 @@ +#include <raylib.h> + #include "pong.h" #include "sounds.h" -#include <raylib.h> struct LeaderboardEntries { - int Score; - char Name[16]; + int Score; + char Name[16]; }; struct LeaderboardEntries Top10[10]; @@ -14,294 +15,290 @@ struct LeaderboardEntries Top10[10]; * But uh, I don't know how it does, but it works! */ static int compare_int(const void *Score1, const void *Score2) { - const struct LeaderboardEntries *Entry1 = *(struct LeaderboardEntries**) Score1; - const struct LeaderboardEntries *Entry2 = *(struct LeaderboardEntries**) Score2; - if (Entry1->Score > Entry2->Score) { - return -1; - } - return +1; + const struct LeaderboardEntries *Entry1 = *(struct LeaderboardEntries **)Score1; + const struct LeaderboardEntries *Entry2 = *(struct LeaderboardEntries **)Score2; + if (Entry1->Score > Entry2->Score) { + return -1; + } + return +1; } static int order_leaderboard() { - /* 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. */ - } - /* 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. */ - struct LeaderboardEntries *TmpStore[UINT16_MAX*2]; - 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] = GC_malloc(sizeof(struct LeaderboardEntries)); - if(*TmpStore == NULL) { /* Check if memory got assigned correctly. */ - exit(1); - } - - /* 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); - - 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 */ - GC_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); - } - return(0); + /* 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. */ + } + /* 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. */ + struct LeaderboardEntries *TmpStore[UINT16_MAX * 2]; + 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] = GC_malloc(sizeof(struct LeaderboardEntries)); + if (*TmpStore == NULL) { /* Check if memory got assigned correctly. */ + exit(1); + } + + /* 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); + + 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 */ + GC_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); + } + return (0); } static void score_screen(Camera2D *MainCamera) { - /* 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. */ - } - - /* Init a temporary pointer to store scores and names into. This will be organized into Top10. */ - struct LeaderboardEntries *Scores[UINT16_MAX*2]; - 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] = GC_malloc(sizeof(struct LeaderboardEntries)); - if(*Scores == NULL) { /* Check if memory got assigned correctly. */ - exit(1); - } - int i = 0; - for (i = 0; i < (UINT16_MAX*2); i++) { - strcpy(Scores[i]->Name, " "); - } - - /* 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; + /* 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. */ + } + + /* Init a temporary pointer to store scores and names into. This will be organized into Top10. */ + struct LeaderboardEntries *Scores[UINT16_MAX * 2]; + 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] = GC_malloc(sizeof(struct LeaderboardEntries)); + if (*Scores == NULL) { /* Check if memory got assigned correctly. */ + exit(1); + } + int i = 0; + for (i = 0; i < (UINT16_MAX * 2); i++) { + strcpy(Scores[i]->Name, " "); } - } - qsort(Scores, size, sizeof(struct LeaderboardEntries *), compare_int); - - /* Begin drawing scores. */ - /* Mouse */ - bool LookingAtScores = true; - bool MouseCursorIn = true; - SetMousePosition(1280/2, 720/2); - Vector2 OldPosition = GetMousePosition(); - Vector2 NewPosition = GetMousePosition(); - Rectangle MouseCursor = {1280/2.0f,720/2.0f,1,1}; - Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); - - /* Page Buttons Rectangles. */ - Rectangle PrevPage = {5, 720-50, 70, 45}; - Rectangle NextPage = {1280-70, 720-50, 70, 45}; - - - int Page = 0; - bool EndOfPages = false; + + /* 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; + } + } + qsort(Scores, size, sizeof(struct LeaderboardEntries *), compare_int); + + /* Begin drawing scores. */ + /* Mouse */ + bool LookingAtScores = true; + bool MouseCursorIn = true; + SetMousePosition(1280 / 2, 720 / 2); + Vector2 OldPosition = GetMousePosition(); + Vector2 NewPosition = GetMousePosition(); + Rectangle MouseCursor = {1280 / 2.0f, 720 / 2.0f, 1, 1}; + Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); + + /* Page Buttons Rectangles. */ + Rectangle PrevPage = {5, 720 - 50, 70, 45}; + Rectangle NextPage = {1280 - 70, 720 - 50, 70, 45}; + + int Page = 0; + bool EndOfPages = false; bool BackButtonSelected = false; - while(LookingAtScores == true && GameGoing == true) { - /* 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 */ - if (MouseCursorIn == true) { - OldPosition = NewPosition; - NewPosition = GetMousePosition(); - MouseCursor.y -= OldPosition.y-NewPosition.y; - MouseCursor.x -= OldPosition.x-NewPosition.x; - if (MouseCursor.y >= 720 || MouseCursor.y <= 0) { - MouseCursor.y += OldPosition.y-NewPosition.y; - } - if (MouseCursor.x >= 1280 || MouseCursor.x <= 0) { - MouseCursor.x += OldPosition.x-NewPosition.x; - } - } - if (IsKeyPressed(KEY_ESCAPE)) { - EnableCursor(); - MouseCursorIn = false; - } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - DisableCursor(); - MouseCursorIn = true; - } - - EndOfPages = false; - BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(*MainCamera); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - /* Scores */ - int a = 0; - 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 */ - if(CheckCollisionRecs(MouseCursor, PrevPage) && Page > 0) { - DrawRectangleRec(PrevPage, RED); - BackButtonSelected = false; - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - if(Page > 0) { - Page--; - } - } - } else if(CheckCollisionRecs(MouseCursor, NextPage) && EndOfPages == false) { - DrawRectangleRec(NextPage, RED); - BackButtonSelected = false; - if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - if(EndOfPages == false) { - Page++; - } - } + while (LookingAtScores == true && GameGoing == true) { + /* 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 */ + if (MouseCursorIn == true) { + OldPosition = NewPosition; + NewPosition = GetMousePosition(); + MouseCursor.y -= OldPosition.y - NewPosition.y; + MouseCursor.x -= OldPosition.x - NewPosition.x; + if (MouseCursor.y >= 720 || MouseCursor.y <= 0) { + MouseCursor.y += OldPosition.y - NewPosition.y; + } + if (MouseCursor.x >= 1280 || MouseCursor.x <= 0) { + MouseCursor.x += OldPosition.x - NewPosition.x; + } + } + if (IsKeyPressed(KEY_ESCAPE)) { + EnableCursor(); + MouseCursorIn = false; + } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + DisableCursor(); + MouseCursorIn = true; + } + + EndOfPages = false; + BeginDrawing(); + ClearBackground(BLACK); + BeginMode2D(*MainCamera); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + /* Scores */ + int a = 0; + 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 */ + if (CheckCollisionRecs(MouseCursor, PrevPage) && Page > 0) { + DrawRectangleRec(PrevPage, RED); + BackButtonSelected = false; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if (Page > 0) { + Page--; } - switch(player_controls_pressed()) { - case CONTROLLER_RIGHT: - if(EndOfPages == false) { - ++Page; - } - break; - case CONTROLLER_LEFT: - if(Page > 0) { - --Page; - } - break; - case CONTROLLER_UP: - BackButtonSelected = true; - break; - case CONTROLLER_ACTIVATE: - if(BackButtonSelected == true) { - LookingAtScores = false; - } - break; - default: - break; + } + } else if (CheckCollisionRecs(MouseCursor, NextPage) && EndOfPages == false) { + DrawRectangleRec(NextPage, RED); + BackButtonSelected = false; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if (EndOfPages == false) { + Page++; } - DrawText("<--", 5, 720-50, 48, WHITE); - DrawText("-->", 1280-70, 720-50, 48, WHITE); - /* Exit Button */ - if(CheckCollisionRecs(MouseCursor, (Rectangle){0,0,42,120}) || BackButtonSelected == true) { - DrawRectangle(0, 0, 42, 120, RED); - BackButtonSelected = true; - if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - LookingAtScores = false; - } + } + } + switch (player_controls_pressed()) { + case CONTROLLER_RIGHT: + if (EndOfPages == false) { + ++Page; + } + break; + case CONTROLLER_LEFT: + if (Page > 0) { + --Page; + } + break; + case CONTROLLER_UP: + BackButtonSelected = true; + break; + case CONTROLLER_ACTIVATE: + if (BackButtonSelected == true) { + LookingAtScores = false; } - - DrawText("<", 0, 0, 128, WHITE); - /* Cursor */ - DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); - EndMode2D(); - EndDrawing(); - } + break; + default: + break; + } + DrawText("<--", 5, 720 - 50, 48, WHITE); + DrawText("-->", 1280 - 70, 720 - 50, 48, WHITE); + /* Exit Button */ + if (CheckCollisionRecs(MouseCursor, (Rectangle){0, 0, 42, 120}) || BackButtonSelected == true) { + DrawRectangle(0, 0, 42, 120, RED); + BackButtonSelected = true; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + LookingAtScores = false; + } + } + + DrawText("<", 0, 0, 128, WHITE); + /* Cursor */ + DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); + EndMode2D(); + EndDrawing(); + } clear_input_buffer(); - return; + return; } static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { - bool SettingsGoing = true; - int MusicBarY = 50; - int SoundBarY = 150; - - Rectangle MusicBar[10] = { - {50,MusicBarY,50,50}, - {105,MusicBarY,50,50}, - {160,MusicBarY,50,50}, - {215,MusicBarY,50,50}, - {270,MusicBarY,50,50}, - {325,MusicBarY,50,50}, - {380,MusicBarY,50,50}, - {435,MusicBarY,50,50}, - {490,MusicBarY,50,50}, - {545,MusicBarY,50,50}, - }; - - Rectangle SoundBar[10] = { - {50,SoundBarY,50,50}, - {105,SoundBarY,50,50}, - {160,SoundBarY,50,50}, - {215,SoundBarY,50,50}, - {270,SoundBarY,50,50}, - {325,SoundBarY,50,50}, - {380,SoundBarY,50,50}, - {435,SoundBarY,50,50}, - {490,SoundBarY,50,50}, - {545,SoundBarY,50,50}, - }; - - /* Back Selection */ - Rectangle BackButton = { - 0,0,42,120 - }; - - /* Screen Buttons */ - int ScreenButtonLength = 250; - Rectangle ScreenButtons[3] = { - {95, 242, ScreenButtonLength, 42}, - {95, 284, ScreenButtonLength, 42}, - {95, 326, ScreenButtonLength, 42} - }; - SetMousePosition(GetScreenWidth()/2, GetScreenHeight()/2); - /* Mouse */ - Rectangle MouseCursor = { - 1280/2.0f,720/2.0f,1,1 - }; - Vector2 OldPosition = GetMousePosition(); - Vector2 NewPosition = GetMousePosition(); - Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); - bool MouseCursorIn = true; + bool SettingsGoing = true; + int MusicBarY = 50; + int SoundBarY = 150; + + Rectangle MusicBar[10] = { + {50, MusicBarY, 50, 50}, + {105, MusicBarY, 50, 50}, + {160, MusicBarY, 50, 50}, + {215, MusicBarY, 50, 50}, + {270, MusicBarY, 50, 50}, + {325, MusicBarY, 50, 50}, + {380, MusicBarY, 50, 50}, + {435, MusicBarY, 50, 50}, + {490, MusicBarY, 50, 50}, + {545, MusicBarY, 50, 50}, + }; + + Rectangle SoundBar[10] = { + {50, SoundBarY, 50, 50}, + {105, SoundBarY, 50, 50}, + {160, SoundBarY, 50, 50}, + {215, SoundBarY, 50, 50}, + {270, SoundBarY, 50, 50}, + {325, SoundBarY, 50, 50}, + {380, SoundBarY, 50, 50}, + {435, SoundBarY, 50, 50}, + {490, SoundBarY, 50, 50}, + {545, SoundBarY, 50, 50}, + }; + + /* Back Selection */ + Rectangle BackButton = { + 0, 0, 42, 120}; + + /* Screen Buttons */ + int ScreenButtonLength = 250; + Rectangle ScreenButtons[3] = { + {95, 242, ScreenButtonLength, 42}, + {95, 284, ScreenButtonLength, 42}, + {95, 326, ScreenButtonLength, 42}}; + SetMousePosition(GetScreenWidth() / 2, GetScreenHeight() / 2); + /* Mouse */ + Rectangle MouseCursor = { + 1280 / 2.0f, 720 / 2.0f, 1, 1}; + Vector2 OldPosition = GetMousePosition(); + Vector2 NewPosition = GetMousePosition(); + Texture2D MouseCursorSprite = LoadTexture("resources/cursor.png"); + bool MouseCursorIn = true; bool OnBackButton = true; int Choice = 0; - 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}; - - /* Mouse */ - if (MouseCursorIn == true) { - OldPosition = NewPosition; - NewPosition = GetMousePosition(); - MouseCursor.y -= OldPosition.y-NewPosition.y; - MouseCursor.x -= OldPosition.x-NewPosition.x; - if (MouseCursor.y >= 720 || MouseCursor.y <= 0) { - MouseCursor.y += OldPosition.y-NewPosition.y; - } - if (MouseCursor.x >= 1280 || MouseCursor.x <= 0) { - MouseCursor.x += OldPosition.x-NewPosition.x; - } - } - - switch(player_controls_pressed()) { + + 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}; + + /* Mouse */ + if (MouseCursorIn == true) { + OldPosition = NewPosition; + NewPosition = GetMousePosition(); + MouseCursor.y -= OldPosition.y - NewPosition.y; + MouseCursor.x -= OldPosition.x - NewPosition.x; + if (MouseCursor.y >= 720 || MouseCursor.y <= 0) { + MouseCursor.y += OldPosition.y - NewPosition.y; + } + if (MouseCursor.x >= 1280 || MouseCursor.x <= 0) { + MouseCursor.x += OldPosition.x - NewPosition.x; + } + } + + switch (player_controls_pressed()) { case CONTROLLER_ACTIVATE: - if(Choice == 0) { + if (Choice == 0) { SettingsGoing = false; } break; @@ -312,7 +309,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { ++Choice; break; case CONTROLLER_LEFT: - switch(Choice) { + switch (Choice) { case 1: GlobalSettings.MusicVolume -= 10; break; @@ -325,7 +322,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { } break; case CONTROLLER_RIGHT: - switch(Choice) { + switch (Choice) { case 1: GlobalSettings.MusicVolume += 10; break; @@ -339,218 +336,207 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { break; } - /* Prevent out-of-bounds volume settings. */ - if(GlobalSettings.MusicVolume > 100) { + if (GlobalSettings.MusicVolume > 100) { GlobalSettings.MusicVolume = 100; - } else if(GlobalSettings.MusicVolume < 0) { + } else if (GlobalSettings.MusicVolume < 0) { GlobalSettings.MusicVolume = 0; } - if(GlobalSettings.SoundVolume > 100) { + if (GlobalSettings.SoundVolume > 100) { GlobalSettings.SoundVolume = 100; - } else if(GlobalSettings.SoundVolume < 0) { + } else if (GlobalSettings.SoundVolume < 0) { GlobalSettings.SoundVolume = 0; } - - - if (IsKeyPressed(KEY_ESCAPE)) { - EnableCursor(); - MouseCursorIn = false; - } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - DisableCursor(); - MouseCursorIn = true; - } - - Mix_VolumeMusic(GlobalSettings.MusicVolume); - BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(*MainCamera); - /* Draw Selection */ - OnBackButton = false; - Color MusicVolumeBarColor = DARKGRAY; - Color SoundVolumeBarColor = DARKGRAY; - switch(Choice) { - case 0: - OnBackButton = true; - break; - case 1: - MusicVolumeBarColor = YELLOW; - break; - case 2: - SoundVolumeBarColor = YELLOW; - break; - default: - break; + if (IsKeyPressed(KEY_ESCAPE)) { + EnableCursor(); + MouseCursorIn = false; + } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + DisableCursor(); + MouseCursorIn = true; + } + + Mix_VolumeMusic(GlobalSettings.MusicVolume); + BeginDrawing(); + ClearBackground(BLACK); + BeginMode2D(*MainCamera); + /* Draw Selection */ + OnBackButton = false; + Color MusicVolumeBarColor = DARKGRAY; + Color SoundVolumeBarColor = DARKGRAY; + switch (Choice) { + case 0: + OnBackButton = true; + break; + case 1: + MusicVolumeBarColor = YELLOW; + break; + case 2: + SoundVolumeBarColor = YELLOW; + break; + default: + break; + } + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + /* Back Button */ + if (CheckCollisionRecs(MouseCursor, BackButton) || OnBackButton == true) { + DrawRectangleRec(BackButton, RED); + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + SettingsGoing = false; + } + } + + /* Music */ + DrawText("Music Volume:", 50, 10, 42, WHITE); + DrawText("<", 0, 0, 128, WHITE); + DrawRectangle(45, MusicBarY - 5, 555, 60, MusicVolumeBarColor); + int i = 0; + for (i = 0; i < 10; i++) { + if (CheckCollisionRecs(MouseCursor, MusicBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + GlobalSettings.MusicVolume = i * 10; + } + if (i <= (GlobalSettings.MusicVolume / 10)) { + DrawRectangleRec(MusicBar[i], RED); + } + } + + /* Sound */ + DrawText("Sound Volume:", 50, 108, 42, WHITE); + DrawRectangle(45, SoundBarY - 5, 555, 60, SoundVolumeBarColor); + i = 0; + for (i = 0; i < 10; i++) { + if (CheckCollisionRecs(MouseCursor, SoundBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + play_audio(SOUND_BOUNCE); + GlobalSettings.SoundVolume = i * 10; + } + if (i <= (GlobalSettings.SoundVolume / 10)) { + DrawRectangleRec(SoundBar[i], RED); + } + } + /* Fullscreen */ + DrawRectangle(95, 243, 250, 125, DARKGRAY); + bool MouseHovering = false; + for (i = 0; i < 3; i++) { + if (CheckCollisionRecs(MouseCursor, ScreenButtons[i])) { + DrawRectangleRec(ScreenButtons[i], RED); + MouseHovering = true; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + GlobalSettings.Fullscreen = i; + set_screen_mode(); + } } - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - /* Back Button */ - if (CheckCollisionRecs(MouseCursor, BackButton) || OnBackButton == true) { - DrawRectangleRec(BackButton, RED); - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - SettingsGoing = false; - } - } - - /* Music */ - DrawText("Music Volume:", 50, 10, 42, WHITE); - DrawText("<", 0,0,128,WHITE); - DrawRectangle(45, MusicBarY-5, 555, 60, MusicVolumeBarColor); - int i = 0; - for(i = 0; i < 10; i++) { - if(CheckCollisionRecs(MouseCursor, MusicBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - GlobalSettings.MusicVolume = i*10; - } - if(i <= (GlobalSettings.MusicVolume/10)) { - DrawRectangleRec(MusicBar[i], RED); - } - } - - /* Sound */ - DrawText("Sound Volume:", 50, 108, 42, WHITE); - DrawRectangle(45, SoundBarY-5, 555, 60, SoundVolumeBarColor); - i = 0; - for(i = 0; i < 10; i++) { - if (CheckCollisionRecs(MouseCursor, SoundBar[i]) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - play_audio(SOUND_BOUNCE); - GlobalSettings.SoundVolume = i*10; - } - if(i <= (GlobalSettings.SoundVolume/10)) { - DrawRectangleRec(SoundBar[i], RED); - } - } - /* Fullscreen */ - DrawRectangle(95, 243, 250, 125, DARKGRAY); - bool MouseHovering = false; - for(i = 0; i < 3; i++) { - if(CheckCollisionRecs(MouseCursor, ScreenButtons[i])) { - DrawRectangleRec(ScreenButtons[i], RED); - MouseHovering = true; - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - GlobalSettings.Fullscreen = i; - set_screen_mode(); - } - } - } - if (MouseHovering == false) { - DrawRectangleRec(ScreenButtons[GlobalSettings.Fullscreen], GREEN); - } - DrawText("Screen Mode:", 50, 205, 42, WHITE); - DrawText("Windowed", 100, 242, 42, WHITE); - DrawText("Fullscreen", 100, 284, 42, WHITE); - DrawText("Borderless", 100, 326, 42, WHITE); - DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); - DrawRectangleRec(MouseCursor, WHITE); - EndMode2D(); - EndDrawing(); - } - char *SettingsDirectory = SDL_GetPrefPath("iotib", "Pong"); - char SettingsFilePath[8192]; - snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory); - FILE *SettingsFile; - /* Put save to file here. */ - if ((SettingsFile = fopen(SettingsFilePath, "w")) == NULL) { - fprintf(stderr, "Unable to create settings file.\n"); - exit(1); - } - fprintf(SettingsFile, "sound_volume %d\n", GlobalSettings.SoundVolume); - fprintf(SettingsFile, "music_volume %d\n", GlobalSettings.MusicVolume); - fprintf(SettingsFile, "fullscreen %d", GlobalSettings.Fullscreen); - fclose(SettingsFile); - return; + } + if (MouseHovering == false) { + DrawRectangleRec(ScreenButtons[GlobalSettings.Fullscreen], GREEN); + } + DrawText("Screen Mode:", 50, 205, 42, WHITE); + DrawText("Windowed", 100, 242, 42, WHITE); + DrawText("Fullscreen", 100, 284, 42, WHITE); + DrawText("Borderless", 100, 326, 42, WHITE); + DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE); + DrawRectangleRec(MouseCursor, WHITE); + EndMode2D(); + EndDrawing(); + } + char *SettingsDirectory = SDL_GetPrefPath("iotib", "Pong"); + char SettingsFilePath[8192]; + snprintf(SettingsFilePath, sizeof(SettingsFilePath), "%s/settings.txt", SettingsDirectory); + FILE *SettingsFile; + /* Put save to file here. */ + if ((SettingsFile = fopen(SettingsFilePath, "w")) == NULL) { + fprintf(stderr, "Unable to create settings file.\n"); + exit(1); + } + fprintf(SettingsFile, "sound_volume %d\n", GlobalSettings.SoundVolume); + fprintf(SettingsFile, "music_volume %d\n", GlobalSettings.MusicVolume); + fprintf(SettingsFile, "fullscreen %d", GlobalSettings.Fullscreen); + fclose(SettingsFile); + return; } int title_screen() { - /* Init Camera */ - Camera2D MainCamera; - MainCamera.offset = (Vector2){0,0}; - MainCamera.target = (Vector2){0,0}; - MainCamera.rotation = 0.0f; - - /* Load leaderboard */ - int NoScores = order_leaderboard(); - - bool TitleScreenGoing = true; - int Choice = 0; - - /* Selection */ - Rectangle Versus = { - 20, 150, 230, 48 - }; - Rectangle Marathon = { - 20, 200, 230, 48 - }; - Rectangle Settings = { - 20, 250, 230, 48 - }; - Rectangle Help = { - 20, 300, 230, 48 - }; - Rectangle Exit = { - 20, 350, 230, 48 - }; - Rectangle Mouse = { - 1280.0f/2, 720.0f/2, 10, 10 - }; - Rectangle AllScores = { - 595, 720-155, 400, 55 - }; - Rectangle *Selected; - Selected = &Versus; - - /* Music */ - Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav"); - Mix_PlayMusic(TitleMusic, -1); - Mix_VolumeMusic(GlobalSettings.MusicVolume); - - /* Mouse */ - Vector2 OldPosition = GetMousePosition(); - Vector2 NewPosition = GetMousePosition(); - Texture2D MouseCursor = LoadTexture("resources/cursor.png"); - bool MouseCursorIn = true; + /* Init Camera */ + Camera2D MainCamera; + MainCamera.offset = (Vector2){0, 0}; + MainCamera.target = (Vector2){0, 0}; + MainCamera.rotation = 0.0f; + + /* Load leaderboard */ + int NoScores = order_leaderboard(); + + bool TitleScreenGoing = true; + int Choice = 0; + + /* Selection */ + Rectangle Versus = { + 20, 150, 230, 48}; + Rectangle Marathon = { + 20, 200, 230, 48}; + Rectangle Settings = { + 20, 250, 230, 48}; + Rectangle Help = { + 20, 300, 230, 48}; + Rectangle Exit = { + 20, 350, 230, 48}; + Rectangle Mouse = { + 1280.0f / 2, 720.0f / 2, 10, 10}; + Rectangle AllScores = { + 595, 720 - 155, 400, 55}; + Rectangle *Selected; + Selected = &Versus; + + /* Music */ + Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav"); + Mix_PlayMusic(TitleMusic, -1); + Mix_VolumeMusic(GlobalSettings.MusicVolume); + + /* Mouse */ + Vector2 OldPosition = GetMousePosition(); + Vector2 NewPosition = GetMousePosition(); + Texture2D MouseCursor = LoadTexture("resources/cursor.png"); + bool MouseCursorIn = true; bool LeaderboardButtonSelected = false; - DisableCursor(); + DisableCursor(); bool OutOfScoreScreen = false; - while(TitleScreenGoing == true && GameGoing == true) { - if (WindowShouldClose()) { /* Quit Game if the window is closed. */ - GameGoing = false; - TitleScreenGoing = false; - } - + while (TitleScreenGoing == true && GameGoing == true) { + if (WindowShouldClose()) { /* Quit Game if the window is closed. */ + GameGoing = false; + TitleScreenGoing = false; + } + /* 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 */ - if (MouseCursorIn == true) { - OldPosition = NewPosition; - NewPosition = GetMousePosition(); - Mouse.y -= OldPosition.y-NewPosition.y; - Mouse.x -= OldPosition.x-NewPosition.x; - if (Mouse.y >= 720 || Mouse.y <= 0) { - Mouse.y += OldPosition.y-NewPosition.y; - } - if (Mouse.x >= 1280 || Mouse.x <= 0) { - Mouse.x += OldPosition.x-NewPosition.x; - } - } + MainCamera.zoom = GetScreenHeight() / 720.0f; + MainCamera.offset = (Vector2){GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}; + MainCamera.target = (Vector2){1280 / 2.0f, 720 / 2.0f}; + + /* Mouse */ + if (MouseCursorIn == true) { + OldPosition = NewPosition; + NewPosition = GetMousePosition(); + Mouse.y -= OldPosition.y - NewPosition.y; + Mouse.x -= OldPosition.x - NewPosition.x; + if (Mouse.y >= 720 || Mouse.y <= 0) { + Mouse.y += OldPosition.y - NewPosition.y; + } + if (Mouse.x >= 1280 || Mouse.x <= 0) { + Mouse.x += OldPosition.x - NewPosition.x; + } + } /* Check which button the mouse is hovering over. */ - if(CheckCollisionRecs(Mouse, Versus)) { + if (CheckCollisionRecs(Mouse, Versus)) { Choice = 0; - } else if(CheckCollisionRecs(Mouse, Marathon)) { + } else if (CheckCollisionRecs(Mouse, Marathon)) { Choice = 1; - } else if(CheckCollisionRecs(Mouse, Settings)) { + } else if (CheckCollisionRecs(Mouse, Settings)) { Choice = 2; - } else if(CheckCollisionRecs(Mouse, Help)) { + } else if (CheckCollisionRecs(Mouse, Help)) { Choice = 3; - } else if(CheckCollisionRecs(Mouse, Exit)) { + } else if (CheckCollisionRecs(Mouse, Exit)) { Choice = 4; - } else if(CheckCollisionRecs(Mouse, AllScores)) { + } else if (CheckCollisionRecs(Mouse, AllScores)) { LeaderboardButtonSelected = true; } - /* Gamepad & Keyboard */ switch (player_controls_pressed()) { case CONTROLLER_UP: @@ -560,64 +546,62 @@ int title_screen() { ++Choice; break; case CONTROLLER_RIGHT: - if(Choice == 1) { + if (Choice == 1) { LeaderboardButtonSelected = true; break; } break; case CONTROLLER_LEFT: - if(LeaderboardButtonSelected == true) { + if (LeaderboardButtonSelected == true) { LeaderboardButtonSelected = false; } break; default: break; } - /* Unselect the Leaderboard button if Marathon is no longer selected. */ - if(Choice != 1) { + if (Choice != 1) { LeaderboardButtonSelected = false; } /* Unlock, and locking cursor */ - if (IsKeyPressed(KEY_ESCAPE)) { - EnableCursor(); - MouseCursorIn = false; - } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && MouseCursorIn != true) { - DisableCursor(); + if (IsKeyPressed(KEY_ESCAPE)) { + EnableCursor(); + MouseCursorIn = false; + } else if (IsCursorOnScreen() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && MouseCursorIn != true) { + DisableCursor(); BeginDrawing(); EndDrawing(); - MouseCursorIn = true; - } + MouseCursorIn = true; + } - /* Highlight selected Menu Item */ - switch(Choice) { + switch (Choice) { case 0: - Selected = &Versus; + Selected = &Versus; break; case 1: - Selected = &Marathon; + Selected = &Marathon; break; case 2: - Selected = &Settings; + Selected = &Settings; break; case 3: - Selected = &Help; + Selected = &Help; break; case 4: - Selected = &Exit; + Selected = &Exit; break; default: break; - } + } /* Activate menu item */ - if( player_controls_pressed() == CONTROLLER_ACTIVATE ){ - switch(Choice) { + if (player_controls_pressed() == CONTROLLER_ACTIVATE) { + switch (Choice) { case 1: - if(OutOfScoreScreen == false && LeaderboardButtonSelected == false) { - return(Choice); + if (OutOfScoreScreen == false && LeaderboardButtonSelected == false) { + return (Choice); } break; case 2: @@ -638,63 +622,63 @@ int title_screen() { break; } } - + /* Make sure choice selector doesn't go off screen */ - if(Choice > 4) { + if (Choice > 4) { Choice = 4; - } else if(Choice < 0) { + } else if (Choice < 0) { Choice = 0; } - + OutOfScoreScreen = false; - BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(MainCamera); - DrawRectangleRec(Mouse, YELLOW); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - DrawRectangleRec(*Selected, RED); - DrawText("PONG", 0, 0, 128, WHITE); - DrawText("Versus", 20, 150, 48, WHITE); - DrawText("Marathon", 20, 200, 48, WHITE); - DrawText("Settings", 20, 250, 48, WHITE); - DrawText("Help", 20, 300, 48, WHITE); - DrawText("Exit", 20, 350, 48, WHITE); - DrawRectangleRec(Mouse, WHITE); - if(Choice == 1) { - DrawText("Leaderboard:", 600, 0, 48, WHITE); - if(NoScores == 1) { - goto skip; - } - char LeaderboardText[1024]; - 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); - } - } - } - skip: - if (Choice == 1) { - if(LeaderboardButtonSelected == true) { - DrawRectangleRec(AllScores, RED); - if(player_controls_pressed() == CONTROLLER_ACTIVATE) { - score_screen(&MainCamera); - OutOfScoreScreen = true; - } - } - DrawText("See All Scores...", 600, 720-150, 48, WHITE); + BeginDrawing(); + ClearBackground(BLACK); + BeginMode2D(MainCamera); + DrawRectangleRec(Mouse, YELLOW); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + DrawRectangleRec(*Selected, RED); + DrawText("PONG", 0, 0, 128, WHITE); + DrawText("Versus", 20, 150, 48, WHITE); + DrawText("Marathon", 20, 200, 48, WHITE); + DrawText("Settings", 20, 250, 48, WHITE); + DrawText("Help", 20, 300, 48, WHITE); + DrawText("Exit", 20, 350, 48, WHITE); + DrawRectangleRec(Mouse, WHITE); + if (Choice == 1) { + DrawText("Leaderboard:", 600, 0, 48, WHITE); + if (NoScores == 1) { + goto skip; + } + char LeaderboardText[1024]; + 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); + } + } + } + skip: + if (Choice == 1) { + if (LeaderboardButtonSelected == true) { + DrawRectangleRec(AllScores, RED); + if (player_controls_pressed() == CONTROLLER_ACTIVATE) { + score_screen(&MainCamera); + OutOfScoreScreen = true; } - DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE); - EndMode2D(); - DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN); - EndDrawing(); + } + DrawText("See All Scores...", 600, 720 - 150, 48, WHITE); + } + DrawTexture(MouseCursor, Mouse.x, Mouse.y, WHITE); + EndMode2D(); + DrawText(VersionString, GetScreenWidth() - 400, GetScreenHeight() - 32, 32, GREEN); + EndDrawing(); if (OutOfScoreScreen == true) { clear_input_buffer(); } - } + } printf("%d\n", Choice); - Mix_HaltMusic(); - Mix_FreeMusic(TitleMusic); - return Choice; + Mix_HaltMusic(); + Mix_FreeMusic(TitleMusic); + return Choice; } diff --git a/src/versus.c b/src/versus.c index fa92fe5..6f4e3d2 100644 --- a/src/versus.c +++ b/src/versus.c @@ -1,8 +1,9 @@ -#include "pong.h" #include <raylib.h> +#include "pong.h" + void versus_main() { - /* Init Player Variables */ + /* Init Player Variables */ struct Players Player; Player.Y = 0; Player.Score = 0; @@ -13,10 +14,10 @@ void versus_main() { 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; + Ball.X = 1280 / 2.0f; + Ball.Y = 720 / 2.0f; Ball.Direction = LEFT; Ball.Speed = 3.0f; Ball.Angle = 0.0f; @@ -25,8 +26,8 @@ void versus_main() { 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"); @@ -34,14 +35,14 @@ void versus_main() { 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 */ - Camera2D MainCamera; - MainCamera.target = (Vector2){0, 0}; - MainCamera.offset = (Vector2){0, 0}; - MainCamera.rotation = 0; + 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 */ + Camera2D MainCamera; + MainCamera.target = (Vector2){0, 0}; + MainCamera.offset = (Vector2){0, 0}; + MainCamera.rotation = 0; bool VersusGoing = true; Ball.NextTick = SDL_AtomicGet(&Ticks); @@ -51,14 +52,13 @@ void versus_main() { Vector2 MouseCurrentPosition = GetMousePosition(); bool MouseMoved = false; - while(VersusGoing == true && GameGoing == true) { - + while (VersusGoing == true && GameGoing == true) { if (WindowShouldClose()) { /* Quit Game if the window is closed. */ - GameGoing = false; - } - MainCamera.zoom = GetScreenHeight()/720.0f; - MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; + GameGoing = false; + } + MainCamera.zoom = GetScreenHeight() / 720.0f; + MainCamera.offset = (Vector2){GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}; + MainCamera.target = (Vector2){1280 / 2.0f, 720 / 2.0f}; /* Who won? */ if (Enemy.Score >= 5) { @@ -66,13 +66,13 @@ void versus_main() { Mix_FreeMusic(Background); play_audio(STOP_ALL_SOUNDS); play_audio(MUSIC_DEFEAT); - while(!IsKeyDown(KEY_SPACE)) { + while (!IsKeyDown(KEY_SPACE)) { BeginDrawing(); - EnableCursor(); - ClearBackground(BLACK); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - DrawText("You Lose.", 0, 0, 128, RED); - DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); + EnableCursor(); + ClearBackground(BLACK); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + DrawText("You Lose.", 0, 0, 128, RED); + DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); EndDrawing(); } return; @@ -81,13 +81,13 @@ void versus_main() { Mix_FreeMusic(Background); play_audio(STOP_ALL_SOUNDS); play_audio(MUSIC_VICTORY); - while(!IsKeyDown(KEY_SPACE)) { + while (!IsKeyDown(KEY_SPACE)) { BeginDrawing(); - EnableCursor(); - ClearBackground(BLACK); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - DrawText("You Win!", 0, 0, 128, BLUE); - DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); + EnableCursor(); + ClearBackground(BLACK); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + DrawText("You Win!", 0, 0, 128, BLUE); + DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); EndDrawing(); } return; @@ -100,26 +100,26 @@ void versus_main() { MouseMoved = true; } MouseLastPosition = MouseCurrentPosition; - if( player_controls() == CONTROLLER_UP ){ + if (player_controls() == CONTROLLER_UP) { Player.Y -= 20; - } else if ( player_controls() == CONTROLLER_DOWN ){ + } else if (player_controls() == CONTROLLER_DOWN) { Player.Y += 20; - } else if ( player_controls() == CONTROLLER_PAUSE ){ - Mix_PauseMusic(); - BeginDrawing(); - EndDrawing(); - VersusGoing = pause_screen(&MainCamera); - Mix_ResumeMusic(); - Ball.NextTick = SDL_AtomicGet(&Ticks)+1; - Enemy.NextTick = SDL_AtomicGet(&Ticks)+1; + } else if (player_controls() == CONTROLLER_PAUSE) { + Mix_PauseMusic(); + BeginDrawing(); + EndDrawing(); + VersusGoing = pause_screen(&MainCamera); + Mix_ResumeMusic(); + Ball.NextTick = SDL_AtomicGet(&Ticks) + 1; + Enemy.NextTick = SDL_AtomicGet(&Ticks) + 1; } else if (MouseMoved == true) { - Player.Y = GetMouseY()-PaddleSprite.height/2.0f; + Player.Y = GetMouseY() - PaddleSprite.height / 2.0f; DisableCursor(); } - - if(GetMouseY() < 0) { + + if (GetMouseY() < 0) { SetMousePosition(0, 0); - } else if(GetMouseY() > 720) { + } else if (GetMouseY() > 720) { SetMousePosition(0, 720); } @@ -131,7 +131,7 @@ void versus_main() { } enemy(&Enemy, Ball); - + /* Collision */ ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score); @@ -142,20 +142,20 @@ void versus_main() { snprintf(PlayerScore, 50, "Player: %d", Player.Score); snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score); BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(MainCamera); - DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); - DrawTexture(PaddleSprite, 0, Player.Y, WHITE); - DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE); - DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); - DrawText(PlayerScore, 0, 0, 32, BLUE); - DrawText(EnemyScore, 1130, 688, 32, RED); - EndMode2D(); + ClearBackground(BLACK); + BeginMode2D(MainCamera); + DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255}); + DrawTexture(PaddleSprite, 0, Player.Y, WHITE); + DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE); + DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); + DrawText(PlayerScore, 0, 0, 32, BLUE); + DrawText(EnemyScore, 1130, 688, 32, RED); + EndMode2D(); EndDrawing(); } Mix_HaltMusic(); Mix_FreeMusic(Background); play_audio(STOP_ALL_SOUNDS); - return; + return; } |