Tricks of the Windows Game Programming Gurus:Advanced Windows Programming

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

## Chapter 3
Advanced Windows Programming

> “Are you sure this sweet machine isn’t going to waste?”

> —Dade, Hackers

It doesn’t take a rocket scientist to realize that Windows programming is a huge subject. However, the cool thing about it is that you don’t need to know much to get a lot done. With that in mind, this chapter supplies some of the most important pieces you need to make a complete Windows application. You’ll learn about:

  • Using resources such as icons, cursors, and sounds
  • Menus
  • Basic GDI and the video system
  • Input devices
  • Sending messages

### Using Resources

One of the main design issues that the creators of Windows wanted to address was storing more than just the program code in a Windows application (even Mac programs do this). They reasoned that the data for a program should also reside within the program’s `.EXE` file. This isn’t a bad idea for a number of reasons:

  • A single `.EXE` that contains both code and data is simpler to distribute.
  • If you don’t have external data files, you can’t lose them.
  • Outside forces can’t easily access your data files—such as `.BMP`s, `.WAV`s, and so on—and hack, jack, and distribute them around the planet.

To facilitate this kind of database technology, Windows programs support what are called resources. These are simply pieces of data combined with your program code that can be loaded in later during runtime by the program itself. Figure 3.1 depicts this concept.

Figure 3.1 The relationship of resources to a Windows application.

So what kind of resources are we talking about here? Well, in reality, there is no limit to the types of data you can compile into your program because Windows programs support user-defined resource types. However, there are some predefined types that should take care of most of your needs:

  • Icons—Small bitmapped images used in a number of places, such as the image that you click on to run a program within a directory. Icons use the `.ICO` file extension.
  • Cursors—A bitmap that represents the mouse pointer. Windows allows you to manipulate cursors in a number of ways. For example, you might want the cursor to change as it is moved from window to window. Cursors use the `.CUR` file extension.
  • Strings—The string resource might not be so obvious a choice for a resource. You might say, “I usually put strings into my program anyway, or in a data file.” I can see your point. Nevertheless, Windows allows you to place a table of strings in your program as a resource and to access them via IDs.
  • Sounds—Most Windows programs make at least minimal use of sounds via `.WAV` files. Hence, `.WAV` files can be added to your resources, too. This is a great way to keep people from hijacking your sound effects!
  • Bitmaps—These are the standard bitmaps that you would imagine: a rectangular matrix of pixels in monochrome or 4-, 8-, 16-, 24-, or 32-bit format. They are very common objects in graphical operating systems such as Windows, so they can be added as resources also. Bitmaps use the `.BMP` file extension.
  • Dialogs—Dialog boxes are so common in Windows that the designers decided to make them a resource rather than something that is loaded externally. Good idea! Therefore, you can either create dialog boxes on-the-fly with code, or design them with an editor and store them as a resource.
  • Metafiles—Metafiles are a bit advanced. They allow you to record a sequence of graphical operations in a file and then play the file back.

Now that you have an idea of what resources are and the types that exist, the next question is, how does it all go together? Well, there is a program called a resource compiler. It takes as input an ASCII text resource file with the extension `.RC`. This file is a C/English-like description of all the resources you want to compile into a single data file. The resource compiler then loads all the resources and places them into one big data file with the extension `.RES`.

This `.RES` file contains all the binary data making up whichever icons, cursors, bitmaps, sounds, and so forth that you may have defined in the `.RC` resource file. Then the `.RES` file is taken, along with your `.CPP`, `.H`, `.LIB`, `.OBJ`, and so on, and compiled into one `.EXE`, and that’s it! Figure 3.2 illustrates the data flow possibilities of this process.

Figure 3.2 The data flow of resources during compilation and linking.

#### Putting Your Resources Together

Back in the old days, you would use an external resource compiler like `RC.EXE` to compile all your resources together. But these days, the compiler IDE does all this for you. Hence, if you want to add a resource to your program, you can simply add it by selecting New (in most cases) from the File menu in your IDE and then selecting the resource type you want to add (more on this later).

Let’s review what the deal is with resources: You can add a number of data types and objects to your program, and they will reside as resources within the `.EXE` itself (somewhere at the end), along with the actual computer code. Then, during runtime, you can access this resource database and load resource data from your program itself instead of from the disk as separate files. Furthermore, to create the resource file, you must have a resource description file that is in ASCII text and named `*.RC`. This file is then fed to the compiler (along with access to the resources) and a `*.RES` file is generated. This `.RES` file is then linked together with all your other program objects to create a final `.EXE`. It’s as simple as that! Yeah, right, and I’m a billionaire!

With all that in mind, let’s cover a number of resource objects and see how to create them and load them into our programs. I’m not going to cover all the resources previously mentioned, but you should be able to figure out any others with the information here. They all work in the same manner, give or take a data type, handle, or psychotic episode of staying up all night and not sleeping.

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.