diff options
author | Charadon <dev@iotib.net> | 2022-06-08 15:19:26 -0400 |
---|---|---|
committer | Charadon <dev@iotib.net> | 2022-06-08 15:19:26 -0400 |
commit | 9433dcc1352d88d61c580f9ecd0e69a0c9d25ac7 (patch) | |
tree | 89c57531f2dd0a0b093e9a336fa061b460c1173a | |
parent | f0592f80f01b52652dfda9dec8096af4767e39d7 (diff) | |
download | Pong-C-9433dcc1352d88d61c580f9ecd0e69a0c9d25ac7.tar.gz |
Finished implementing settings.
-rw-r--r-- | src/main.c | 30 | ||||
-rw-r--r-- | src/pong.h | 1 | ||||
-rw-r--r-- | src/title.c | 82 |
3 files changed, 110 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c index 7d760b6..6a2ba9c 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,29 @@ SDL_atomic_t AudioInitializing; int AudioQueue[20]; +void set_screen_mode() { + switch(GlobalSettings.Fullscreen) { + case 1: + SetWindowSize(GetMonitorWidth(GetCurrentMonitor()), GetMonitorHeight(GetCurrentMonitor())); + SetWindowState(FLAG_FULLSCREEN_MODE); + break; + case 2: + SetWindowState(FLAG_WINDOW_TOPMOST); + SetWindowState(FLAG_WINDOW_UNDECORATED); + SetWindowPosition(0, 0); + SetWindowSize(GetMonitorWidth(GetCurrentMonitor()), GetMonitorHeight(GetCurrentMonitor())); + break; + default: + ClearWindowState(FLAG_WINDOW_TOPMOST); + ClearWindowState(FLAG_WINDOW_UNDECORATED); + ClearWindowState(FLAG_FULLSCREEN_MODE); + SetWindowSize(1280, 720); + SetWindowPosition(GetMonitorHeight(GetCurrentMonitor())/5, GetMonitorHeight(GetCurrentMonitor())/5); + break; + } + return; +} + int internal_clock() { const struct timespec Delay = { 0, 20000000 @@ -109,6 +132,11 @@ int main(int argc, char *argv[]) { SetWindowState(FLAG_WINDOW_RESIZABLE); SetWindowMinSize(1280, 720); + //Settings declaration to prevent undefined behavior. + GlobalSettings.Fullscreen = 0; + GlobalSettings.SoundVolume = 0; + GlobalSettings.MusicVolume = 0; + //SDL Init SDL_Init(SDL_INIT_AUDIO); @@ -163,6 +191,8 @@ int main(int argc, char *argv[]) { } } fclose(SettingsFile); + + set_screen_mode(); // Wait for audio to finish initializing. struct timespec a = { diff --git a/src/pong.h b/src/pong.h index 64e3f26..99e58e3 100644 --- a/src/pong.h +++ b/src/pong.h @@ -59,5 +59,6 @@ bool play_audio(int SoundEffect); int title_screen(); void versus_main(); void marathon_main(); +void set_screen_mode(); #endif diff --git a/src/title.c b/src/title.c index 6066797..04cf0ca 100644 --- a/src/title.c +++ b/src/title.c @@ -3,6 +3,7 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { bool SettingsGoing = true; int MusicBarY = 50; + int SoundBarY = 150; Rectangle MouseCursor = { 0,0,1,1 @@ -20,13 +21,48 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { {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) { - MouseCursor.x = GetMouseX(); - MouseCursor.y = GetMouseY(); + 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); @@ -41,12 +77,52 @@ static void settings(Camera2D *MainCamera, Mix_Music *TitleScreenMusic) { } // 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; } |