Any way to force a console window?

Started by
6 comments, last by ddx 17 years ago
Hi, I just started another project using SDL and it occurs to me that I'd really like to be running a console window so that I can debug it on the fly, rather than having all my 'cout's dumped in a .txt file when the program ends. I'm using VS '05 and initialising with the usual:

int main(int argc, char *argv[])
{
	
	SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);

        // etc...

}
Advertisement
You can use a function like AllocConsole() to get a console, and use freopen with stdin/out to redirect output to that... I think ( I haven't tried it ) [smile]
Here's what I do in my LogManager class. Call FreeConsole() when you are done with it. All my output, even from Python and Lua, goes to the console window.

void LogManagerConsole::redirectIo() {    int hConHandle;    long 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 = MaxConsoleLines;    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);        // position it    const char* uniqueTitle = "my_console_window";    SetConsoleTitle(uniqueTitle);    Sleep(40);    HWND consoleWnd = FindWindow(NULL, uniqueTitle);     SetWindowPos(consoleWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);     SetConsoleTitle("My log window");            // redirect unbuffered STDOUT to the console    lStdHandle = (long)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 = (long)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 = (long)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();}
Greenlights across the board except 'MaxConsoleLines'? Is this in a header somewhere or do you define it yourself?
coninfo.dwSize.Y = MaxConsoleLines;

CONSOLE_SCREEN_BUFFER_INFO

dwSize
A COORD structure that contains the size of the console screen buffer, in character columns and rows.

Y is the horizontal value ie character columns

MaxConsoleLines appears to be a global constant. It's probably set to 80. That is the typical number of characters per column in a console window.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Beautiful. Thanks chaps :)
Characters per column doesn't make any sense. That should be characters per line (or per row). And Y suggests vertical not horizontal (doh!!). That would mean that MaxConsoleLines is probably set to 25. 25 lines at 80 characters per line. Sorry for the bum steer. I inverted the two.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Oops, sorry about the MaxConsoleLines. That field, according to MSDN, is:

dwSize
A COORD structure that contains the size of the console screen buffer, in character columns and rows.

I have the Y set to 200. It can be whatever you want the buffer to be able to hold.

This topic is closed to new replies.

Advertisement