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

However, I do want to show you the `.RC` and `.H` files that it uses. They are `DEMO3_2.RC` and `DEMO3_2RES.H`, respectively:

Contents of `DEMO3_2RES.H`:

    // defines for sound ids

    #define SOUND_ID_CREATE      1

    #define SOUND_ID_MUSIC       2

    // defines for icons

    #define ICON_T3DX            500

    // defines for cursors

    #define CURSOR_CROSSHAIR     600

Contents of `DEMO3_2.RC`:

    #include "DEMO3_2RES.H"

    // the sound resources

    SOUND_ID_CREATE   WAVE create.wav

    SOUND_ID_MUSIC    WAVE techno.wav

    // icon resources

    ICON_T3DX ICON T3DX.ICO

    // cursor resources

    CURSOR_CROSSHAIR CURSOR CROSSHAIR.CUR

You’ll notice that I have also included the `ICON` and `CURSOR` resources just to make things a little more exciting.

To make `DEMO3_2.CPP`, I took the standard Window demo we have been working with and added calls to sound code in two places: the `WM_CREATE` message and the `WM_DESTROY` message. In `WM_CREATE`, I start two sound effects. One of them says `Creating window` and stops, and the other is a short song in loop mode so it will continue to play. Then, in the `WM_DESTROY` section, I stop all sounds.

> Note: I used the `SND_SYNC` flag as one of the flags for the first sound. This flag is needed because you are only allowed to play one sound at a time with `PlaySound()`, and I didn’t want the second sound to stop the first one in midplay.

Here’s the added code to the `WM_CREATE` and `WM_DESTROY` messages from `DEMO3_2.CPP`:

       case WM_CREATE:

           {

           // do initialization stuff here

           // play the create sound once

           PlaySound(MAKEINTRESOURCE(SOUND_ID_CREATE),

                     hinstance_app, SND_RESOURCE | SND_SYNC);

           // play the music in loop mode

           PlaySound(MAKEINTRESOURCE(SOUND_ID_MUSIC),

                     hinstance_app, SND_RESOURCE | SND_ASYNC | SND_LOOP);

           // return success

       return(0);

       } break;

       case WM_DESTROY:

       {

           // stop the sounds first

           PlaySound(NULL, hinstance_app, SND_PURGE);

       // kill the application, this sends a WM_QUIT message

       PostQuitMessage(0);

           // return success

       return(0);

       } break;

Also, you’ll notice that there is a variable, `histance_app`, used as the instance handle to the application in the `PlaySound()` calls. This is simply a global that saves the `hinstance` sent in `WinMain()`. It is coded right after the class definition in `WinMain()`, like this:

    .

    .

    // save hinstance in global

    hinstance_app = hinstance;

    // register the window class

    if (!RegisterClassEx(&winclass))

     return(0);

    .

    .

To build this application, you’ll need the following files in your project:

  `DEMO3_2.CPP`—The main source file.
  `DEMO3_2RES.H`—The header file contains all the symbols.
  `DEMO3_2.RC`—The resource script itself.
  `TECHNO.WAV`—The music clip, which just needs to be in the working directory.
  `CREATE.WAV`—The creating window vocalization, which needs to be the in working directory.
  `WINMM.LIB`—The Windows Multimedia Library Extensions. This file is found in your compiler’s `LIB\` directory. You should add it to all projects from here on out.
  `MMSYSTEM.H`—The header for `WINMM.LIB`. This is already included as part of `DEMO3_2.CPP`, and all my demos, for that matter. All you need to know is that you need it in your compiler’s search path. It is part of the standard Win32 header file collection.

#### Last, But Not Least—Using the Compiler to Create .RC Files

Most compilers that generate Windows applications come with a quite extensive development environment, such as Microsoft’s Visual Development Studio and so on. Each of these IDEs contains one or more tools to create various resources, resource scripts, and the associated headers automatically and/or with drag-and-drop technology.

The only problem with using these tools is that you have to learn them! Moreover, `.RC` files created with the IDE are in human-readable ASCII, but they have a great deal of added `#defines` and macros that the compiler adds to help automate and simplify the selection of constants and interfacing to MFC (wash your mouth out).

Since I’m a Microsoft VC++ 5.0 user these days, I’ll briefly cover some key elements of using VC++ 5.0’s resource manipulation support. First, there are two ways that you can add resources to your project:

Method 1—Using the File, New option from the main menu, you can add a number of resources to your project. Figure 3.7 is a screen shot of the dialog that comes up. When you add resources like icons, cursors, and bitmaps, the compiler IDE will automatically launch the Image Editor (as shown back in Figure 3.4). This is a crude image editing utility that you can use to draw your cursors and icons. If you add a menu resource (which we will get to in the next section), the menu editor will appear.

Figure 3.7 Adding resources with File, New in VC++ 5.0.

Method 2—This is a bit more flexible and contains all possible resource types, whereas method 1 only supports a few. To add any type of resource to your project, you can use the Insert, Resource option on the main menu. The dialog that appears is shown in Figure 3.8. However, this method does some stuff under the hood. Whenever you add a resource, you must add it to a resource script—right? Therefore, if your project doesn’t already have a resource script, the compiler IDE will generate one for you and call it `SCRIPT*.RC`. In addition, both methods will end up generating (and/or modifying) a file named `RESOURCE.H`. This file contains the resource symbols, ID values, and so on that you define with the editor(s) in relation to resources.

Figure 3.8 Using Insert, Resource to add resources to your application.

I would like to delve much more into the area of resource editing via the IDE, but it’s really a topic for an entire chapter—if not a whole book. Please review your particular compiler’s documentation on the subject. We aren’t going to use many resources in this book, so the info I have already given you will suffice. Let’s move on to a more complex type of resource—the menu.

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.