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

Table 2.1 contains all the prefix codes used in Hungarian notation. These codes are used to prefix variable names in most cases, along with other conventions depending on what is being named. Refer to the table for the remaining explanations.

Table 2.1 The Hungarian Notation Prefix Codes Specification

Prefix | Data Type (Base Type)
--- | ---

`c` | `char`
`by` | `BYTE` (unsigned `char`)
`n` | `short` or `int` (refers to a number)
`i` | `int`
`x`, `y` | `short` (used as x-coordinate or y-coordinate, generally)
`cx`, `cy` | `short` (used to denote x or y lengths; `c` stands for count)
`b` | `BOOL` (`int`)
`w` | `UINT` (unsigned `int`) or `WORD` (unsigned `WORD`)
`l` | `LONG` (`long`)
`dw` | `DWORD` (unsigned `long`)
`fn` | Function pointer
`s` | String
`sz`, `str` | String terminated by 0 byte
`lp` | 32-bit long pointer
`h` | Handle (used to refer to Windows objects)
`msg` | Message

#### Variable Naming

With Hungarian notation, variables are prefixed by the codes in Table 2.1. In addition, if a variable name is made up of one or more subnames, each subname is capitalized. Here are some examples:

    char *szFileName; // a null terminated string

    int *lpiData;     // a 32-bit pointer to an int

    BOOL bSemaphore;  // a boolean value

    WORD dwMaxCount;  // a 32-bit unsigned WORD

Although I know of no specification for local variables of a function, there is a loose one for globals:

    int g_iXPos;      // a global x-position

    int g_iTimer;     // a global y-position

    char *g_szString; // a global NULL terminated string

Basically, you begin the variable with `g_`, or sometimes just plain `g`. “When I grew up, I was a big G, lots of money…” Sorry, I had a rap attack <BG>.

#### Function Naming

Functions are named in the same way variables are, but without the prefixes. In other words, just capitalize all the first letters of subnames. Here are some examples:

    int PlotPixel(int ix, int iy, int ic);

    void *MemScan(char *szString);

Also, underscores are illegal. For example, the following wouldn’t be a valid Hungarian-compliant function name:

    int Get_Pixel(int ix, int iy);

#### Type and Constant Naming

All types and constants are in uppercase, but you’re allowed to use underscores in the names. For example:

    const LONG NUM_SECTORS = 100; // a C++ style constant

    #define MAX_CELLS 64          // a C style constant

    #define POWERUNIT 100         // a C style constant

    typedef unsigned char UCHAR;  // a user defined type

Nothing too unusual here—fairly standard definitions. Although most Microsoft programmers don’t use underscores, I prefer to use them because it makes the names more readable.

> Tip: In C++, the `const` keyword has more than one meaning, but in the preceding code lines, it’s used to create a constant variable. This is similar to `#define`, but it has the added property of retaining the type information. `const` is more like a variable than a simple preprocessed text replacement like `#define`. It allows compiler type-checking and casting to occur.

#### Class Naming

The naming conventions used for classes might bother you a bit. However, I have seen many people who use this convention and just made it up on their own. Anyway, all C++ classes must be prefixed by a capital `C`, and the first letter of each subname of the class name must be capitalized. Here is an example:

    class CVector

    {

    public:

    CVector(); {ix=iy=iz=imagnitude = 0;}

    CVector(int x, int y, int z) {ix=x; iy=y; iz=z;}

    .

    .

    private:

        int ix,iy,iz;   // the position of the vector

        int imagnitude;  // the magnitude of the vector

    };

#### Parameter Naming

Parameters to functions follow the same naming conventions that normal variables do. However, this is not a necessity. For example, you might see a function definition that looks like this:

    UCHAR GetPixel(int x, int y);

In this case, the more Hungarian prototype would be

    UCHAR GetPixel(int ix, int iy);

But I have seen it both ways.

And finally, you might not even see the variable names, but just the types, as in this example:

    UCHAR GetPixel(int, int);

Of course, this would only be used for the prototype, and the real function declaration must have variable names to bind to, but you get the point.

> Note: Just because you know how to read Hungarian notation doesn’t mean that you have to use it! In fact, I have been programming for over 20 years, and I’m not going to change my programming style for anyone (well, maybe Pamela Anderson). Hence, the code in this book will use a Hungarian-like coding style where Win32 API functions are concerned, but it’ll use my own style in other places. One thing is for certain—I’m not capitalizing each word of my variable names! And I’m using underscores, too!

### The World’s Simplest Windows Program

Now that you have a general overview of the Windows OS and some of its properties and underlying design issues, let’s begin our journey into real Windows programming with our first Windows program.

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.