Skip to content

Commit 193d61a

Browse files
committed
Fix console output in Windows
1 parent d67fb82 commit 193d61a

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Geargrafx - PC Engine / TurboGrafx Emulator
3+
* Copyright (C) 2024 Ignacio Sanchez
4+
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* any later version.
9+
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/
17+
*
18+
*/
19+
20+
#ifndef CONSOLE_UTILS_H
21+
#define CONSOLE_UTILS_H
22+
23+
#if defined(_WIN32)
24+
#include <windows.h>
25+
#include <io.h>
26+
#include <fcntl.h>
27+
#include <cstring>
28+
29+
inline bool attach_parent_console(int argc, char* argv[])
30+
{
31+
bool is_mcp_stdio = false;
32+
for (int i = 1; i < argc; i++)
33+
{
34+
if (strcmp(argv[i], "--mcp-stdio") == 0)
35+
{
36+
is_mcp_stdio = true;
37+
break;
38+
}
39+
}
40+
41+
if (is_mcp_stdio)
42+
return false;
43+
44+
if (AttachConsole(ATTACH_PARENT_CONSOLE))
45+
{
46+
FILE* fp_stdout = nullptr;
47+
FILE* fp_stderr = nullptr;
48+
49+
freopen_s(&fp_stdout, "CONOUT$", "w", stdout);
50+
freopen_s(&fp_stderr, "CONOUT$", "w", stderr);
51+
52+
setvbuf(stdout, NULL, _IONBF, 0);
53+
setvbuf(stderr, NULL, _IONBF, 0);
54+
55+
return true;
56+
}
57+
58+
return false;
59+
}
60+
61+
#else
62+
63+
inline bool attach_parent_console(int argc, char* argv[])
64+
{
65+
(void)argc;
66+
(void)argv;
67+
return false;
68+
}
69+
70+
#endif
71+
72+
#endif /* CONSOLE_UTILS_H */

platforms/shared/desktop/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
#include "geargrafx.h"
2121
#include "application.h"
2222
#include "config.h"
23+
#include "console_utils.h"
2324

2425
extern bool g_mcp_stdio_mode;
2526

2627
int main(int argc, char* argv[])
2728
{
29+
attach_parent_console(argc, argv);
30+
2831
char* rom_file = NULL;
2932
char* symbol_file = NULL;
3033
bool show_usage = false;

platforms/windows/Geargrafx.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@
184184
<ClInclude Include="..\..\src\huc6270.h" />
185185
<ClInclude Include="..\..\src\huc6280_inline.h" />
186186
<ClInclude Include="..\..\src\huc6280_opcodes_inline.h" />
187-
<ClInclude Include="..\..\src\huc6280_opcodes.h" />
188187
<ClInclude Include="..\..\src\huc6280_psg_inline.h" />
189188
<ClInclude Include="..\..\src\huc6280_psg.h" />
190189
<ClInclude Include="..\..\src\huc6280_registers.h" />
@@ -211,9 +210,11 @@
211210
<ClInclude Include="..\shared\desktop\keyboard.h" />
212211
<ClInclude Include="..\shared\dependencies\nfd\nfd_sdl2.h" />
213212
<ClInclude Include="..\shared\desktop\sound_queue.h" />
213+
<ClInclude Include="..\shared\desktop\single_instance.h" />
214214
<ClInclude Include="..\shared\desktop\application.h" />
215215
<ClInclude Include="..\shared\desktop\backers.h" />
216216
<ClInclude Include="..\shared\desktop\config.h" />
217+
<ClInclude Include="..\shared\desktop\console_utils.h" />
217218
<ClInclude Include="..\shared\desktop\emu.h" />
218219
<ClInclude Include="..\shared\dependencies\json\json.hpp" />
219220
<ClInclude Include="..\shared\desktop\mcp\mcp_debug_adapter.h" />
@@ -235,6 +236,7 @@
235236
<ClInclude Include="..\shared\desktop\gui_debug_adpcm.h" />
236237
<ClInclude Include="..\shared\desktop\gui_debug_text.h" />
237238
<ClInclude Include="..\shared\desktop\gui_debug_trace_logger.h" />
239+
<ClInclude Include="..\shared\desktop\gui_debug_widgets.h" />
238240
<ClInclude Include="..\shared\desktop\gui_debug.h" />
239241
<ClInclude Include="..\shared\desktop\gui_filedialogs.h" />
240242
<ClInclude Include="..\shared\desktop\gui_hes.h" />

platforms/windows/Geargrafx.vcxproj.filters

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@
237237
<ClInclude Include="..\shared\desktop\config.h">
238238
<Filter>desktop</Filter>
239239
</ClInclude>
240+
<ClInclude Include="..\shared\desktop\console_utils.h">
241+
<Filter>desktop</Filter>
242+
</ClInclude>
240243
<ClInclude Include="..\shared\desktop\emu.h">
241244
<Filter>desktop</Filter>
242245
</ClInclude>
@@ -303,6 +306,9 @@
303306
<ClInclude Include="..\shared\desktop\gui_debug_trace_logger.h">
304307
<Filter>desktop</Filter>
305308
</ClInclude>
309+
<ClInclude Include="..\shared\desktop\gui_debug_widgets.h">
310+
<Filter>desktop</Filter>
311+
</ClInclude>
306312
<ClInclude Include="..\shared\desktop\license.h">
307313
<Filter>desktop</Filter>
308314
</ClInclude>
@@ -420,9 +426,6 @@
420426
<ClInclude Include="..\..\src\huc6280_inline.h">
421427
<Filter>src</Filter>
422428
</ClInclude>
423-
<ClInclude Include="..\..\src\huc6280_opcodes.h">
424-
<Filter>src</Filter>
425-
</ClInclude>
426429
<ClInclude Include="..\..\src\huc6280_opcodes_inline.h">
427430
<Filter>src</Filter>
428431
</ClInclude>

0 commit comments

Comments
 (0)