Fix running Flax on Windows with STD console output

This commit is contained in:
Wojtek Figat
2021-05-14 21:44:13 +02:00
parent 16e67780ad
commit 8f1f88b3ba
2 changed files with 25 additions and 1 deletions

View File

@@ -27,7 +27,7 @@ __declspec(dllexport) int32 AmdPowerXpressRequestHighPerformance = 1;
extern LONG CALLBACK SehExceptionHandler(EXCEPTION_POINTERS* ep);
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nCmdShow)
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
#ifdef USE_VS_MEM_LEAKS_CHECK
// Memory leaks detect inside VS

View File

@@ -10,6 +10,7 @@
#include "Engine/Core/Math/Math.h"
#include "Engine/Core/Collections/HashFunctions.h"
#include "Engine/Core/Log.h"
#include "Engine/Engine/CommandLine.h"
#include "IncludeWindowsHeaders.h"
#include <Psapi.h>
#include <WinSock2.h>
@@ -18,6 +19,7 @@
#include <WinBase.h>
#include <xmmintrin.h>
#include <intrin.h>
#include <cstdio>
#pragma comment(lib, "Iphlpapi.lib")
namespace
@@ -63,6 +65,28 @@ bool Win32Platform::Init()
if (PlatformBase::Init())
return true;
// Init console output (engine is linked with /SUBSYSTEM:WINDOWS so it lacks of proper console output on Windows)
if (CommandLine::Options.Std)
{
// Attaches output of application to parent console, returns true if running in console-mode
// [Reference: https://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application]
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
const HANDLE consoleHandleOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (consoleHandleOut != INVALID_HANDLE_VALUE)
{
freopen("CONOUT$", "w", stdout);
setvbuf(stdout, NULL, _IONBF, 0);
}
const HANDLE consoleHandleError = GetStdHandle(STD_ERROR_HANDLE);
if (consoleHandleError != INVALID_HANDLE_VALUE)
{
freopen("CONOUT$", "w", stderr);
setvbuf(stderr, NULL, _IONBF, 0);
}
}
}
// Init timing
LARGE_INTEGER frequency;
const auto freqResult = QueryPerformanceFrequency(&frequency);