ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!
Resource Files
Resource files enable you to store bitmaps, waves, AVI's, icons, cursors, strings,
and more... all in one file. I know, I know... Momma always told you not to put all
of your eggs in one basket, right? Well Momma should take her bitchin' to the
kitchen (I KID, I KID! Don't yell at Lucky!). It is to your advantage to use a
resource file, the alternative is to leave all of your resources exposed, where the
user can get at them and modify them. Once you've squished them into a resource file
the user cannot touch them! HA!
One of the reasons that Lucky loves resource files so much is because he's paranoid.
Lucky is paranoid that once he releases the game he's working on (have
a look) that people would alter
his naked bitmaps or wave files and then re-distribute his game! They could draw a
penis on the Altairian Space Station and then give it to other people, and they'd think
that Lucky did it! I'll stick with my resource files, thank-you!
Why does Lucky always refer to himself in the third-person? Lucky's response:
"It's either that, or use the Royal 'we', and that's a bit snooty. Who is this mystical
'third person' anyway? We'd like to give him a piece of our mind."
Ahem. So, to make a resource file you need a couple of things from your VB disk.
Go root around in there and find the \Tools subdirectory of the main Visual Basic directory.
Found it? Good. That RC.EXE file is what you need (have a look at the help files too).
Store it on your HD somewhere, it's just easier that way.
Now, you need to make a file called FILENAME.RC in a text editor. Change "FILENAME" to
whatever you want the name of your resource file to be. Inside the text editor, you will
describe which files you want to include in the resource, and how you want to refer to
them in code. The syntax is:
nameID keyword filename
- nameID - the name by which you will refer to this item in code.
- keyword - a keyword defining what type of file this is. It can be BITMAP, CURSOR,
ICON, SOUND, or VIDEO. SOUND is a .WAV file, VIDEO is an .AVI file.
- filename - the path to the file on your HD.
Ok, for example, if I want to load a bitmap called "VIEWSCRN.BMP" into my resource file,
and refer to that bitmap as "ViewScreen" in my code, I'd write:
ViewScreen BITMAP C:\BITMAPS\VIEWSCRN.BMP
We keep adding entries to the .RC file until we've included everything we wish to include.
It'll end up looking something like this:
Once you're done, save the file and run the RC.EXE file on it. This program will gather
all of the files you've listed and compile them into a file called FILENAME.RES. Use the
command line (yes, I said command line... venture out into the world of DOS for a moment)
RC /r FILENAME.RC
... and it'll spit out the resource file for you. Now, go into your VB Project and add the
resource file by pressing Ctrl-D and locating the file. Once you've included the resource
in your project you can start using it in code. Use LoadResData or LoadResPicture to use
the data you've stored in the resource file.
LoadResPicture("ViewScreen",0)
This command will return the bitmap "VIEWSCRN.BMP" that we stored in the resource file.
We can then place it in a picture box control, or on a DirectDraw surface. The second
argument for the LoadResPicture function determines what type of file we're accessing,
"0" refers to bitmaps. "1" refers to icons, and "2" to cursors. Other types (Such as
AVI's and WAV's) must be accessed through the LoadResData function.