.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
ISBN:1590590511
Apress
© 2003
(696 pages)
The authors of this text show how easy it can be to produce
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Preface
Introduction
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
-
.Netterpillars: Artificial Intelligence and Sprites
Chapter 3
-
Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+
Chapter 4
-
River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio
Chapter 5
List of Tables
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
ISBN:1590590511
Apress
© 2003
(696 pages)
The authors of this text show how easy it can be to produce
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Preface
Introduction
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
-
.Netterpillars: Artificial Intelligence and Sprites
Chapter 3
-
Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+
Chapter 4
-
Index
List of Figures
List of Tables
Back Cover
Presented in easy-to-understand language,
.NET Game Programming with DirectX 9.0
is a must-have title for those
who want to create out-of-the-ordinary but simple games, but have never had the time to expend reading hard-
core game developers books. Lobao and Hatton show how easy it can be to produce interesting multimedia games
using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's
Visual Studio. Although their focus is on simplicity, the authors are able to explore all basic concepts within
Managed DirectX 9.0: Direct3D, DirectSound, DirectMusic (using the COM interface), DirectInput (including force-
feedback joysticks), DirectShow, and DirectPlay. There are also chapters covering other technologies that can be
used for game programming: the Speech API to generate game characters' voices, multithreading, and GDI+ for
simple games. A bonus chapter shows how to port a simple game to Pocket PC. Sample games are presented over
two chapters throughout the book. The first chapter presents the game with simple features, and the second
chapter extends the game while presenting new concepts. A library of game programming helper classes is also
created, step-by-step, in each chapter.
About the Authors
Alexandre Santos Lobão has been a non-professional game developer since 1981, when he got his first computer at
12 years of age. He received a bachelor's degree in Computer Science in 1991, from the University of Braslia. When
he was attending the University, he helped to develop a graphics program and a computer graphics language
(LAFITH - Hierarchical Animation Language for Three-dimensional Figures), which organizes objects in a
hierarchical way and calculates the results of forces over the objects, giving them velocity and acceleration. This
language was presented in many Brazilian conferences, and at the international conference, Compugraphics, in
Sesimbra, in 1991.In 2000, he published his first book,
The Box of Pandora and Other Stories
, with short stories.
Although he has written many technical articles, this is his first technical book.
Ellen Hatton is currently completing a computer science degree at Edinburgh University. She was exposed to
Chapter 4
-
River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio
Chapter 5
-
River Pla.Net II: DirectInput and Writing Text to Screen
Chapter 6
-
Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow
Chapter 7
-
Magic KindergarteN. II: Animation Techniques and Speech API
Chapter 8
-
.Netterpillars II: Multiplayer Games and Directplay
Chapter 9
-
D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to
Nonmanaged Code
Bonus Chapter Porting .Nettrix to Pocket PC
Appendix A
-
The State of PC Gaming
Appendix B
-
Motivations in Games
Appendix C
-
How Do I Make Games?
Appendix D
Artist and Cover Designer:
Kurt Krames
Indexer:
Lynn Armstrong
Manufacturing Manager:
Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue,
New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr.
17, 69112 Heidelberg, Germany.
In the United States, phone 1-800-SPRINGER, email <
>, or visit
.
Outside the United States, fax +49 6221 345229, email <
>, or visit
.
For information on translations, please contact Apress directly at 2560 9th Street, Suite 219, Berkeley, CA
94710.
Phone 510-549-5930, fax: 510-549-5939, email <
>, or visit
.
The information in this book is distributed on an "as is" basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by
.NET Game Programming with DirectX 9.0
-
River Pla.Net II: DirectInput and Writing Text to Screen
Chapter 6
-
Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow
Chapter 7
-
Magic KindergarteN. II: Animation Techniques and Speech API
Chapter 8
-
.Netterpillars II: Multiplayer Games and Directplay
Chapter 9
-
D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to
Nonmanaged Code
Bonus Chapter Porting .Nettrix to Pocket PC
Appendix A
-
The State of PC Gaming
Appendix B
-
Motivations in Games
Appendix C
-
How Do I Make Games?
Appendix D
-
Guidelines for Developing Successful Games
Index
List of Figures
Consulting, Johnson & Johnson, City of Hope National Medical Center, Moss Software, ARCO Products,
and Ernst & Young. He has specialized in distributed application and data warehousing solutions over the
Internet and intranets using Visual Basic, C#, and other Internet technology.
Mr. Jung has coauthored an extensive array of books and articles on programming and debugging Visual
Basic, Microsoft Outlook, Java, and other Internet solutions that have been published internationally. He
frequently is a speaker at seminars and user groups discussing how Visual Basic, Java, C#, and Internet
technology can be integrated into business solutions. He has published several Microsoft Windows utility
programs that help track down and eliminate system crashes as well as help prevent script-based viruses
from spreading on Windows-based computers. Two of his programs, DLL Checker and VBS Defender,
were reviewed by PC World and were given an Editor's Choice award, and have been sold to users all
over the world.
Acknowledgments
Alexandre Lobão
To Kentaro Takahashi, the guy who helped me take my first steps in the computer graphics world, back at
university about 15 years ago, and helped me with all my previously published works in this area. I think
this is a late acknowledgement, but better late than never!
To Homero Picollo, the best teacher at my university, who guided me through these first steps.
To Craig Jardine, Jonty Bell, and all the guys from Virtually Real, who believed in my work even before
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
ISBN:1590590511
Apress
© 2003
(696 pages)
The authors of this text show how easy it can be to produce
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Chapter 9
-
D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to
Nonmanaged Code
Bonus Chapter Porting .Nettrix to Pocket PC
Appendix A
-
The State of PC Gaming
Appendix B
-
Motivations in Games
Appendix C
-
How Do I Make Games?
Appendix D
-
Guidelines for Developing Successful Games
Index
List of Figures
List of Tables
they had seen it!
To Evali Gamarra and Keniston Rodrigues, who helped me take my first steps with DirectX.
To Peter Blackburn, who believed in my dream, and to all the Apress guys who helped me to make it
come true.
To Tom Miller, the development leader from the Microsoft's Managed DirectX team, who always had the
right answer for me, and great patience.
To Phillip Taylor, the managed DirectX program manager, and to all members of the Managed DirectX
SIG, who always supported me when I needed.
To Igor Ripoll, who gave me invaluable logistics help, which made this book possible.
To Igor Sinkovec and Waldivar Cesar, who helped me with their great graphics skills.
,
6-4
, and
6-5
, pages 351–352: Sam & Max Hit the Road® and The Secret of Monkey Island®
© 2003 LucasArts Entertainment Company LLC &
™
or ® as indicated. All rights reserved. Used under
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
ISBN:1590590511
Apress
© 2003
(696 pages)
The authors of this text show how easy it can be to produce
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Preface
Introduction
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
Motivations in Games
Appendix C
-
How Do I Make Games?
Appendix D
-
Guidelines for Developing Successful Games
Index
List of Figures
List of Tables
authorization. LucasArts and the LucasArts logo are registered trademarks of Lucasfilm Ltd.
Figures A-1
and
A-2
, page 597: American McGee's Alice
™
Software © 2000 Electronic Arts Inc. All rights
reserved. Portions © 2000 Rogue Entertainment, Inc. All rights reserved. This product contains software
technology licensed from Id Software, Inc. Id Technology © 2000 Id Software, Inc. All rights reserved.
Electronic Arts, the Electronic Arts logo and American McGee's Alice are trademarks or registered
trademarks of Electronic Arts Inc. in the U.S. and/or other countries. The Rogue Entertainment logo is a
trademark of Rogue Entertainment, Inc.
Figure A-3
, page 600: Quake® is a trademark of Id Software.
Figure A-4
, page 600: Unreal Tournament ©1999–2001 Epic Games Inc. Created by Epic Games, Inc. in
collaboration with Digital Extremes. Unreal and the Unreal logos are trademarks of Epic Games, Inc. All
rights reserved. All other trademarks are the property of their respective companies.
Figures A-5
and
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Preface
Introduction
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
-
.Netterpillars: Artificial Intelligence and Sprites
Chapter 3
-
Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+
Chapter 4
-
River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio
Chapter 5
-
River Pla.Net II: DirectInput and Writing Text to Screen
Chapter 6
-
Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow
Chapter 7
-
Magic KindergarteN. II: Animation Techniques and Speech API
When I heard that Alexandre and Ellen were writing a book on gaming, I jumped at the chance to take an
early look. When I heard that the book focused on Visual Basic, I was convinced that I wanted to read it
(and got cajoled into writing this foreword). It's been a long road for BASIC. I used (the other) Bill's BASIC
on my Z80 system and even wrote CP/M systems software using the BASIC-80 compiler in the early
1980s. BASIC has always been seen as a fun little language to write simple "toy" applications—something
like the "Easy-Bake Oven" language with a small lightbulb for heat. Visual Basic 1.0 changed a lot of
minds, but Visual Basic 5.0 and its "real" compiler changed a lot more. Visual Basic .NET (which I fondly
call "Visual Fred" due to its lack of similarity to any previous version of Visual Basic) is a deadly serious
full-featured development tool. It's suitable for authoring virtually anything. No, I would not write a device
driver with Visual Basic .NET, but I'll bet
you
could. It's a great choice for serious developers because it's
so easy to learn and use. For MBASIC-80 developers, it will be somewhat of a challenge to learn, but for
Visual Basic folks, it will take no time at all to come back up to speed (so to speak).
DirectX (in my opinion) is the new de facto "Windows" interface of the gaming industry. In the early days,
no computer would sell (no matter how technically superior it was) if it didn't run Lotus 1-2-3. That's
because the application software was so closely tied to the hardware. When an application shipped in the
DOS world, it had to have drivers for your printer or you couldn't print and drivers for your video card or you
couldn't run the application at all. In those days, game companies got in bed with the video card
companies and wrote straight to their newest hardware. There was only a slim chance a game would work
with some other card. Windows changed some of that. It standardized video management, printing,
memory management, and more to help application developers write just applications and leave all of the
hardware worries to Microsoft and device driver developers. The game folks found that Windows simply
got in the way; it
wasn't nearly fast enough for anything except solitaire—and that was sluggish. When
DirectX arrived, a new dawn appeared for the game developers. Now they had a much faster and generic
way to write to the hardware and not worry (so much) about performance. Now (nine versions later)
DirectX games are pervasive. They're fast, fun, easy to install (and uninstall), and easy to write, maintain,
and support.
As far as the technical content of the book, in case you're wondering, I took an extra close look at
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Preface
Introduction
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
-
.Netterpillars: Artificial Intelligence and Sprites
Chapter 3
-
Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+
Chapter 4
-
River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio
Chapter 5
-
River Pla.Net II: DirectInput and Writing Text to Screen
Chapter 6
-
Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow
Chapter 7
-
Magic KindergarteN. II: Animation Techniques and Speech API
way, then we just create some basic classes that do the dirty work, and create everything else over these
classes.
The whole book is designed to be read in a continuous way. In
Chapter 1
, we start by creating a very
simple game while presenting the basics of collision detection.
Chapter 2
shows how to build a new game,
using the concepts presented in
Chapter 1
and adding new explanations and examples about artificial
intelligence in games.
In the following chapters, we continue to build new games and explore new topics relating to game
programming, such as the basics of sprite creation, scrolling games, adventure games, multiplayer
features, voice generation, porting a game to Pocket PC, and much more. We start with the basics and
increase the complexity as we go along, so that by the time you come to the advanced topics, you have all
the background you need to gain the most from them.
But there's one more thing that makes this book unique: our discussion about several nonprogramming
topics related to game creation—for example, how to design good character movement, the history of
adventure games, and how to create an adventure game from a basic storyline.
We don't restrict ourselves to showing only advanced game programming techniques, like DirectX and the
Speech API. We teach you some simple tricks that don't require a lot of expertise, like how to control
simple dialog in adventure games, using data from a Microsoft Access database, or how to create a
nonrectangular window to add some extra spice to a game's screens.
Although we don't explore all the details about managed DirectX 9.0, this book is a very good starting point
for Managed DirectX 9.0 programming because we delve into the basics of every DirectX
component—Direct3D, DirectSound, DirectMusic, DirectPlay, DirectInput, and DirectShow—creating
reusable classes that will make it easier to use each of these components.
We also discuss other programming topics apart from DirectX, including ADO.NET, Speech API for voice
generation, GDI+ for creating simple games, programming for Pocket PCs with the .NET Compact
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
-
.Netterpillars: Artificial Intelligence and Sprites
Chapter 3
-
Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+
Chapter 4
-
River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio
Chapter 5
-
River Pla.Net II: DirectInput and Writing Text to Screen
Chapter 6
-
Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow
Chapter 7
-
Magic KindergarteN. II: Animation Techniques and Speech API
Chapter 8
-
.Netterpillars II: Multiplayer Games and Directplay
Chapter 9
-
D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to
Nonmanaged Code
Bonus Chapter Porting .Nettrix to Pocket PC
Appendix A
every time, with less programming and higher quality.
Of course, the .NET Framework is still taking its first steps, but it's already worth a closer look for anyone
seriously interested in programming in general, particularly game programming.
Why Use .NET?
Certainly there are many good reasons to use .NET besides the fact that it's a new programming
paradigm that will probably take on the world in the next few years. Let's just review some of them:
.NET is multiplatform: Although Java had promised this before, and sadly didn't make it, .NET had a
bigger industrial commitment, not only by Microsoft and its partners, even before the launch of the
final version of Visual Studio. One example: There are many initiatives to make a .NET JIT (just-in-
time) compiler in Linux and other operating systems. Oh, yes, and there's another advantage over
Java when running on a Web site: The .NET programs are just-in-time compiled before they run, and
that means that they run in machine-native code (not interpreted), which makes for better
performance. Another interesting point is that the compiled program is stored in memory, so the next
executions don't have to pass to the compiler unless the computer is turned off or needs to free the
memory used by the program.
.NET is NOT just a new programming environment: It's a whole new framework of applications
and services, many of them already released by Microsoft, and many others to come from Microsoft
and other companies.
.NET is service based:
There are many servers that expose special features that we can use, and
there are a huge collection of services coming from Microsoft and many other companies. What does
this mean for game developers? For example, do you want to make your game speak, or understand
what the player says? Do you need to add multiplayer features? Do you want to create a 3-D texture-
mapped environment? Well, some services already do all of this and more. Why code again and
again if someone has already done it?
.NET is multilingual: You can write your .NET program in any language supported by the Visual
Studio environment—right now, that means Visual Basic, Visual C#, Visual C++, FORTRAN, Visual
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
Chapter 6
-
Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow
Chapter 7
-
Magic KindergarteN. II: Animation Techniques and Speech API
Chapter 8
-
.Netterpillars II: Multiplayer Games and Directplay
Chapter 9
-
D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to
Nonmanaged Code
Bonus Chapter Porting .Nettrix to Pocket PC
Appendix A
-
The State of PC Gaming
Appendix B
-
Motivations in Games
Appendix C
-
How Do I Make Games?
Appendix D
-
Guidelines for Developing Successful Games
Index
List of Figures
List of Tables
J#, and COBOL, among others. Microsoft created a single and open environment to run it all, so you
Just look at games like the X-COM series, or the SIMCity series. We don't see anything that we can't do in
Visual Basic. They aren't graphically intensive games. For fast-action shooters like Quake, we confess that
Visual Basic won't be the best choice for programming for a while. But for other sophisticated games, if
you can do them with a simple language like VB, why use assembly or C? Just think about the simplicity of
coding and debugging, and you'll see there's no other choice. Even some hardcore C++ programmers
gave us very good feedback about this book, telling us how they were astonished with the ease of creating
games in Visual Basic that still have acceptable performance.
But you don't have to just take our word for it. Just read the rest of the book, and you'll see for yourself.
A Game Starts with a Good Idea
Although the games released nowadays are more and more graphics intensive, the main point in a game
is sometimes forgotten: the playability.
We see games with breathtaking graphics, amazing cut-scenes, and 3-D worlds to make your eyes pop
out, but many of them are really annoying to play. Even when a game's responsiveness is okay,
sometimes the gameplay isn't clear or isn't fair.
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
ISBN:1590590511
Apress
© 2003
(696 pages)
The authors of this text show how easy it can be to produce
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Bonus Chapter Porting .Nettrix to Pocket PC
Appendix A
-
The State of PC Gaming
Appendix B
-
Motivations in Games
Appendix C
-
How Do I Make Games?
Appendix D
-
Guidelines for Developing Successful Games
Index
List of Figures
List of Tables
What about playing an old Pac-Man? With all these gorgeous games around, Pac-Man and the earlier
versions of Mario Brothers on Nintendo are still successes with kids.
We aren't here to tell you to forget everything and get back to basics. Instead, remember that a good
game always starts with a good idea, and sometimes that's enough.
One of the most cloned games ever, Tetris, was designed by a single man, a Russian programmer. It's
still interesting to play after all these years, and, of course, we have a Tetris clone here too—our version of
a "Hello World" program in the first chapter.
You could say that Tetris is one in a million, and we'd agree. But if you were to say that creating a good
game by yourself is only possible if it's as simple as Tetris, then we'd have to disagree. Older guys will
remember Another World, a game that has a sequel called FlashBack. The game had very good graphics
and sound for its time, with very nice character animation and various cut-scenes that completed the game
story by showing the characters and a fantastic world from many different points of view. Well, a single
person, a French programmer, designed this game.
Today we can see many sites on the Web with games from amateur game programmers. Some of them
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
Hatton
ISBN:1590590511
Apress
© 2003
(696 pages)
The authors of this text show how easy it can be to produce
interesting multimedia games using Managed DirectX 9.0 and
programming with Visual Basic .NET on Everett, the latest
version of Microsoft's Visual Studio.
Table of Contents
.NET Game Programming with DirectX 9.0
Foreword
Preface
Introduction
Chapter 1
-
.Nettrix: GDI+ and Collision Detection
Chapter 2
-
.Netterpillars: Artificial Intelligence and Sprites
Chapter 3
-
Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+
Chapter 4
-
River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio
List of Figures
List of Tables
Level design:
The level designers are the ones who'll always be working to ensure optimum
gameplay and the most enjoyable playing experience for players.
Quality assurance:
If we can't afford to have a very good quality assurance team, we're better off not
bothering to make games. A buggy game is by far the worst thing that can happen in a game
company's profile.
Project management: Working with many people with different skills and personalities requires an
organized way to get the best from each of them. Even when we are working alone, we mustn't
underestimate the importance of a good project: If we don't set some milestones to control our
project, we may work on it forever and never see any good results. It's far beyond the scope of this
book to teach you how to manage a project, but we strongly suggest you take a look at some stuff on
this topic, if you've never had the opportunity of working with an organized team.
Etc.:
There'll be lots more too, but in general we must be ready to deal with any new and unexpected
problems.
The task of creating a commercial game nowadays is anything but simple. The time when the "lone wolf"
programmer could create a new hit and even get rich with it is most certainly over. Nevertheless, let's keep
one thing in mind: This book is for those who love game design, who will be happy with making games just
to have the pleasure of seeing people enjoy their ideas. If you want to make professional games, or if you
want to learn DirectX or .NET, this book is a really good starting point, but there's a lot more you need to
study before entering the game industry.
As we have seen, it takes a lot of hard work and coordinated effort to make a blockbuster game
nowadays, but don't be scared off by the size of the mountain we are about to climb. Remember: Maybe
your game will be the next Pac-Man, Tetris, or Flight Simulator.
Just keep in mind one thing: A game
starts
with a good idea!