From ec600f80c3be34733209270c3772596ef1794991 Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Mon, 20 Nov 2023 11:02:49 +0700 Subject: [PATCH] safe exit when console is closed on Windows (#1269) --- src/i_main.c | 9 ++++++++- win32/win_launcher.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/i_main.c b/src/i_main.c index 9ae26984..4bf3d7b6 100644 --- a/src/i_main.c +++ b/src/i_main.c @@ -17,12 +17,12 @@ // //----------------------------------------------------------------------------- -#include #include "config.h" #include "SDL.h" // haleyjd #include "i_printf.h" +#include "i_system.h" #include "m_argv.h" #include "version.h" @@ -34,6 +34,13 @@ void D_DoomMain(void); +#if defined(WIN_LAUNCHER) +__declspec(dllexport) void Woof_Exit(void) +{ + I_SafeExit(0); +} +#endif + #if defined(WIN_LAUNCHER) __declspec(dllexport) int Woof_Main(int argc, char **argv) #else diff --git a/win32/win_launcher.c b/win32/win_launcher.c index d623fb26..9b869313 100644 --- a/win32/win_launcher.c +++ b/win32/win_launcher.c @@ -13,10 +13,25 @@ // #include "SDL.h" +#define WIN32_LEAN_AND_MEAN +#include __declspec(dllexport) extern int Woof_Main(int argc, char **argv); +__declspec(dllexport) extern void Woof_Exit(void); + +BOOL CtrlHandler(DWORD event) +{ + if (event == CTRL_CLOSE_EVENT) + { + Woof_Exit(); + return TRUE; + } + return FALSE; +} int main(int argc, char **argv) { + SetConsoleCtrlHandler((PHANDLER_ROUTINE)(CtrlHandler), TRUE); + return Woof_Main(argc, argv); }