ebook img

Tricks of the Game Programming Gurus PDF

769 Pages·1994·195.474 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 Tricks of the Game Programming Gurus

• ' . . Andre L-iMotl1c lives in Sa11 Jose, Califor11ia. 1-Ie holds degrees i11 Matl1e1narics, Co111~)uter Scic11ce, and Electrical Engi11ecring a11d l1as worl<ed i11 many differe11t areas of the co111pt1ting industry, including 11et1ral 11etworks, 3-D grapl1ics, virtt1al reality, robotics and communications. He has pt1blisl1e<l articles in CornJ1t,ter l1111g1111ge, Circi,it Cellar, a11cl Carne Devel oper. Currently A11drc is working 011 producing 3-D graphics engines for lice11si11g to otl1er com1Janies. John Ratcliff is a grapl1ic artist, designer, and program mer \vl10 lives in St. Louis. John's diverse background inclt1des tl1e developn1ent of numerous educational sofnvare products, five years as a cardiovascular researcher at St. Louis University Hospital, and more recently as the author of several computer games. His encercainn1ent prodt1cts include 688 Arrack Sub and SSN-21 Seawolf from Electronic Arcs, and Kaleid0S011ics fron1 Masqt1e Publishing. John has co11cribt1ted video, communications, 111usic, and sound technology to over 100 e11rertainment, educacio11al, and multimedia products. He already is starting on new projects in virtual reality simulation and interactive art ar1d rnusic. Yot1 can contact John on Compuserve at 70253,3237 or on his BBS at 1-314-939-0200 at up to 14.4K, or \.Vrite to him at tl1e following address: 747 Napa Lane, Sr. Cl1arles, MO 63304. If you write, please provide an electronic account or telephone number. Denise Mark Seminatore is a ft11l-rime Tyler is tl1e mechanical engineer creati• ve and a parr-tin1e computer const1lrant. director of He is co-founder and Brick Wall • presider1t of M & E Produc Const1lring, a small tions of co1nputer consulting firrn in Albany, Ne,v Madison, Wisconsin. She specializes in • • York. He has been programn1ing for 111ore 2-D and 3-D graphics and animatio11 for • than 15 years no,v and specializes in gan1es, 111ulrin1edia, arid desktop video. Numerical Analysis/Optimi1,arion, Windo,vs Denise created tl1e arn.vork for Niten1are-3D, apps, and l1igh-performance graphics. Mark a new 3-D action/arcade gan1e by David P. also wrote tl1e article "1\. Raycasting Engine iit Gray (Hugo's Hot1se of Horrors trilogy C++" for Doctor Dobb'sjour11nl. In his spare creator). Otl1er ga1ne projects are cin1e, l1e a11cl his ,vife e11joy l eing outdoors. also in tl1e \.vorks. 1 . ' I ' . ' • ■ l I - ■ - l I • I • Andre LaMothe l John Ratcliff Mark Seminatore • • . . . 1 J Denise Tyler • • • • • • • • • • J , . . l ' . • ' s.AMs I PUBLISHING • I A () ivisiun of f\1acn,ill.1n Cornpucer Publishing. '. A Preutic<: ll all M;.1cntill:1n Contpany 101 West lO) rJ Srrcct, lndi:1n:1polis, IN 46290 t • • -· ' • • ,:a t ; I .. I dedicate this book to my mother andf ather for letting me chase mv d.-earns. - A L "J , To my fiancee Erin·-thanks for the support. I couldn't have done it with out yoi,. -Ms To Ed, for his endless support and understanding that creativity knows no tirneclock. - D T • Copyright © 1994 by Sams Publishing FIRST EDITION All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmit tptpSfeioeuaodrrmbn mdbl sicayis somP hsanueianotrbgay nlaei i nmsnsf ehrdreodie anma snhguu se,ltt ,trhh aieenoe idl grneip c.vaf utsiArrsbsooiullmotnimhsni hco etoe,hu rnfmeg. o MhuNe screaoeehvcs aeomppnrafoyii tltcn lehpasanerlin,be t icipClnliaihafotuoobymtrtii fomlopioctnrauyo t thepiieosrayr rsnai o Pn sbrcsguseou ,beo nmlnrrite sa eocthidaomni knrweedigdsniis, nt ihh2igone 0,nrr 1eestosh .i rnpWeN oe. pct.eFh tri 1oeetthrpr0ow ea3 itrnrirh asdfieeost i,S roa umcwnns.e,y ai o tI tohlnifioofad tn buthihaii,tnsle iaw atdbiypnrdo iofartooetslkeisrssmnus,, m tahfN eed 46290. International Standard Book Number: 0-672-30507-0 Library of Congress Catalog Card Number: 94-66272 4 3 2 97 96 95 94 Ipnritnertipnrge;t atthioe nr ioghf ttmheo pstr isnintignlge -cdoigdiet:, tthhee rniugmhtmbeors ot fd tohueb bleo-odkig,si tp nriunmtinbger. Fis otrh eex yaemarp loef, tah ep rbinotoinkg,s code of 94-1 shows that the first printing of the book occurred in 1994. Composed in A Garamond and MCPdigital by Macmillan Computer Pi,hlishing Printed in the United States ofA merica Trademarks Aateplrlpm treo ripnmr istah tmiesl eybn octioaopkni tseahdloi ziuneld dt.h nSisoa mtb obseo P krue tbghlaairstdh aeirdne g ak scn aaonfwfnenoc ttti noatg tb eetsh tte rt aovd atelhimdeia taryck cosu for aarnc syye rotvrfiac tdehe imms aianrrkfkos rohmra asveteir ovbniec.ee n Um sea rokf. a . .. • • •!_l(i.:»e ,.,. ....; ,,. • Publisher Technical Reviewer Production Richard K. Swadley Robert Bogue Troy Barnes Kacy Bodenmiller Associate Publisher Marketing Manager Carol Bowers Jordan Gold Don Brown Gregg Bushyeager Cheryl B. Cameron Acquisitions Manager ' .,,,,_~y Cornwell Cover Art ., - ·M ary Ann Cosby Stacy Hiquet Denise Tyler Steph Davis Michael Dietsch Managing Editor Cover Designer Terri Edwards Cindy Morrow Karen Ruggles Wendy Frost Kimberly K. Hannel Acquisitions Editor Book Designer Greg Kemp Chris Denny Alyssa Yesh Debbie Kincaid Beth Lewis Development Editor Director of Production and Pete Lippincott Jamie Milazzo Manufacturing Scott Parker Shelly Palma Jeff Valler Chad Poore Software Development Casey Price Specialist Imprint Manager Ryan Rader Keith Davenport Juli Cook Bech Rago Kaylene Riemen Production Editor Manufacturing Coordinator Clair Schweinler Marc Sheeter Anne Barrett Paul Gilchrist Susan Shepard Editor Production Analysts Tonya R. Simpson SA Springer Dennis Clay Hager Jill Bond St1zanne Tully Mary Beth Wakefield Dennis Z. Wesner Editorial Coordinator Michelle Worthington Graphics Image Specialists Bill Whitmer Tim Montgomery Indexers Editorial Assistants Dennis Sheehan Greg Eldred Susan VandeWalle Carol Ackerman C. Small Sharon Cox Lynette Quinn • •• • • verv1ew xv Introduction 1 AV ideo-Game Primer 1 9 Assembly Language Basics 2 35 Input Device Basics 3 71 The Mechanics of Two-Dimensional Graphics 4 111 The Mysteries of the VGA Card 5 169 The Third Dimension 6 Advanced Bit-Mapped Graphics and Special FX 235 7 301 High-Speed 3-0 Sprites 8 g Sound FX and Music 351 10 Implementing Computer Game Music 385 11 Video Game Algorithms, Data Structures, and Methodologies 433 12 Surreal Time, Interrupts, and Multitasking 465 13 Synthetic Intelligence 501 14 Linking Up 537 15 The To olchest 577 16 Creating Art for Your Game 593 17 Parallax Scrolling Techniques 625 18 Optimization Techniques 663 19 Warlock 691 Index 721 Contents 1 A Video-Game Primer 1 ~o Writes Video Games? ........................................................... 2 ere Do the Ideas Come From? ................................................. 2 The Pha~es of Video-Game Design ............................................... 4 What Will You Learn from This Book? ......................................... 6 Summary................... 6 •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 2 Assembly Language Basics 9 Why Do We Need Assembly Language in Video Games? ........... 10 The 80x86 Family Overview ....................................................... 11 CPU Registers ............................................................................. 12 The Form of an Assembly Language Function ............................ 12 .Passing Parameters ...................................................................... 15 The USES D ireccive ............................................................... 18 Passing Pointers ...................................................................... 20 Local Variables ........................................................................ 20 The LOCAL Directive ............................................................ 22 Making External References ........................................................ 23 25 Talking Back co the Caller ........................................................... Setting the Video Mode .............................................................. 26 Clearing the Screen at Light Speed .............................................. 27 Using the In-Line Assembler ....................................................... 31 Summary ..................................................................................... 32 35 3 Input Device Basics Interfacing to Video Games ......................................................... 36 The Joystick ................................................................................ 37 How Joysticks Work ............................................................... 38 Joystick Buttons ...................................................................... 38 Reading the Joystick Position .................................................. 41 Joystick Calibration ................................................................ 42 • The Keyboard ........................................................................ •. ... 49 Scan Codes ............................................................................. 51 Shift States .............................................................................. 54 Obtaining Scan Codes from Keyboard Input .......................... 55 Obtaining ASCII Characters from Keyboard Input ................ 56 All Together Now: a Keyboard Demo Program ...................... 57 The Mouse ........................ •·· ...... ••···•··•·· .. ••·••····•·• .. ····••················ 61 ............................................................... 69 Summary •••••••••••••••••••••• 71 The Mechanics of Two-Dimensional Graphics 4 The Two-Dimensional Plane ...................................................... 72 Point~, Lines, and Polygons .......................... •·· .. •• · •. ... ·· .............. ·· ;; .................................................. ·······. ······ .. Po. ints · ·................. 74 Lines ............ •• • • • • • · · ·· · · ·· · ·· · ·· · ·· · · ·· · ·· ·· · ·· · ·· · ·· ·· ·· · · ·· ·· ·· · ·· · · ·· · · ·· ·· · ·· · ·· ·· 76 ................. . Po ly go ns ......................... •. •• • • • • • • • • • • • • • · · · · · · · · · · · · · · · · · · · · · • Tricks of the Game-Programming Gurus Vt - ....... a p 01 ' 41 Objects ........................................................................................ 79 Positioning an Object ......................... ................................... 79 ~ Translating an Object ............................................................. 81 Scaling an Object .................................................................... 83 Rotating an Object ................................................................. 84 Clipping ................................................. _. ............................... 92 Matrices ...................................................................................... 94 Doing Operations on Matrices ............................................... 94 The Identity Matrix ................................................................ 96 Using Matrices in Games ............................................................ 97 A General Translation Matrix ................................................. 97 A General Scaling Matrix ........................................................ 97 A General Rotation Matrix ..................................................... 98 A General Scaling, Rotation, and Translation Matrix .............. 98 The Vertex Normalizing Component ..................................... 98 An Asteroid Program with Matrices ........................................ 99 In Case You Were Wondering .................................................. 106 Basic Collision Detection ...................................................... 107 S umma.ry ................................................................................... 109 5 The Mysteries of the VGA Card 111 An Overview of the VGA Card ................................................. 112 The 256-Color Mode ................................................................ I I 3 The Configuration of Video Memo.ry ....................................... 114 The Color Look-Up Table ........................................................ 115 Remapping the Color Palette .................................................... 116 Color Rotation .......................................................................... 121 Plotting Pixels .......................................................................... . 121 Drawing Lines .......................................................................... . 124 PCX Files ................................................................................. . 131 Bir Blitting ............................................................................... . 139 Sprites ...................................................................................... . 140 Text Blirting ............................................................................. . 147 Double Buffering ..................................................................... . 150 Vertical Retrace Syncing ........................................................... . 151 . . . E h. T 1m1ng 1s veryt 1ng ................................................................ . 152 Tombstone ............................................................................... . 153 Su mma.ry .................................................................................. . 168 6 The Third Dimension 169 Three-Dimensional Space ......................................................... 170 Points, Lines, Polygons, and Objects ......................................... 172 Translation, Scaling, and Rotation in 3-D Space ....................... 177 Translating a 3-D Object ...................................................... 177 Scaling a 3-D Object ............................................................ 177 Rotating a 3-D Object .......................................................... 178 The Last Word on 3-D Transformations .............................. 179 • r ' ' • I Contents •• Vll p . . rtect~ons . ·: ............................................................................. l 79 v?J1ng with the 2-D Video Screen ....................................... 181 ;t~:•:c~:~e P;r:if • M~··························································· 184 J th ....................................................... 184 Perspective-Projection Math ................................................. 185 The Viewing Volume ............................................................ 185 Solid Modeling ......................................................................... 186 Hidden-Surface Removal ...................................................... 187 The Painter's Algorithm ........................................................ 188 The Z-Buffering Algorithm .................................................. 193 Image Space vs. Object Space ............................................... 195 Texturing .................................................................................. 195 Ray Tracing .......................................................................... 196 Ray Casting .......................................................................... 197 The Math Behind Ray Casting ............................................. 204 Generating Lines .........................._ . ........................................ 205 Computing the First Intersection .......................................... 207 Computing the Remaining Intersections ............................... 209 Calculating Distance ............................................................. 21 O Computing e.he Scale ............................................................ 212 Rectifying View Distortion ................................................... 213 Drawing the Sliver ................................................................ 215 Implementing a Ray Caster ....................................................... 215 Improving Our Ray Caster ........................................................ 225 Implementing Doors and Transparency .................................... 225 Lighting, Shading, and the Color Palette ................................... 227 Am.bi enc Lighting ................................................................. 227 Local Lighting ....................................................................... 228 Lambert's Law ...................................................................... 229 The Inverse Square Law ........................................................ 229 Let There Be a Light Model .................................................. 229 Summary ................................................................................... 233 7 Advanced Bit-Mapped Graphics and Special FX 235 Faster Bit Blitting ...................................................................... 236 Logical Bit Operations .............................................................. 238 Transparency Encoding ............................................................. 243 Bit Clipping .............................................................................. 245 Sprite Collision ......................................................................... 250 Double Buffering ...................................................................... 251 Locking on Target with the Vertical Sync ................................. 255 Color-Register Animation ......................................................... 258 ;- Illuminating Your Games .......................................................... 263 Con rexr-Sensi rive Animation .................................................... 264 "An. or1·on" •············ 265 lffi •••••..•••••••••••.••.•••••..•••••.•••••••.••••.•••.••••.••••... Scrolling ........................................... •• ·· •·· •·· •• • ························ ···· 27 l ., ,.._ ••• Vlll - .......................... 27 7 ·a l FX ·· · ·· · ·· · ·· · . ........ . S pect ........... ·: ..... ·:............. . ......................... 277 Backgrou11d ~1mat1on ..................... ····· ... . . ········ ............ 278 Screen Trans1t1ons ............................................ 285 ' Tex tu res, Anyone. . .. . .. . . . .. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . .. . 286 ························· Bit-Mapped Image Scalin.g ······························:::::::::::::::::::::::::: 293 Bit-Mapped Image Rotation •·························· 300 ............... . Su m mary ......... •• • • • • · · · · · · · · · · · · · · · • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • . 301 8 High-Speed 3-0 Sp ntes 302 T he Me c h an1·c s o f 3 -D S pri. te .s · · ········ · ······· ·· · · ·· · ·· · · · · ··· · ·· · · ··· ·· · ···· ·. 305 The Perspective T ransformat1on •• • • · •· · · · · · · · · · · · · · · · ·· · · · · · ·· · · · · · · · · · · · · · ··· · 6 · h P S ale . •• • • • • • • • • • · •· · · · · · · 3 0 C omput1ng t e roper c ............. •• ······· •· ·· · ·· · · · . · V 1 ......•.••..••.••....• 307 The V1ew1ng o ume ..... ················· ........... ... . . .. . . 308 A New Version of the Scaler ....................... ••••••·•·•····················· . · · 3 D S "Sprt'te Style" ........... · 319 Cl1pp1ng 1n - pace, ... •··· •· •······ · ·· · ··· ···· · · T · · ............ 321 C rearing raJector1es ................................ •·· •··· •···· · ··· ·· ··· Angli11g In on the Right Sprite .................................................. 323 A Perspective Starfield .......................................... •• ·· •• · •• ··· •······ ·· 328 Real.Ism I~;~·~~~~·::::::::::::: .................................... •.•.•.•.•.•.•.•.•. 329 S~~ ~fi·;id ·U Crea tin~·~· ~·i·~·~· 3 30 Digitizing Objects and Model.ing ........................................... 343 Creating a Miniature Production Studio ............................... 344 Video for Windows ...................................... •• • • • • • • • • • • • • • • • • • • • • • • • 3 47 The Color Palette ................................................... •. . •· •• • • . •• • · 348 The Wrap on Modeling ........................................................ 350 Cut! (I mean, Summary) ........................................................... 350 g Sound FX and Music 351 The Fundamentals of Sound ..................................................... 352 5 The Sound Blaster and It s Architecture ..................................... 3 5 Digitized Sound ........................................................................ 357 Playing Digitized Sound ....................................................... 359 Usi11g the Driver Functions to Play .VOC Files ............. : ...... 364 The FM Synthesizer .................................................................. 378 Music and MIDI ....................................................................... 379 Using Sound in Video Games ................................................... 380 Sumn1ary ................................................................................... 383 10 Implementing Computer Game Music 385 Types of Audio .......................................................................... 390 Tl1e DIGPAK & MIDPAK Developers Kit .............................. 393 What is "eXcended MIDI"? ....................................................... 398 What Format Shot1ld the MIDI Data Be In? ............................. 399 Professional Interactive Media Composers ................................ 399 Questions and Answers ............................................................. 400 When I Play My MIDI Music with MIDPAK, Some of the Music Seems to Be Missing ............................. 400 l

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.