Skip to content

Commit 35d0bdc

Browse files
committed
…..
1 parent 03828f4 commit 35d0bdc

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

game/platform.cc

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,57 @@ int Platform::defaultBackEnd() {
113113
}
114114

115115
#if (BOOST_OS_WINDOWS)
116+
// std::ofstream Platform::conOutStream;
117+
std::unique_ptr<FILE, decltype(&fclose)> Platform::stdErrStream{nullptr, fclose};
118+
// std::unique_ptr<FILE, decltype(&fclose)> Platform::stdOutFd{nullptr, fclose};
119+
// std::unique_ptr<FILE, decltype(&fclose)> Platform::oldStdOutFd{nullptr, fclose};
116120
int Platform::stderr_fd;
121+
// int Platform::stdout_file;
122+
123+
std::unique_ptr<HANDLE, decltype(&CloseHandle)> Platform::stdOutHandle{nullptr, CloseHandle};
117124

118125
void Platform::initWindowsConsole() {
119126
if (AttachConsole(ATTACH_PARENT_PROCESS) == 0 || fileno(stdout) == -2 || fileno(stderr) == -2) {
120127
auto ptr = stdErrStream.get();
121128
freopen_s(&ptr, "NUL", "w", stderr);
122129
}
123130
else {
124-
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
125131
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
126-
std::ios::sync_with_stdio(true);
132+
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
133+
HANDLE hStdout = CreateFile(
134+
"CONOUT$", GENERIC_READ | GENERIC_WRITE,
135+
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
136+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
137+
);
138+
stdOutHandle.reset(&hStdout);
139+
SetStdHandle(STD_OUTPUT_HANDLE, *stdOutHandle);
140+
// int conOutHandle = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
141+
// if (conOutHandle != -1) {
142+
// stdOutFd.reset(_fdopen(conOutHandle, "w"));
143+
// if (stdOutFd) {
144+
// oldStdOutFd.reset(stdout);
145+
// *stdout = *stdOutFd;
146+
// }
147+
// }
148+
// else {
149+
// fmt::print("Error in call to _open_osfhandle.");
150+
// }
127151
std::setvbuf(stdout, nullptr, _IONBF, 0);
128-
std::setvbuf(stderr, nullptr, _IONBF, 0);
129152
std::cout.clear();
130-
std::cerr.clear();
131153
std::wcout.clear();
132-
std::wcerr.clear();
154+
std::cout << "Testing cout, derp." << std::endl;
155+
// conOutStream = std::ofstream{"CONOUT$", std::ios::out};
156+
// std::cout.rdbuf(conOutStream.rdbuf());
157+
// std::setvbuf(stdout, nullptr, _IONBF, 0);
158+
159+
// std::ios::sync_with_stdio(true);
160+
// std::setvbuf(stderr, nullptr, _IONBF, 0);
161+
162+
// std::cerr.clear();
163+
// std::wcerr.clear();
133164
}
134165
stderr_fd = fileno(stderr);
166+
// stdout_file = fileno(stdout);
135167
}
136168

137169
extern "C" {

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)