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

                 


                                                                         
                        
 


                             
 











                              


























                                                              
                                                       


                                                     



                                                    






                                                              













                                                                                                              










                                                                                                              
                         

















                                                                                  


                     



                                                                                                   
                             







                                                                           



                         


 

                    



                                       
 
                                 


                   



















                          
                   


                                                               
                                  

                                                






                                                                       





                                                          

                                         


                                                         
                                                          

                                         
             

                                                         


                                                          





                                                          
                            

             









                                                         
                        
                                                                                           

                     


                              
 
#include "pong.h"

static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) {
    bool SettingsGoing = true;
    int MusicBarY = 50;
    int SoundBarY = 150;

    Rectangle MouseCursor = {
        0,0,1,1
    };

    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);
    while(SettingsGoing == true && GameGoing == true) {
        MainCamera->zoom = GetScreenHeight()/720.0f;
        MouseCursor.x = GetMouseX()/MainCamera->zoom;
        MouseCursor.y = GetMouseY()/MainCamera->zoom;
        Mix_VolumeMusic(GlobalSettings.MusicVolume);
        BeginDrawing();
        ClearBackground(BLACK);
        BeginMode2D(*MainCamera);
            // Back Button
            if (CheckCollisionRecs(MouseCursor, BackButton)) {
                DrawRectangleRec(BackButton, RED);
                if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                    SettingsGoing = false;
                }
            }

            // Music
            DrawText("Music Volume:", 50, 10, 42, WHITE);
            DrawText("<", 0,0,128,WHITE);
            for(int 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);
            DrawText("<", 0,0,128,WHITE);
            for(int 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
            bool MouseHovering = false;
            for(int 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);
        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;
}

static void help_text() {

}

int title_screen() {
    // Init Camera
    Camera2D MainCamera;
    MainCamera.offset = (Vector2){0,0};
    MainCamera.target = (Vector2){0,0};
    MainCamera.rotation = 0.0f;

    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 = {
        0, 0, 10, 10
    };
    Rectangle *Selected;
    Selected = &Versus;
    EnableCursor();

    //Music
    Mix_Music *TitleMusic = Mix_LoadMUS("resources/title.wav");
    Mix_PlayMusic(TitleMusic, -1);
    Mix_VolumeMusic(GlobalSettings.MusicVolume);

    while(TitleScreenGoing == true && GameGoing == true) {
        
        if (WindowShouldClose()) { //Quit Game if the window is closed.
            GameGoing = false;
            TitleScreenGoing = false;
        }

        MainCamera.zoom = GetScreenHeight()/720.0f;
        Mouse.x = GetMouseX()/MainCamera.zoom;
        Mouse.y = GetMouseY()/MainCamera.zoom;
        if (CheckCollisionRecs(Mouse, Versus)) {
            Selected = &Versus;
            if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                Choice = 0;
                TitleScreenGoing = false;
            }
        } else if (CheckCollisionRecs(Mouse, Marathon)) {
            Selected = &Marathon;
            if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                Choice = 1;
                TitleScreenGoing = false;
            }
        } else if (CheckCollisionRecs(Mouse, Settings)) {
            Selected = &Settings;
            if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                settings(&MainCamera, TitleMusic);
            }
        } else if (CheckCollisionRecs(Mouse, Help)) {
            Selected = &Help;
        } else if (CheckCollisionRecs(Mouse, Exit)) {
            Selected = &Exit;
            if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                GameGoing = false;
                Choice = -1;
            }
        }
        BeginDrawing();
            ClearBackground(BLACK);
            BeginMode2D(MainCamera);
                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);
            EndMode2D();
            DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN);
        EndDrawing();
    }
    Mix_HaltMusic();
    Mix_FreeMusic(TitleMusic);
    return Choice;
}