当 SDL 2 程序为 运行 时如何打印到控制台?

How do I print to the console while an SDL 2 program is running?

我想在 运行 我的 SDL 2 程序时向控制台打印一些调试信息,但这似乎不可能。 printf("Hi!\n")SDL_Log("Hi!\n") 都对我没有任何好处。

我什至尝试在初始化 SDL 之前(以及退出之后)进行打印,但无济于事。似乎仅仅 导入 SDL 库就无法将任何内容打印到控制台。

以下是我正在编译的参数,因为这可能与它有关:

g++ hello.cc -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -std=c++11

有什么想法吗?

所以,我弄清楚是什么阻止了我看到输出。这些编译选项

-Wl,-subsystem,windows

基本上禁用控制台window,防止显示输出。这对游戏结束时很有用,但对调试来说很糟糕。所以,我继续删除了那些编译选项,现在 printf()SDL_Log() 工作得很好。

我使用这种方法调试控制台:

static ULONG_PTR GetParentProcessId() // By Napalm @ NetCore2K
{
    ULONG_PTR pbi[6];
    ULONG ulSize = 0;
    LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
            PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength); 
    *(FARPROC *)&NtQueryInformationProcess = 
        GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
    if(NtQueryInformationProcess){
        if(NtQueryInformationProcess(GetCurrentProcess(), 0,
                    &pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
            return pbi[5];
    }
    return (ULONG_PTR)-1;
}

static void _windows_init_console(int argc, char **argv) {
    (void)argc, (void)argv;
    ULONG_PTR ppid = GetParentProcessId();
    if(ppid == (ULONG_PTR)-1) {
        AllocConsole();
    } else {
        AttachConsole(ppid);
    }

    freopen("CONIN$", "r", stdin); 
    freopen("CONOUT$", "w", stdout); 
    freopen("CONOUT$", "w", stderr); 
}

GetParentProcessId 来自 How can a Win32 process get the pid of its parent?)。工作得很好,但我仍然无法使其与 grep/findstr 等一起工作(即 'true' 带有管道重定向的标准输出)。

由于 SDL2 使用 windows 子系统,您可以使用 Win32 API.

打开控制台 window

根据这个blog post

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    // SDL2 init code...

    // Just before the event loop
    AllocConsole();

    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
    int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
    FILE* hf_out = _fdopen(hCrt, "w");
    setvbuf(hf_out, NULL, _IONBF, 1);
    *stdout = *hf_out;

    HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
    hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
    FILE* hf_in = _fdopen(hCrt, "r");
    setvbuf(hf_in, NULL, _IONBF, 128);
    *stdin = *hf_in;

    // use the console just like a normal one - printf(), getchar(), ...
}

希望对您有所帮助。

由于在使用 mingw 时 window 中的 SDL2 仍然存在这个问题,这里是我发现并测试过的更好的解决方案。

不要像其他人建议的那样删除 -mwindows 构建选项。您应该添加 `pkg-config --libs SDL2` 作为构建选项,但对于调试构建选项,您还应该添加 -mconsole 在结束。它应该在 -mwindows 标志之后。

调试:`pkg-config --libs SDL2` -mconsole
发布:`pkg-config --libs SDL2`

注意:我正在为 Windows 10、SDL2 v2.0.9、Msys64、mingw64、Code::Blocks 17.12
编译 `pkg-config --libs SDL2` 扩展为:
-LC:/msys64/mingw64/lib -lmingw32 -lSDL2main -lSDL2 -mwindows

参考文献:
SDL2: keep the -mwindows flag in the pkg-config --libs output #2419
configure: force -mconsole when linking SDL under MinGW