Skip to content

Commit 4caa286

Browse files
committed
…..
1 parent 03828f4 commit 4caa286

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

game/platform.cc

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,33 @@ int Platform::defaultBackEnd() {
113113
}
114114

115115
#if (BOOST_OS_WINDOWS)
116+
std::unique_ptr<FILE, decltype(&fclose)> Platform::stdErrStream{nullptr, fclose};
117+
std::unique_ptr<HANDLE, decltype(&CloseHandle)> Platform::stdOutHandle{nullptr, CloseHandle};
116118
int Platform::stderr_fd;
117119

118120
void Platform::initWindowsConsole() {
119-
if (AttachConsole(ATTACH_PARENT_PROCESS) == 0 || fileno(stdout) == -2 || fileno(stderr) == -2) {
121+
if (int res = AttachConsole(ATTACH_PARENT_PROCESS) == 0 && (fileno(stdout) == -2 || fileno(stderr) == -2)) {
120122
auto ptr = stdErrStream.get();
121123
freopen_s(&ptr, "NUL", "w", stderr);
122124
}
123125
else {
124-
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
125126
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
126-
std::ios::sync_with_stdio(true);
127-
std::setvbuf(stdout, nullptr, _IONBF, 0);
128-
std::setvbuf(stderr, nullptr, _IONBF, 0);
129-
std::cout.clear();
130-
std::cerr.clear();
131-
std::wcout.clear();
132-
std::wcerr.clear();
127+
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
128+
HANDLE hStdout = CreateFile(
129+
"CONOUT$", GENERIC_READ | GENERIC_WRITE,
130+
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
131+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
132+
);
133+
if (hStdout == INVALID_HANDLE_VALUE) {
134+
std::fprintf(stderr, "Unable to get a handle to the stdout console. Error=%u", GetLastError());
135+
}
136+
else {
137+
stdOutHandle.reset(&hStdout);
138+
SetStdHandle(STD_OUTPUT_HANDLE, *stdOutHandle);
139+
std::setvbuf(stdout, nullptr, _IONBF, 0);
140+
std::cout.clear();
141+
std::wcout.clear();
142+
}
133143
}
134144
stderr_fd = fileno(stderr);
135145
}

game/platform.hh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
#include <array>
44
#include <cstdint>
5+
#include <fstream>
56
#include <iostream>
67
#include <memory>
78

89
#include <boost/predef/os.h>
910
#include <SDL_events.h>
1011

11-
#if (BOOST_OS_WINDOWS)
12+
#if defined(_MSC_VER)
13+
#define NOMINMAX
14+
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
15+
#define _AMD64_
16+
#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__i386__) || defined(_M_IX86)
17+
#define _X86_
18+
#elif defined(__arm__) || defined(_M_ARM) || defined(_M_ARMT)
19+
#define _ARM_
20+
#endif
21+
#endif
22+
23+
#if (BOOST_OS_WINDOWS)
24+
#include <handleapi.h>
25+
1226
#include <cstdio>
1327
#include <sys/types.h>
1428
#include <sys/stat.h>
@@ -33,8 +47,13 @@ struct Platform {
3347

3448
#if (BOOST_OS_WINDOWS)
3549
static int stderr_fd;
50+
// static int stdout_file;
3651
private:
37-
std::unique_ptr<FILE, decltype(&fclose)> stdErrStream{nullptr, fclose};
52+
// static std::ofstream conOutStream;
53+
static std::unique_ptr<FILE, decltype(&fclose)> stdErrStream;
54+
static std::unique_ptr<HANDLE, decltype(&CloseHandle)> stdOutHandle;
55+
// static std::unique_ptr<FILE, decltype(&fclose)> stdOutFd;
56+
// static std::unique_ptr<FILE, decltype(&fclose)> oldStdOutFd;
3857
#else
3958
static constexpr int stderr_fd = STDERR_FILENO;
4059
#endif

0 commit comments

Comments
 (0)