about summary refs log blame commit diff stats
path: root/src/marathon.c
blob: a04519a642ea0a3be0d023439a7a321c7e572fba (plain) (tree)
1
2
3
4
5
6
7
8
9
                 
 

                                    


                                
            
                                  


                                   


                                
                










                                                                               
                                     



                                                                                             
                       























                                                                                      













                                                                                                               

 


                      

                                                                  
                                                







                              
                                            





















                                                                                       




                                                                       
 

                                                              

                                                                                         

                  



























                                                                                        
 
                   
                                               


                                                 


                                  
                       

                                        
                                                                     




                                                                       
     

                              


                   
#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;
    for(int 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;
        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;
}

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;
	Ball.Direction = LEFT;
	Ball.Speed = 3.0f;
	Ball.Angle = 0.0f;
    Ball.NextTick = SDL_AtomicGet(&Ticks)+1;

    // Init Player
    struct Players Player;
    Player.Y = 0;
    Player.Direction = 0;
    Player.Score = 0;

    // Init sprites
    Texture2D PaddleSprite = LoadTexture("resources/paddle.png");
	Texture2D BallSprite = LoadTexture("resources/ball.png");

    char PlayerScore[50]; // Used later to display score on screen.

    // 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;
    while(MarathonGoing == true && GameGoing == true) {
        if (WindowShouldClose()) { //Quit Game if the window is closed.
            GameGoing = 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};

       	//Controls
        if(IsKeyDown(KEY_UP)) {
            Player.Y -= 10;
        } else if (IsKeyDown(KEY_DOWN)) {
            Player.Y += 10;
        } else if(IsKeyPressed(KEY_ESCAPE)) {
            EnableCursor();
        } else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) {
            Player.Y = GetMouseY()-PaddleSprite.height/2.0f;
            DisableCursor();
        }
        
        if(GetMouseY() < 0) {
            SetMousePosition(0, 0);
        } else if(GetMouseY() > 720) {
            SetMousePosition(0, 720);
        }

        //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 || IsKeyPressed(KEY_Q)) {
	    if (Player.Score > 0) {
            	leaderboard_record(Player.Score);
	    }
            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_FreeMusic(Background);
    EnableCursor();
    return;
}