From 12658a0b3dd3260f83758bd686e2969a1393566a Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Fri, 12 Mar 2021 15:31:09 +0100 Subject: [PATCH] :bug: Fix console output on Windows and SDL1 targets Fixes #921 --- Source/diablo.cpp | 65 +++++++++++++++++---------------- Source/interfac.cpp | 4 +- Source/movie.cpp | 2 +- SourceS/console.h | 35 ++++++++++++++++++ SourceS/file_util.h | 1 + SourceS/miniwin/misc.h | 4 +- SourceS/sdl2_to_1_2_backports.h | 11 ++++-- SourceX/miniwin/misc_msg.cpp | 4 +- 8 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 SourceS/console.h diff --git a/Source/diablo.cpp b/Source/diablo.cpp index b6eee2ab6..859970479 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -5,6 +5,7 @@ */ #include "all.h" #include "paths.h" +#include "console.h" #include "../3rdParty/Storm/Source/storm.h" #include "../DiabloUI/diabloui.h" #include @@ -92,36 +93,36 @@ BOOL was_snd_init = false; static void print_help_and_exit() { - printf("Options:\n"); - printf(" %-20s %-30s\n", "-h, --help", "Print this message and exit"); - printf(" %-20s %-30s\n", "--version", "Print the version and exit"); - printf(" %-20s %-30s\n", "--data-dir", "Specify the folder of diabdat.mpq"); - printf(" %-20s %-30s\n", "--save-dir", "Specify the folder of save files"); - printf(" %-20s %-30s\n", "--config-dir", "Specify the location of diablo.ini"); - printf(" %-20s %-30s\n", "-n", "Skip startup videos"); - printf(" %-20s %-30s\n", "-f", "Display frames per second"); - printf(" %-20s %-30s\n", "-x", "Run in windowed mode"); - printf(" %-20s %-30s\n", "--spawn", "Force spawn mode even if diabdat.mpq is found"); - printf("\nHellfire options:\n"); - printf(" %-20s %-30s\n", "--diablo", "Force diablo mode even if hellfire.mpq is found"); - printf(" %-20s %-30s\n", "--nestart", "Use alternate nest palette"); + printInConsole("Options:\n"); + printInConsole(" %-20s %-30s\n", "-h, --help", "Print this message and exit"); + printInConsole(" %-20s %-30s\n", "--version", "Print the version and exit"); + printInConsole(" %-20s %-30s\n", "--data-dir", "Specify the folder of diabdat.mpq"); + printInConsole(" %-20s %-30s\n", "--save-dir", "Specify the folder of save files"); + printInConsole(" %-20s %-30s\n", "--config-dir", "Specify the location of diablo.ini"); + printInConsole(" %-20s %-30s\n", "-n", "Skip startup videos"); + printInConsole(" %-20s %-30s\n", "-f", "Display frames per second"); + printInConsole(" %-20s %-30s\n", "-x", "Run in windowed mode"); + printInConsole(" %-20s %-30s\n", "--spawn", "Force spawn mode even if diabdat.mpq is found"); + printInConsole("\nHellfire options:\n"); + printInConsole(" %-20s %-30s\n", "--diablo", "Force diablo mode even if hellfire.mpq is found"); + printInConsole(" %-20s %-30s\n", "--nestart", "Use alternate nest palette"); #ifdef _DEBUG - printf("\nDebug options:\n"); - printf(" %-20s %-30s\n", "-d", "Increaased item drops"); - printf(" %-20s %-30s\n", "-w", "Enable cheats"); - printf(" %-20s %-30s\n", "-$", "Enable god mode"); - printf(" %-20s %-30s\n", "-^", "Enable god mode and debug tools"); - //printf(" %-20s %-30s\n", "-b", "Enable item drop log"); - printf(" %-20s %-30s\n", "-v", "Highlight visibility"); - printf(" %-20s %-30s\n", "-i", "Ignore network timeout"); - //printf(" %-20s %-30s\n", "-j <##>", "Init trigger at level"); - printf(" %-20s %-30s\n", "-l <##> <##>", "Start in level as type"); - printf(" %-20s %-30s\n", "-m <##>", "Add debug monster, up to 10 allowed"); - printf(" %-20s %-30s\n", "-q <#>", "Force a certain quest"); - printf(" %-20s %-30s\n", "-r <##########>", "Set map seed"); - printf(" %-20s %-30s\n", "-t <##>", "Set current quest level"); + printInConsole("\nDebug options:\n"); + printInConsole(" %-20s %-30s\n", "-d", "Increaased item drops"); + printInConsole(" %-20s %-30s\n", "-w", "Enable cheats"); + printInConsole(" %-20s %-30s\n", "-$", "Enable god mode"); + printInConsole(" %-20s %-30s\n", "-^", "Enable god mode and debug tools"); + //printInConsole(" %-20s %-30s\n", "-b", "Enable item drop log"); + printInConsole(" %-20s %-30s\n", "-v", "Highlight visibility"); + printInConsole(" %-20s %-30s\n", "-i", "Ignore network timeout"); + //printInConsole(" %-20s %-30s\n", "-j <##>", "Init trigger at level"); + printInConsole(" %-20s %-30s\n", "-l <##> <##>", "Start in level as type"); + printInConsole(" %-20s %-30s\n", "-m <##>", "Add debug monster, up to 10 allowed"); + printInConsole(" %-20s %-30s\n", "-q <#>", "Force a certain quest"); + printInConsole(" %-20s %-30s\n", "-r <##########>", "Set map seed"); + printInConsole(" %-20s %-30s\n", "-t <##>", "Set current quest level"); #endif - printf("\nReport bugs at https://github.com/diasurgical/devilutionX/\n"); + printInConsole("\nReport bugs at https://github.com/diasurgical/devilutionX/\n"); diablo_quit(0); } @@ -131,7 +132,7 @@ static void diablo_parse_flags(int argc, char **argv) if (strcasecmp("-h", argv[i]) == 0 || strcasecmp("--help", argv[i]) == 0) { print_help_and_exit(); } else if (strcasecmp("--version", argv[i]) == 0) { - printf("%s v%s\n", PROJECT_NAME, PROJECT_VERSION); + printInConsole("%s v%s\n", PROJECT_NAME, PROJECT_VERSION); diablo_quit(0); } else if (strcasecmp("--data-dir", argv[i]) == 0) { SetBasePath(argv[++i]); @@ -195,7 +196,7 @@ static void diablo_parse_flags(int argc, char **argv) debug_mode_key_w = TRUE; #endif } else { - printf("unrecognized option '%s'\n", argv[i]); + printInConsole("unrecognized option '%s'\n", argv[i]); print_help_and_exit(); } } @@ -309,14 +310,14 @@ static void run_game_loop(unsigned int uMsg) nthread_ignore_mutex(FALSE); while (gbRunGame) { - while (PeekMessage(&msg)) { + while (FetchMessage(&msg)) { if (msg.message == DVL_WM_QUIT) { gbRunGameResult = FALSE; gbRunGame = FALSE; break; } TranslateMessage(&msg); - DispatchMessage(&msg); + PushMessage(&msg); } if (!gbRunGame) break; diff --git a/Source/interfac.cpp b/Source/interfac.cpp index 73e8ce95d..9a600894f 100644 --- a/Source/interfac.cpp +++ b/Source/interfac.cpp @@ -265,10 +265,10 @@ void interface_msg_pump() { MSG Msg; - while (PeekMessage(&Msg)) { + while (FetchMessage(&Msg)) { if (Msg.message != DVL_WM_QUIT) { TranslateMessage(&Msg); - DispatchMessage(&Msg); + PushMessage(&Msg); } } } diff --git a/Source/movie.cpp b/Source/movie.cpp index 499e74d51..e2c5ada92 100644 --- a/Source/movie.cpp +++ b/Source/movie.cpp @@ -31,7 +31,7 @@ void play_movie(const char *pszMovie, BOOL user_can_close) SVidPlayBegin(pszMovie, 0, 0, 0, 0, loop_movie ? 0x100C0808 : 0x10280808, &video_stream); MSG Msg; while (video_stream && movie_playing) { - while (movie_playing && PeekMessage(&Msg)) { + while (movie_playing && FetchMessage(&Msg)) { switch (Msg.message) { case DVL_WM_KEYDOWN: case DVL_WM_LBUTTONDOWN: diff --git a/SourceS/console.h b/SourceS/console.h new file mode 100644 index 000000000..614efa848 --- /dev/null +++ b/SourceS/console.h @@ -0,0 +1,35 @@ +#pragma once + +#if defined(_WIN64) || defined(_WIN32) +// Suppress definitions of `min` and `max` macros by : +#define NOMINMAX 1 +#define WIN32_LEAN_AND_MEAN +#include + +namespace dvl { + +void printInConsole(const char *fmt, ...) +{ + static HANDLE stderrHandle = NULL; + if (stderrHandle == NULL) { + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + stderrHandle = GetStdHandle(STD_ERROR_HANDLE); + } + } + + if (stderrHandle == NULL) + return; + + char message[256]; + va_list ap; + va_start(ap, fmt); + vsprintf(message, fmt, ap); + va_end(ap); + + WriteConsole(stderrHandle, message, strlen(message), NULL, NULL); +} + +} // namespace dvl +#else +#define printInConsole printf +#endif diff --git a/SourceS/file_util.h b/SourceS/file_util.h index f2a24a01f..56def8628 100644 --- a/SourceS/file_util.h +++ b/SourceS/file_util.h @@ -13,6 +13,7 @@ #if defined(_WIN64) || defined(_WIN32) // Suppress definitions of `min` and `max` macros by : #define NOMINMAX 1 +#define WIN32_LEAN_AND_MEAN #include #endif diff --git a/SourceS/miniwin/misc.h b/SourceS/miniwin/misc.h index 27c894b03..53ccb97ec 100644 --- a/SourceS/miniwin/misc.h +++ b/SourceS/miniwin/misc.h @@ -45,10 +45,10 @@ void FocusOnCharInfo(); SHORT GetAsyncKeyState(int vKey); -bool PeekMessage(LPMSG lpMsg); +bool FetchMessage(LPMSG lpMsg); bool TranslateMessage(const MSG *lpMsg); -void DispatchMessage(const MSG *lpMsg); +void PushMessage(const MSG *lpMsg); bool PostMessage(UINT Msg, WPARAM wParam, LPARAM lParam); #ifndef TRUE diff --git a/SourceS/sdl2_to_1_2_backports.h b/SourceS/sdl2_to_1_2_backports.h index 3ad21b015..25e368fe9 100644 --- a/SourceS/sdl2_to_1_2_backports.h +++ b/SourceS/sdl2_to_1_2_backports.h @@ -9,6 +9,7 @@ #include #include +#include "console.h" #include "../SourceX/stubs.h" #define WINDOW_ICON_NAME 0 @@ -55,11 +56,13 @@ inline void SDL_Log(const char *fmt, ...) { + char message[256]; va_list ap; va_start(ap, fmt); - vprintf(fmt, ap); + vsprintf(message, fmt, ap); va_end(ap); - puts(""); + + printInConsole("INFO: %s\n", message); } inline void SDL_StartTextInput() @@ -116,7 +119,7 @@ inline void SDL_GetWindowPosition(SDL_Window *window, int *x, int *y) { *x = window->clip_rect.x; *y = window->clip_rect.x; - printf("SDL_GetWindowPosition %d %d", *x, *y); + SDL_Log("SDL_GetWindowPosition %d %d", *x, *y); } inline void SDL_SetWindowPosition(SDL_Window *window, int x, int y) @@ -128,7 +131,7 @@ inline void SDL_GetWindowSize(SDL_Window *window, int *w, int *h) { *w = window->clip_rect.w; *h = window->clip_rect.h; - printf("SDL_GetWindowSize %d %d", *w, *h); + SDL_Log("SDL_GetWindowSize %d %d", *w, *h); } inline void SDL_ShowWindow(SDL_Window *window) diff --git a/SourceX/miniwin/misc_msg.cpp b/SourceX/miniwin/misc_msg.cpp index cf4f833a4..703c6b51d 100644 --- a/SourceX/miniwin/misc_msg.cpp +++ b/SourceX/miniwin/misc_msg.cpp @@ -301,7 +301,7 @@ bool BlurInventory() return true; } -bool PeekMessage(LPMSG lpMsg) +bool FetchMessage(LPMSG lpMsg) { #ifdef __SWITCH__ HandleDocking(); @@ -714,7 +714,7 @@ SHORT GetAsyncKeyState(int vKey) } } -void DispatchMessage(const MSG *lpMsg) +void PushMessage(const MSG *lpMsg) { assert(CurrentProc);