diff --git a/src/GameReference.cpp b/src/GameReference.cpp index b98f23a..d598e14 100644 --- a/src/GameReference.cpp +++ b/src/GameReference.cpp @@ -8,6 +8,8 @@ #include "game/entities/item/weapons/EntityGuns.h" #include "game/entities/Projectile.h" +LoadedSound GameReference::sQuitSound("ui.quit"); + GameReference::GameReference() { m_Window = nullptr; m_Renderer = nullptr; @@ -101,6 +103,12 @@ bool GameReference::InitializeTTF() { return true; } +void GameReference::WaitForSoundToFinish() { + while (Mix_Playing(-1)) { + SDL_Delay(10); + } +} + bool GameReference::Initialize() { SDL_version Version; SDL_GetVersion(&Version); @@ -155,10 +163,10 @@ bool GameReference::Initialize() { return true; } -void GameReference::Deinitialize(bool keep_sound) { +void GameReference::Deinitialize(bool play_quit_sound) { std::cout << FStringColors( - "&8---------------------------- &fDeinitializing(keep_sound = %s) &8----------------------------", - keep_sound ? "true" : "false") << std::endl; + "&8---------------------------- &fDeinitializing(play_quit_sound = %s) &8----------------------------", + play_quit_sound ? "true" : "false") << std::endl; Assets::PauseMusic(); delete m_GameWorld; m_GameWorld = nullptr; @@ -191,24 +199,31 @@ void GameReference::Deinitialize(bool keep_sound) { std::cout << FStringColors("[Game] &8Closed Images") << std::endl; } - if (!keep_sound) { // TODO: Check this out -_- looks very sus - Assets::deinitialize(); - if (m_InitializedAudio) { - m_InitializedAudio = false; - Mix_CloseAudio(); - std::cout << FStringColors("[Game] &8Closed Audio") << std::endl; - } - if (m_InitializedMix) { - m_InitializedMix = false; - Mix_Quit(); - std::cout << FStringColors("[Game] &8Closed Mixer") << std::endl; - } - if (m_InitializedSDL) { - m_InitializedSDL = false; - SDL_Quit(); - std::cout << FStringColors("[Game] &8Closed SDL") << std::endl; - } + if (play_quit_sound) { // TODO: Check this out -_- looks very sus + sQuitSound.GetSound()->PlaySound(); + WaitForSoundToFinish(); + delete this; + return; + } + + Assets::deinitialize(); + if (m_InitializedAudio) { + m_InitializedAudio = false; + Mix_CloseAudio(); + std::cout << FStringColors("[Game] &8Closed Audio") << std::endl; } + if (m_InitializedMix) { + m_InitializedMix = false; + Mix_Quit(); + std::cout << FStringColors("[Game] &8Closed Mixer") << std::endl; + } + if (m_InitializedSDL) { + m_InitializedSDL = false; + SDL_Quit(); + std::cout << FStringColors("[Game] &8Closed SDL") << std::endl; + } + + exit(0); } void GameReference::Event(const SDL_Event& event) { diff --git a/src/GameReference.h b/src/GameReference.h index 19574bc..d7e3ca6 100644 --- a/src/GameReference.h +++ b/src/GameReference.h @@ -36,6 +36,8 @@ class GameReference { bool m_InitializedImages; bool m_InitializedTTF; + static LoadedSound sQuitSound; + // Manipulating void UpdateDimensions(int width, int height); bool InitializeSDL(); @@ -44,6 +46,9 @@ class GameReference { bool InitializeImages(); bool InitializeTTF(); + // Waiting + void WaitForSoundToFinish(); + public: GameReference(); ~GameReference(); @@ -65,7 +70,7 @@ class GameReference { // Manipulating bool Initialize(); - void Deinitialize(bool keep_sound); + void Deinitialize(bool play_quit_sound); void TestEnvironment(); // Listening diff --git a/src/game/interface/LevelUpMenu.cpp b/src/game/interface/LevelUpMenu.cpp index f168f67..1403818 100644 --- a/src/game/interface/LevelUpMenu.cpp +++ b/src/game/interface/LevelUpMenu.cpp @@ -76,7 +76,6 @@ void LevelUpMenu::HandleEvent(const SDL_Event &event) switch (event.type) { case SDL_QUIT: - Assets::Get()->GetSound("ui.quit")->PlaySound(); m_GameWindow->Deinitialize(true); while (Mix_Playing(-1)) {} // wait until last sound is done playing delete m_GameWindow; diff --git a/src/game/interface/MainMenu.cpp b/src/game/interface/MainMenu.cpp index 63839bc..78a5f3b 100644 --- a/src/game/interface/MainMenu.cpp +++ b/src/game/interface/MainMenu.cpp @@ -50,7 +50,6 @@ void MainMenu::HandleEvent(const SDL_Event &event, bool &running, bool &menuOpen switch (event.type) { case SDL_QUIT: - Assets::Get()->GetSound("ui.quit")->PlaySound(); m_GameWindow->Deinitialize(true); // close everything except sound while (Mix_Playing(-1)) {} // wait until last sound is done playing delete m_GameWindow; @@ -77,7 +76,6 @@ void MainMenu::HandleEvent(const SDL_Event &event, bool &running, bool &menuOpen if (x >= m_ExitButtonRect.x && x < m_ExitButtonRect.x + m_ExitButtonRect.w && y >= m_ExitButtonRect.y && y < m_ExitButtonRect.y + m_ExitButtonRect.h) { - Assets::Get()->GetSound("ui.quit")->PlaySound(); m_GameWindow->Deinitialize(true); while (Mix_Playing(-1)) {} // wait until last sound is done playing delete m_GameWindow; @@ -95,18 +93,11 @@ void MainMenu::HandleEvent(const SDL_Event &event, bool &running, bool &menuOpen if ((x >= m_PlayButtonRect.x && x < m_PlayButtonRect.x + m_PlayButtonRect.w && y >= m_PlayButtonRect.y && y < m_PlayButtonRect.y + m_PlayButtonRect.h) || (x >= m_ExitButtonRect.x && x < m_ExitButtonRect.x + m_ExitButtonRect.w && - y >= m_ExitButtonRect.y && y < m_ExitButtonRect.y + m_ExitButtonRect.h)) - { + y >= m_ExitButtonRect.y && y < m_ExitButtonRect.y + m_ExitButtonRect.h)) { hovering = true; } - if (hovering) - { - SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND)); - } - else - { - SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW)); - } + if (hovering) { SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND)); } + else { SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW)); } } break; case SDL_WINDOWEVENT: @@ -116,8 +107,8 @@ void MainMenu::HandleEvent(const SDL_Event &event, bool &running, bool &menuOpen m_ExitButtonRect = {int(m_GameWindow->GetWidth2()) - 180, int(m_GameWindow->GetHeight2()) + 121, 360, 80}; m_GameWindow->Render()->Clear(); m_GameWindow->Render()->UpdateWindow(); + break; } - break; } } diff --git a/src/game/interface/PauseMenu.cpp b/src/game/interface/PauseMenu.cpp index 2cb6988..63d0525 100644 --- a/src/game/interface/PauseMenu.cpp +++ b/src/game/interface/PauseMenu.cpp @@ -32,7 +32,6 @@ void PauseMenu::HandleEvent(const SDL_Event &event) switch (event.type) { case SDL_QUIT: - Assets::Get()->GetSound("ui.quit")->PlaySound(); m_GameWindow->Deinitialize(true); while (Mix_Playing(-1)) {} // wait until last sound is done playing delete m_GameWindow; diff --git a/src/main.cpp b/src/main.cpp index 750f4f6..90300af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,8 @@ #include GameReference *GameWindow; +LoadedSound sConnectedSound("ui.pitch.mid"); +LoadedSound sDisconnectedSound("ui.pitch.low"); bool Initialize() { srand(time(nullptr)); @@ -70,14 +72,13 @@ int main() { MainMenu mainMenu(GameWindow); mainMenu.Show(); - bool Running = true; PauseMenu pauseMenu(GameWindow->World(), &mainMenu); LevelUpMenu* activeLevelUpMenu = nullptr; std::queue levelUpMenuQueue; bool pauseMenuOpen = false; bool levelUpMenuOpen = false; - while (Running) { + while (true) { pauseMenuOpen = pauseMenu.Paused(); if (!levelUpMenuOpen) { @@ -117,12 +118,8 @@ int main() { switch (CurrentEvent.type) { case SDL_QUIT: - Assets::Get()->GetSound("ui.quit")->PlaySound(); - GameWindow->Deinitialize(true); // close everything except sound - - while (Mix_Playing(-1)) {} // wait until last sound is done playing - delete GameWindow; - return 0; + GameWindow->Deinitialize(true); + return 0; // This should happen in every quit scenario, but menus didn't think about that case SDL_KEYDOWN: { SDL_Scancode ScancodeKey = CurrentEvent.key.keysym.scancode; @@ -150,7 +147,7 @@ int main() { NewChar->GiveWeapon(new WeaponGlock(nullptr)); NewChar->SetGameController(CurrentController); - Assets::Get()->GetSound("ui.pitch.high")->PlaySound(); + sConnectedSound.GetSound()->PlaySound(); break; } case SDL_CONTROLLERDEVICEREMOVED: { @@ -158,7 +155,7 @@ int main() { GameController* DeletedController = GameWindow->Controllers()->CloseController(InstanceID); GameWindow->World()->DestroyPlayerByController(DeletedController); GameWindow->World()->DestroyCharacterByController(DeletedController); - Assets::Get()->GetSound("ui.pitch.low")->PlaySound(); + sDisconnectedSound.GetSound()->PlaySound(); break; } } @@ -191,12 +188,12 @@ int main() { Render->UpdateWindow(); if (GameWindow->World()->GetDelay() && (levelUpMenuOpen)) { -// SDL_Delay(1000); // Delay for 1000 milliseconds (1 second) - SDL_Event event; - while (SDL_PollEvent(&event)) { - // Discard events - } - +//// SDL_Delay(1000); // Delay for 1000 milliseconds (1 second) +// SDL_Event event; +// while (SDL_PollEvent(&event)) { +// // Discard events +// } +// GameWindow->World()->SetDelay(false); // Reset the delay flag after the delay }