Tricks of the Windows Game Programming Gurus:The Windows Programming Model

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

It’s customary to write a “Hello World” program in any new language or OS that you’re learning, so let’s try that. Listing 2.1 is the standard DOS-based “Hello World.”

Listing 2.1 A DOS-Based “Hello World” Program

    // DEMO2_1.CPP - standard version

    #include <stdio.h>

    // main entry point for all standard DOS/console programs

    void main(void)

    {

    printf("\nTHERE CAN BE ONLY ONE!!!\n");

    } // end main

Now let’s see how it’s done with Windows.

By the way, if you want to compile `DEMO2_1.CPP`, you can actually create what’s called a console application with the VC++ or Borland compilers. These are like DOS applications, but 32-bit. They run only in text mode, but they’re great for testing out ideas and algorithms.

#### It All Begins with WinMain()

As I mentioned before, all Windows programs begin execution at the function named `WinMain()`. This is equivalent to `main()` in a straight DOS program. What you do in `WinMain()` is up to you. If you want, you can create a window, start processing events, and draw things on the screen. On the other hand, you can just make a call to one of the hundreds (or are there thousands?) of Win32 API functions. This is what we’re going to do.

I just want to print something on the screen in a little message box. There just so happens to be a Win32 API function that does this—`MessageBox()`. Listing 2.2 is a complete, compilable Windows program that creates and displays a message box that you can move around and close.

Listing 2.2 Your First Windows Program

    // DEMO2_2.CPP - a simple message box

    #define WIN32_LEAN_AND_MEAN

    #include <windows.h>        // the main windows headers

    #include <windowsx.h>       // a lot of cool macros

    // main entry point for all windows programs

    int WINAPI WinMain(HINSTANCE hinstance,

                       HINSTANCE hprevinstance,

                       LPSTR lpcmdline,

                       int ncmdshow)

    {

    // call message box api with NULL for parent window handle

    MessageBox(NULL, "THERE CAN BE ONLY ONE!!!",

                     "MY FIRST WINDOWS PROGRAM",

                      MB_OK | MB_ICONEXCLAMATION);

    // exit program

    return(0);

    } // end WinMain

To compile the program, follow these steps:

  1. Create a new Win32 `.EXE` project and include `DEMO2_2.CPP` from `T3DCHAP02\` on the CD-ROM.
  2. Compile and link the program.
  3. Run it! (Or run the precompiled version, `DEMO2_2.EXE`, on the CD-ROM.)

And you thought that a basic Windows program had hundreds of lines of code! Anyway, when you compile and run the program, you should see something like what’s depicted in Figure 2.5.

Figure 2.5 Running `DEMO2_2.EXE`.

#### Dissecting the Program

Now that you have a complete Windows program, let’s take it apart line by line and see what’s going on. The very first line of code is

    #define WIN32_LEAN_AND_MEAN

This deserves a bit of explanation. There are two ways to create Windows programs—with the Microsoft Foundation Classes (MFC), or with the Software Development Kit (SDK). MFC is much more complex, totally based on C++ and classes, and 10 times more powerful and complicated than you will ever need for games. On the other hand, the SDK is manageable, can be learned in a week or two (at least the rudiments of it), and uses straight C. Hence, the SDK is what I’m going to use in this book.

So, `WIN32_LEAN_AND_MEAN` instructs the compiler (header file logic, actually) not to include extraneous MFC overhead. Now that we have that out of the way, let’s move on.

Next, the following header files are included:

    #include "windows.h"

    #include "windowsx.h"

The first include of `“windows.h”` really includes all the Windows header files. There are a lot of them, so this is something like an inclusion macro to save you from manually including dozens of explicit header files.

The second include, `“windowsx.h”`, is a header that contains a number of important macros and constants that make Windows programming easier.

And now, for the important part—the main entry point of all Windows applications, `WinMain()`:

    int WINAPI WinMain(HINSTANCE hinstance,

                       HINSTANCE hprevinstance,

                  LPSTR lpcmdline,

                 int ncmdshow);

First off, you should notice that weird `WINAPI` declarator. This is equivalent to the `PASCAL` function declarator, which forces the parameters to be passed from left to right, rather than the normal right-to-left order with the default `CDECL`. However, the `PASCAL` calling convention declarator is now obsolete, and `WINAPI` has taken its place. You must use `WINAPI` for the `WinMain ()` function; otherwise, the startup code will end up passing the parameters incorrectly to the function!

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.