Data Structures and Algorithms for Game Developers Limited Warranty and Disclaimer of Liability
Total Page:16
File Type:pdf, Size:1020Kb
DATA STRUCTURES AND ALGORITHMS FOR GAME DEVELOPERS LIMITED WARRANTY AND DISCLAIMER OF LIABILITY THE CD-ROM THAT ACCOMPANIES THE BOOK MAY BE USED ON A SINGLE PC ONLY. THE LICENSE DOES NOT PERMIT THE USE ON A NETWORK (OF ANY KIND). YOU FURTHER AGREE THAT THIS LICENSE GRANTS PERMISSION TO USE THE PRODUCTS CONTAINED HEREIN, BUT DOES NOT GIVE YOU RIGHT OF OWNERSHIP TO ANY OF THE CONTENT OR PRODUCT CONTAINED ON THIS CD-ROM. USE OF THIRD-PARTY SOFTWARE CONTAINED ON THIS CD-ROM IS LIMITED TO AND SUBJECT TO LICENSING TERMS FOR THE RESPECTIVE PRODUCTS. CHARLES RIVER MEDIA, INC. (“CRM”) AND/OR ANYONE WHO HAS BEEN INVOLVED IN THE WRITING, CREATION, OR PRODUCTION OF THE ACCOMPA- NYING CODE (“THE SOFTWARE”) OR THE THIRD-PARTY PRODUCTS CON- TAINED ON THE CD-ROM OR TEXTUAL MATERIAL IN THE BOOK, CANNOT AND DO NOT WARRANT THE PERFORMANCE OR RESULTS THAT MAY BE OBTAINED BY USING THE SOFTWARE OR CONTENTS OF THE BOOK. THE AUTHOR AND PUBLISHER HAVE USED THEIR BEST EFFORTS TO ENSURE THE ACCURACY AND FUNCTIONALITY OF THE TEXTUAL MATERIAL AND PROGRAMS CONTAINED HEREIN. WE HOWEVER, MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, REGARDING THE PERFORMANCE OF THESE PROGRAMS OR CON- TENTS. THE SOFTWARE IS SOLD “AS IS” WITHOUT WARRANTY (EXCEPT FOR DEFECTIVE MATERIALS USED IN MANUFACTURING THE DISK OR DUE TO FAULTY WORKMANSHIP). THE AUTHOR, THE PUBLISHER, DEVELOPERS OF THIRD-PARTY SOFTWARE, AND ANYONE INVOLVED IN THE PRODUCTION AND MANUFACTURING OF THIS WORK SHALL NOT BE LIABLE FOR DAMAGES OF ANY KIND ARISING OUT OF THE USE OF (OR THE INABILITY TO USE) THE PROGRAMS, SOURCE CODE, OR TEXTUAL MATERIAL CONTAINED IN THIS PUBLICATION. THIS INCLUDES, BUT IS NOT LIMITED TO, LOSS OF REVENUE OR PROFIT, OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THE PRODUCT. THE SOLE REMEDY IN THE EVENT OF A CLAIM OF ANY KIND IS EXPRESSLY LIMITED TO REPLACEMENT OF THE BOOK AND/OR CD-ROM, AND ONLY AT THE DISCRETION OF CRM. THE USE OF “IMPLIED WARRANTY” AND CERTAIN “EXCLUSIONS” VARIES FROM STATE TO STATE, AND MAY NOT APPLY TO THE PURCHASER OF THIS PRODUCT. DATA STRUCTURES AND ALGORITHMS FOR GAME DEVELOPERS ALLEN SHERROD CHARLES RIVER MEDIA Boston, Massachusetts Copyright 2007 Career & Professional Group, a division of Thomson Learning, Inc. Published by Charles River Media, an imprint of Thomson Learning Inc. All rights reserved. No part of this publication may be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means or media, electronic or mechanical, including, but not limited to, photocopy, recording, or scanning, without prior permission in writing from the publisher. Cover Design: Tyler Creative CHARLES RIVER MEDIA 25 Thomson Place Boston, Massachusetts 02210 617-757-7900 617-757-7951 (FAX) [email protected] www.charlesriver.com This book is printed on acid-free paper. Allen Sherrod. Data Structures and algorithms for Game Developers. ISBN: 1-58450-495-1 ISBN-13: 978-1-58450-495-5 eISBN: 1-58450-663-6 All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks should not be regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. Library of Congress Cataloging-in-Publication Data Sherrod, Allen. Data structures and algorithms for game developers / Allen Sherrod. p. cm. ISBN-13: 978-1-58450-495-5 (hardback with cd-rom : alk. paper) ISBN-10: 1-58450-495-1 (hardback with cd-rom : alk. paper) 1. Computer games--Programming. 2. Data structures (Computer science) 3. Computer algorithms. I. Title. QA76.76.C672S535 2007 794.8'1526--dc22 2007006782 Printed in the United States of America 07 7 6 5 4 3 2 First Edition CHARLES RIVER MEDIA titles are available for site license or bulk purchase by institutions, user groups, corporations, etc. For additional information, please contact the Special Sales Department at 800-347-7707. Requests for replacement of a defective CD-ROM must be accompanied by the original disc, your mailing address, telephone number, date of purchase and purchase price. Please state the nature of the problem, and send the information to CHARLES RIVER MEDIA, 25 Thomson Place, Boston, Massachusetts 02210. CRM’s sole obligation to the purchaser is to replace the disc, based on defective materials or faulty workmanship, but not on the operation or functionality of the product. Contents Acknowledgments ix Introduction xi About the Author xvi 1Introduction to Data Structures 1 Data Structures and Algorithms 2 Data Structures in Games and Simulations 4 C++ versus Java and C# 6 The C++ STL 11 Template Classes and Functions 12 Big-O Notation 15 Summary 17 Chapter Review Questions 17 2 Arrays 19 The Data Structures Known as Arrays 20 Algorithms: Insertion and Deletion 25 Ordered Arrays 31 Algorithms: Basic Searches 33 STL Arrays 47 Bit Arrays 63 Summary 70 Chapter Review Questions 70 Programming Projects 71 3 Recursion 73 Recursion Defined 75 Triangular Numbers 81 Factorials 86 Summary 89 Chapter Review Questions 89 Programming Projects 90 v vi Contents 4 Introduction to Sorting 91 Introduction to Sorting 92 The Bubble Sort 93 The Selection Sort 98 The Insertion Sort 101 STL Sorting 105 The Merge Sort 109 Summary 115 Chapter Review Questions 116 Programming Projects 118 5 Link Lists 119 Introduction to Link Lists 120 Singly and Double-Ended Linked Lists 124 Doubly Linked Lists 135 STL Link Lists 141 Tips and Things to Remember When Using Link Lists 151 Summary 152 Chapter Review Questions 152 Programming Projects 153 6 Stacks and Queues 155 Introduction to Stacks 156 STL Stacks 168 Introduction to Queues 172 STL Queues 194 Summary 206 Chapter Review Questions 206 Programming Projects 207 7 Hash Tables 209 Introduction to Hash Tables 210 Hash Functions 213 Working with Hash Tables 215 Implementing Hash Tables 220 Nonstandard Hash Containers 242 Summary 250 Chapter Review Questions 251 Programming Projects 252 8 Advanced Sorting 253 Advanced Sorting Topics 254 Contents vii Shellsort 255 Partitioning 260 Quicksort 265 Radix Sort 275 Additional Types of Sorting 280 Summary 282 Chapter Review Questions 282 Programming Projects 284 9 Trees 285 Introduction to Trees 286 Tree Example 289 Binary Trees 298 k-dimensional Trees 315 Additional Types of Trees 322 Summary 324 Chapter Review Questions 324 Programming Projects 326 10 Heaps 327 Introduction to Heaps 328 Heap Sort 336 Priority Queues Using Heaps 339 STL Heap Functions 346 Summary 348 Chapter Review Questions 348 Programming Projects 350 11 Graphs 351 Introduction to Graphs 352 Searching with Graphs 357 Topological Sorting 376 Weighted Graphs 385 Artificial Intelligence 391 Summary 393 Chapter Review Questions 394 Programming Projects 396 12 Additional STL Algorithms 397 Strings 398 map and multimap 404 set and multiset 411 viii Contents STL Algorithms 415 Summary 427 Chapter Review Questions 428 Programming Projects 429 13 Scene Management 431 Introduction to Scene Management 432 Game Math 435 Scene Graphs 449 Binary Space Partitioning Trees 460 Quad-Trees and Octrees 466 Additional Management Topics 472 Summary 477 Chapter Review Questions 478 Programming Projects 479 14 Data Compression 481 Introduction to Data Compression 482 Introduction to Texture Compression 494 Introduction to Data Encryption 514 Summary 515 Chapter Review Questions 515 Programming Projects 517 15 Conclusions 519 Quick Review 520 The Next Step 524 Summary 525 Appendix A Additional Resources 527 Appendix B Chapter Review Question Answers 531 Appendix C OpenGL 539 Appendix D NonStandard Containers and Algorithms 543 Index Acknowledgments ith every book I write I learn a lot about myself as a writer and as a person. Along my journey I’ve received a lot of help and support that Wwent a long way to making each of my book projects possible. I would like to thank my friends, family, and the men and women at Charles River Media and Thomson Learning who believed in me from the beginning. I would also like to thank the readers of each of my books and the loyal visitors of www. UltimateGameProgramming.com. I would like to give a special thanks to Jenifer Niles of Charles River Media for believing in me and giving me this chance. ix This page intentionally left blank Introduction What to Expect in the First Edition Who this Book is For What You Need to Know Before Reading this Book The Software Needed for this Book How this Book Is Organized WHAT TO EXPECT IN THE FIRST EDITION The creation of a video game is no easy task for anyone to undertake. When it comes to programming, a lot of knowledge needs to be obtained to create an effi- cient and solid product. The game industry is a very competitive business, where games are defined heavily by the products that came before them. These products have raised the bar in terms of what is considered cutting-edge and standard in the industry. The more advanced games become, the more data is required to realize a game, especially from a graphics point of view. As games continue to push the en- velope of what can be done and as they continue to increase gamers’ expectations, it becomes harder to create top-of-the-line quality products. This book is the first edition of Data Structures for Game Developers. In this book you can expect to walk away with detailed knowledge about various data structures and the algorithms that can be performed on them. This combination is the key to creating real-time simulations that push the envelope on what is consid- ered cutting-edge.