Beginning Game Programming.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
TEAM LinG - Live, Informative, Non-cost and Genuine! Beginning Game Programming Jonathan S. Harbour TEAM LinG - Live, Informative, Non-cost and Genuine! © 2005 by Thomson Course Technology PTR. All rights reserved. No SVP,Course Professional, Trade, part of this book may be reproduced or transmitted in any form or by Reference Group: any means, electronic or mechanical, including photocopying, record- Andy Shafran ing, or by any information storage or retrieval system without written Publisher: permission from Thomson Course Technology PTR, except for the Stacy L. Hiquet inclusion of brief quotations in a review. The Premier Press and Thomson Course Technology PTR logo and Senior Marketing Manager: related trade dress are trademarks of Thomson Course Technology PTR Sarah O’Donnell and may not be used without written permission. Marketing Manager: Microsoft, Windows, DirectX, and Direct3D are either registered trade- Heather Hurley marks or trademarks of Microsoft Corporation in the United States Manager of Editorial Services: and/or other countries. Heather Talbot Clip art images copyright JupiterImages. Acquisitions Editor: All other trademarks are the property of their respective owners. Mitzi Koontz Important: Thomson Course Technology PTR cannot provide software Senior Editor: support. Please contact the appropriate software manufacturer’s techni- Mark Garvey cal support line or Web site for assistance. Associate Marketing Manager: Thomson Course Technology PTR and the author have attempted Kristin Eisenzopf throughout this book to distinguish proprietary trademarks from descrip- tive terms by following the capitalization style used by the manufacturer. Marketing Coordinator: Jordan Casey Information contained in this book has been obtained by Thomson Course Technology PTR from sources believed to be reliable. However, Project Editor and Copy Editor: because of the possibility of human or mechanical error by our sources, Estelle Manticas Thomson Course Technology PTR, or others, the Publisher does not Technical Reviewers: guarantee the accuracy, adequacy, or completeness of any information Joshua Smith and Sebastien St-Laurent and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of PTR Editorial Services: the fact that the Internet is an ever-changing entity. Some facts may have Elizabeth Furbish changed since this book went to press. Interior Layout: Educational facilities, companies, and organizations interested in multiple Argosy Publishing copies or licensing of this book should contact the publisher for quantity Cover Designer: discount information. Training manuals, CD-ROMs, and portions of this Mike Tanamachi book are also available individually or can be tailored for specific needs. CD-ROM Producer: ISBN: 1-59200-585-3 Brandon Penticuff Library of Congress Catalog Card Number: 2004109697 Printed in the United States of America Indexer: 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1 Larry Sweazy Proofreader: Kim Cofer Course PTR, a division of Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com TEAM LinG - Live, Informative, Non-cost and Genuine! For my mother, Vicki Myrlene Harbour TEAM LinG - Live, Informative, Non-cost and Genuine! Acknowledgments am grateful to my wife, Jennifer, for allowing me to write while also working full time. Thank you for being so supportive. I love you. I am also blessed to have two wonder- Iful kids, Jeremiah and Kayleigh, who help me take a break and play now and then. I thank God for all of these blessings in life. I am indebted to the hardworking editors, artists, and layout specialists at Course PTR and to all of the freelancers for doing such a fine job. Many thanks to Estelle Manticas, Jenny Davidson, Brandon Penticuff, Mitzi Koontz, and Emi Smith. Thanks go to Joshua Smith and Sebastien St-Laurent for their technical review, which was very helpful. I believe you will find this a true gem of a game programming book due to all of their efforts. I want to send greetings to friends, co-workers, and relatives from whom I occasionally derive inspiration (or is it consternation?). After all, I’m not working in a hole somewhere (although sometimes it feels that way!). Thanks to the following for your friendship: Peter Blue, Nathan Warthan, John Striker, Gerald “Dr. Ghastly” Winkler, Chris “Vermis” Henson, Trammel “Banshee” Stevens, Matt Klein, Jennifer Whitwell, Matt Hamby, Wade and Lind- sey Eutsey, Justin and Kim Galloway, Brandon and Emily Figg, Jason and Kelly Trisco. And to the Friday night gang: could you please stahhhhp messing up the house? Just kidding! This has been a very challenging year for many of my friends and co-workers due to the tragic loss of two of our best. I want to remember a fellow gamer and friend who passed away this year, Brian “Zonious” Parker, who worked as a WAN engineer. I also honor the memory of Jason Ward, a fellow programmer and sports-car enthusiast. Rest in peace. iv TEAM LinG - Live, Informative, Non-cost and Genuine! About the Author Jonathan S. Harbour has been an avid gamer and programmer for 17 years, having started with early systems like the Commodore PET, Apple II, and Tandy 1000. He holds a bachelor of science degree in Computer Information Systems and has earned a position as senior programmer with seven years of professional experience. He enjoys writing code mainly in C, C++, and VB, and has experience with a wide variety of platforms, including Windows, Linux, Pocket PC, and Game Boy Advance. Jonathan has written six books on the subject of game programming. In addition to his recent Game Programming All In One, 2nd Edition, he has also written Pocket PC Game Programming, Microsoft Visual Basic Game Programming with DirectX, Microsoft Visual Basic .NET Programming for the Absolute Beginner, Beginner’s Guide to DarkBASIC Game Programming, and Programming the Game Boy Advance. He is currently working on his next two books, Visual Basic Game Programming for Teens and The Black Art of Xbox Mods. He maintains a Web site dedicated to game programming at www.jharbour.com. v TEAM LinG - Live, Informative, Non-cost and Genuine! Contents Introduction . xiii Part I: Windows Programming . .1 Chapter 1 Getting Started with Windows and DirectX . 3 Welcome to the Adventure! . .4 Let’s Talk about Compilers . 5 What’s Your Skill Level? . 7 Where to Begin? . 9 An Overview of Windows Programming . .10 “Getting” Windows . 11 Understanding Windows Messaging . 11 Multi-Tasking . 12 Multi-Threading . 15 Event Handling . 16 A Quick Overview of DirectX . .17 What Is Direct3D? . 19 What You Have Learned . .20 Review Questions . .20 Chapter 2 Windows Programming Basics . 23 The Basics of a Windows Program . .24 Creating a Win32 Project. 25 Understanding WinMain . 29 vi TEAM LinG - Live, Informative, Non-cost and Genuine! Contents vii The Complete WinMain. 30 What You Have Learned . .33 Review Questions . .33 On Your Own . .34 Chapter 3 Windows Messaging and Event Handling . .35 Writing a Full-Blown Windows Program . .36 Understanding InitInstance. 40 Understanding MyRegisterClass . 43 Understanding WinProc . 45 What You Have Learned . .50 Review Questions . .50 On Your Own . .51 Chapter 4 The Real-Time Game Loop . .53 What Is a Game Loop? . .54 The Old WinMain . 54 WinMain and Looping . 57 The GameLoop Project . .60 Source Code for the GameLoop Program. 61 What You Have Learned . .68 Review Questions . .68 On Your Own . .69 Part II: DirectX Programming . .71 Chapter 5 Your First DirectX Graphics Program . .73 Getting Started with Direct3D . .74 The Direct3D Interfaces . 74 Creating the Direct3D Object. 75 Taking Direct3D for a Spin . 78 Direct3D in Fullscreen Mode . 87 What You Have Learned . .89 Review Questions . .89 On Your Own . .90 Chapter 6 Bitmaps and Surfaces . .91 Surfaces and Bitmaps . .92 The Primary Surfaces . 94 TEAM LinG - Live, Informative, Non-cost and Genuine! viii Contents Secondary Offscreen Surfaces . 95 The Create_Surface Example . 97 Loading Bitmaps from Disk . 104 The Load_Bitmap Program. 105 What You Have Learned . .109 Review Questions . .110 On Your Own . .110 Chapter 7 Drawing Animated Sprites . .111 How to Draw Animated Sprites . .112 The Anim_Sprite Project. ..