Instant ebooks textbook Windows Game Programming with Visual Basic and DirectX 1st Edition Freeze download all chapters
Instant ebooks textbook Windows Game Programming with Visual Basic and DirectX 1st Edition Freeze download all chapters
https://ebookultra.com
https://ebookultra.com/download/windows-game-
programming-with-visual-basic-and-directx-1st-
edition-freeze/
https://ebookultra.com/download/introduction-to-3d-game-programming-
with-directx-11-1st-edition-frank-luna/
ebookultra.com
https://ebookultra.com/download/advanced-3d-game-programming-with-
directx-10-0-wordware-game-and-graphics-library-1st-edition-peter-
walsh/
ebookultra.com
https://ebookultra.com/download/special-effects-game-programming-with-
directx-the-premier-press-game-development-series-1st-edition-mason-
mccuskey/
ebookultra.com
https://ebookultra.com/download/directx-11-1-game-programming-1st-
edition-pooya-eimandar/
ebookultra.com
Programming Role Playing Games with DirectX Game
Development Series 2nd Edition Jim Adams
https://ebookultra.com/download/programming-role-playing-games-with-
directx-game-development-series-2nd-edition-jim-adams/
ebookultra.com
https://ebookultra.com/download/programming-with-microsoft-visual-
basic-2012-6th-edition-diane-zak/
ebookultra.com
Programming the Web with Visual Basic NET 1st Edition Lynn
Torkelson
https://ebookultra.com/download/programming-the-web-with-visual-basic-
net-1st-edition-lynn-torkelson/
ebookultra.com
https://ebookultra.com/download/an-introduction-to-programming-using-
visual-basic-2008-with-visual-studio-expression-edition-dvd-schneider/
ebookultra.com
https://ebookultra.com/download/practical-database-programming-with-
visual-basic-net-second-edition-ying-baiauth/
ebookultra.com
Windows Game Programming with Visual Basic and
DirectX 1st Edition Freeze Digital Instant Download
Author(s): Freeze, Wayne S
ISBN(s): 9780789725929, 0789725924
Edition: 1
File Details: PDF, 37.57 MB
Year: 2001
Language: english
00 0789725924 FM 11/21/01 12:54 PM Page i
Contents at a Glance
Introduction 1
Windows
4 Turning 2D Graphics into 3D Graphics 71
®
5 Loading Mall Information 89
6 Displaying 3D Graphics 111
Programming 9
10
11
Simulating the Mall 173
Simulating Customers 193
Simulating Stores and Money 211
Basic and 13
14
15
Commanding the Game 247
Attention, Shoppers 271
A Map and Customers 293
Index 383
Every effort has been made to make this book as complete Page Layout
Susan Geiselman
and as accurate as possible, but no warranty or fitness is
Brad Lenser
implied. The information provided is on an “as is” basis. Michelle Mitchell
The author and the publisher shall have neither liability
nor responsibility to any person or entity with respect to
any loss or damages arising from the information con-
tained in this book or from the use of the CD or programs
accompanying it.
00 0789725924 FM 11/21/01 12:54 PM Page iii
Understanding DirectX 24
Why DirectX? 24
I Diving into the Game DirectX Services 25
Final Thoughts 22
00 0789725924 FM 11/21/01 12:54 PM Page iv
Design Details 90
Disk-Based Structures 90 Part II Simulating Reality
Swim Mall File Format 92
Memory-Based Structures 94 7 Random Numbers Ain’t Random 135
Loading a Saved Game 94 Defining a Random Number 136
The Game Class 94
Using the Game Class 95 Random Numbers and Computers 136
Module Variables in the Game
Creating Random Numbers 138
Class 96
Inside the LoadGame Method 97 Random Numbers and Visual Basic 139
Extracting Data from the Saved Game Randomize and Rnd 139
File 98 Generating Random Numbers with
Reading Data Using the SwimFile Rnd 140
Class 101 Rolling Dice 141
Loading Mall and Store
Information 103 Ranges of Random Numbers 143
Creating Stores and other Mall Scaling Continuous Random
Items 105 Numbers 143
Anchor Properties 108 Creating Discrete Random
Managing Collections of Anchors 108 Numbers 145
Contents v
Contents vii
Index 383
00 0789725924 FM 11/21/01 12:54 PM Page ix
Dedication
In memory of Deb Falkinburg.
Acknowledgments
I want to remember a special lady who is no longer with us. About a dozen years ago, she
was a major influence in my life, along with her husband Tim. Both of them helped me
through a very difficult time in my life. Time and distance has separated us recently, but she
has been in my thoughts a lot lately. I will really miss you, Deb.
There is a long story behind this book, but the short version is that I was supposed to write
it for a different publisher and after a long string of broken promises I decided it wasn’t
worth my sanity to write the book. Then one day while I was talking to Candace Hall at
Que, I described the book to her. Before I could think twice, I was digging the foundation
for Swim Mall. Thanks, Candy.
Along the way, I was able to work with some extraordinary people, such as Michelle
Newcomb, who was very patient when I missed deadline after deadline, and Howard Jones,
who seemed to fill several different roles in this book in addition to the one he was hired
for.
Without the help of a number of people at Caligari, Syntrillium, and Adobe, this book
would not have been possible. I want to thank Roman Ormandy and Kristine Gardner from
Caligari Corporation and Hart Shafer at Syntrillium Software Corporation for copies of
their software, plus permission to include copies of trueSpace and Cool Edit on the CD-
ROM. I also want to thank Will Saso from Adobe Systems Incorporated for his help with
getting copies of Photoshop and Illustrator for use with this book.
I want to acknowledge the support my agent Laura Belt has given me while this book
unfolded. She’s worth her weight in gold (or is that goldens). She helps to keep my friendly
neighborhood banker happy, which in turns helps me keep a roof over my head.
I also want to mention some of my friends: Rick, who believes there isn’t anything I can’t
program; Shaun, who has some misguided views of Java; Ian, for telling me about Cool
Edit; and Bob K., who I thought about when I was discussing the probability theory in this
book. I also want to mention Ariane, Elwyn, Dr. Bob, Veronica, Scott, Randy, and Vikki, all
of whom I miss now that I live in the middle of nowhere.
A big thank-you needs to go to Bucky and Goose for helping out with so many things.
I haven’t seen my Mom and Dad for over a year as I write this, and it makes me sad. Maybe
one of these days I’ll show up on your doorstep and surprise you. In the meantime, Mom,
please get better.
Samantha is my little girl who wants to be a dancing veterinarian, who performs at the
Olympics; I just want her to sit on my lap and be cute. I love you beans.
00 0789725924 FM 11/21/01 12:54 PM Page xi
xi
I want to say to my lovely wife Jill that I truly love you and I’m sorry about the Dummies
book. I know you’ll do well with your plan to create FP while you finish your great
American novel.
Finally, I want to say thank-you to my son and beta tester, Christopher. It was your sense of
humor that really made Swim Mall fun to write. It’s time for bed, CJ. Third grade awaits
you in the morning.
00 0789725924 FM 11/21/01 12:54 PM Page xii
INTRODUCTION
In this introduction
Creating a Computer Game 2
Your Challenge 5
Who Should Read this Book 5
System Requirements 5
Who This Book Covers 6
What This Book Doesn’t Cover 7
Conventions Used in This Book 7
Questions and Comments 8
Other Resources 8
01 0789725924 Intro 11/21/01 12:58 PM Page 2
2 Introduction
I’ve been fascinated by computer games for years. Some of my first experiences date back to
the late 1960s when I was in junior high school. I was allowed to skip math class once a
week to use an old teletype terminal. I spent hours punching a tic-tac-toe game written in
BASIC onto paper tape so that I could play it when I connected to the mainframe.
In college, I was hooked on a Star Trek game written in BASIC that ran on a Hewlett
Packard 2000 timesharing computer. I spent many hours playing the game and discussing
strategy with my classmates. One summer, a few friends and I created a more advanced ver-
sion of the game based on a source code listing we found in Creative Computing, one of the
first hobbyist computer magazines ever created.
Later, I spent a lot of time playing Adventure (also known as The Colossal Cave), which
was created by a pair of MIT students, Willie Crowther and Don Woods, on a mainframe
computer. This game used phrases like “You are standing at the end of a road before a small
brick building” to create images in your mind. It was the first of an entire genre of comput-
er games now known as Interactive Fiction.
Back then, I didn’t care much for labels, I just wanted to beat the game. I created lots of
notes but wasn’t able to beat it. Fortunately, the source code for the game was readily avail-
able. I studied the source code and eventually defeated it with a perfect score.
The first time I saw SimCity, I thought the entire premise for the game seemed dumb. My
wife, on the other hand, thought it was an interesting concept, so she bought it anyway. She
asked me to install in on our computer early the next morning. Eight hours later she found
me still at the computer, playing the game. I hadn’t left my seat! Ever since then, I’ve always
been first in line each time a new version of SimCity came out.
Types of Games
There are many different types of computer games. The key types are listed here:
• Action games were first made popular by such titles as Doom and Tomb Raider. The
premise is that you control the actions of a 3D character as you wander around trying
to solve a set of puzzles. A subset of these games are known as first person shooters,
where a major part of the action involves killing everything in sight.
• Adventure games are similar to the original Adventure game in that you guide one or
more characters on a quest. These games are sometimes known as role playing.
• Arcade games are generally simple 2D games that involve manipulating an object
trough a maze to avoid a series of traps or firing at objects to prevent being destroyed.
Classic examples of this type of game include PacMan, Centipede, and Space Invaders.
• Driving/flying games generally place you behind the wheel while you drive a car or fly
an airplane. The classic example of this type of game is the Microsoft Flight Simulator
series of games.
• Platform games are essentially 2D games where you guide a character through a maze
to solve a puzzle. Their name comes from the fact that in most of the games your char-
acter walks or jumps from platform to platform while solving the puzzle. Mario
Brothers for the Nintendo console is probably the most famous game of this type.
• Puzzle games exist solely to solve puzzles. Examples of these games include the
Solitaire game included with Windows and the Incredible Machine.
• Simulation games attempt to simulate a particular environment. You are not allowed
to directly control the game, rather you must indirectly control the game by modifying
various game parameters. These parameters in turn influence how the simulator con-
trols the details of the game. The defining example of this game type is the SimCity
series of games from Maxis.
• Sports games provide a way to play games such as football, baseball, or hockey on
your computer.
• Strategy games provide you with resources that you can directly control. These
resources can be devoted to creating other resources, expanding to reach a goal or
engaging in warfare. My favorite strategy game is Civilization.
It is often difficult to classify a game as a specific type, because it may have characteristics
that span multiple types.
Visual Basic
The first programs I ever wrote were in a computer language known as BASIC, and
although I have used many programming languages over the years, somehow I keep return-
ing to it. Microsoft’s Visual Basic is the most popular form of the language today and it is
used by more than 3,000,000 programmers, making it the most popular programming lan-
guage in the world.
01 0789725924 Intro 11/21/01 12:58 PM Page 4
4 Introduction
I like Visual Basic for many reasons, but the biggest reason is that it is the easiest language
to use when you want to write an application for Microsoft Windows. Visual Basic includes
excellent debugging tools and many of the problems that drive C++ programmers crazy are
automatically handled for you. Because I’m also known as “The Lazy Programmer,” the less
time I spend writing and debugging programs means that much more time to spend with
my family (or more time to play computer games).
DirectX
DirectX refers to a collection of objects you can incorporate in a program to build a game.
DirectX includes many separate components that address various aspects of game program-
ming. In its latest version, DirectX 8 has evolved to become the primary API for game
developers. Here are some of the key components of DirectX 8.
• DirectDraw is a high-performance interface that makes it practical for a programmer
to create 2D graphics. Although DirectDraw is still included in DirectX 8, it hasn’t
been enhanced since DirectX 7. It continues to exist for compatibility reasons.
• Direct3D is the 3D graphics interface. It uses the hardware assists found in many
video cards to create 3D displays that were impossible only a few short years ago.
• DirectInput provides a unified approach to handing input from the user. This means
your program need not know if the input is coming from a joystick, mouse, or key-
board.
• DirectMusic allows you to play many different music formats on your computer,
including MIDI and MP3.
• DirectSound allows you to create or play various types of sounds in your computer,
which are typically saved in .WAV files.
• DirectPlay makes it easier to coordinate communications between multiple computers
in a multiplayer game.
System Requirements 5
My original intent for Swim Mall was to use 2D graphics along the lines of the original
SimCity game. However, when I started working with DirectX 8, I decided to abandon the
2D approach and go with 3D graphic images. I’m sure that the appearance of the game is
much better with 3D graphics. At the same time, using Direct3D simplified the code need-
ed to generate graphics.
Your Challenge
As you read this book, you may want to keep a running list of ideas of changes that you
would like to make to the game. These changes can range from minor code improvements
to additional game objects or brand new features not implemented in the original game.
When you’re finished with the book, your challenge is to implement these changes using
the source code provided on the CD-ROM and submit them to my Web site at
www.JustPC.com. I’ll collect the changes and post them so that other readers can download
them. Once each month, I’ll review the submissions with my panel of judges and select the
best one. That person will win a small prize. A complete set of rules and instructions will be
posted on the Web site, so stop by, take a look, and send me your code.
System Requirements
Creating a game using 3D graphics requires a relatively substantial computer system. If
your computer doesn’t have 3D accelerator video card, you may as well forget about trying
01 0789725924 Intro 11/21/01 12:58 PM Page 6
6 Introduction
to run it. Also, don’t try to run this game in anything less then a 500Mhz processor. I sug-
gest a minimum of 128MB of memory and recommend at least 256.
I developed this system on a 600MHz computer running Windows 2000 with 384MB of
memory. Windows 98 or Windows Me should also be acceptable, though be prepared to
reboot your computer frequently, as some mistakes will cause your computer to lock up.
Don’t assume that Windows 2000 is the perfect solution either; I managed to crash my sys-
tem several times while debugging parts of the game.
You’ll need a copy of Visual Basic 6 and the DirectX 8 software development kit (SDK).
You can use any edition of Visual Basic, including the Learning Edition, because the user
interface elements come from the DirectX SDK, not the ActiveX controls you would typi-
cally use.
In addition to the DirectX 8 SDK, you’ll need the Microsoft Speech SDK, version 5.1, to
add the text to speech facilities used in the game.
I suggested earlier knowledge of Photoshop or PaintShopPro would prove useful, which
means that it is only useful if you have access to the software. I’ve been using Photoshop for
a long time and highly recommend it to anyone who needs to create 2D graphics.
On the other hand, designing 3D graphics is a true art form and using a 3D design package
like trueSpace requires a lot of time and effort, but it can be done. You won’t absolutely
need trueSpace to run the game as it exists in this book. I’ve already created those images.
However, if you wish to design a new 3D image for the challenge, you’ll need a tool like
trueSpace.
Finally, you’ll need a tool such as Cool Edit to create and edit sound files used in the game.
Among other things, you can use it to create .WAV files from a microphone, copy music
CDs to .MP3 files, and edit and transform the contents of a sound file much like Photoshop
edits and transforms an image.
For your convenience, I’ve included the DirectX and Speech SDKs on the book’s CD-
ROM. Also, I’ve included demo copies of both trueSpace and Photoshop on the CD-ROM.
When you see a note in this book, it indicates additional information that can help you
better understand a topic or avoid problems related to the subject at hand.
8 Introduction
Cautions warn you of hazardous procedures (for example, actions that have the poten-
tial to compromise the security of a system).
Other Resources
Fortunately, there are a lot of resources available for game programmers. Aside from the
Microsoft newsgroups listed previously, there are a number of other DirectX groups like
msnews.microsoft.com that provide more focused discussions on specific features of
DirectX like graphics, sound, input devices, and video. Look for the newsgroups beginning
with the microsoft.public.directx prefix. Be prepared, though, because nearly all of the
discussions in these groups use C++.
The DirectX SDK documentation files and sample programs contain a wealth of informa-
tion that shouldn’t be ignored. Examples are provided for both Visual Basic and C++ and
span the full range of topics from drawing a simple triangle to advanced topics like using
programmable shaders, which go far beyond the scope of this book.
01 0789725924 Intro 11/21/01 12:58 PM Page 9
Other Resources 9
PART
I
Part I: Diving into the Game
2 Introducing DirectX 23
3 Creating 3D Graphics 49
CHAPTER
1
Developing an Idea into a Game
In this chapter
Designing the Game 14
Game Design Document for Swim Mall 15
Chris’s Thoughts 22
Final Thoughts 22
03 0789725924 CH01 11/21/01 12:59 PM Page 14
At one time or another, most computer gamers have wished they could change the way a
game worked. Making the game easier to beat is probably the number one change on most
people’s lists. The second most popular change would be to make the game more challeng-
ing, and third is probably to customize the game by inserting information about people and
places they know.
Changing a game is much easier than developing a game from scratch. For a new game, you
have many details to resolve, such as the story behind the game, the conflict within the
game, and which features should be included, not to mention something so basic as picking
the type of game.
The goal of this chapter is to produce a simple blueprint, or game design document, for imple-
menting Swim Mall. To get a feel for information that will go into the document, you need
to get a feel for Swim Mall. Because the easiest way to get a feel of a piece of software is to
play around with it a bit, I’m going to show you how to install the game and then walk you
though some of the game’s key features. Once you’ve done that, I’ll present the design docu-
ment I used to build this game.
Part
Philosophy
The primary purpose of Swim Mall is to teach someone the concepts of game
programming; therefore many aspects of the game need to be simplified to
ensure that the basic concepts are easily understood.
Swim Mall also must be easily expandable to anyone with access to the source
code. This provides the capability for someone to test his or her knowledge of
the game by adding extensions—a new shopper, a new store, a unique emer-
gency, and so on.
Performance is not a primary issue with this game. While this doesn’t mean that
performance will be ignored, it does imply that the game will not deliver the
same level of performance that a commercial game of similar complexity would
have. This is a direct result of keeping the game simple and understandable.
Optimizing a game is a black art that involves a lot of experience and often
makes the final product difficult to maintain.
As a side effect of focusing on technique rather than performance, all of the files
used by the game will be unencrypted and uncompressed. This may waste some
disk space, but it will make the inner workings of the game easier to understand.
A game need not be complex to be fun to play. Many classic games such as
Solitaire, Asteroids, and SimCity are based on a relatively simple set of rules.
Humor such as puns, jokes, and funny visual elements are used throughout the
game to help compensate for lack of complexity.
The philosophy section is important, because it establishes a set of rules that govern how
the game will work. You will have to make many decisions not spelled out in the game
design document while building the game. By stating the game’s philosophy up front, you
make the choice that best fits with the game’s philosophy.
03 0789725924 CH01 11/21/01 12:59 PM Page 16
Story
Swim Mall is a tale of the Oceanic Mall. Your job as the mall manager is to
transform this small shopping center into the most successful undersea mall in
existence. To increase your customer satisfaction, you must make your mall
attractive to the customers in your neighborhood. Success often brings new
competition, so if you are too successful, you can expect that the other mall
managers will try to steal your customers.
Advertising is important when you need to attract customers to your mall. You
need to worry about where to target your advertisements so they do the most
good.
You also need to worry about which stores you have in the mall to make it as
attractive as possible to your customers. You don’t want a situation where your
mall has five pet stores and six banks.
Each store has a lease agreement with the mall, however, if you set the rent too
high you may drive the store out of business. Likewise if you set the rent to low,
you may not make enough money to survive. Of course, you’d better remember
to renew the leases before they expire or the store will be forced to leave the
mall before the lease is up.
Every game has a story that sets the stage for the game. The storyline for a game like
Duke Nukem is “Kill all of the aliens before they kill us.” Most storylines are more
complex than this because they need to introduce the game’s characters and setting.
The story should also identify the major sources of conflict and competition in the game.
In the end, all games revolve around conflict and competition, no matter if it’s saving
the Earth from invading space aliens or beating the rest of the cars in the race. Without
conflict and competition, you don’t have a game.
Like the game’s philosophy, the story will provide a basis to decide many of the game’s
details. In this case, I couldn’t include an invasion of rats as a disaster simply because the
rats couldn’t survive underwater. However, you can use a crusty crab to get the same
results.
Technical Features
To be appealing to the game player, the game contains these technical features.
These features communicate why the game is technically advanced. Think of them as
advertising that would appear on the game’s box under the heading Technical Features.
■ Real-time simulation engine—Models the economics of the shopping
mall based on the criteria outlined in this document.
03 0789725924 CH01 11/21/01 12:59 PM Page 17
Part
3D graphics engine—Displays the mall as a 3D image (based on Direct3D
■
as implemented in DirectX 8). The graphics engine enables you to view the
I
mall from several different directions as well as control the level of detail Ch
the player can see. 1
■ 3D animation engine—Displays the customers in the mall in real-time.
■ MP3 music player—Provides background music for the game.
■ Speech Synthesizer—Translates text messages into speech.
■ Extendibility—Allows other game developers to modify the game’s source
code and objects to add new features and scenarios.
Game Objects
The game revolves around two main objects, customers and stores, plus a hand-
ful of other objects.
These objects are listed here mostly to help define words and concepts used when describ-
ing how the user controls the game and how the computer will respond.
Customers
Each customer in the game has these characteristics.
■ Name—A value that can be modified by the game’s player.
■ Image—An index into a table of images that will be displayed on the screen
when the customer is present in the mall. Like names, images can be modi-
fied by the player.
■ Needs—A list of needs that are satisfied by the stores in the mall.
■ Location—The zone where the customer is located.
■ Satisfaction—An index for each store in the mall that is a factor in choos-
ing where to shop.
■ Type—Specifies the general type of the customer.
■ Position—Identifies the customer’s position in the mall only when the cus-
tomer is actually in the mall.
Stores
Like customers, each store has a set of characteristics that describe how they are
used within the mall.
■ Name—A value that uniquely identifies the store.
■ Image—A 3D image of the store that will be displayed in the mall.
03 0789725924 CH01 11/21/01 12:59 PM Page 18
Other Objects
The other important objects included in the game are listed below.
■ Bank—Holds money in the checking account for the mall. Also loans
money to the mall based on the total value of the mall.
■ Neighborhood—A high-level view of the area surrounding the mall. This
is used primarily to direct advertising to a particular location and to spot
the location of the other shopping malls with which you are competing.
Note that characteristics of a neighborhood will change over time, which
implies that the customers that shop at the player’s mall will have different
needs over time.
■ Raymond—The assistant mall manager, who provides information and
advice about how to play the game.
■ Mall Strait Journal—The industry standard newspaper for catching the
latest shopping news. It is also the best location to place ads to attract new
stores to your mall.
User Interaction
The user interacts with the game through a well-defined set of controls. There
are three main areas of controls; the pre-game setup where the user selects the
initial conditions for the game, the gameplay activities where the user is actively
playing the game and the game options, where the player can tweak the way the
game works.
Pre-game Setup
In the pregame setup, the user can specify the following items. Note that in
most cases the player doesn’t have complete control over the item, but can
merely select one item from a list of items.
03 0789725924 CH01 11/21/01 12:59 PM Page 19
Part
Initial type of mall—Choose from a predefined list of empty malls. Some
■
variations include a strip mall where the stores are all arranged on one side,
I
a single-story mall and a two-story mall. Note that the initial type of the Ch
mall will also determine the number of stores available. 1
■ Initial funding—Choose the amount of cash and/or initial loan amount,
plus the untapped credit limit. This determines the amount of money with
which the player will start the game.
■ Size and type of neighborhood—Choose from a predefined list of neigh-
borhoods.
■ Number and type of competitors—Choose the maximum number of
other malls in the same neighborhood.
■ Winning condition—Determine the criteria for winning.
Gameplay Activities
The user directly controls the following aspects of the game while the game is
active.
This list should be fairly complete before you start building your game. Adding major
features later could have a negative impact on the overall vision of the game.
■ Location of mall services—Initial placement of mall managed facilities
such as the entrances, food court, bathrooms, stairs, elevators, security
offices, utility plant, benches, telephones, information desk, vending
machines, trashcans, plants and the mall office.
■ Expansion and renovation—Choose when and how to expand the mall to
accommodate new stores and mall services. Also renovate various parts of
the mall as they wear out.
■ Attract stores—Place ads informing potential store owners that the mall is
looking for particular types of stores.
■ Select stores—From the set of stores that respond to the ad, select the
appropriate mix of stores for the mall, negotiate contracts with the stores
(fixed rent, percentage of profit, or a combination of the two), and choose
the appropriate store size plus their store’s placement in the mall. Also con-
trol carts in the hallways.
■ Attract customers by advertising—Place advertising in local and regional
newspapers, TV, and radio.
03 0789725924 CH01 11/21/01 12:59 PM Page 20
Game Options
The player can specify options that permit them to control these items.
■ Game speed—Affects how fast the master clock runs relative to real time.
The player can choose from several speed options and a pause mode that
suspends the game’s activity.
■ Save and restore games—Gives the player a choice to save a currently
running game or to restore a previously saved game.
■ Control sound and music—A general switch that instructs the program to
enable or disable sound effects and background music. Also contains a vol-
ume control to adjust the relative sound levels.
■ Location of music files—Because no MP3 files will be distributed with the
game, it will be the player’s responsibility to supply the name of a directory
where these files may be found.
Part
Neighborhood—Sets the number of the customers available in the neigh-
■
borhood and their average distance from the mall. The neighborhood is
I
broken into zones by average income. Ch
Game Variables
Game variables track how well the player is doing in the game. These variables,
along with the master clock, can be used to determine the winning conditions.
■ Customer satisfaction—Describes the overall success of the shopping
mall. A high level of customer satisfaction will attract customers to the mall,
while a low level will drive customers away.
■ Cash—Contains the amount of money you have to spend in improvements.
In general more money is better than less, though may make negotiating
rents with stores difficult.
■ Loans—Contains the total amount of money that the player has borrowed
from the bank. There is an upper limit on the amount of money that the
bank is willing to lend the mall, based on the total value of the mall.
■ Value—Contains the current value of the mall. Note that the individual
stores aren’t counted as part of the mall, because they represent individual
businesses. However, their leases add value to the mall.
■ Interest rate—Contains the current short-term interest rate. This affects
the interest payment the player must make to the bank.
Cheat Codes
No computer game will be complete without the capability to enter cheat codes.
A list of cheat codes follows.
03 0789725924 CH01 11/21/01 12:59 PM Page 22
Cheat codes are really just another term for debugging aids. They provide shortcuts to
allow someone testing the game to reach a certain stage in the game more quickly,
where they can test a particular feature.
■ Congressperson—Gives you lots of money.
■ Alan Greenspan—Makes interest rates low.
■ Beach Boys Music—Improves the mall’s customer satisfaction level.
■ Income Taxes—Reduces the mall’s customer satisfaction level.
■ Mall Of America—Attracts all of the stores in the game to the player’s
mall.
■ Porsche 944—Displays frame rate, plus camera and viewpoint information
on the screen.
Easter Eggs
These are items hidden in the mall that the player might find while playing the
game. These items can only be found after entering a cheat code.
■ Who is Chris?—Displays a picture of Chris.
■ Electric Wayne—Displays a funny picture of Wayne.
Final Thoughts
The game design document is a way to outline how the game will work. It defines the
things the user will control and how the computer will respond. It’s important to recognize
that the game design document is not a complete design document—it merely outlines the
major pieces of the game from a functional perspective. If the design starts to look like code,
you’ve gone too far.
04 0789725924 CH02 11/21/01 12:55 PM Page 23
CHAPTER
2
Introducing DirectX
In this chapter
Understanding DirectX 24
Introducing 3D Graphics 27
I’m a Little Teapot 31
Final Thoughts 47
04 0789725924 CH02 11/21/01 12:55 PM Page 24
If you want to build a game in Visual Basic you have only one real option, which is to use
DirectX. Without DirectX, you might create a game using a combination of controls and
clever code, but you’d have a difficult time doing animations, and even drawing simple pic-
tures would take a lot of time and resources. Although DirectX can be complicated to use, it
has the advantage of displaying graphical information much faster than the native VB tools
and allows you deal with more complicated graphics than you might believe.
This chapter focuses on how to set up DirectX and draw some simple 3D graphics. The
code in this chapter assumes that you’ve installed the DirectX SDK on your computer.
Although in theory you need only install the DirectX runtime components, it is worthwhile
to install the entire SDK, or at least the Visual Basic components if you’re tight on disk
space. There are many sample programs and other utilities that you’ll find useful, especially
the DirectX Documentation library.
Understanding DirectX
DirectX is an application programming interface (API) that provides low-level access to the
computer’s hardware to create games and other multimedia applications. In addition to pro-
viding support for 2D and 3D graphics, it also provides a standard way to access input
devices, play music and sounds, and coordinate play between multiple players over a net-
work.
Why DirectX?
DirectX was developed as a low-level, high performance replacement for using Win32 APIs
to display information on a screen. Although Win32 APIs in Windows have a lot of capabil-
ity, they are very slow when displaying graphics on the screen. Thus for many years, most
game developers chose to stick with DOS because DOS allowed them to access video cards
directly to give their games the best possible performance.
The downside to using DOS was that the game developer needed a different set of video
drivers for each video card on the market. This meant that some games might not run on a
particular video card because the card wasn’t sufficiently popular enough for the game
developer to develop a video driver or the video card was released after the game.
04 0789725924 CH02 11/21/01 12:55 PM Page 25
Understanding DirectX 25
Because Microsoft wanted to encourage game developers to switch from DOS to Windows,
it needed to provide an alternate way to display graphics that met the needs of game devel-
opers. They came up with an approach that provided a low-level, no frills access to develop-
ers, called DirectX.
DirectX eliminated the need for game developers to write drivers for each video card.
Instead, video card manufacturers were required to supply drivers to standards developed by
Microsoft. Then calls to DirectX would be translated into calls to the video card driver. Part
Although DirectX offered many advantages for the game developer, the early versions I
weren’t well accepted by game developers. However, Microsoft persisted and after awhile
Ch
most game developers began to use DirectX. DirectX version 5 was the first version of
DirectX that was really popular with the game developer community and most games after 2
that point were developed for DirectX.
Even though each new version of DirectX added new capabilities that didn’t exist in previ-
ous versions, Microsoft was careful to preserve the functionality from previous releases. This
ensures that if your game ran properly on DirectX 5, it will run properly on DirectX 6, 7, 8,
and beyond.
Until DirectX version 7, it wasn’t practical for Visual Basic programmers to use DirectX.
Prior to DirectX 7, only a C-style interface was provided. If you wanted to access DirectX
from Visual Basic, you needed a special program that acted as a COM wrapper. It provided a
way to translate COM calls into DirectX calls. In DirectX version 7, Microsoft added a
COM interface to DirectX that allowed Visual Basic direct access to DirectX.
Microsoft made major changes to DirectX 8, including merging the 2D graphics support
into the 3D graphics support. This really means that if you want to develop 2D graphics,
you should stick with the DirectX 7 interfaces. However, Microsoft added many new
functions in DirectX 8 that make it a lot easier to create 3D graphics, as you’ll see in this
chapter.
DirectX Services
DirectX is a collection of COM components organized into four main functional services:
graphics, input management, audio programming, and networking support (see Figure 2.1).
Graphics services are further broken into DirectDraw and Direct3D, whereas DirectX
Audio is broken into DirectMusic and DirectSound.
■ DirectX is the root of all of the DirectX components. It provides the environment in
which the rest of the services operate.
■ DirectDraw displays 2D graphics. Although this service has been stabilized with
DirectX 7 and is no longer enhanced, you can continue to use it in DirectX 8 without
change.
■ Direct3D displays 3D graphics. To use Direct3D, you need a 3D accelerator video card
to ensure adequate performance. Version 8 of Direct3D includes a rich collection of
tools that make 3D programming much easier than it was in the past.
04 0789725924 CH02 11/21/01 12:55 PM Page 26
Figure 2.1
DirectX
The DirectX object
model provides four
main services. DirectX Graphics
DirectDraw
Direct3D
DirectInput
DirectAudio
DirectMusic
DirectSound
DirectPlay
Introducing 3D Graphics 27
Introducing 3D Graphics
Swim Mall makes extensive use of Direct3D graphics. Believe it or not, this simplifies much
of the programming required to display the graphics. If you try to present a 3D appearance
using 2D graphics, you have to develop complex algorithms to determine which object is in
front of another and then hide the parts of the objects that aren’t visible. With Direct3D,
you simply draw your graphics using a 3D coordinate system and Direct3D does the rest.
Part
The 2D Coordinate System I
Visualizing 2D graphics is pretty easy because everything is placed according to two axes Ch
(see Figure 2.2). A coordinate of (3,2) means that this point is located 3 units down the
X-axis, and 2 units down the Y-axis. 2
Figure 2.2 Y
The location (3,2) is
easy to visualize in a
2D coordinate system.
(3,2)
2
X
3
The values of a coordinate increase as you move to the right on the X-axis and up on the
Y-axis. The location where the X- and Y-axes touch is known as the origin and has the coor-
dinate value (0,0). Values to the left of the origin along the X-axis or down from the origin
along the Y-axis are negative.
You can define a 2D shape such as a rectangle by specifying the coordinates for each corner
of the rectangle. For instance, in Figure 2.2 you can see a rectangle formed by using the fol-
lowing coordinates: (3,2), (3,0), (0,0), and (0,2).
Figure 2.3 Y Z
The location (3,2,1) is
harder to visualize in (3,2,1)
a 2D coordinate
system. 2
X
3
Like the 2D coordinate system, values below and to the left of the origin use negative num-
bers. Likewise, values along the Z-axis mean that the coordinate is closer to you than the
origin.
To define a 3D shape such as a box, you need to define the coordinates for each corner of
the box. Because a box has eight corners you need to define eight coordinates. Using the
box shown in Figure 2.3, you have the following coordinates: (3,2,1), (3,2,0), (3,0,0), (3,0,1),
(0,2,0), (0,2,1), (0,0,1), and (0,0,0).
If you select any four coordinates, you basically have the four corners of a square. For
instance the coordinates (3,2,0), (3,0,0), (0,0,0), and (0,2,0) define the side of the box that is
facing you. In fact, if you look at the coordinates you should notice that they are essentially
the same as the square shown in Figure 2.1, except that they include a Z-axis value of 0. So
another way of looking at the box is that it’s merely composed of six rectangles.
Introducing 3D Graphics 29
(3,0,1)
(0,0,0) (3,0,0)
Part
More complex objects can be drawn with triangles. For instance, in Figure 2.5 you can see I
how a circle can be approximated by using a collection of triangles. In the first example, six
Ch
triangles are arranged with their points together in the center of the circle. In the second
example, eight triangles are used. Finally in the third example, 16 triangles are used, giving 2
a fairly close approximation of a circle. If 16 triangles don’t yield a smooth enough circle,
you can always add more until you get the circle you want.
Figure 2.5
The more triangles
you add, the rounder
your circle will look.
The same technique can be applied to nearly any shape. If you use enough triangles you can
approximate anything from a sphere to a fish or even a human.
One of the biggest limitations of graphics is moving data around. Let’s assume that your 3D
object contains 2,000 triangles (which isn’t a lot for some objects). If you use three single
values to store the coordinates of each vertex, you have 72,000 bytes of information to move
(2,000 triangles×3 vertexes per triangle×3 coordinate values for each vertex×4 bytes for each
single value in the coordinate). This is a lot of data, especially if you store other information
for each vertex.
04 0789725924 CH02 11/21/01 12:55 PM Page 30
However, when you’re drawing shapes, nearly all of the triangles share at least one vertex
with another triangle. This is very obvious if you look at Figure 2.5 again. Rather than stor-
ing each triangle explicitly, DirectX allows you to store triangles as either strips or fans (see
Figure 2.6).
1 2,5 1 3 3 2
Discrete Triangle Triangle
Triangles Strip Fan
To draw a regular square with discrete triangles requires six vertexes. However, with a tri-
angle strip, you need only four. The key behind a triangle strip is that after the first trian-
gle, each new vertex uses the last two vertexes to draw a triangle. In theory, this means that
you need only n+2 vertexes to draw a sequence of n triangles. Of course you can’t use trian-
gle strips everywhere, but even they can make a big difference.
Triangle fans are very useful for drawing curved shapes. The first vertex value is the center
of the shape. The next two vertexes define a triangle using the center vertex. The fourth
vertex defines another triangle by using the previous vertex and the center vertex. Like tri-
angle strips, triangle fans are very efficient.
Colors
If you are familiar with Web programming, you may have seen how to specify a color value
using a combination of red, blue, and green values. You can specify a value for each of the
colors in the range of 0 to 255, where 0 means that none of the color is included and 255 is
the maximum.
Colors in DirectX have four components. Like Web colors, you have red, blue, and green
components. However in DirectX, the possible values for the colors range between 0.00
and 1.00, where 0.00 means no color and 1.00 means maximum color.
The fourth component of a DirectX color controls the transparency of an object. This
value is known as alpha. Like red, blue, and green, this value ranges from 0.00 (totally
transparent) to 1.00 (totally opaque).
There are two main kinds of lights in DirectX: direct and ambient. An ambient light illumi-
nates everything equally whereas a direct light is used to cast shadows on your 3D objects.
Ambient lighting doesn’t have a direction and shows up uniformly on your object. Through
the use of ambient lighting, you can see all sides of your 3D objects. Keep in mind that rely-
ing totally on ambient lighting means that your objects won’t have a light side and a dark
side, so some 3D effects are lost.
The location of a direct light is specified as a 3D coordinate value. A simple direct light is Part
unidirectional, meaning that it will shine on the surfaces that are directly in line with the I
light. One problem with using triangles to represent an object is determining which side of
Ch
the triangle is up. A triangle has two sides; a front and a back. The back of the triangle
shouldn’t be visible, whereas the front is supposed to reflect light. 2
To address this problem, DirectX computes a normal vector that describes which way the
face is pointing. It does this by using the three vertexes of the triangle to determine the vec-
tor, which is at right angles to the surface of the triangle. The amount of direct light that
will appear on the object is determined by the angle between the normal vector and the vec-
tor that points from the surface in the direction of the light. The smaller the angle, the
more light will fall onto the surface. If angle is greater than 90 degrees, the surface will not
receive any light.
A viewpoint requires two 3D coordinate values. The first one marks the location of the
viewer, whereas the second marks the location of where the viewer is looking. This estab-
lishes the direction of view. You will see any 3D objects that are in the direction of view.
In addition to the viewpoint vector, you also need to specify the field of view. The field of
view can range from wide angle to telephoto just like a zoom lens on a camera. If you
choose to use a wide angle or telephoto field of view, you may get some distortion so it is a
good idea to leave the field of view alone and simply move the location of the viewer to see
an object up close.
The DXGraphics class (see Table 2.1) deals with the DirectX graphics routines and does most
of the work in this program. The main form merely collects commands that set various
DXGraphics properties or call the appropriate DXGraphics method.
It’s up to you
The entire source code for this program is found on the CD-ROM in
VBGame\Chapter02. You should take some time to load the program into Visual Basic
and run it. Also, please take the time to examine all of the code in this program.
Installing DirectX
Before you develop programs with DirectX 8, you need to install the DirectX 8 Software
Development Kit (SDK) on your computer. A copy of the SDK is on the book’s CD-ROM
in the \Microsoft\DirectX8 directory. Simply run the program dx8a_sdk.exe to install the
SDK. As you follow the steps in the Installation Wizard, make sure that you install the doc-
umentation, the utilities, and the sample programs for Visual Basic. These will prove
extremely helpful for writing your own DirectX programs.
Exploring the Variety of Random
Documents with Different Content
R. of Rs. 35: 256. F. ’07. 40w.
“His effort ‘to be fair to all schools of thought and to all men to
the time’ has, in the opinion of the reviewer, met with indifferent
success. Chapters 7–9 (141 pages) are devoted to the Council of
Trent. Here we at once become aware that the author is treading
on firmer ground. He no longer deals in vague generalities or
manifests the ‘possession’ on his part of vast supplies of
ignorance and misinformation, but he shows interest in the
minutest details and the possession of a creditable amount of
authentic knowledge. These chapters constitute the only really
valuable part of the work and justify its publication.” Albert Henry
Newman.
+ Am. Hist. R. 12: 876. Jl. ’07. 820w.
−
“The treatment of the very large subject is brief and summary,
the point of view is Anglican, and the spirit non-partisan.”
+ Ind. 63: 763. S. 26, ’07. 50w.
“He has to do his work his own way, and he has done it
admirably. But we are sorry to say that he has sometimes been
hasty, and has allowed ill-shapen sentences and sometimes
errors of fact to escape his notice.”
+ Sat. R. 104: 244. Ag. 24, ’07. 440w.
+
−
“A choice collection.”
+ A. L. A. Bkl. 3: 23. Ja. ’07. ✠
“Less hackneyed than those of the Cinderella kind. For that
reason they will read strangely, yet entertainingly to modern
ears.”
+ Ind. 61: 1407. D. 13, ’06. 40w.