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

#### Creating String Table Resources

As I mentioned in the introduction, Windows supports string resources. Unlike other resources, you can only have one string table that must contain all your strings. Furthermore, string resources do not allow definition by string. Therefore, all string tables defined in your `.RC` files must be accompanied by symbolic reference constants and the associated `.H` header file to resolve the references.

I’m still not sure how I feel about string resources. Using them is equivalent to just using header files, and in either case—string resources or plain header files—you have to recompile. So I don’t see the need for them! But if you really want to get complicated, you can put string resources into `.DLL`s and the main program doesn’t have to be recompiled. However, I’m a scientist, not a philosopher, so who cares?

To create a string table in your `.RC` file, you must use the following syntax

    STRINGTABLE

    {

    ID_STRING1, "string 1"

    ID_STRING2, "string 2"

    .

    .

    }

Of course, the symbolic constants can be anything, as can the strings within the quotes. However, there is one rule: no line can be longer than 255 characters—including the constant itself.

Here’s an example of an `.H` and `.RC` file containing a string table that you might use in a game for the main menu. The `.H` file contains

    // the constant values are up to you

    #define ID_STRING_START_GAME       16

    #define ID_STRING_LOAD_GAME        17

    #define ID_STRING_SAVE_GAME        18

    #define ID_STRING_OPTIONS          19

    #define ID_STRING_EXIT             20

The `.RC` file contains

    // note the stringtable does not have a name since

    // only one stringtable is allowed per .RC file

    STRINGTABLE

    {

    ID_STRING_START_GAME,    "Kill Some Aliens"

    ID_STRING_LOAD_GAME,     "Download Logs"

    ID_STRING_SAVE_GAME,     "Upload Data"

    ID_STRING_OPTIONS,       "Tweak The Settings"

    ID_STRING_EXIT,          "Let’s Bail!"

    }

> Tip: You can put almost anything you want in the strings, including `printf()` command specifiers like `%d`, `%s`, etc. You can’t use escape sequences like `“\n”`, but you can use octal sequences like `\015` and so on.

Once you have created your resource files containing the string resources, you can use the `LoadString()` function to load in a particular string. Here’s its prototype:

    int LoadString(HINSTANCE hInstance,//handle of module withstring resource

                   UINT uID,           //resource identifier

                   LPTSTR lpBuffer,    //address of buffer for resource

                   int nBufferMax);    //size of buffer

`LoadString()` returns the number of characters read, or 0 if the call was unsuccessful. Here’s how you would use the function to load and save game strings during runtime:

    // create some storage space

    char load_string[80], // used to hold load game string

         save_string[80]; // used to hold save game string

    // load in the first string and check for error

    if (!LoadString(hinstance, ID_STRING_LOAD_GAME, load_string,80))

       {

       // there’s an error!

       } // end if

    // load in the second string and check for error

    if (!LoadString(hinstance, ID_STRING_SAVE_GAME, save_string,80))

       {

       // there’s an error!

       } // end if

    // use the strings now

As usual, `hinstance` is the instance of your application as passed in `WinMain()`.

That wraps it up for string resources. If you can find a good use for them, email me at `ceo@xgames3d.com`!

#### Using Sound .WAV Resources

By now you’re either getting very comfortable with resource scripting or you’re so upset that you’re about to hack into my Web site and destroy me. Remember, it wasn’t me—it was Microsoft (http://www.microsoft.com) that invented all this stuff. I’m just trying to make sense of it too!

All right, dog. Now that I’ve given you my occasional disclaimer, let’s continue by loading some sound resources!

Most games use one of two types of sounds:

  • Digital `.WAV` files
  • MIDI `.MID` music files

To my knowledge, the standard resources for Windows only support `.WAV` files, so I’m only going to show you how to create `.WAV` resources. However, even if `.MID`s aren’t supported, you can always create a user-defined resource type. I’m not going to go into this, but the ability to do so is there.

The first thing you need is a `.WAV` file, which is simply a digital waveform of data that contains a number of 8- or 16-bit samples at some frequency. Typical sample frequencies for game sound effects are 11KHz, 22KHz, and 44KHz (for CD-level quality). This stuff doesn’t concern you yet, but I just wanted to give you a heads up. You’ll learn all about digital sampling theory and `.WAV` files when we cover DirectSound. But for now, just know that sample size and rate are issues.

With that in mind, let’s assume that you have a `.WAV` file on disk, and you want to add it to a resource file and be able to load and play it programmatically. Okay, let’s go! The resource type for `.WAV` files is `WAVE`—there’s a surprise. To add it to your `.RC` file, you would use the following syntax.

Method 1—By string name:

    wave_name WAVE FILENAME.WAV

Examples:

    BigExplosion WAVE expl1.wavr

    FireWeapons WAVE fire.wav

Method 2—By integer ID:

    ID_WAVE CURSOR FILENAME.CUR

Examples:

    DEATH_SOUND_ID WAVE die.wav

    20             WAVE intro.wav

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.