Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Beginning C#
PREMIUM
Số trang
342
Kích thước
4.6 MB
Định dạng
PDF
Lượt xem
1264

Beginning C#

Nội dung xem thử

Mô tả chi tiết

TEAM LinG

Beginning C#

Game Programming

TEAM LinG

© 2005 by Thomson Course Technology PTR. All rights reserved. No

part of this book may be reproduced or transmitted in any form or by

any means, electronic or mechanical, including photocopying, recording,

or by any information storage or retrieval system without written

permission from Thomson Course Technology PTR, except for the

inclusion of brief quotations in a review.

The Premier Press and Thomson Course Technology PTR logo and

related trade dress are trademarks of Thomson Course Technology PTR

and may not be used without written permission.

Microsoft and DirectX are registered trademarks of Microsoft Corpora￾tion in the United States and/or other countries. All other trademarks

are the property of their respective owners.

Important: Thomson Course Technology PTR cannot provide software

support. Please contact the appropriate software manufacturer’s techni￾cal support line or Web site for assistance.

Thomson Course Technology PTR and the author have attempted

throughout this book to distinguish proprietary trademarks from descrip￾tive terms by following the capitalization style used by the manufacturer.

Information contained in this book has been obtained by Thomson

Course Technology PTR from sources believed to be reliable. However,

because of the possibility of human or mechanical error by our sources,

Thomson Course Technology PTR, or others, the Publisher does not

guarantee the accuracy, adequacy, or completeness of any information

and is not responsible for any errors or omissions or the results obtained

from use of such information. Readers should be particularly aware of

the fact that the Internet is an ever-changing entity. Some facts may have

changed since this book went to press.

Educational facilities, companies, and organizations interested in multi￾ple copies or licensing of this book should contact the publisher for

quantity discount information. Training manuals, CD-ROMs, and por￾tions of this book are also available individually or can be tailored for

specific needs.

ISBN: 1-59200-517-9

Library of Congress Catalog Card Number: 2004107745

Printed in the United States of America

04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1

Thomson Course Technology PTR,

a division of Thomson Course Technology

25 Thomson Place

Boston, MA 02210

http://www.courseptr.com

SVP, Thomson

Course Technology PTR:

Andy Shafran

Publisher:

Stacy L. Hiquet

Senior Marketing Manager:

Sarah O’Donnell

Marketing Manager:

Heather Hurley

Manager of Editorial Services:

Heather Talbot

Senior Acquisitions Editor:

Emi Smith

Senior Editor:

Mark Garvey

Associate Marketing Manager:

Kristin Eisenzopf

Marketing Coordinator:

Jordan Casey

Project Editor/Copy Editor:

Estelle Manticas

Technical Reviewer:

Brian Lich

PTR Editorial Services Coordinator:

Elizabeth Furbish

Interior Layout Tech:

Shawn Morningstar

Cover Designer:

Mike Tanamachi

CD-ROM Producer:

Brandon Penticuff

Indexer:

Katherine Stimson

Proofreader:

Nancy Sixsmith

Beginning C#

Game Programming

Ron Penton

iv

I

would first like to thank my family for supporting me through this, my third book. It’s

been a long three years, hasn’t it?

I would also like to thank all of my friends for their encouragement and friendship, espe￾cially Jim, Andrew, Dan, James, Scott, Tracy, Jenny, Josefina, Brett, Kristy, Wendy, Lisa,

Marla, Irina, Yelena, Tina, Jordi, and Liz.

I would also like to thank everyone at work.

Finally, I would like to thank everyone I know in the game development scene, specifically

(and in no particular order): Dave Astle, Kevin Hawkins, Trent Polack, Evan Pipho, April

Gould, Joseph Fernald, Andrew Vehlies, Andrew Nguyen, John Hattan, Ken Kinnison, Seth

Robinson, Ernest Pazera, Denis Lukianov, Sean Kent, Nicholas Cooper, Ian Overgard,

Greg Rosenblatt, Yannick Loitière, Henrik Stuart, Chris Hargrove, Richard Benson, Mat

Noguchi, Richard “Superpig” Fine, Anthony Casteel, Danny McCue, Tyler “Acoustica”

Roehmholdt (socialite extraordinaire), Mike Stedman, Pouya Larjani, “They Call Me

Fred” Fred, Mark “SteelGolem” Yorke, Jesse Towner, Jean McGuire, Andrew Russell,

Thomas Cowell, Matthew “Programmer One” Varga, Dillon Cower, Matthew Daley, Jack

McCormack, Patrick van der Willik, and Kent “_dot_” Lai Shiaw San.

Acknowledgments

RON PENTON has always tinkered around with video games. From the age of 11, when his

parents bought him his first game-programming book on how to make adventure games,

Ron has always striven to learn the most about how games work and how to create them.

Ron holds a bachelor’s degree in Computer Science and a minor in Mathematics from The

State University of New York at Buffalo. He has written two other books, Data Structures for

Game Programmers, and MUD Game Programming. Ron has also contributed to Bruno de

Sousa’s book Game Programming All in One.

You can view Ron’s personal Web site at http://ronpenton.net.

v

About the Author

This page intentionally left blank

vii

Contents at a Glance

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvi

Part I: Learning C# 1

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

Chapter 2 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13

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

Chapter 4 Advanced C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63

Chapter 5 One More C# Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87

Part II: Game Programming in C# 121

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

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

Chapter 8 DirectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197

Chapter 9 DirectSound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219

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

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

Part III: Appendixes 285

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

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

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

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvi

Part I: Learning C# 1

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

A Brief History of Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3

Machine and Assembly Languages . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Portability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

High-Level Languages Save the Day . . . . . . . . . . . . . . . . . . . . . . . . . 5

Portability with Virtual Machines. . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

.NET to the Rescue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7

Just In Time Compilation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Reduction Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

The Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10

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

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

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

viii

Contents

Chapter 2 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13

Why You Should Read This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . .13

Your First C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14

Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

The Entry Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Hello, C#!! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Compiling and Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16

Basic Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Typecasts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23

if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

Switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Short-Circuit Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27

while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

do-while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Break and Continue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31

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

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

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

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

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

Value Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Basics of Structures and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39

Creating Classes and Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

Differences between Structures and Classes . . . . . . . . . . . . . . . . . . 40

Putting Functions in Your Classes and Structures . . . . . . . . . . . . . . 41

Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Contents ix

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

x Contents

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

Contents xi

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

xii Contents

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

Contents xiii

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

xiv Contents

Tải ngay đi em, còn do dự, trời tối mất!