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

Beginning CSharp Game Programming (2005) [eng]

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

Beginning C#

Game Programming

© 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 Corporation 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 technical support line or Web site for assistance.

Thomson Course Technology PTR and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive 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 multiple copies or licensing of this book should contact the publisher for quantity discount information. Training manuals, CD-ROMs, and portions 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

Acknowledgments

would first like to thank my family for supporting me through this, my third book. It’s Ibeen a long three years, hasn’t it?

I would also like to thank all of my friends for their encouragement and friendship, especially 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.

iv

About the Author

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

This page intentionally left blank

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

vii

 

 

Contents

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 ix

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