From e602e36495700549c395e4b9a7f462c1fd50674f Mon Sep 17 00:00:00 2001 From: rr- Date: Mon, 29 Mar 2021 18:04:05 +0200 Subject: [PATCH] deactivate game when alt-tabbed (fixes #1) --- src/global/vars.c | 1 + src/global/vars.h | 1 + src/specific/frontend.c | 7 +++--- src/specific/smain.c | 47 +++++++++++++++++++++++++++++++++++------ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/global/vars.c b/src/global/vars.c index d12663269..e44927e18 100644 --- a/src/global/vars.c +++ b/src/global/vars.c @@ -28,6 +28,7 @@ void (*EffectRoutines[])(ITEM_INFO *item) = { ChainBlock, Flicker, }; +int8_t IsGameWindowActive = 1; int32_t NoInputCount = 0; int32_t IDelay; int32_t IDCount; diff --git a/src/global/vars.h b/src/global/vars.h index 341a1f7fa..92c0ae734 100644 --- a/src/global/vars.h +++ b/src/global/vars.h @@ -196,6 +196,7 @@ extern KEYSTUFF* KeyData; extern char *ATIUserSettingsPath; extern char *T1MUserSettingsPath; +extern int8_t IsGameWindowActive; extern double UITextScale; extern double UIBarScale; diff --git a/src/specific/frontend.c b/src/specific/frontend.c index 3f0443a7b..02ee00841 100644 --- a/src/specific/frontend.c +++ b/src/specific/frontend.c @@ -15,6 +15,7 @@ #include "specific/smain.h" #include "util.h" +#include #include const char *FMVPaths[] = { @@ -186,13 +187,13 @@ int32_t WinPlayFMV(int32_t sequence, int32_t mode) WinSpinMessageLoop(); if (T1MConfig.fix_fmv_esc_key) { - if (KeyData->keymap[1]) { + if (KeyData->keymap[DIK_ESCAPE]) { keypress = 1; - } else if (keypress && !KeyData->keymap[1]) { + } else if (keypress && !KeyData->keymap[DIK_ESCAPE]) { break; } } else { - if (KeyData->keymap[1]) { + if (KeyData->keymap[DIK_ESCAPE]) { break; } } diff --git a/src/specific/smain.c b/src/specific/smain.c index 0a7684a3d..53d2a7e0d 100644 --- a/src/specific/smain.c +++ b/src/specific/smain.c @@ -50,14 +50,16 @@ void ShowFatalError(const char *message) int32_t WinSpinMessageLoop() { MSG msg; - while (PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE)) { - if (!GetMessageA(&msg, 0, 0, 0)) { - TerminateGame(0); + do { + while (PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE)) { + if (!GetMessageA(&msg, 0, 0, 0)) { + TerminateGame(0); + } + + TranslateMessage(&msg); + DispatchMessageA(&msg); } - - TranslateMessage(&msg); - DispatchMessageA(&msg); - } + } while (!IsGameWindowActive); int32_t time_ms = timeGetTime(); int32_t old_ticks = Ticks; @@ -113,20 +115,48 @@ static LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_CLOSE: DestroyWindow(TombHWND); return 0; + case WM_DESTROY: PostQuitMessage(0); return 0; + case WM_SIZE: return 1; + case WM_MOVE: return 0; + case WM_SETCURSOR: SetCursor(0); return 1; + case WM_ERASEBKGND: return 1; + + case WM_ACTIVATEAPP: + // mute the music when the game is not active + if (wParam && !IsGameWindowActive) { + if (OptionMusicVolume) { + S_MusicVolume(OptionMusicVolume * 25 + 5); + } else { + S_MusicVolume(0); + } + } else if (!wParam && IsGameWindowActive) { + S_MusicVolume(0); + } + if (wParam && !IsGameWindowActive) { + // TODO: remove this after switching to DInput + for (int i = 0; i < 256; i++) { + KeyData->keymap[i] = 0; + } + KeyData->keys_held = 0; + } + IsGameWindowActive = wParam != 0; + return 1; + case WM_NCPAINT: return 0; + case WM_GETMINMAXINFO: { MINMAXINFO *min_max_info = (MINMAXINFO *)lParam; min_max_info->ptMinTrackSize.x = DDrawSurfaceWidth; @@ -136,12 +166,15 @@ static LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return DefWindowProcA( hWnd, WM_GETMINMAXINFO, wParam, (LPARAM)min_max_info); } + case WM_MOVING: GetWindowRect(TombHWND, (LPRECT)lParam); return 1; + case MM_MCINOTIFY: MusicPlayLooped(); return 0; + default: return DefWindowProcA(hWnd, uMsg, wParam, lParam); }