Sign in to follow this  
acron86

Any way to force a console window?

Recommended Posts

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...

}

Share this post


Link to post
Share on other sites
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();
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this