1. Trang chủ
  2. » Công Nghệ Thông Tin

data structure for game programers - ron penton

978 709 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Data Structures for Game Programmers
Tác giả Ron Penton
Định dạng
Số trang 978
Dung lượng 19,61 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Application: Using Arrays to Store Game Data.. With that in mind, I wanted a data structures book that was like no other—a book using today’s technology that could live up to my high sta

Trang 3

Data Structures for Game Programmers

Trang 5

Data Structures

Ron Penton

TM for Game Programmers

Trang 6

© 2003 by Premier Press, a division of Course Technology 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

The Premier Press logo and related trade dress are trademarks of Premier Press and may not

be used without written permission

TM

Publisher: Stacy L Hiquet

Marketing Manager: Heather Hurley

Acquisitions Editor: Emi Smith

Project Editor: Karen A Gill

Technical Reviewer: André LaMothe

Copyeditor: Stephanie Koutek

Interior Layout: LJ Graphics, Susan Honeywell

Cover Design: Mike Tanamachi

Indexer: Kelly Talbot

Proofreader: Jenny Davidson

Microsoft, Windows, and Visual C++ are trademarks of Microsoft Corporation.Wolfenstein, Doom, and Quake are trademarks of Id Software, Inc Warcraft and Starcraft are trademarks of Blizzard

Entertainment

The artwork used in this book is copyrighted by its respective owners, and you may not use it in your own commercial works

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 marks from descriptive terms by following the capitalization style used by the manufacturer

trade-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-94-2

Library of Congress Catalog Card Number: 2002111226

Printed in the United States of America

Trang 8

I would like to thank everyone at work for supporting me through this endeavor

I especially want to thank Ernest Pazera, André LaMothe, and everyone else at Premier Press for giving me this tremendous opportunity and believing in me

I would like to thank Bruno Sousa for opening the door to writing for me

I want to thank the pioneers of Gamedev.net, Kevin Hawkins and Dave Astle, for paving the road for me and making a book such as this possible

I would like to thank all of you in the #gamedev crew, specifically (in no particular order) Trent Polack, Evan Pipho, April Gould, Joseph Fernald, Andrew Vehlies, Andrew Nguyen, John Hattan, Ken Kinnison, Seth Robinson, Denis Lukianov, Sean Kent, Nicholas Cooper, Ian Overgard, Greg Rosenblatt, Yannick Loitière, Henrik Stuart, Chris Hargrove, Richard Benson, Mat Noguchi, and everyone else!

I would like to thank my artists, Steven Seator and Ari Feldman, who made this book’s demos look so much better than they would have been

And finally, I would like to thank the Pepsi Corporation, for making that wonderful

“stay awake” juice known as Mountain Dew

Trang 9

About the Author

Ron Penton’s lifelong dream has always been to be a game programmer From the age of 11, when his parents bought him his first game programming book on how

to make adventure games, he has always striven to learn the most about how games work and how to create them

Ron is currently finishing up his bachelor’s degree in computer science at the State University of New York at Buffalo He hopes to have a long career in game develop-ment

Trang 10

Contents at a Glance

Introduction xxxii

Concepts 1

Chapter 1 Basic Algorithm Analysis 3

Chapter 2 Templates 13

The Basics 37

Chapter 3 Arrays 39

Chapter 4 Bitvectors 83

Chapter 5 Multi-Dimensional Arrays 107

Chapter 6 Linked Lists 147

Chapter 7 Stacks and Queues 189

Chapter 8 Hash Tables 217

Chapter 9 Tying It Together: The Basics 241

Recursion and Trees 315

Chapter 10 Recursion 317

Chapter 11 Trees 329

Trang 11

Contents at a Glance

Chapter 15 Game Trees and Minimax Trees 431

Chapter 16 Tying It Together: Trees 463

Part Four Graphs 477

Chapter 17 Graphs 479

Chapter 18 Using Graphs for AI: Finite State Machines 529

Chapter 19 Tying It Together: Graphs 563

Part Five Algorithms 597

Chapter 20 Sorting Data 599

Chapter 21 Data Compression 645

Chapter 22 Random Numbers 697

Chapter 23 Pathfinding 715

Chapter 24 Tying It Together: Algorithms 769

Conclusion 793

Part Six Appendixes 799

Appendix A A C++ Primer 801

Appendix B The Memory Layout of a Computer Program 835

Appendix C Introduction to SDL 847

Appendix D Introduction to the Standard Template Library 879

Index 901

Trang 12

Contents

Letter from the Series Editor xxx

Introduction xxxii

Concepts 1

Chapter 1 Basic Algorithm Analysis 3

A Quick Lesson on Algorithm Analysis 4

Big-O Notation 4

Comparing the Various Complexities 9

Graphical Demonstration: Algorithm Complexity 10

Conclusion 11

Chapter 2 Templates 13

What Are Templates? 14

Template Functions 15

Doing It the Old Way 15

Doing It with Templates 17

Template Classes 19

Multiple Parameterized Types 24

Using Values as Template Parameters 27

Trang 13

Contents

Problems with Templates 32

Visual C++ and Templates 34

Under the Hood 34

Conclusion 35

The Basics 37

Chapter 3 Arrays 39

What Is an Array? 40

Graphical Demonstration: Arrays 41

Increasing or Decreasing Array Size 43

Inserting or Removing an Item 43

Native C Arrays and Pointers 43

Static Arrays 43

Dynamic Arrays 49

An Array Class and Useful Algorithms 59

The Data 59

The Constructor 59

The Destructor 60

The Resize Algorithm 60

The Access Operator 62

The Conversion Operator 63

Inserting an Item Between Two Existing Items 64

Removing an Item from the Array 65

A Faster Removal Method 66

Retrieving the Size of an Array 67

Example 3-3 67

Storing/Loading Arrays on Disk 68

Writing an Array to Disk 69

Reading an Array from Disk 70

Considerations for Writing and Reading Files 71

Trang 14

Application: Using Arrays to Store Game Data 71

The Monster Class 72

Declaring a Monster Array 72

Adding a Monster to the Game 72

Making a Better Insertion Algorithm 73

Removing a Monster from the Game 74

Checking for Monster Removal 75

Playing the Game 76

Analysis of Arrays in Games 77

Cache Issues 77

Resizing Arrays 80

Inserting/Removing Cells 80

Conclusion 80

Chapter 4 Bitvectors 83

What Is a Bitvector? 84

Graphical Demonstration: Bitvectors 85

The Main Screen 86

Using the Buttons 86

Creating a Bitvector Class 86

The Data 87

The Constructor 87

The Destructor 87

The Resize Algorithm 88

The Access Operator 89

The Set Function 91

The ClearAll Function 93

The SetAll Function 93

Trang 15

Contents

Application:The Quicksave 96

Creating a Player Class 97

Storing the Players in the Game 98

Initializing the Data Structures 98

Modifying Player Attributes 99

Saving the Player Array to Disk 100

Playing the Game 102

Bitfields 102

Declaring a Bitfield 103

Using a Bitfield 103

Analysis of Bitvectors and Bitfields in Games 105

Conclusion 106

Chapter 5 Multi-Dimensional Arrays 107

What Is a Multi-Dimensional Array? 108

Graphical Demonstration 111

Native Multi-Dimensional Arrays 112

Declaring a Multi-Dimensional Array 112

Accessing a Multi-Dimensional Array 115

Inside a Multi-Dimensional Array 116

Dynamic Multi-Dimensional Arrays 121

The Array2D Class 121

The Array3D Class 127

Application: Using 2D Arrays as Tilemaps 131

Storing the Tilemap 133

Generating the Tilemap 134

Drawing the Tilemap 135

Playing the Game 136

Application: Layered Tilemaps 136

Redefining the Tilemap 138

Reinitializing the Tilemap 139

Modifying the Rendering Algorithm 140

Trang 16

Playing the Game 141

Comparing Performance 142

Comparing Size 144

Analysis of Multi-Dimensional Arrays in Games 144

Conclusion 145

Chapter 6 Linked Lists 147

What Is a Linked List? 148

Singly Linked Lists 149

Graphical Demonstration: Singly Linked Lists 149

Structure 150

Example 6-4 168

Final Thoughts on Singly Linked Lists 169

Doubly Linked Lists 169

Graphical Demonstration: Doubly Linked Lists 170

Creating a Doubly Linked List 171

Doubly Linked List Algorithms 172

Reading and Writing Lists to Disk 174

Writing a Linked List 174

Reading a Linked List 175

Application: Game Inventories 176

The Player Class 177

The Item Class 177

Adding an Item to the Inventory 178

Removing an Item from the Inventory 178

Playing the Demo 179

Application: Layered Tilemaps Revisited 180

Declaring the Tilemap 181

Trang 17

Contents

Size Comparisons 185

Real-World Issues 187

Conclusion 188

Chapter 7 Stacks and Queues 189

Stacks 190

What Is a Stack? 190

Graphical Demonstration: Stacks 192

The Stack Functions 193

Implementing a Stack 193

Application: Game Menus 199

Queues 204

Graphical Demonstration: Queues 204

The Queue Functions 206

Implementing a Queue 206

Application: Command Queues 212

Conclusion 216

Chapter 8 Hash Tables 217

What Is Sparse Data? 218

The Basic Hash Table 219

Collisions 221

Hashing Functions 221

Enhancing the Hash Table Structure 224

Linear Overflow 224

Quadratic Overflow 225

Linked Overflow 225

Graphical Demonstration: Hash Tables 226

Implementing a Hash Table 228

Trang 18

The HashEntry Class 228

The HashTable Class 229

Example 8-1: Using the Hash Table 233

Application: Using Hash Tables to Store Resources 235

The String Class 236

Using the Table 237

How the Demo Loads Resources 237

Playing the Demo 238

Conclusion 239

Chapter 9 Tying It Together: The Basics 241

Why Classes Are Good 242

Storing Data in a Class 243

Hiding Data 245

Inheritance 248

Using the Classes in a Game 260

Making a Game 265

Adventure:Version One 266

Game 2—The Map Editor 310

Conclusion 314

Recursion and Trees 315

Chapter 10 Recursion 317

What Is Recursion? 318

A Simple Example: Powers 319

Trang 19

Contents

Solving the Puzzle with a Computer 323

Terminating Conditions 325

Example 10-1: Coding the Algorithm for Real 325

Graphical Demonstration:Towers of Hanoi 327

Conclusion 328

Chapter 11 Trees 329

What Is a Tree? 330

The Recursive Nature of Trees 332

Common Structure of Trees 332

Graphical Demonstration:Trees 333

Tutorial 336

Building the Tree Class 338

The Structure 339

The Constructor 340

The Destructor 340

The Destroy Function 341

The Count Function 342

The Tree Iterator 342

The Structure 343

The Basic Iterator Functions 343

The Vertical Iterator Functions 345

The Horizontal Iterator Functions 346

The Other Functions 346

Building a Tree 347

Top Down 347

Bottom Up 347

Traversing a Tree 347

The Preorder Traversal 348

The Postorder Traversal 350

Graphical Demonstration:Tree Traversals 351

Trang 20

Game Demo 11-1: Plotlines 352

Using Trees to Store Plotlines 354

Playing the Game 356

Conclusion 358

Chapter 12 Binary Trees 359

What Is a Binary Tree? 360

Fullness 361

Denseness 361

Balance 362

Structure of Binary Trees 362

Linked Binary Trees 362

Arrayed Binary Trees 363

Graphical Demonstration: Binary Trees 366

Coding a Binary Tree 368

The Structure 368

The Constructor 369

The Destructor and the Destroy Function 369

The Count Function 370

Using the BinaryTree Class 370

Traversing the Binary Tree 371

The Preorder Traversal 372

The Postorder Traversal 372

The Inorder Traversal 372

Graphical Demonstration: Binary Tree Traversals 373

Application: Parsing 374

Arithmetic Expressions 376

Parsing an Arithmetic Expression 376

Trang 21

Contents

Chapter 13

Binary Search Trees 389

What Is a BST? 390

Inserting Data into a BST 391

Finding Data in a BST 394

Removing Data from a BST 394

The BST Rules 394

Sub-Optimal Trees 395

Graphical Demonstration: BSTs 395

Coding a BST 397

The Structure 397

Comparison Functions 397

The Constructor 398

The Destructor 398

The Insert Function 399

The Find Function 400

Example 13-1: Using the BST Class 401

Application: Storing Resources, Revisited 402

The Resource Class 402

The Comparison Function 403

Inserting Resources 403

Finding Resources 403

Playing the Demo 404

Conclusion 405

Chapter 14 Priority Queues and Heaps 407

What Is a Priority Queue? 408

What Is a Heap? 410

Why Can a Heap Be a Priority Queue? 411

Graphical Demonstration: Heaps 417

Coding a Heap Class 418

Trang 22

The Structure 419The Constructor 419The Enqueue Function 420The WalkUp Function 420The Dequeue Function 422The WalkDown Function 422

Application: Building Queues 424

The Units 426Creating a Factory 426The Heap 427Enqueuing a Unit 427Starting Construction 428Completing Construction 428Playing the Demo 429

Conclusion 430

Chapter 15

Game Trees and Minimax Trees 431

What Is a Game Tree? 432 What Is a Minimax Tree? 434 Graphical Demonstration: Minimax Trees 437 Game States 439 More Complex Games 442 Application: Rock Piles 442

The Game State 443The Global Variables 445Generating the Game Tree 446Simulating Play 452Playing the Game 454

Trang 23

Expanding the Game 464

Altering the Map Format 465Game Demo 16-1: Altering the Game 466The Map Editor 473

Further Enhancements 475 Conclusion 475

Graphs 477

Chapter 17 Graphs 479

What Is a Graph? 480

Linked Lists and Trees 480Graphs 482Parts of a Graph 482

Types of Graphs 482

Bi-Directional Graphs 483Uni-Directional Graphs 483Weighted Graphs 484Tilemaps 485

Implementing a Graph 486

Adjacency Tables 486Direction Tables 488General-Purpose Linked Graphs 489

Graphical Demonstration: Graphs 492 Graph Traversals 493

Trang 24

The Depth-First Search 493The Breadth-First Search 495

A Final Word on Graph Traversals 499Graphical Demonstration: Graph Traversals 500

The Graph Class 501

The GraphArc Class 501The GraphNode Classes 502The Graph Class 504

Application: Making a Direction-Table Dungeon 512

The Map 512Creating the Map 513Drawing the Map 514Moving Around the Map 516Playing the Demo 517

Application: Portal Engines 518

Sectors 519Determining Sector Visibility 521Coding the Demo 522Playing the Demo 527

Trang 25

Conclusion 560

Chapter 19 Tying It Together: Graphs 563

The New Map Format 564

The New Room Entry Structure 565The File Format 566

Game Demonstration 19-1: Adding the New Map Format 567

The DirectionMap 568Changes to the Game Logic 580Playing the Game 582

Converting Old Maps 583 The Directionmap Map Editor 584

The Initial Map 585Setting and Clearing Tiles 586Loading a Map 588Saving a Map 590Using the Editor 593

Upgrading the Tilemap Editor 594

The Save Function 594The Load Function 595

Conclusion 596

Trang 26

Algorithms 597

Chapter 20

Sorting Data 599

The Simplest Sort: Bubble Sort 600

Worst-Case Bubble Sort 601Graphical Demonstration: Bubble Sort 602Coding the Bubble Sort 604

The Hacked Sort: Heap Sort 609

Graphical Demonstration: Heap Sort 611Coding the Heap Sort 613

The Fastest Sort: Quicksort 616

Picking the Pivot 616Performing the Quicksort 618Graphical Demonstration: Quicksort 621Coding the Quicksort 623

Graphical Demonstration: Race 627 The Clever Sort: Radix Sort 630

Graphical Demonstration: Radix Sorts 631Coding the Radix Sort 633

Other Sorts 637 Application: Depth-Based Games 638

The Player Class 639The Globals 640The Player Comparison Function 640Initializing the Players 640Sorting the Players 641Drawing the Players 641

Trang 27

Contents

Chapter 21 Data Compression 645

Why Compress Data? 646

Data Busses 647The Internet 649

Run Length Encoding 649

What Kinds of Data Can Be Used for RLE? 650Graphical Demonstration: RLEs 651Coding an RLE Compressor and Decompressor 656

Huffman Trees 665

Huffman Decoding 665Creating a Huffman Tree 667Coding a Huffman Tree Class 676Example 21-3 691Test Files 692Example 21-4 693

Data Encryption 693 Further Topics in Compression 694 Conclusion 694

Chapter 22 Random Numbers 697

Generating Random Integers 698

Generating Random Numbers in a Program 699Using rand and srand 700Using a Non-Constant Seed Value 702Generating a Random Number Within a Range 702

Generating Random Percents 705 Generating Random Floats 706 Generating Non-Linear Random Numbers 707

Probability Distribution Graphs 707Adding Two Random Numbers 709

Trang 28

Adding Three Random Numbers 711Graphical Demonstration: Random Distribution Graphs 712

Robust Pathfinding 721

The Breadth-First Search 721Making a Smarter Pathfinder 739Making a Better Heuristic 746The A* Pathfinder 750Graphical Demonstration: Path Comparisons 753

Weighted Maps 754

Application: Stealth 756

Thinking Beyond Tile-Based Pathfinding 762

Line-Based Pathfinding 762Quadtrees 764Waypoints 765

Conclusion 767

Chapter 24

Tying It Together: Algorithms 769

Making the Enemies Smarter with Pathfinding 770

Adding Pathfinding to the TileMap Class 771Adding Pathfinding to the DirectionMap Class 780Visualizing the GetClosestCell Algorithm 785

Trang 29

Contents

Conclusion 793

Extra Topics 794 Further Reading and References 795

Data Structure Books 795C++ Books 796Game Programming Books 797Web Sites 798

Standard C/C++ Functions Used in This Book 811

Basic Input/Output 811File I/O 814Math Functions 817The Time Function 818The Random Functions 819

Exceptions and Error Handling 820

Assertions 820Return Codes 820Exceptions 821

Why C++? 823

Trang 30

Class Topics 824

Constructors 824Destructors 826Operator Overloads 827Conversion Operators 829The This Pointer 830Inline Functions 830Function Pointers 832

Global Variables 838Static Variables 839

The Stack 840

Local Variables 840Parameters 842Return Values 843

The Free Store 844 Conclusion 845

Appendix C

Introduction to SDL 847

The Licensing 848 Setting Up SDL 849

Trang 31

Contents

Setting Up SDL_TTF 856 Distributing Your Programs 858 Using SDL 858

SDL_Video 858SDL Event Handling 861SDL_Timer 863SDL_TTF 863

The SDLHelpers Library 865 The SDLFrame 867 The SDLGUI Library 869

The SDLGUI Class 869The SDLGUIItem Class 874The SDLGUI Items 876The SDLGUIFrame 876

Conclusion 878

Appendix D Introduction to the Standard Template Library 879

STLPort 880 STL Versus This Book 882 Namespaces 883 The Organization of STL 885 Containers 889

Sequence Containers 890Associative Containers 896Container Adaptors 896The Miscellaneous Containers 898

Conclusion 899

Index 901

Trang 32

Letter from the Series Editor

Dear reader, I’ve always wanted to write a book on data structures However, there is simply no way to do the job right unless you use graphics and animation, and that means a lot of work I personally think that all computer books will be animated, annotated, and interactive within 10 years—they have to

be There is simply too much information these days to convey with text alone; we need to use graphics, color, sound, animation—anything and everything to try to make the complex computer science subjects under-standable these days

With that in mind, I wanted a data structures book that was like no other—a book using today’s technology that could live up to my high stan-dards So I set out to find the perfect author and finally Ron Penton came along to take on the challenge Ron, too, had my same vision for a data structures book We couldn’t do something that had been done—there are

a zillion boring data structure books—but if we could apply gaming nology and graphics to teach the subject, we would have something unique Moreover, this book is for anyone who wants to learn data struc-tures and related important algorithms Sure, if you’re a game program-mer then you will feel at home, but if you’re not, then believe me, put down that hardbound college text and pick this book up because not only will you absolutely know this stuff inside and out by the time you’re done, but you will have an image in your mind like you have never had before All right, now I want to talk about what you’re going to find inside

tech-First, Ron has really outdone himself with the demonstrations in this book

I would have been happy with little dots moving around and some arrows, but he has created an entire system to build the book demos in so that you can see the data structures working and the algorithms processing them It’s simply amazing to actually see bubble sort, quick sort, heap sort, and

so on all race each other, or the insertion and deletion of nodes in a tree Only a game programmer could bring these and more to you—no one

Trang 33

xxxi xxxi Letter from the Series Editor

pull this off On the other hand, if you are a game programmer, then you will greatly appreciate Ron’s insight into applications of various data struc-tures and algorithms for game-related programs In fact, he came up with some pretty cool applications I hadn’t thought of!

So what’s inside? Well, the book starts off with an introduction, gets you warmed up with arrays, bit vectors, and simple stuff like that, and talks about the use of SDL (the simple direct media layer) used for the demos

Then the book drives a steak through the heart of the data structure

drag-on and covers asymptotic analysis, linked lists, queues, heaps, binary trees, graphs, hash tables, and the list goes on and on After Ron has made you a believer that hash tables are the key to the universe, he switches gears to algorithms and covers many of the classic algorithms in computer science, such as sorting, searching, compression, and more Of course, no book like this would be complete without coverage of recursion, and that’s in

here, too—but you will love it because for once, you will be able to see the

recursion! Finally, the book ends with primers on C++, SDL, and the dard template library, so basically you will be a data structure god when you’re done!

stan-In conclusion, this book is for the person who is looking for both a cal and a theoretical base in data structures and algorithms I guarantee that it will get you farther from ground zero than anything else

practi-André LaMothe Series Editor

Trang 34

Introduction

What is a computer program? When you get down to the lowest level, you can rate a program into two main sections: the data and the instructions that operate

sepa-on the data These two sectisepa-ons of a program are commsepa-only called the data

struc-tures and the algorithms

This book will teach you how to create many data structures, ranging from the very simple to the moderately complex

Understanding data structures and algorithms is an essential part of game gramming Knowing the most efficient way to store data and work with the data is

pro-an importpro-ant part of game programming; you wpro-ant your games to run as quickly as possible so you can pack as many cool features into them as you can

I have a few goals with this book:

■ Teach you how the most popular data structures and algorithms work

■ Teach you how to make the structures and algorithms

■ Teach you how to use the data structures in computer games Mark Twain once said this:

It is a good thing, perhaps, to write for the amusement of the public But it is a far higher and nobler thing to write for their instruction

I have always tried to help people whenever they need it However, most of my help has been interactive—in chat rooms or in person People ask me questions, and I answer them If they don’t understand, I can explain it better A book is a different format for me because you cannot ask me a question if there is something you don’t understand So I have used the only method I can think of to prevent you

from needing to ask questions: I explain everything Well, not quite everything

because that is pretty much impossible, but I have tried to explain as much as ble to help you understand things better

Trang 35

Introduction

If you’re standing in the bookstore reading this Introduction and wondering, “Is this book good for me?”, then read this section If you’ve already bought the book, thank you! I am going to assume that you’re reading this book because you want to learn more (unless some diabolical person is forcing you to read this as an arcane form of torture )

This is a somewhat complex book because it deals with lots of concepts However, I feel that I have included ample introductory material as well Therefore, this book

is for the game programmer who is just starting out at an intermediate level So what

do I expect you to know?

I expect you to know basic C++, but don’t feel confused if you don’t feel like an expert Pretty much every complex topic I use in C++ is covered in Appendix A, so

if you’re unfamiliar with a concept or just forget how something works, take a few minutes to read that appendix

The most complex feature of C++ that I use is templates, but you don’t need to know

about them before you read this book Chapter 2 is an extensive introduction to templates, so don’t worry if you don’t know what they are just yet

One advanced concept I use often in the later parts of the book is recursion, but

you don’t have to know about that, either Chapter 10 is a small introduction to recursion

This book is for anyone who wants to learn more about how a computer works, how to store data, and how to efficiently work on that data All of this material is essential to game programming, so take a glance at the Table of Contents If there

is anything there that you don’t already know about, this book is for you Even if you know a little about the topics, this book is still good for you because every chapter goes in depth about these subjects

In this book, I cover many data structures and how to use them in games, ranging from the simple (arrays) to the complex (graphs and trees)

I have tried to make every chapter follow a certain format First, I begin explaining the data structure or algorithm in theory so that you can see how it works and

why it works After that, I show you an interactive Graphical Demonstration of the

structure, which is a demo on the CD that you can play around with to help you

Trang 36

understand how it works These demonstrations all use the Simple DirectMedia Layer

(SDL) multimedia library, which I go more into depth on in just a little bit All of

these demonstrations are located in the \demonstrations\ directory on the CD After that, I show you how to actually code the structure or algorithm in C++ The code for these sections is mostly platform free, so it will usually compile on any compiler I mention any sections that are platform-specific in the book All of the code for the data structures and algorithms can be found on the CD in the directory \structures\ for your convenience Copies of the files have also been placed in the directories of every demo that uses them Whenever necessary, I have

included console mode Examples on how these structures work in the \examples\

directory on the CD All of the examples use pure C/C++, with no extra SDKs or APIs needed, so they use input and output to the text console window on your computer

CAUTION You are free to use any of the data structures included on the CD in any projects you use However, be warned; they were designed to demonstrate the structures and are not super-optimized Many functions can be made faster, particularly the small functions that can be inlined (see Appendix A).You cannot copy any of the structures because none of them implements proper copy constructors

Whenever you pass a structure into a function as a parameter, make absolutely certain that you pass-by-reference or use a pointer; otherwise, it will mess up your structure If you don’t know what this means just yet, look at the functions that use the data structures; they demonstrate how to use them correctly

Finally, I show you an interactive Game Demonstration, which highlights the usage of

the structure or algorithm in a game-like atmosphere Most of these games are ple, but they prove a point These demonstrations also use the SDL multimedia library and are located on the CD in the directory \demonstrations\

sim-Some chapters might deviate from the format to show you different versions of the structures

I’ve separated this book into six main parts:

Trang 37

■ Basic Algorithm Analysis—This chapter is a little on the theoretical side, and

it deals with topics that are usually taught in school This chapter shows you how algorithms are rated for speed so that you can see how to choose the best algorithm for your needs

■ Templates—This is a somewhat advanced C++ concept Some C++ books don’t cover templates well, and because this book uses them extensively, I feel that it is a good idea to include a chapter on how to use them

You can safely skip this section if you already know the material

an important structure in computing

■ Bitvectors—Bitvectors are an important part of space optimization This chapter shows you how to store data in as small of a place as possible

■ Multi-Dimensional Arrays—This chapter expands on the array chapter and shows you how to use arrays with more than one dimension

■ Linked Lists—This chapter introduces you to the concept of linked data, which has many insertion and deletion benefits

Trang 38

■ Stacks and Queues—This is the first chapter that doesn’t introduce you to a new structure Instead, it shows you how to access data in certain ways

■ Hash Tables—This chapter shows you an advanced method of storing data by using both arrays and linked lists It is the last structure covered in this part

of the book

In addition to those, the last chapter in this part (Chapter 9) is the first of the

“Tying It Together” chapters There are four of these chapters throughout the book, one at the end of Parts Two, Three, Four, and Five In Chapter 9, I introduce you to the ideas of learning how to store custom game data and designing your own classes After that, I show you how to design a basic game using many of the structures from this part of the book

In this Part, I introduce you to the ideas of recursion, recursive algorithms, and

recursive data structures, namely trees This Part includes the following chapters:

■ Recursion—This is a small chapter introducing you to the idea of recursion and how it works Recursion is a tough subject and isn’t covered well in most C++ books, so I felt that I needed to include an introduction to the concept

■ Trees—This chapter introduces you to the idea of a linked tree data ture and how it is used

struc-■ Binary Trees—This chapter shows you a specific subset of trees Binary trees are the most frequently used tree structures in computing

■ Binary Search Trees—This chapter shows you how to store data in a recursive manner so that you can access it quickly later

■ Priority Queues and Heaps—Heaps are another variation of the binary tree This chapter shows you how to use a binary tree to implement an efficient queue variation called the priority queue

■ Game Trees and Minimax Trees—Game Trees are a different kind of tree used to store state information about turn-based games

In addition, Chapter 16 expands upon Chapter 9 and adds some tree-like ties to the game from Chapter 9

Trang 39

Introduction

In this part, I introduce you to the graph data structure, which is another linked data structure that is somewhat like trees This part of the book is broken down into the following chapters:

■ Graphs—This chapter introduces you to the idea of the graph structure and its many derivatives Graphs are used all over in game programming

■ Using Graphs for AI: Finite State Machines—This is an application of the graph data structure to the field of artificial intelligence—a way to make your games smarter

Chapter 19 applies some concepts from the graph chapter and adds them to the game from Chapter 16

Originally, I had planned to include these topics in the previous three parts, but they really fit better in a section of their own Some of the topics use concepts from all three of the previous parts, and others don’t This part is composed of the fol-lowing chapters:

■ Sorting Data—This chapter covers four different sorting algorithms

■ Data Compression—This chapter shows you two ways to compress data

■ Random Numbers—This chapter shows you how to use the random number generator built into the C standard library and how to use some algorithms

to get impressive results from generating random numbers

■ Pathfinding—This chapter shows you four different pathfinding algorithms

to use on the maps you create in your games

The final chapter, Chapter 24, expands on the game from Chapters 9, 16, and 19

by adding pathfinding support to the AIs in the game

Appendixes

Finally, there are four appendixes in the book that cover a variety of topics:

■ A C++ Primer—This appendix attempts to cover the features of C++ that are used in this book so you don’t have to go running for a reference book every time I use something that you want to know more about

Trang 40

■ The Memory Layout of a Computer Program—To understand how to use a computer to its fullest extent, you must know about how it structures its memory This appendix tells you this information

■ Introduction to SDL—This is a basic introduction to the Simple DirectMedia Layer library, which the book uses for all of the demonstrations It also goes over the two SDL libraries I’ve developed to make the demonstrations in the book

■ Introduction to the Standard Template Library—This appendix introduces you to the C++ Standard Template Library, which is a built-in structure and algorithm library that should come with every compiler

The CD for this book contains every Example, Game Demonstration, and

Graphical Demonstration for the book There are 33 Examples, 26 Game

Demonstrations, and 34 Graphical Demonstrations That is 93 examples and demonstrations! That should be enough to keep you busy for a while

Just in case you end up wanting more, however, there’s even more stuff on the CD There are 19 code files full of the data structures and algorithms in this book, con-veniently located in the directory \structures\, as well as the two SDL libraries I’ve developed for the book (see Appendix C)

In the \goodies\ directory, there are four articles—two dealing with trees and two dealing with SDL They expand on the topics covered in this book

In addition, the SDL, SDL_TTF, STLPort, and FreeType libraries (see Appendixes

C and D for more information) are in that directory

Figure I.1 shows you the layout of the CD

Ngày đăng: 17/04/2014, 09:15

TỪ KHÓA LIÊN QUAN