Skip to content

Commit

Permalink
…..
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Kamina committed Jan 17, 2025
1 parent 03828f4 commit 9de7f72
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
43 changes: 37 additions & 6 deletions game/platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <errhandlingapi.h>
#include <fcntl.h>
#include <IntSafe.h>
#include <ProcessEnv.h>
#include <wincon.h>

#elif (BOOST_OS_MACOS)
Expand Down Expand Up @@ -113,25 +112,57 @@ int Platform::defaultBackEnd() {
}

#if (BOOST_OS_WINDOWS)
// std::ofstream Platform::conOutStream;
std::unique_ptr<FILE, decltype(&fclose)> Platform::stdErrStream{nullptr, fclose};
// std::unique_ptr<FILE, decltype(&fclose)> Platform::stdOutFd{nullptr, fclose};
// std::unique_ptr<FILE, decltype(&fclose)> Platform::oldStdOutFd{nullptr, fclose};
int Platform::stderr_fd;
// int Platform::stdout_file;

std::unique_ptr<HANDLE, decltype(&CloseHandle)> Platform::stdOutHandle{nullptr, CloseHandle};

void Platform::initWindowsConsole() {
if (AttachConsole(ATTACH_PARENT_PROCESS) == 0 || fileno(stdout) == -2 || fileno(stderr) == -2) {
auto ptr = stdErrStream.get();
freopen_s(&ptr, "NUL", "w", stderr);
}
else {
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
std::ios::sync_with_stdio(true);
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
HANDLE hStdout = CreateFile(
"CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
);
stdOutHandle.reset(&hStdout);
SetStdHandle(STD_OUTPUT_HANDLE, *stdOutHandle);
// int conOutHandle = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
// if (conOutHandle != -1) {
// stdOutFd.reset(_fdopen(conOutHandle, "w"));
// if (stdOutFd) {
// oldStdOutFd.reset(stdout);
// *stdout = *stdOutFd;
// }
// }
// else {
// fmt::print("Error in call to _open_osfhandle.");
// }
std::setvbuf(stdout, nullptr, _IONBF, 0);
std::setvbuf(stderr, nullptr, _IONBF, 0);
std::cout.clear();
std::cerr.clear();
std::wcout.clear();
std::wcerr.clear();
std::cout << "Testing cout, derp." << std::endl;
// conOutStream = std::ofstream{"CONOUT$", std::ios::out};
// std::cout.rdbuf(conOutStream.rdbuf());
// std::setvbuf(stdout, nullptr, _IONBF, 0);

// std::ios::sync_with_stdio(true);
// std::setvbuf(stderr, nullptr, _IONBF, 0);

// std::cerr.clear();
// std::wcerr.clear();
}
stderr_fd = fileno(stderr);
// stdout_file = fileno(stdout);
}

extern "C" {
Expand Down
13 changes: 11 additions & 2 deletions game/platform.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

#include <array>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <memory>

#include <boost/predef/os.h>
#include <SDL_events.h>

#if (BOOST_OS_WINDOWS)
#if (BOOST_OS_WINDOWS)
#include <ProcessEnv.h>
#include <handleapi.h>

#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -33,8 +37,13 @@ struct Platform {

#if (BOOST_OS_WINDOWS)
static int stderr_fd;
// static int stdout_file;
private:
std::unique_ptr<FILE, decltype(&fclose)> stdErrStream{nullptr, fclose};
// static std::ofstream conOutStream;
static std::unique_ptr<FILE, decltype(&fclose)> stdErrStream;
static std::unique_ptr<HANDLE, decltype(&CloseHandle)> stdOutHandle;
// static std::unique_ptr<FILE, decltype(&fclose)> stdOutFd;
// static std::unique_ptr<FILE, decltype(&fclose)> oldStdOutFd;
#else
static constexpr int stderr_fd = STDERR_FILENO;
#endif
Expand Down

0 comments on commit 9de7f72

Please sign in to comment.