Game Programming All in One Third Edition Jonathan S. Harbour (cid:2)2007 ThomsonCourse Technology, adivisionof ThomsonLearning Publisher and General Manager, Inc. All rights reserved. No part of this book may be reproduced or Thomson Course Technology PTR: transmitted in any form or by any means, electronic or mechanical, Stacy L. Hiquet including photocopying, recording, or by any information storage or retrieval system without written permission from Thomson Course Associate Directorof Marketing: TechnologyPTR,exceptfortheinclusionofbriefquotationsinareview. Sarah O’Donnell TheThomsonCourseTechnologyPTRlogoandrelatedtradedressare Manager ofEditorial Services: trademarks of Thomson Course Technology, a division of Thomson Heather Talbot Learning Inc.,and may notbeusedwithout written permission. Marketing Manager: Allegro, Audacity, and SourceForge are trademarks of VA Software Heather Hurley Corporation.DirectXisaregisteredtrademarkofMicrosoftCorpora- tionintheUnitedStatesand/orothercountries.Anim8orwascreated Senior Acquisitions Editor: by R. Steven Glanville. Dev-C++ 5.0 is distributed by BloodshedSoft- Emi Smith ware.Pro Motion isowned byCosmigo. Marketing Coordinator: All other trademarks aretheproperty oftheirrespective owners. Adena Flitt Important:ThomsonCourseTechnologyPTRcannotprovidesoftware support. Please contact the appropriate software manufacturer’s Project Editor: technical supportline or Website for assistance. Jenny Davidson Thomson Course Technology PTR and the author have attempted Technical Reviewer: throughout this book to distinguish proprietary trademarks from Joshua R.Smith descriptive terms by following the capitalization style used by the manufacturer. PTR Editorial Services Coordinator: ErinJohnson Information contained in this book has been obtained by Thomson CourseTechnologyPTRfromsourcesbelievedtobereliable.However, Interior Layout Tech: becauseofthepossibilityofhumanormechanicalerrorbyoursources, Interactive Composition Corporation Thomson Course Technology PTR, or others, the Publisher does not guaranteetheaccuracy,adequacy,orcompletenessofanyinformation Cover Designer: and is not responsible for any errors or omissions or the results Mike Tanamachi obtainedfromuseofsuchinformation.Readersshouldbeparticularly awareofthefactthattheInternetisanever-changingentity.Somefacts CD-ROM Producer: mayhave changed sincethis book went topress. Brandon Penticuff Educational facilities, companies, and organizations interested in Indexer: multiplecopiesorlicensingofthisbookshouldcontactthePublisher Sharon Shock forquantitydiscountinformation.Trainingmanuals,CD-ROMs,and portionsofthisbookarealsoavailableindividuallyorcanbetailored Proofreader: forspecific needs. Heather Urschel ISBN-10:1-59863-289-2 ISBN-13:978-1-59863-289-7 eISBN-10:1-59863-780-0 LibraryofCongress CatalogCard Number:2006927128 Printed intheUnited StatesofAmerica 07080910 11PH10 98 76 54 32 1 ThomsonCourseTechnologyPTR, adivisionofThomsonLearningInc. 25ThomsonPlace Boston,MA02210 http://www.courseptr.com For Jeremiah Acknowledgments Abookofthissizeinvolvesalotofworkevenafterthewritingisdone.Ittakesa while just to read through a programming book once, so you can imagine how difficultitistoreadthroughitseveraltimes,makingchangesandnotesalongthe way,refining,correcting,preparingthebookforprint.Iamindebtedtothehard work of the editors, artists, and layout specialists at Course Technology who do such a fine job. Many thanks go out to Jenny Davidson and Emi Smith. IoweabigthankyoutoJoshuaSmithforhistechnicalexpertise.Hismanyyears of experience in the game industry were invaluable for finding programming errors and offering sound advice. I believe you willfind this a true gem of a gameprogramming book dueto their efforts. About the Author Jonathan S. Harbour is a senior instructor of game development at the Uni- versity of Advancing Technology (www.uat.edu) in Tempe, Arizona, where he teachesavarietyofgameprogrammingcourses.Whennotteachingothersabout games, writing about games, or playing games, he enjoys audio/video editing, wrenchingonoldFords(andgoingtolocalcarshows),andwatchingmovies.His favoritegamedevelopmenttoolsareDarkBASIC,Allegro,andDirectX.Jonathan is the author of three recent books: DarkBASIC Pro Game Programming, Second Edition (with Joshua Smith), Beginning Java 5 Game Programming, and The Gadget Geek’s Guide to Your Xbox 360. Jonathanistheprojectleadofacontinuation gameintheclassicStarflightseries (withthesupportofseriesfounderRodMcConnell).Thisgameisdueforrelease in late 2007, and is being developed in C++ and Allegro as a 2D game (like the originals).StarflightIwaspublishedbyElectronicArtsin1986,whilethesequel, StarflightII:TradeRoutesoftheCloudNebula,cameoutin1989.Healsofounded PrimevalGames,asmallstudiodedicatedtoproducinghumorouscasualgames, and is working on a space shooter, intended for retail sale. He lives in the Arizona desert with his wife, Jennifer, and four children: Jeremiah, Kayleigh, Kaitlyn, and newcomer Kourtney. He can be reached at www.jharbour.com. Contents Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii PART I CROSS-PLATFORM GAME PROGRAMMING WITH THE ALLEGRO GAME LIBRARY . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Demystifying Game Development. . . . . . . . . . . . . . . . . . . .3 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Practical Game Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Goals Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 The High-Level View of Game Development . . . . . . . . . . . . . . . . . . 7 Recognizing Your Personal Motivations. . . . . . . . . . . . . . . . . . . . . 10 Decision Point: College versus Job. . . . . . . . . . . . . . . . . . . . . . . 11 Every Situation Is Unique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 A Note about Specialization. . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Game Industry Speculation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Emphasizing 2D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Finding Your Niche . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Getting into the Spirit of Gaming . . . . . . . . . . . . . . . . . . . . . . . . . 22 Starship Battles: An Inspired Fan Game. . . . . . . . . . . . . . . . . . . 22 Axis & Allies: Hobby Wargaming. . . . . . . . . . . . . . . . . . . . . . . . 30 Setting Realistic Expectations for Yourself. . . . . . . . . . . . . . . . . 33 vi Contents vii An Introduction to Allegro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 DirectX Is Just Another Game Library . . . . . . . . . . . . . . . . . . . . 34 Introducing the Allegro Game Library. . . . . . . . . . . . . . . . . . . . 36 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Chapter Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Chapter 2 Getting Started with the Allegro Game Library. . . . . . . . . 43 Allegro’s Versatility Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Configuring Your Favorite C++ Compiler. . . . . . . . . . . . . . . . . . . . 45 Overview of the Compiler Configurations. . . . . . . . . . . . . . . . . 46 Windows Platform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Linux Platform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Mac Platform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Taking Allegro for a Spin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Introducing Some of Allegro’s Features. . . . . . . . . . . . . . . . . . . 81 Running the GetInfo Program . . . . . . . . . . . . . . . . . . . . . . . . . 82 Adding to the GetInfo Program . . . . . . . . . . . . . . . . . . . . . . . . 82 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Chapter Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Chapter 3 2D Vector Graphics Programming. . . . . . . . . . . . . . . . . . . 93 Your Ideal Game?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Graphics Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 The InitGraphics Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 The DrawBitmap Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Drawing Graphics Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Drawing Pixels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Drawing Lines and Rectangles . . . . . . . . . . . . . . . . . . . . . . . . 109 Drawing Circles and Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Drawing Splines, Triangles, and Polygons . . . . . . . . . . . . . . . . 128 Filling In Regions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Printing Text on the Screen. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Constant Text Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Variable Text Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Testing Text Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Fun with Math and Vectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Chapter Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 viii Contents Chapter 4 Writing Your First Allegro Game. . . . . . . . . . . . . . . . . . . 149 Tank War . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Creating the Tanks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Firing Weapons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Tank Movement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Collision Detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 The Complete Tank War Source Code. . . . . . . . . . . . . . . . . . . 157 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Chapter Quiz. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Chapter 5 Getting Input from the Player. . . . . . . . . . . . . . . . . . . . . 175 Handling Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 The Keyboard Handler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Detecting Key Presses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 The Stargate Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Buffered Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Simulating Key Presses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 The KeyTest Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Handling Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 The Mouse Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Reading the Mouse Position. . . . . . . . . . . . . . . . . . . . . . . . . . 187 Detecting Mouse Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Showing and Hiding the Mouse Pointer . . . . . . . . . . . . . . . . . 188 The Strategic Defense Game. . . . . . . . . . . . . . . . . . . . . . . . . . 190 Setting the Mouse Position. . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Limiting Mouse Movement and Speed . . . . . . . . . . . . . . . . . . 200 Relative Mouse Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Using a Mouse Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Handling Joystick Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 The Joystick Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Detecting Controller Stick Movement . . . . . . . . . . . . . . . . . . . 205 Detecting Controller Buttons . . . . . . . . . . . . . . . . . . . . . . . . . 209 Testing the Joystick Routines . . . . . . . . . . . . . . . . . . . . . . . . . 210 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Chapter Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Chapter 6 Mastering the Audible Realm. . . . . . . . . . . . . . . . . . . . . 221 The PlayWave Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Sound Initialization Routines. . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Detecting the Digital Sound Driver. . . . . . . . . . . . . . . . . . . . . 225 Contents ix Reserving Voices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Setting an Individual Voice Volume . . . . . . . . . . . . . . . . . . . . 225 Initializing the Sound Driver. . . . . . . . . . . . . . . . . . . . . . . . . . 226 Removing the Sound Driver . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Changing the Volume . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Standard Sample Playback Routines. . . . . . . . . . . . . . . . . . . . . . . 227 Loading a Sample File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Playing and Stopping a Sample . . . . . . . . . . . . . . . . . . . . . . . 228 Altering a Sample’s Properties . . . . . . . . . . . . . . . . . . . . . . . . 228 Creating and Destroying Samples . . . . . . . . . . . . . . . . . . . . . . 228 Low-Level Sample Playback Routines. . . . . . . . . . . . . . . . . . . . . . 229 Allocating and Releasing Voices . . . . . . . . . . . . . . . . . . . . . . . 229 Starting and Stopping Playback . . . . . . . . . . . . . . . . . . . . . . . 229 Status and Priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Controlling the Playback Position. . . . . . . . . . . . . . . . . . . . . . 230 Altering the Playback Mode. . . . . . . . . . . . . . . . . . . . . . . . . . 230 Volume Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Pitch Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Panning Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 The SampleMixer Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Playing Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Midi Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Loading a Midi File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Getting the Midi Length. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Playing a Midi File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Example Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Chapter Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 PART II SPRITE PROGRAMMING. . . . . . . . . . . . . . . . . . . . . . . . . 241 Chapter 7 Basic Bitmap Handling and Blitting. . . . . . . . . . . . . . . . . 243 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Dealing with Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Creating Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Cleaning House . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Bitmap Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Acquiring and Releasing Bitmaps . . . . . . . . . . . . . . . . . . . . . . 252 Bitmap Clipping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Loading Bitmaps from Disk. . . . . . . . . . . . . . . . . . . . . . . . . . . 253