This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Console/Windowed Mode Submitted by

Some may already know this little trick in Visual C++ (6.0), but I just ran across it last night.
I'm currently working on a game framework for windows that doesn't care if you are running as a windowed or console application. And I've been looking for a nice way of wrapping the functionality of switching between the two modes as some kind of macro set. After a fair bit of twiddling, I've put together the following little code snippet.

    // Uncomment the next line and define the appropriate
    // subsytem in the linker
    //#define _CONSOLE
    #ifndef _CONSOLE
    #pragma comment( linker, "/subsystem:windows" )
    int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    #else

    #pragma comment( linker, "/subsystem:console" )
    int main(int argc, char* argv[] )
    #endif
    {
       //do stuff
    }

Yeah, it's an ugly looking directive set and there have got to be at least half a dozen ways to make it pretty. However, the point is, by defining _CONSOLE, I get a console application. By leaving it as is, I get a windowed application. Providing that I do the appropriate code for creating a window.
Why do this? Well, recently I've been seeing a lot of COTD's that have been talking about output to a console. Seems to me that if you replace _CONSOLE with _DEBUG ... you can get a relatively cheap log window.
Your thoughts? Suggestions? Anyone want to try this on a compiler other than VC++?
-Ash

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.