C ++ / SDL debugging with console window

I play with some OpenGL using SDL to handle window / input, etc. I am currently displaying any information that I want to see in the HUD. Well, this gets too cumbersome, and I was wondering if there is an easy way to open a separate console window to tell me this information. I am still new to C ++, so it’s easy on me if this is obvious.

+6
source share
3 answers

In Linker β†’ System, in your project properties, make sure that SubSystem is "Console (/ SUBSYSTEM: CONSOLE)". This leads to the fact that when you start your program, a separate console window opens. If your current entry point is not main , then you will need to change it if you do.

+4
source

The following code is for Windows. It’s always convenient for me to support the ability to create a console window on demand:

  int hConHandle; intptr_t lStdHandle; CONSOLE_SCREEN_BUFFER_INFO coninfo; FILE *fp; // allocate a console for this app AllocConsole(); // set the screen buffer to be big enough to let us scroll text GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); coninfo.dwSize.Y = 500; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); // redirect unbuffered STDOUT to the console lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); // redirect unbuffered STDIN to the console lStdHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "r" ); *stdin = *fp; setvbuf( stdin, NULL, _IONBF, 0 ); // redirect unbuffered STDERR to the console lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stderr = *fp; setvbuf( stderr, NULL, _IONBF, 0 ); // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well std::ios::sync_with_stdio(); //Keep our window in focus SetForegroundWindow(m_hWnd); // Slightly Higher Priority SetFocus(m_hWnd); // Sets Keyboard Focus To The Window 

This code assumes that HWND is in a variable called m_hWnd ; it is copied from the shell of the class that I am using. However, how do you get HWND from the SDL, you.

To free the console, call this:

 FreeConsole(); 

AllocConsole and FreeConsole are Win32 API functions.

+6
source

If you are working from the command line and using printf() , you should see the messages displayed in the terminal window. Otherwise, you can enter the file and use the tail -f on * nix style fields to view the output as it appears.

What environment are you using? Most IDEs will display this output in debug output windows.

0
source

Source: https://habr.com/ru/post/903640/


All Articles