From 996036bdf2653540f7e8d8be6886c7aa46293b17 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Mon, 18 Sep 2023 21:28:23 +0200 Subject: [PATCH 1/4] disable audio on lost focus --- client/eventsSDL/InputHandler.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/client/eventsSDL/InputHandler.cpp b/client/eventsSDL/InputHandler.cpp index 5dbb914c2..93524e142 100644 --- a/client/eventsSDL/InputHandler.cpp +++ b/client/eventsSDL/InputHandler.cpp @@ -24,6 +24,7 @@ #include "../CMT.h" #include "../CPlayerInterface.h" #include "../CGameInfo.h" +#include "../CMusicHandler.h" #include "../../lib/CConfigHandler.h" @@ -153,6 +154,18 @@ void InputHandler::preprocessEvent(const SDL_Event & ev) } #endif break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + if(settings["general"]["enableUiEnhancements"].Bool()) { + CCS->musich->setVolume(settings["general"]["music"].Integer()); + CCS->soundh->setVolume(settings["general"]["sound"].Integer()); + } + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + if(settings["general"]["enableUiEnhancements"].Bool()) { + CCS->musich->setVolume(0); + CCS->soundh->setVolume(0); + } + break; } return; } From bcf32984cebc64714352c44192d04045189e9863 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 19 Sep 2023 01:08:49 +0200 Subject: [PATCH 2/4] always sound at chat message --- client/adventureMap/CInGameConsole.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/client/adventureMap/CInGameConsole.cpp b/client/adventureMap/CInGameConsole.cpp index 8ee0145ae..28ef32a34 100644 --- a/client/adventureMap/CInGameConsole.cpp +++ b/client/adventureMap/CInGameConsole.cpp @@ -105,7 +105,13 @@ void CInGameConsole::print(const std::string & txt) } GH.windows().totalRedraw(); // FIXME: ingame console has no parent widget set - CCS->soundh->playSound(AudioPath::builtin("CHAT")); + + int volume = CCS->soundh->getVolume(); + if(volume == 0) + CCS->soundh->setVolume(settings["general"]["sound"].Integer()); + int handle = CCS->soundh->playSound(AudioPath::builtin("CHAT")); + if(volume == 0) + CCS->soundh->setCallback(handle, [&]() { CCS->soundh->setVolume(0); }); } bool CInGameConsole::captureThisKey(EShortcut key) From 6ae09d845874e9c93926a92a5c17642bcdd874b6 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:52:32 +0200 Subject: [PATCH 3/4] newday --- client/adventureMap/CInfoBar.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/client/adventureMap/CInfoBar.cpp b/client/adventureMap/CInfoBar.cpp index ef23a86b9..fbdd164ca 100644 --- a/client/adventureMap/CInfoBar.cpp +++ b/client/adventureMap/CInfoBar.cpp @@ -228,14 +228,22 @@ CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const std::vectorsoundh->getVolume(); + int handle = -1; + if(volume == 0) + CCS->soundh->setVolume(settings["general"]["sound"].Integer()); + if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week - CCS->soundh->playSound(soundBase::newDay); + handle = CCS->soundh->playSound(soundBase::newDay); else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month - CCS->soundh->playSound(soundBase::newWeek); + handle = CCS->soundh->playSound(soundBase::newWeek); else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month - CCS->soundh->playSound(soundBase::newMonth); + handle = CCS->soundh->playSound(soundBase::newMonth); else - CCS->soundh->playSound(soundBase::newDay); + handle = CCS->soundh->playSound(soundBase::newDay); + + if(volume == 0) + CCS->soundh->setCallback(handle, [&]() { CCS->soundh->setVolume(0); }); } void CInfoBar::reset() From a2174dc83fdf55d535aa573caa89e77d02945126 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:20:16 +0200 Subject: [PATCH 4/4] fix window open while sound playing --- client/adventureMap/CInGameConsole.cpp | 3 ++- client/adventureMap/CInfoBar.cpp | 3 ++- client/render/IScreenHandler.h | 3 +++ client/renderSDL/ScreenHandler.cpp | 6 ++++++ client/renderSDL/ScreenHandler.h | 3 +++ 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/client/adventureMap/CInGameConsole.cpp b/client/adventureMap/CInGameConsole.cpp index 28ef32a34..555923631 100644 --- a/client/adventureMap/CInGameConsole.cpp +++ b/client/adventureMap/CInGameConsole.cpp @@ -22,6 +22,7 @@ #include "../gui/TextAlignment.h" #include "../render/Colors.h" #include "../render/Canvas.h" +#include "../render/IScreenHandler.h" #include "../adventureMap/AdventureMapInterface.h" #include "../windows/CMessage.h" @@ -111,7 +112,7 @@ void CInGameConsole::print(const std::string & txt) CCS->soundh->setVolume(settings["general"]["sound"].Integer()); int handle = CCS->soundh->playSound(AudioPath::builtin("CHAT")); if(volume == 0) - CCS->soundh->setCallback(handle, [&]() { CCS->soundh->setVolume(0); }); + CCS->soundh->setCallback(handle, [&]() { if(!GH.screenHandler().hasFocus()) CCS->soundh->setVolume(0); }); } bool CInGameConsole::captureThisKey(EShortcut key) diff --git a/client/adventureMap/CInfoBar.cpp b/client/adventureMap/CInfoBar.cpp index fbdd164ca..bdcf76766 100644 --- a/client/adventureMap/CInfoBar.cpp +++ b/client/adventureMap/CInfoBar.cpp @@ -25,6 +25,7 @@ #include "../PlayerLocalState.h" #include "../gui/CGuiHandler.h" #include "../gui/WindowHandler.h" +#include "../render/IScreenHandler.h" #include "../../CCallback.h" #include "../../lib/CConfigHandler.h" @@ -243,7 +244,7 @@ void CInfoBar::playNewDaySound() handle = CCS->soundh->playSound(soundBase::newDay); if(volume == 0) - CCS->soundh->setCallback(handle, [&]() { CCS->soundh->setVolume(0); }); + CCS->soundh->setCallback(handle, [&]() { if(!GH.screenHandler().hasFocus()) CCS->soundh->setVolume(0); }); } void CInfoBar::reset() diff --git a/client/render/IScreenHandler.h b/client/render/IScreenHandler.h index 51cc3dda2..49e5cd95e 100644 --- a/client/render/IScreenHandler.h +++ b/client/render/IScreenHandler.h @@ -40,4 +40,7 @@ public: /// Dimensions of render output virtual Point getRenderResolution() const = 0; + + /// Window has focus + virtual bool hasFocus() = 0; }; diff --git a/client/renderSDL/ScreenHandler.cpp b/client/renderSDL/ScreenHandler.cpp index 623289074..a513454b8 100644 --- a/client/renderSDL/ScreenHandler.cpp +++ b/client/renderSDL/ScreenHandler.cpp @@ -565,3 +565,9 @@ std::vector ScreenHandler::getSupportedResolutions( int displayIndex) con return result; } + +bool ScreenHandler::hasFocus() +{ + ui32 flags = SDL_GetWindowFlags(mainWindow); + return flags & SDL_WINDOW_INPUT_FOCUS; +} \ No newline at end of file diff --git a/client/renderSDL/ScreenHandler.h b/client/renderSDL/ScreenHandler.h index c7a057144..fb3d6a334 100644 --- a/client/renderSDL/ScreenHandler.h +++ b/client/renderSDL/ScreenHandler.h @@ -86,6 +86,9 @@ public: /// Dimensions of render output, usually same as window size except for high-DPI screens on macOS / iOS Point getRenderResolution() const final; + /// Window has focus + bool hasFocus() final; + std::vector getSupportedResolutions() const final; std::vector getSupportedResolutions(int displayIndex) const; std::tuple getSupportedScalingRange() const final;