Skip to content

Commit 9de7f72

Browse files
committed
…..
1 parent 03828f4 commit 9de7f72

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

game/platform.cc

+37-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <errhandlingapi.h>
1515
#include <fcntl.h>
1616
#include <IntSafe.h>
17-
#include <ProcessEnv.h>
1817
#include <wincon.h>
1918

2019
#elif (BOOST_OS_MACOS)
@@ -113,25 +112,57 @@ int Platform::defaultBackEnd() {
113112
}
114113

115114
#if (BOOST_OS_WINDOWS)
115+
// std::ofstream Platform::conOutStream;
116+
std::unique_ptr<FILE, decltype(&fclose)> Platform::stdErrStream{nullptr, fclose};
117+
// std::unique_ptr<FILE, decltype(&fclose)> Platform::stdOutFd{nullptr, fclose};
118+
// std::unique_ptr<FILE, decltype(&fclose)> Platform::oldStdOutFd{nullptr, fclose};
116119
int Platform::stderr_fd;
120+
// int Platform::stdout_file;
121+
122+
std::unique_ptr<HANDLE, decltype(&CloseHandle)> Platform::stdOutHandle{nullptr, CloseHandle};
117123

118124
void Platform::initWindowsConsole() {
119125
if (AttachConsole(ATTACH_PARENT_PROCESS) == 0 || fileno(stdout) == -2 || fileno(stderr) == -2) {
120126
auto ptr = stdErrStream.get();
121127
freopen_s(&ptr, "NUL", "w", stderr);
122128
}
123129
else {
124-
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
125130
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
126-
std::ios::sync_with_stdio(true);
131+
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
132+
HANDLE hStdout = CreateFile(
133+
"CONOUT$", GENERIC_READ | GENERIC_WRITE,
134+
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
135+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
136+
);
137+
stdOutHandle.reset(&hStdout);
138+
SetStdHandle(STD_OUTPUT_HANDLE, *stdOutHandle);
139+
// int conOutHandle = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
140+
// if (conOutHandle != -1) {
141+
// stdOutFd.reset(_fdopen(conOutHandle, "w"));
142+
// if (stdOutFd) {
143+
// oldStdOutFd.reset(stdout);
144+
// *stdout = *stdOutFd;
145+
// }
146+
// }
147+
// else {
148+
// fmt::print("Error in call to _open_osfhandle.");
149+
// }
127150
std::setvbuf(stdout, nullptr, _IONBF, 0);
128-
std::setvbuf(stderr, nullptr, _IONBF, 0);
129151
std::cout.clear();
130-
std::cerr.clear();
131152
std::wcout.clear();
132-
std::wcerr.clear();
153+
std::cout << "Testing cout, derp." << std::endl;
154+
// conOutStream = std::ofstream{"CONOUT$", std::ios::out};
155+
// std::cout.rdbuf(conOutStream.rdbuf());
156+
// std::setvbuf(stdout, nullptr, _IONBF, 0);
157+
158+
// std::ios::sync_with_stdio(true);
159+
// std::setvbuf(stderr, nullptr, _IONBF, 0);
160+
161+
// std::cerr.clear();
162+
// std::wcerr.clear();
133163
}
134164
stderr_fd = fileno(stderr);
165+
// stdout_file = fileno(stdout);
135166
}
136167

137168
extern "C" {

game/platform.hh

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
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 (BOOST_OS_WINDOWS)
13+
#include <ProcessEnv.h>
14+
#include <handleapi.h>
15+
1216
#include <cstdio>
1317
#include <sys/types.h>
1418
#include <sys/stat.h>
@@ -33,8 +37,13 @@ struct Platform {
3337

3438
#if (BOOST_OS_WINDOWS)
3539
static int stderr_fd;
40+
// static int stdout_file;
3641
private:
37-
std::unique_ptr<FILE, decltype(&fclose)> stdErrStream{nullptr, fclose};
42+
// static std::ofstream conOutStream;
43+
static std::unique_ptr<FILE, decltype(&fclose)> stdErrStream;
44+
static std::unique_ptr<HANDLE, decltype(&CloseHandle)> stdOutHandle;
45+
// static std::unique_ptr<FILE, decltype(&fclose)> stdOutFd;
46+
// static std::unique_ptr<FILE, decltype(&fclose)> oldStdOutFd;
3847
#else
3948
static constexpr int stderr_fd = STDERR_FILENO;
4049
#endif

0 commit comments

Comments
 (0)