Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 1.029 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
1.029
Dung lượng
16,04 MB
Nội dung
201 West 103rd Street, Indianapolis, Indiana 46290 TricksoftheWindowsGameProgrammingGurus fundamentals of 2D and 3D GameProgrammingAndréLamothe 0072313618 FM 10/26/99 9:32 AM Page i TricksoftheWindowsGameProgrammingGurus Fundamentals of 2D and 3D GameProgramming Copyright 1999 by Sams All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- copying, recording, or otherwise, without written permission from the pub- lisher. No patent liability is assumed with respect to the use ofthe information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use ofthe information contained herein. International Standard Book Number: 0-672-31361-8 Library of Congress Catalog Card Number: 98-85491 Printed in the United States of America First Printing: October 1999 01 00 99 4 3 2 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages aris- ing from the information contained in this book or from the use ofthe CD or programs accompanying it. Executive Editor Don Roche Acquisitions Editor Angela Kozlowski Development Editors Erik Dafforn Kezia Endsley Managing Editor Charlotte Clapp Project Editor Carol Bowers Copy Editors Sean Medlock Aaron Black Howard Jones Indexer Erika Millen Proofreader Betsy Smith Technical Editor Steve Haines Software Development Specialists John Warriner Dan Scherf Interior Design Gary Adair Cover Design Alan Clements Layout Technicians Brandon Allen Tim Osborn Staci Somers 0072313618 FM 10/26/99 9:32 AM Page ii Contents at a Glance Introduction 1 Part I WindowsProgramming Foundations 7 1 Journey into the Abyss 9 2 TheWindowsProgramming Model 47 3 Advanced WindowsProgramming 95 4 Windows GDI, Controls, and Last-Minute Gift Ideas 165 Part II DirectX and 2D Fundamentals 211 5 DirectX Fundamentals and the Dreaded COM 213 6 First Contact: DirectDraw 241 7 Advanced DirectDraw and Bitmapped Graphics 287 8 Vector Rasterization and 2D Transformations 401 9 Uplinking with DirectInput and Force Feedback 537 10 Sounding Off with DirectSound and DirectMusic 589 Part III Hardcore GameProgramming 645 11 Algorithms, Data Structures, Memory Management, and Multithreading 647 12 Making Silicon Think with Artificial Intelligence 713 13 Playing God: Basic Physics Modeling 797 14 Putting It All Together: You Got Game! 875 0072313618 FM 10/26/99 9:32 AM Page iii Part IV Appendixes 901 A What’s on the CD 903 B Installing DirectX and Using the C/C++ Compiler 907 C Math and Trigonometry Review 911 D C++ Primer 925 E GameProgramming Resources 949 F ASCII Tables 955 Index 961 0072313618 FM 10/26/99 9:32 AM Page iv Table of Contents Introduction 1 PART I WindowsProgramming Foundations 7 1 Journey into the Abyss 9 A Little History 9 Designing Games 13 Types of Games 13 Brainstorming 14 The Design Document and Storyboards 15 Making theGame Fun 16 The Components of a Game 16 Section 1: Initialization 17 Section 2: Enter Game Loop 17 Section 3: Retrieve Player Input 17 Section 4: Perform AI and Game Logic 17 Section 5: Render Next Frame 18 Section 6: Synchronize Display 18 Section 7: Loop 18 Section 8: Shutdown 18 General GameProgramming Guidelines 21 Using Tools 26 C/C++ Compilers 26 2D Art Software 26 Sound Processing Software 26 3D Modelers 26 Music and MIDI Sequencing Programs 27 Setting Up to Get Down—Using the Compiler 27 An Example: FreakOut 29 Summary 46 2 TheWindowsProgramming Model 47 The Genesis ofWindows 48 Early Windows Versions 48 Windows 3.x 48 Windows 95 49 Windows 98 50 Windows NT 50 Basic Windows Architecture: Win9X/NT 50 Multitasking and Multithreading 51 Getting Info on the Threads 52 The Event Model 53 0072313618 FM 10/26/99 9:32 AM Page v vi TRICKSOFTHEWINDOWSGAMEPROGRAMMINGGURUSProgrammingthe Microsoft Way: Hungarian Notation 55 Variable Naming 56 Function Naming 56 Type and Constant Naming 57 Class Naming 57 Parameter Naming 58 The World’s Simplest Windows Program 58 It All Begins with WinMain() 59 Dissecting the Program 60 Choosing a Message Box 63 Real-World Windows Applications (Without Puck) 66 TheWindows Class 66 Registering theWindows Class 74 Creating the Window 75 The Event Handler 77 The Main Event Loop 84 Making a Real-Time Event Loop 89 Opening More Windows 90 Summary 93 3 Advanced WindowsProgramming 95 Using Resources 96 Putting Your Resources Together 98 Using Icon Resources 99 Using Cursor Resources 102 Creating String Table Resources 106 Using Sound .WAV Resources 108 Last, But Not Least—Using the Compiler to Create .RC Files 114 Working with Menus 116 Creating a Menu 116 Loading a Menu 119 Responding to Menu Event Messages 122 Introduction to GDI 128 The WM_PAINT Message Once Again 128 Video Display Basics and Color 133 RGB and Palletized Modes 135 Basic Text Printing 137 Handling Important Events 143 Window Manipulation 143 Banging on the Keyboard 150 Squeezing the Mouse 158 Sending Messages Yourself 161 Summary 163 0072313618 FM 10/26/99 9:32 AM Page vi CONTENTS vii 4 Windows GDI, Controls, and Last-Minute Gift Ideas 165 Advanced GDI Graphics 166 Under the Hood with the Graphics Device Context 166 Color, Pens, and Brushes 167 Working with Pens 168 Painting with Brushes 172 Points, Lines, Polygons, and Circles 173 Straight to the Point 173 Getting a Line on Things 175 Getting Rectangular 177 Round and Round She Goes—Circles 180 Polygon, Polygon, Wherefore Art Thou, Polygon? 181 More on Text and Fonts 182 Timing Is Everything 184 The WM_TIMER Message 184 Low-Level Timing 187 Playing with Controls 190 Buttons 191 Sending Messages to Child Controls 195 Getting Information 197 The T3D Game Console 205 Summary 210 PART II DirectX and 2D Fundamentals 211 5 DirectX Fundamentals and the Dreaded COM 213 DirectX Primer 214 The HEL and HAL 216 The DirectX Foundation Classes in Depth 216 COM: Is It the Work of Microsoft… or Demons? 218 What Exactly Is a COM Object? 219 More on Interface IDs and GUIDs 223 Building a Quasi-COM Object 224 A Quick Recap of COM 226 A Working COM Program 226 Working with DirectX COM Objects 231 COM and Function Pointers 232 Creating and Using DirectX Interfaces 236 Querying for Interfaces 237 The Future of COM 238 Summary 239 0072313618 FM 10/26/99 9:32 AM Page vii viii TRICKSOFTHEWINDOWSGAMEPROGRAMMINGGURUS 6 First Contact: DirectDraw 241 The Interfaces of DirectDraw 242 Interface Characteristics 242 Using the Interfaces Together 244 Creating a DirectDraw Object 245 Error Handling with DirectDraw 246 Getting an Interface Lift 247 Cooperating with Windows 250 Getting into the Mode of Things 255 The Subtleties of Color 259 Building a Display Surface 263 Creating a Primary Surface 264 Attaching the Palette 272 Plotting Pixels 272 Cleaning Up 284 Summary 285 7 Advanced DirectDraw and Bitmapped Graphics 287 Working with High-Color Modes 288 16-Bit High-Color Mode 289 Getting the Pixel Format 290 24/32-Bit High-Color Mode 299 Double Buffering 301 Surface Dynamics 307 Page Flipping 311 Using the Blitter 317 Using the Blitter for Memory Filling 320 Copying Bitmaps from Surface to Surface 328 Clipper Fundamentals 332 Clipping Pixels to a Viewport 332 Clipping Bitmaps the Hard Way 334 Making a DirectDraw Clip with IDirectDrawClipper 339 Working with Bitmaps 345 Loading .BMP files 345 Working with Bitmaps 352 Loading an 8-Bit Bitmap 353 Loading a 16-Bit Bitmap 354 Loading a 24-Bit Bitmap 355 Last Word on Bitmaps 356 Offscreen Surfaces 356 Creating Offscreen Surfaces 356 Blitting Offscreen Surfaces 358 Setting Up the Blitter 359 Color Keys 360 Source Color Keying 361 0072313618 FM 10/26/99 9:32 AM Page viii CONTENTS ix Destination Color Keying 364 Using the Blitter (Finally!) 365 Bitmap Rotation and Scaling 366 Discrete Sampling Theory 368 Color Effects 373 Color Animation in 256-Color Modes 373 Color Rotation in 256-Color Modes 379 Tricks with RGB Modes 381 Manual Color Transforms and Lookup Tables 381 The New DirectX Color and Gamma Controls Interface 382 Mixing GDI and DirectX 383 Getting the Lowdown on DirectDraw 386 The Main DirectDraw Object 386 Surfing on Surfaces 388 Playing with Palettes 389 Using DirectDraw in Windowed Modes 390 Drawing Pixels in a Window 392 Finding the Real Client Area (51) 395 Clipping a DirectX Window 397 Working with 8-Bit Windowed Modes 398 Summary 400 8 Vector Rasterization and 2D Transformations 401 Drawing Lines 402 Bresenham’s Algorithm 403 Speeding Up the Algorithm 409 Basic 2D Clipping 411 Computing the Intersection of Two Lines Using the Point Slope Form 413 Computing the Intersection of Two Lines Using the General Form 416 Computing the Intersection of Two Lines Using the Matrix Form 416 Clipping the Line 419 The Cohen-Sutherland Algorithm 420 Wireframe Polygons 427 Polygon Data Structures 428 Drawing and Clipping Polygons 430 Transformations in the 2D Plane 432 Translation 433 Rotation 435 Scaling 445 Introduction to Matrices 446 The Identity Matrix 448 Matrix Addition 449 0072313618 FM 10/26/99 9:32 AM Page ix x TRICKSOFTHEWINDOWSGAMEPROGRAMMINGGURUS Matrix Multiplication 449 Transformations Using Matrices 452 Translation 454 Scaling 455 Rotation 455 Solid Filled Polygons 458 Types of Triangles and Quadrilaterals 459 Drawing Triangles and Quadrilaterals 461 Triangular Deconstruction Details 464 The General Case of Rasterizing a Quadrilateral 472 Triangulating Quads 473 Collision Detection with Polygons 478 Proximity AKA Bounding Sphere/Circle 478 Bounding Box 481 Point Containment 484 More on Timing and Synchronization 486 Scrolling and Panning 488 Page Scrolling Engines 488 Homogeneous Tile Engines 489 Sparse Bitmap Tile Engines 494 Fake 3D Isometric Engines 496 Method 1: Cell-Based, Totally 2D 496 Method 2: Full-Screen-Based, with 2D or 3D Collision Networks 498 Method 3: Using Full 3D Math, with a Fixed Camera View 500 The T3DLIB1 Library 500 The Engine Architecture 500 Basic Definitions 501 Working Macros 502 Data Types and Structures 503 Global Domination 506 The DirectDraw Interface 507 2D Polygon Functions 511 2D Graphic Primitives 513 Math and Error Functions 517 Bitmap Functions 519 Palette Functions 522 Utility Functions 525 The BOB (Blitter Object) Engine 527 Summary 535 9 Uplinking with DirectInput and Force Feedback 537 The Input Loop Revisited 538 DirectInput Overture 539 The Components of DirectInput 541 The General Steps for Setting Up DirectInput 542 0072313618 FM 10/26/99 9:32 AM Page x [...]... and artificial intelligence He is the author ofTricksoftheGameProgramming Gurus, Sams Teach Yourself GameProgramming in 21 Days, TheGameProgramming Starter Kit, The Black Art of 3D Game Programming, and WindowsGameProgramming for Dummies, all bestsellers In addition, he coauthored Ciarcia’s Circuit Cellar I and II Mr LaMothe has also taught at the University of Santa Cruz Extension Multimedia... areas of software engineering that push the limits ofthe hardware, the software, and the programmer himself as much as gameprogramming does There are so many intricate pieces that have to work together perfectly: math, physics, AI, graphics, sound, music, GUI, data structures, and so forth This is where Tricks of theWindows Game ProgrammingGurus proves itself to be an essential tool in the art of programming. .. Game! 875 The Initial Design of Outpost 876 The Story 876 Designing the Gameplay 877 The Tools Used to Write theGame 877 TheGame Universe: Scrolling in Space 878 The Player’s Ship: The Wraith” 880 The Asteroid Field 882 The Enemies 884 The Outposts 885 The Predator Mines 886 The Gunships 888 The Power-Ups ... Journey into the Abyss “Oh, you want some too?!?” —Hudson, Aliens Windowsprogramming has been an ongoing war with a long history Game programmers have resisted theWindows platform since the beginning of time, but like the Borg say, “Resistance is futile…” I tend to agree In this chapter you’re going to take a whirlwind tour of Windows: • History of games • Types of games • The elements ofgame programming. .. Primer Appendix E GameProgramming Resources Appendix F ASCII Tables 3 0172313618 Intro 4 8/27/99 9:12 AM Page 4 TRICKS OF THEWINDOWS GAME PROGRAMMINGGURUS Installing the CD-ROM The CD-ROM contains all the source, executables, sample programs, stock art, 3D modelers, sound effects, and bonus technical articles that make up the book Here’s the directory structure: CD-DRIVE:\ T3DGAME\ SOURCE\ T3DCHAP01\... Organized Tricks of theWindows Game ProgrammingGurus is divided into four parts, covering 14 chapters and six appendixes Part I: WindowsProgramming Foundations Chapter 1 Journey into the Abyss Chapter 2 TheWindowsProgramming Model Chapter 3 Advanced WindowsProgramming Chapter 4 Windows GDI, Controls, and Last-Minute Gift Ideas Part II: DirectX and 2D Fundamentals Chapter 5 DirectX Fundamentals and the. .. good There were even some 3D games on the market such as Wing Commander and Flight Simulator, but the PC was definitely at the back ofthe line ofgame machines By 1985, the Amiga 500 and Atari ST reigned supreme as the ultimate game- playing computers However, the PC slowly gained popularity due to its low price and usefulness in the business sector And the bottom line is that the computer with the. .. covers all the elements necessary to create a Windows 9X/NT-based game for the PC: • Win32 programming • DirectX Foundation • 2D graphics and algorithms 0172313618 Intro 2 8/27/99 9:12 AM Page 2 TRICKS OF THEWINDOWS GAME PROGRAMMINGGURUS • Gameprogramming techniques and data structures • Multithreaded programming • Artificial intelligence • Physics modeling • Using 3D acceleration hardware (on the CD)... PART I WindowsProgramming Foundations The funny thing is, back then most games were networked! I mean, 90 percent of thegame programs were MUDs (Multi-User Dungeons) or similar simulations, like Star Trek and war simulations However, the masses never got a taste of computer games until the quintessential Pong came out Designed by Nolan Busnell, this single game really started the whole video game arcade... Atari was born Then, around 1976–1978, the TRS-80, Apple, and Atari 800 all hit the market These were the first computers that a consumer could buy Of course, before then you could buy kits like the Altair 8000, but who wanted to put them together? In any case, these computers all had their pros and cons The Atari 800 was by far the most powerful (I’m convinced I could write a version of Wolfenstein . 46290 Tricks of the Windows Game Programming Gurus fundamentals of 2D and 3D Game Programming André Lamothe 0072313618 FM 10/26/99 9:32 AM Page i Tricks of the Windows Game Programming Gurus Fundamentals. author of Tricks of the Game Programming Gurus, Sams Teach Yourself Game Programming in 21 Days, The Game Programming Starter Kit, The Black Art of 3D Game Programming, and Windows Game Programming. where Tricks of the Windows Game Programming Gurus proves itself to be an essential tool in the art of programming the video games of today and tomorrow. This book takes you to the next level in game