From 099b97b934c73377aaf95f4c43ab5b7a94d87827 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+AgentXLP@users.noreply.github.com> Date: Sun, 30 Jun 2024 11:42:37 -0400 Subject: [PATCH] Add mute window when unfocused option Co-Authored-By: Tulpen <63363329+tulpenkiste@users.noreply.github.com> --- lang/Czech.ini | 1 + lang/Dutch.ini | 1 + lang/English.ini | 1 + lang/French.ini | 1 + lang/German.ini | 1 + lang/Italian.ini | 1 + lang/Polish.ini | 1 + lang/Portuguese.ini | 1 + lang/Russian.ini | 1 + lang/Spanish.ini | 1 + src/pc/configfile.c | 6 ++++-- src/pc/configfile.h | 1 + src/pc/controller/controller_sdl2.c | 2 +- src/pc/djui/djui_panel_sound.c | 1 + src/pc/gfx/gfx_dummy.c | 17 +++++++++++------ src/pc/gfx/gfx_dxgi.cpp | 7 ++++++- src/pc/gfx/gfx_sdl1.c | 7 ++++++- src/pc/gfx/gfx_sdl2.c | 7 ++++++- src/pc/gfx/gfx_window_manager_api.h | 1 + src/pc/pc_main.c | 7 ++++--- 20 files changed, 51 insertions(+), 15 deletions(-) diff --git a/lang/Czech.ini b/lang/Czech.ini index 3d2425b7..cc5281ca 100644 --- a/lang/Czech.ini +++ b/lang/Czech.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Hlasitost písničky" SFX_VOLUME = "Hlasitost zvuků" ENV_VOLUME = "Hlasitost prostředí" FADEOUT = "Ztišit daleké zvuky" +MUTE_FOCUS_LOSS = "Ztlumit, když je okno na pozadí" [LANGUAGE] LANGUAGE = "JAZYK" diff --git a/lang/Dutch.ini b/lang/Dutch.ini index d5693691..ec15f3a8 100644 --- a/lang/Dutch.ini +++ b/lang/Dutch.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Muziek Geluid" SFX_VOLUME = "Sfx Geluid" ENV_VOLUME = "Env Geluid" FADEOUT = "Vervaagd Geluid in de Verte" +MUTE_FOCUS_LOSS = "Dempen wanneer venster niet meer scherp is" [LANGUAGE] LANGUAGE = "TAAL" diff --git a/lang/English.ini b/lang/English.ini index cc8d232e..5cbb9df6 100644 --- a/lang/English.ini +++ b/lang/English.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Music Volume" SFX_VOLUME = "Sound Effects Volume" ENV_VOLUME = "Environment Volume" FADEOUT = "Fade Out Distant Sounds" +MUTE_FOCUS_LOSS = "Mute When Window Unfocused" [LANGUAGE] LANGUAGE = "LANGUAGE" diff --git a/lang/French.ini b/lang/French.ini index 4987cd69..55a1fc71 100644 --- a/lang/French.ini +++ b/lang/French.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Volume de la musique" SFX_VOLUME = "Volume des effets sonores" ENV_VOLUME = "Volume de l'environnement" FADEOUT = "Disparition des sons distants" +MUTE_FOCUS_LOSS = "Muet en arrière plan" [LANGUAGE] LANGUAGE = "LANGUE" diff --git a/lang/German.ini b/lang/German.ini index 79d656b8..52af0e1c 100644 --- a/lang/German.ini +++ b/lang/German.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Musiklautstärke" SFX_VOLUME = "Effektlautstärke" ENV_VOLUME = "Umgebungslautstärke" FADEOUT = "Ausblenden" +MUTE_FOCUS_LOSS = "Stumm wenn nicht fokussiert" [LANGUAGE] LANGUAGE = "SPRACHE" diff --git a/lang/Italian.ini b/lang/Italian.ini index 1684789e..0894572d 100644 --- a/lang/Italian.ini +++ b/lang/Italian.ini @@ -360,6 +360,7 @@ MUSIC_VOLUME = "Musica" SFX_VOLUME = "Effetti sonori" ENV_VOLUME = "Ambiente" FADEOUT = "Suoni lontani in dissolvenza" +MUTE_FOCUS_LOSS = "Disattiva l'audio quando la finestra non è a fuoco" [LANGUAGE] LANGUAGE = "LINGUA" diff --git a/lang/Polish.ini b/lang/Polish.ini index 518184d1..d15da618 100644 --- a/lang/Polish.ini +++ b/lang/Polish.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Głośność Muzyki" SFX_VOLUME = "Głośność Efektów" ENV_VOLUME = "Głośność Środowiska" FADEOUT = "Zanikanie Odległych Dźwięków" +MUTE_FOCUS_LOSS = "Wycisz dźwięk, gdy okno traci fokus" [LANGUAGE] LANGUAGE = "JĘZYK" diff --git a/lang/Portuguese.ini b/lang/Portuguese.ini index 68143215..f8f42fcc 100644 --- a/lang/Portuguese.ini +++ b/lang/Portuguese.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Volume da música" SFX_VOLUME = "Volume dos efeitos" ENV_VOLUME = "Volume ambiente" FADEOUT = "Desvanecer sons distantes" +MUTE_FOCUS_LOSS = "Silenciar quando a janela estiver desfocada" [LANGUAGE] LANGUAGE = "IDIOMA" diff --git a/lang/Russian.ini b/lang/Russian.ini index 453735f1..5fbf4ced 100644 --- a/lang/Russian.ini +++ b/lang/Russian.ini @@ -361,6 +361,7 @@ MUSIC_VOLUME = "Громкость музыки" SFX_VOLUME = "Громкость звуков" ENV_VOLUME = "Объёмное звучание" FADEOUT = "Затухание звуков на расстоянии" +MUTE_FOCUS_LOSS = "Выключить звук когда окно не выбрано" [LANGUAGE] LANGUAGE = "LANGUAGE" diff --git a/lang/Spanish.ini b/lang/Spanish.ini index 5b8cca5a..441c7d37 100644 --- a/lang/Spanish.ini +++ b/lang/Spanish.ini @@ -362,6 +362,7 @@ MUSIC_VOLUME = "Volumen de Música" SFX_VOLUME = "Volumen de Efectos de Sonido" ENV_VOLUME = "Volumen de Entorno" FADEOUT = "Disminuir sonidos lejanos" +MUTE_FOCUS_LOSS = "Muda sonido cuando ventana esta desenfocado" [LANGUAGE] LANGUAGE = "IDIOMA" diff --git a/src/pc/configfile.c b/src/pc/configfile.c index 8d5f7279..767f9c30 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -71,7 +71,7 @@ ConfigWindow configWindow = { .y = WAPI_WIN_CENTERPOS, .w = DESIRED_SCREEN_WIDTH, .h = DESIRED_SCREEN_HEIGHT, - .vsync = 0, + .vsync = 1, .reset = false, .fullscreen = false, .exiting_fullscreen = false, @@ -82,7 +82,7 @@ ConfigWindow configWindow = { // display settings unsigned int configFiltering = 2; // 0 = Nearest, 1 = Bilinear, 2 = Trilinear bool configShowFPS = false; -bool configUncappedFramerate = false; +bool configUncappedFramerate = true; unsigned int configFrameLimit = 60; unsigned int configInterpolationMode = 1; unsigned int configDrawDistance = 4; @@ -92,6 +92,7 @@ unsigned int configMusicVolume = MAX_VOLUME; unsigned int configSfxVolume = MAX_VOLUME; unsigned int configEnvVolume = MAX_VOLUME; bool configFadeoutDistantSounds = false; +bool configMuteFocusLoss = false; // control binds unsigned int configKeyA[MAX_BINDS] = { 0x0026, 0x1000, 0x1103 }; unsigned int configKeyB[MAX_BINDS] = { 0x0033, 0x1001, 0x1101 }; @@ -207,6 +208,7 @@ static const struct ConfigOption options[] = { {.name = "sfx_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configSfxVolume}, {.name = "env_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configEnvVolume}, {.name = "fade_distant_sounds", .type = CONFIG_TYPE_BOOL, .boolValue = &configFadeoutDistantSounds}, + {.name = "mute_focus_loss", .type = CONFIG_TYPE_BOOL, .boolValue = &configMuteFocusLoss}, // control binds {.name = "key_a", .type = CONFIG_TYPE_BIND, .uintValue = configKeyA}, {.name = "key_b", .type = CONFIG_TYPE_BIND, .uintValue = configKeyB}, diff --git a/src/pc/configfile.h b/src/pc/configfile.h index 4a328bcd..8d8e8c46 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -43,6 +43,7 @@ extern unsigned int configMusicVolume; extern unsigned int configSfxVolume; extern unsigned int configEnvVolume; extern bool configFadeoutDistantSounds; +extern bool configMuteFocusLoss; // control binds extern unsigned int configKeyA[MAX_BINDS]; extern unsigned int configKeyB[MAX_BINDS]; diff --git a/src/pc/controller/controller_sdl2.c b/src/pc/controller/controller_sdl2.c index dcd64d2f..6f99e3ec 100644 --- a/src/pc/controller/controller_sdl2.c +++ b/src/pc/controller/controller_sdl2.c @@ -173,7 +173,7 @@ static void controller_sdl_read(OSContPad *pad) { return; } - if ((newcam_mouse == 1 || gFirstPersonCamera.enabled || gDjuiHudLockMouse) && gMenuMode == -1 && !gDjuiInMainMenu && !gDjuiChatBoxFocus && !gDjuiConsoleFocus) { + if ((newcam_mouse == 1 || gFirstPersonCamera.enabled || gDjuiHudLockMouse) && gMenuMode == -1 && !gDjuiInMainMenu && !gDjuiChatBoxFocus && !gDjuiConsoleFocus && WAPI.has_focus()) { SDL_SetRelativeMouseMode(SDL_TRUE); } else { SDL_SetRelativeMouseMode(SDL_FALSE); diff --git a/src/pc/djui/djui_panel_sound.c b/src/pc/djui/djui_panel_sound.c index 9946b13e..2ffd0f42 100644 --- a/src/pc/djui/djui_panel_sound.c +++ b/src/pc/djui/djui_panel_sound.c @@ -18,6 +18,7 @@ void djui_panel_sound_create(struct DjuiBase* caller) { djui_slider_create(body, DLANG(SOUND, SFX_VOLUME), &configSfxVolume, 0, 127, djui_panel_sound_value_change); djui_slider_create(body, DLANG(SOUND, ENV_VOLUME), &configEnvVolume, 0, 127, djui_panel_sound_value_change); djui_checkbox_create(body, DLANG(SOUND, FADEOUT), &configFadeoutDistantSounds, NULL); + djui_checkbox_create(body, DLANG(SOUND, MUTE_FOCUS_LOSS), &configMuteFocusLoss, NULL); djui_button_create(body, DLANG(MENU, BACK), DJUI_BUTTON_STYLE_BACK, djui_panel_menu_back); } diff --git a/src/pc/gfx/gfx_dummy.c b/src/pc/gfx/gfx_dummy.c index aed6594b..b0fda743 100644 --- a/src/pc/gfx/gfx_dummy.c +++ b/src/pc/gfx/gfx_dummy.c @@ -80,14 +80,14 @@ static void gfx_dummy_wm_delay(u32 ms) { sleep_ms(ms); } -static int gfx_dummy_get_max_msaa(void) { +static int gfx_dummy_wm_get_max_msaa(void) { return 0; } -static void gfx_dummy_set_window_title(UNUSED const char* title) { +static void gfx_dummy_wm_set_window_title(UNUSED const char* title) { } -static void gfx_dummy_reset_window_title(void) { +static void gfx_dummy_wm_reset_window_title(void) { } static void gfx_dummy_wm_swap_buffers_begin(void) { @@ -119,6 +119,10 @@ static void gfx_dummy_wm_set_clipboard_text(UNUSED char* text) { static void gfx_dummy_wm_set_cursor_visible(UNUSED bool visible) { } +static bool gfx_dummy_wm_has_focus(void) { + return true; +} + static bool gfx_dummy_renderer_z_is_from_0_to_1(void) { return false; } @@ -212,9 +216,10 @@ struct GfxWindowManagerAPI gfx_dummy_wm_api = { gfx_dummy_wm_set_clipboard_text, gfx_dummy_wm_set_cursor_visible, gfx_dummy_wm_delay, - gfx_dummy_get_max_msaa, - gfx_dummy_set_window_title, - gfx_dummy_reset_window_title + gfx_dummy_wm_get_max_msaa, + gfx_dummy_wm_set_window_title, + gfx_dummy_wm_reset_window_title, + gfx_dummy_wm_has_focus }; struct GfxRenderingAPI gfx_dummy_renderer_api = { diff --git a/src/pc/gfx/gfx_dxgi.cpp b/src/pc/gfx/gfx_dxgi.cpp index e6fbad35..468a913f 100644 --- a/src/pc/gfx/gfx_dxgi.cpp +++ b/src/pc/gfx/gfx_dxgi.cpp @@ -704,6 +704,10 @@ static void gfx_dxgi_reset_window_title(void) { SetWindowTextA(dxgi.h_wnd, TITLE); } +static bool gfx_dxgi_has_focus(void) { + return GetFocus() == dxgi.h_wnd; +} + HWND gfx_dxgi_get_h_wnd(void) { return dxgi.h_wnd; } @@ -775,7 +779,8 @@ struct GfxWindowManagerAPI gfx_dxgi = { gfx_dxgi_delay, gfx_dxgi_get_max_msaa, gfx_dxgi_set_window_title, - gfx_dxgi_reset_window_title + gfx_dxgi_reset_window_title, + gfx_dxgi_has_focus }; #endif diff --git a/src/pc/gfx/gfx_sdl1.c b/src/pc/gfx/gfx_sdl1.c index 49398979..378c981a 100644 --- a/src/pc/gfx/gfx_sdl1.c +++ b/src/pc/gfx/gfx_sdl1.c @@ -205,6 +205,10 @@ static void gfx_sdl_shutdown(void) { SDL_Quit(); } +static bool gfx_sdl_has_focus(void) { + return SDL_GetAppState() & SDL_APPINPUTFOCUS; +} + static void gfx_sdl_start_text_input(void) { return; } static void gfx_sdl_stop_text_input(void) { return; } static char* gfx_sdl_get_clipboard_text(void) { return NULL; } @@ -230,7 +234,8 @@ struct GfxWindowManagerAPI gfx_sdl = { gfx_sdl_delay, gfx_sdl_get_max_msaa, gfx_sdl_set_window_title, - gfx_sdl_reset_window_title + gfx_sdl_reset_window_title, + gfx_sdl_has_focus }; #endif // BACKEND_WM diff --git a/src/pc/gfx/gfx_sdl2.c b/src/pc/gfx/gfx_sdl2.c index 78739576..92619d2b 100644 --- a/src/pc/gfx/gfx_sdl2.c +++ b/src/pc/gfx/gfx_sdl2.c @@ -298,6 +298,10 @@ static void gfx_sdl_shutdown(void) { } } +static bool gfx_sdl_has_focus(void) { + return (SDL_GetWindowFlags(wnd) & SDL_WINDOW_INPUT_FOCUS); +} + static void gfx_sdl_start_text_input(void) { SDL_StartTextInput(); } static void gfx_sdl_stop_text_input(void) { SDL_StopTextInput(); } static char* gfx_sdl_get_clipboard_text(void) { return SDL_GetClipboardText(); } @@ -323,7 +327,8 @@ struct GfxWindowManagerAPI gfx_sdl = { gfx_sdl_delay, gfx_sdl_get_max_msaa, gfx_sdl_set_window_title, - gfx_sdl_reset_window_title + gfx_sdl_reset_window_title, + gfx_sdl_has_focus }; #endif // BACKEND_WM diff --git a/src/pc/gfx/gfx_window_manager_api.h b/src/pc/gfx/gfx_window_manager_api.h index ebb5eefb..b94ecdd5 100644 --- a/src/pc/gfx/gfx_window_manager_api.h +++ b/src/pc/gfx/gfx_window_manager_api.h @@ -29,6 +29,7 @@ struct GfxWindowManagerAPI { int (*get_max_msaa)(void); void (*set_window_title)(const char* title); void (*reset_window_title)(void); + bool (*has_focus)(void); }; #endif diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 8c5139de..f1df6809 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -218,10 +218,11 @@ void produce_interpolation_frames_and_delay(void) { } inline static void buffer_audio(void) { + bool shouldMute = configMuteFocusLoss && !WAPI.has_focus(); const f32 masterMod = (f32)configMasterVolume / 127.0f * (f32)gLuaVolumeMaster / 127.0f; - set_sequence_player_volume(SEQ_PLAYER_LEVEL, (f32)configMusicVolume / 127.0f * (f32)gLuaVolumeLevel / 127.0f * masterMod); - set_sequence_player_volume(SEQ_PLAYER_SFX, (f32)configSfxVolume / 127.0f * (f32)gLuaVolumeSfx / 127.0f * masterMod); - set_sequence_player_volume(SEQ_PLAYER_ENV, (f32)configEnvVolume / 127.0f * (f32)gLuaVolumeEnv / 127.0f * masterMod); + set_sequence_player_volume(SEQ_PLAYER_LEVEL, shouldMute ? 0 : (f32)configMusicVolume / 127.0f * (f32)gLuaVolumeLevel / 127.0f * masterMod); + set_sequence_player_volume(SEQ_PLAYER_SFX, shouldMute ? 0 : (f32)configSfxVolume / 127.0f * (f32)gLuaVolumeSfx / 127.0f * masterMod); + set_sequence_player_volume(SEQ_PLAYER_ENV, shouldMute ? 0 : (f32)configEnvVolume / 127.0f * (f32)gLuaVolumeEnv / 127.0f * masterMod); int samplesLeft = audio_api->buffered(); u32 numAudioSamples = samplesLeft < audio_api->get_desired_buffered() ? SAMPLES_HIGH : SAMPLES_LOW;