ebook img

Game Programming All in One PDF

991 Pages·2002·9.037 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Game Programming All in One

Y L F M A E T Team-Fly® Game Programming All in One Bruno Miguel Teixeira de Sousa © 2002 by Premier Press. 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 Premier Press, except for the inclusion of brief quotations in a review. Premier Press, Inc. is a registered trademark of Premier Press, Inc. Publisher:Stacy L. Hiquet Marketing Manager: Heather Buzzingham Managing Editor:Sandy Doell Acquisitions Editor:Mitzi Foster Series Editor: André LaMothe Project Editor:Heather Talbot Technical Reviewer:André LaMothe Copy Editor: Jenny Davidson Interior Layout:Marian Hartsough Cover Design:Mike Tanamachi CD-ROM Producer:Keith Davenport Indexer: Kelly Talbot Proofreaders:Anne Owen, Fran Blauw, Linda Seifert Microsoft, DirectX, DirectSound, and DirectInput are registered trademarks of Microsoft Corporation. Jasc and Paint Shop Pro are trademarks or registered trademarks of Jasc Software, Inc. All other trademarks are the property of their respective owners. Important:Premier Press cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. Premier Press and the author have attempted throughout this book to distinguish proprietary trade- marks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Premier Press from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Premier Press, 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-chang- ing entity. Some facts may have changed since this book went to press. ISBN: 1-931841-23-3 Library of Congress Catalog Card Number: 2001096486 Printed in the United States of America 02 03 04 05 06 RI 10 9 8 7 6 5 4 3 2 1 De todas as coisas que quero, és a única coisa que eu preciso. Para ti, Ana. Acknowledgments N ow is the time I should go up to the stage, get the Oscar, and recite a book- length script of all the people that helped make this book. However, the thank you would probably be as big as this book, so to prevent from forgetting any- one, I would like to thank everyone that directly or indirectly made this book possi- ble. On the technical side, I would like to thank the people at Premier Press for giving me the opportunity to write this book. I would also like to thank my editors, Emi Smith, Mitzi Foster, Heather Talbot, and Jenny Davidson for all their patience and all they had to put up with. (Yes, the usual delays and the incessant questions.) Please remember that what you are reading is not a book that I wrote myself, but one that comprises the work of many talented people who are usually forgotten. I would also like to thank André LaMothe for reviewing the book. On the personal side, I would like to thank my mom and dad for their support and love during my life, and of course, for paying those enormous Internet bills when I was still learning game programming. I would also like to thank all of my friends and relatives for their support not only with the book, but also with my life. I would like to send a special thanks to Diana for always being there for me whenever I needed her. Last, and probably most important, I would like to thank Ana for her love, support, patience, and just about everything. I love you from the bottom of my heart. About the Author BRUNO MIGUEL TEIXEIRA DE SOUSA began programming at age 11. Although he began his programming career at age 15 as a database programmer in Visual Basic, he never lost his passion for game development. Two years later, he began a full- time career performing general game programming for a UK-based company. He has been using C++ for more than 4 years and remains an avid game hobbyist. RONALD PENTON wrote Chapter 17. Ron started programming on his Tandy 1000TL way back in 1989, when he became interested in making games, rather than just playing them. Ever since then, he has been on a never-ending quest to learn more about computers and become more efficient at programming them. He started school at the Rochester Institute of Technology in 1998, and is currently finishing his bachelor’s degree in computer science at The University of Buffalo. Contents at a Glance Introduction . . . . . . . . . . . . . . . . xxxvi Part One C++ Programming. . . . . . . . . . . . . . . . . . . 1 Chapter 1 Introduction to C++ Programming. . . . . 3 Chapter 2 Variables and Operators. . . . . . . . . 25 Chapter 3 Functions and Program Flow . . . . . . . 51 Chapter 4 Multiple Files and the Preprocessor . . . . . . . . . . . . . . . . 95 Chapter 5 Arrays, Pointers, and Strings. . . . . 107 Chapter 6 Classes. . . . . . . . . . . . . . . . . . . . 153 Chapter 7 Developing Monster . . . . . . . . . . . . 197 Chapter 8 Streams . . . . . . . . . . . . . . . . . . . 245 Chapter 9 Basic Software Architecture . . . . . 279 Part Two Windows Programming . . . . . . . . . . . 297 Chapter 10 Designing Your Game Library: Mirus . . . . . . . . . . . . . . 299 Chapter 11 Beginning Windows Programming . . . . 317 Chapter 12 Introduction to DirectX . . . . . . . . . 357 Chapter 13 DirectX Graphics . . . . . . . . . . . . . 369 Chapter 14 DirectInput . . . . . . . . . . . . . . . . . 521 Chapter 15 DirectSound . . . . . . . . . . . . . . . . 567 Contents at a Glance ix Part Three Hardcore Game Programming . . . . . . 595 Chapter 16 Introduction to Game Design . . . . . . 597 Chapter 17 Data Structures and Algorithms . . . . . . . . . . . . . . . . . 609 Chapter 18 The Mathematical Side of Games . . . 661 Chapter 19 Introduction to Artificial Intelligence . . . . . . . . . . . . . . . . . 697 Chapter 20 Introduction to Physics Modeling . . . 723 Chapter 21 Building Breaking Through . . . . . . . 791 Chapter 22 Publishing Your Game . . . . . . . . . . 851 Part Four Appendixes . . . . . . . . . . . . . . . . . . . . 863 Appendix A What’s on the CD-ROM . . . . . . . . . 865 Appendix B Debugging Using Microsoft Visual C++ . . . . . . . . . . . . . . . . . . 870 Appendix C Binary, Hexadecimal, and Decimal System. . . . . . . . . . . . . . . 874 Appendix D A C Primer . . . . . . . . . . . . . . . . . 877 Appendix E Answers to the Exercises . . . . . . . 885 Appendix F C++ Keywords . . . . . . . . . . . . . . . 893 Appendix G Useful Tables . . . . . . . . . . . . . . . 895 Appendix H More Resources . . . . . . . . . . . . . 902 Index . . . . . . . . . . . . . . . . . . . . . 907 Contents Letter from the Series Editor . . . . . . . xxxiv Y Introduction . . . . . . . . . . . . . . . . . . . xxxvi L F Part One M C++ Programming. . . . . . . . . . . . . . . . . . . 1 A Chapter 1 E Introduction to C++ Programming . . . 3 T Why Use C++?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Setting Up Visual C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Creating a Workspace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Creating Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Creating and Adding Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Your First Program:“Hello all you happy people” . . . . . . . . . . . . . . . . . . . . . 9 Structure of a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Program Design Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Program Source and Compiling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Objects and Linking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Executable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Catching Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Questions and Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Team-Fly® Contents xi Chapter 2 Variables and Operators . . . . . . 25 Variables and Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 What Type of Variables Are There? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Using Variables in Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Declaring a Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Using Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Initializing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Variable Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Variable Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Redefining Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 What Is an Operator? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Mathematical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Binary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Compound Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Bitwise Shift Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Relational Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Questions and Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.