Tricks of the Windows Game Programming Gurus:Windows GDI, Controls, and Last-Minute Gift Ideas

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

### The T3D Game Console

In the beginning of the book I mentioned that Win32/DirectX programming is almost like 32-bit DOS programming if you create a shell Windows application and then create a code structure that hides the details of the dull Windows stuff that’s going on. Now you know enough to do this. In this section you’ll see how to put together the T3D Game Console, which will be the basis for all the demos and games from here on.

At this point, you know that to create a Windows application you need a `WinProc()` and a `WinMain()` and that’s about it. So we’ll create a minimal Windows application that has these components and create a generic window. The application will then call out to three functions that perform the game logic. As a result, the details of handling Windows messages and other Win32 related drama won’t be an issue (unless of course you want it to be). Take a look at Figure 4.14 to see the T3D Game Console architecture.

Figure 4.14 The architecture of the T3D Game Console.

As you can see, there are only three functions that are needed to implement the console:

    int Game_Init(void *parms = NULL, int num_parms = 0);

    int Game_Shutdown(void *parms = NULL, int num_parms = 0);

    int Game_Main(void *parms = NULL, int num_parms = 0);

  • `Game_Init()` is called before the main event loop in `WinMain()` is entered and is called only once. Here is where you’ll initialize everything for your game.
  • `Game_Main()` is like `main()` in a normal C/C++ program except that it is called each cycle after any Windows message handling is performed by the main event loop. This is where the entire logic of your game will be. You’ll do all the rendering, sound, AI, and so forth in `Game_Main()` or as calls out of `Game_Main()`. The only caveat about `Game_Main()` is that you must draw only one frame and then return, so you don’t starve the `WinMain()` event handler. Also, because this function is entered and exited each cycle, remember that automatic variables are transient—if you want data to stick around, make it global or local static to `Game_Main()`.
  • `Game_Shutdown()` is called after the main event loop in `WinMain()` is exited, which is caused by a message sent from the user, ultimately causing a `WM_QUIT` message to be posted. In `Game_Shutdown()` you’ll do all your housekeeping and cleanup of resources allocated during game play.

The T3D Game Console is contained in the file `T3DCONSOLE.CPP`. Below is the `WinMain()` section showing the calls to all the console functions:

    // WINMAIN ////////////////////////////////////////////////

    int WINAPI WinMain(HINSTANCE hinstance,

                HINSTANCE hprevinstance,

                LPSTR lpcmdline,

                int ncmdshow)

    {

    WNDCLASSEX winclass; // this holds the class we create

    HWND       hwnd;     // generic window handle

    MSG       msg;          // generic message

    HDC        hdc;      // graphics device context

    // first fill in the window class structure

    winclass.cbSize         = sizeof(WNDCLASSEX);

    winclass.style        = CS_DBLCLKS | CS_OWNDC |

                            CS_HREDRAW | CS_VREDRAW;

    winclass.lpfnWndProc    = WindowProc;

    winclass.cbClsExtra        = 0;

    winclass.cbWndExtra        = 0;

    winclass.hInstance        = hinstance;

    winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);

    winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);

    winclass.hbrBackground    = GetStockObject(BLACK_BRUSH);

    winclass.lpszMenuName    = NULL;

    winclass.lpszClassName    = WINDOW_CLASS_NAME;

    winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

    // save hinstance in global

    hinstance_app = hinstance;

    // register the window class

    if (!RegisterClassEx(&winclass))

        return(0);

    // create the window

    if (!(hwnd = CreateWindowEx(NULL,                  // extended style

                                WINDOW_CLASS_NAME,     // class

                    "T3D Game Console Version 1.0", // title

                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,

                    0,0,     // initial x,y

                    400,300,   // initial width, height

                    NULL,    // handle to parent

                    NULL,    // handle to menu

                    hinstance, // instance of this application

                    NULL)))    // extra creation parms

    return(0);

    // save main window handle

    main_window_handle = hwnd;

    // initialize game here

    Game_Init();

    // enter main event loop

    while(TRUE)

        {

        // test if there is a message in queue, if so get it

        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

           {

           // test if this is a quit

           if (msg.message == WM_QUIT)

               break;

           // translate any accelerator keys

           TranslateMessage(&msg);

           // send the message to the window proc

           DispatchMessage(&msg);

           } // end if

           // main game processing goes here

           Game_Main();

        } // end while

    // closedown game here

    Game_Shutdown();

    // return to Windows like this

    return(msg.wParam);

    } // end WinMain

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.