Skip to content

Commit

Permalink
Deinitializing without errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Matqyou committed Dec 8, 2024
1 parent caefb12 commit 927320d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 52 deletions.
55 changes: 35 additions & 20 deletions src/GameReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/GameReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class GameReference {
bool m_InitializedImages;
bool m_InitializedTTF;

static LoadedSound sQuitSound;

// Manipulating
void UpdateDimensions(int width, int height);
bool InitializeSDL();
Expand All @@ -44,6 +46,9 @@ class GameReference {
bool InitializeImages();
bool InitializeTTF();

// Waiting
void WaitForSoundToFinish();

public:
GameReference();
~GameReference();
Expand All @@ -65,7 +70,7 @@ class GameReference {

// Manipulating
bool Initialize();
void Deinitialize(bool keep_sound);
void Deinitialize(bool play_quit_sound);
void TestEnvironment();

// Listening
Expand Down
1 change: 0 additions & 1 deletion src/game/interface/LevelUpMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 4 additions & 13 deletions src/game/interface/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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:
Expand All @@ -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;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/game/interface/PauseMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 13 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <cstdlib>

GameReference *GameWindow;
LoadedSound sConnectedSound("ui.pitch.mid");
LoadedSound sDisconnectedSound("ui.pitch.low");

bool Initialize() {
srand(time(nullptr));
Expand Down Expand Up @@ -70,14 +72,13 @@ int main() {

MainMenu mainMenu(GameWindow);
mainMenu.Show();
bool Running = true;

PauseMenu pauseMenu(GameWindow->World(), &mainMenu);
LevelUpMenu* activeLevelUpMenu = nullptr;
std::queue<LevelUpMenu*> levelUpMenuQueue;
bool pauseMenuOpen = false;
bool levelUpMenuOpen = false;
while (Running) {
while (true) {
pauseMenuOpen = pauseMenu.Paused();

if (!levelUpMenuOpen) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -150,15 +147,15 @@ int main() {

NewChar->GiveWeapon(new WeaponGlock(nullptr));
NewChar->SetGameController(CurrentController);
Assets::Get()->GetSound("ui.pitch.high")->PlaySound();
sConnectedSound.GetSound()->PlaySound();
break;
}
case SDL_CONTROLLERDEVICEREMOVED: {
int InstanceID = CurrentEvent.cdevice.which;
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;
}
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 927320d

Please sign in to comment.