Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning CSharp Game Programming (2005) [eng]

.pdf
Скачиваний:
151
Добавлен:
16.08.2013
Размер:
4.97 Mб
Скачать

x

Contents

 

 

 

 

More Advanced Class Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.48

 

 

The Basics of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

48

 

 

Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53

 

 

Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

 

 

Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.57

 

 

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

 

 

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

 

 

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

 

 

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

 

Chapter 4

Advanced C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

63

 

 

Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

 

 

Creating Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

65

 

 

Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

 

 

Namespace Aliasing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

 

 

Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

 

 

Basic Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

 

 

Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

68

 

 

Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

 

 

Polymorphism and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

 

 

Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

73

 

 

Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

74

 

 

A Basic Array Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

74

 

 

What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

75

 

 

Inline Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

 

 

References versus Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

 

 

Inheritance and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

 

 

Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

 

 

Another Kind of Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

81

 

 

Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82

 

 

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

 

 

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

 

 

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

85

 

Chapter 5

One More C# Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

87

 

 

Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

88

 

 

Interfaces versus Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . .

89

 

 

Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

 

 

Extending and Combining Interfaces. . . . . . . . . . . . . . . . . . . . . . . .

93

Contents xi

Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94

Exception Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Advanced Exception Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100

Creating a Delegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Chaining Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102

The Array List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Hash Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Stacks and Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Other Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107

Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Readers and Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

File Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114

Seeds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

Generating Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

Other Generation Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

Above and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116

The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

Variable Parameter Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Unsafe Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

C# 2.0 Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

Part II: Game Programming in C#

121

Chapter 6 Setting Up a Framework . . . . . . . . . . . . . . . . . . . . . . . . . . .123

Creating a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123

SharpDevelop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

Visual C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Visual C#’s D3D Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

xii Contents

The Advanced Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134

Have You Got the Time? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

Problems with the Timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

Changes to the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Chapter 7 Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145

DirectX Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146

One Device to Rule Them All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146

It’s All about Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

Buffers and Buffer Swapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

Creating a Device. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

The Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Updating the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154

Setting Up a Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

Handling Multi-Tasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Actually Drawing Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159

Vertexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

Defining Some Vertexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Final Touches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

Colors and Alpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163

Playing with Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Playing with Alpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Another Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

Texturing and Other Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169

Texturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Other Forms of Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Demo 7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177

The Sprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Making the Code Better . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

Demo 7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190

Creating a System Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Demo 7.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

 

 

Contents

xiii

 

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . .193

 

 

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . 194

 

 

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 194

 

 

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 195

 

Chapter 8

DirectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . .197

 

 

Keyboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .197

 

 

Creating a Device. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 198

 

 

Gathering Input by Polling . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 199

 

 

Mice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .200

 

 

Creating a Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 200

 

 

Polling a Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 200

 

 

Game Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .201

 

 

Finding a Game Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 202

 

 

Creating a Game Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 202

 

 

Getting Joystick Axis Data . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 203

 

 

Modifying Axis Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 204

 

 

More Joystick Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 206

 

 

Demo 8.3: Joysticks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 207

 

 

Force Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .210

 

 

The Effect Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 210

 

 

Loading Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 211

 

 

Playing Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 212

 

 

Stopping Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 213

 

 

Demo 8.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 213

 

 

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .216

 

 

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 216

 

 

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 217

 

 

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 217

 

Chapter 9

DirectSound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . .219

 

 

The Sound Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .219

 

 

Sound Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .220

 

 

Playing Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 220

 

 

Buffer Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 221

 

 

Demo 9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 222

 

 

Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .222

 

 

Sound in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .223

 

 

3D Buffers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 223

 

 

Additional 3D Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . 223

 

xiv Contents

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

Chapter 10 Putting Together a Game . . . . . . . . . . . . . . . . . . . . . . . . . . .227

Setting Up a Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 The Game Genre . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 Deciding How the Game Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 The Universe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 The Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229 Spaceships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Weapons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Projectiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Powerups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Common Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 A New Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 Setting Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Device Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Device Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Input Checkers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

Joysticks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 Game States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 State Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 A Sample State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 The Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

Generic Space Shooter 3000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 Game Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 The States for GSS3K . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 The Help State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 The Game State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Playing GSS3K . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

The Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 3D Worlds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Advanced Collision Detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Advanced Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Contents xv

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280

What You Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283

Part III: Appendixes

285

Appendix A Answers to Review Questions . . . . . . . . . . . . . . . . . . . .

. . .287

Chapter 1: The History of C# . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .287

Chapter 2: The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .288

Chapter 3: A Brief Introduction to Classes . . . . . . . . . . . . . . . . . .

. . .291

Chapter 4: Advanced C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . .294

Chapter 5: One More C# Chapter . . . . . . . . . . . . . . . . . . . . . . . . .

. . .296

Chapter 6: Setting Up a Framework . . . . . . . . . . . . . . . . . . . . . . .

. . .298

Chapter 7: Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . .299

Chapter 8: DirectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . .300

Chapter 9: DirectSound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . .301

Chapter 10: Putting Together a Game . . . . . . . . . . . . . . . . . . . . .

. . .301

Appendix B Setting Up DirectX and .NET . . . . . . . . . . . . . . . . . . . . . . . .303

The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303

The .NET SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303

Integrated Development Environments . . . . . . . . . . . . . . . . . . . . . . .304

Managed DirectX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304

Setting Up References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307

Introduction

Only a few short years ago, everyone programmed games in C. There was no question about it—if you wanted to program cutting-edge games, you did so in C. Sure, C++ was around, but it was too “slow.” The advanced features that C++ offered took off too much processing power, and that was simply unacceptable to a game programmer.

Over time, computers got faster and faster and video games got bigger and bigger. Soon, people realized that games were just getting too big to write in C. When programs were small, C was a great language to use because there was no real need for a lot of management in your code. One person could write a program and easily understand what everything did. But C becomes a problem when programs get bigger; it’s just too hard to manage a large program written in C. I’m not going to get into why here—if you’ve ever used C, then you know why.

C++ fixed a lot of problems with C, but maintaining backwards-compatibility was a major problem, and as a result, C++ ended up being one of the biggest language mutations in existence. It’s also a great language, but it has a mighty long list of flaws associated with it.

It used to be that your computer was outdated almost the minute you walked out the computer-store door with it. I found myself upgrading my video card once a year, easily; true die-hard gamers would upgrade twice or even three times a year! Things aren’t like that anymore. My computer has been sitting here for a year and a half, and I haven’t touched the inside of it except to add a new hard drive.

Computers have gotten to a point where they are fast enough to handle most of what you need them to in a reasonable amount of time, and there’s really no huge benefit to upgrading your computer to run the newest games because the newest games are so close to reaching photorealistic quality that huge advances just aren’t being made anymore.

xvi

Introduction xvii

It’s no wonder that “slow” languages like C# and everything else that’s part of .NET are now becoming popular again. Managed languages like C# take a lot more overhead than older languages, but they offer so much more in terms of protection that statistically, you’re much less likely to make bugs in your programs, just because of the way the language is designed. Sure, these languages take more processing power to do more checking for you, but people are realizing that it’s worth it in the end because they allow you to make games in less time, without worrying about tiny little nuances.

Who This Book Is For

This book is for anyone who wants to learn how to program in C# and DirectX 9. You are not required to have any knowledge of C# at all in order to read this book, but some programming background (in any language) would be helpful.

Additionally, you don't have to go out and buy any tools in order to dig into C# programming because everything you need to program in C# is available for free! Look into Appendix B for more information on getting set up to program in C#.

This book will not be a complete comprehensive guide to C#, DirectX, or game programming in general. It is simply intended to give you a jumpstart into the topic. It would be impossible to offer a complete guide to any of those topics in a book of this size (and it would be impossible to offer a complete guide to game programming in a book of any size), so I’ve gone through C# and DirectX and picked out the fundamental topics to cover, as well as other topics that are especially important to game programming.

Book Layout

This book is broken into three different parts. Each part and chapter is previewed in the next sections.

Part I: Learning C#

This section of the book is intended to give you a good look at how to start programming in C#.

Chapter 1: The History of C#

You can’t get a good grasp of any concept without understanding how it came to be, so this chapter tells you why C# and .NET were created and how they work.

Chapter 2: The Basics

This chapter will give you a look at your very first C# program and will introduce you to some basic language concepts, including data types, mathematical operators, variables, constants, type conversions, conditional logic, and looping logic.

xviiiIntroduction

Chapter 3: A Brief Introduction to Classes

Classes are the basic building blocks of any object-oriented language. This chapter will go over how to create classes, the differences between value and reference types, garbage collection, structures, functions, constructors, inheritance, enumerated types, and properties.

Chapter 4: Advanced C#

Once you know all the basics of C# programming, this chapter will take you deeper into the jungle, introducing you to the concepts of namespaces, polymorphism, abstraction, and basic data structures.

Chapter 5: One More C# Chapter

This chapter goes over all the important topics that weren’t covered in the previous chapters, such as interfaces, exceptions, delegates, file access, random numbers, and more advanced data structures.

Part II: Game Programming in C#

Now that you’ve gotten all the basic C# stuff out of the way, this section of the book will introduce you to the basics of accessing DirectX and making a computer game using the various video, input, and sound components.

Chapter 6: Setting Up a Framework

There’s a lot of setup necessary when you’re initializing the various components of a game; this chapter goes over how to create a basic framework with which to start your game projects.

Chapter 7: Direct3D

Graphics programming is one of the most complex parts of games these days, so it’s no surprise that this is one of the longest chapters in the book. It goes over what you need to know in order to create a Direct3D device, back buffers, and display formats, as well as how to handle multi-tasking and how to draw triangles. It also covers color shading, blending, textures, sprites, and text.

Chapter 8: DirectInput

Getting user input is an essential part of game programming, and this chapter covers it all, from keyboards to mice and every game device in between. This chapter also covers force feedback programming.

Introduction xix

Chapter 9: DirectSound

Sound is the final major media component of a game. In this chapter, you will learn how to load and play sounds from disk, and you’ll get to play around with some of the neat effects programming and 3D sound programming features that DirectSound offers as well.

Chapter 10: Putting Together a Game

In this final chapter, you will learn how to combine the knowledge you gained in all of the previous chapters and program an actual game, Generic Space Shooter 3000.

Appendixes

There are two appendixes in this book.

Appendix A: Answers to Review Questions

Every chapter has review questions at the end of it, and this appendix contains the answers to these questions.

Appendix B: Setting Up DirectX and .NET

This appendix goes over how to set up the various components you’ll need in order to start programming your games in C#.

Here We Go!

You’re ready to start reading (and programming in C#!). If you have any questions I’d be glad to answer them; just send me an e-mail at CSBook@ronpenton.net. Please be patient when waiting for a reply—I have many e-mails to answer on a daily basis, and I don’t always have time to get to them in a timely manner.

Are you ready? You’d better be! Here we go!