Mac Game Programming 00 Macgame FM.Qxd 2/21/03 5:02 PM Page Ii
Total Page:16
File Type:pdf, Size:1020Kb
00 Macgame_FM.qxd 2/21/03 5:02 PM Page i Mac Game Programming 00 Macgame_FM.qxd 2/21/03 5:02 PM Page ii This page intentionally left blank 00 Macgame_FM.qxd 2/21/03 5:02 PM Page iii Mac Game Programming Mark Szymczyk 00 Macgame_FM.qxd 2/21/03 5:02 PM Page iv © 2002 by Premier Press, Inc. All rights reserved. No part of this book may be reproduced or trans- mitted 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, top edge printing, and related trade dress are trademarks of Premier Press, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. Publisher: Stacy L. Hiquet Marketing Manager: Heather Buzzingham Managing Editor: Sandy Doell Acquisitions Editor: Emi Smith Series Editor: André LaMothe Editorial Assistant: Margaret Bauer Marketing Coordinator: Kelly Poffenbarger Technical Reviewer: Darrell Walisser Copy Editor: Alice Martina Smith Interior Layout: LJ Graphics Cover Design: Mike Tanamachi CD Producer: Darrell Walisser Indexer: Johnna VanHoose Dinse Proofreader: Kim Benbow Microsoft, Windows, Internet Explorer, Notepad, VBScript, ActiveX, and FrontPage are trademarks or registered trademarks of Microsoft Corporation. Netscape is a registered trademark of Netscape Communications Corporation. Macintosh is a registered trademark of Apple Computer, Inc. Premier Press and the author have attempted throughout this book to distinguish proprietary trade- marks from descriptive terms by following the capitalization style used by the manufacturer. Important: Premier Press cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. 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-18-7 Library of Congress Catalog Card Number: 2001096218 Printed in the United States of America 02 03 04 05 RI 10 9 8 7 6 5 4 3 2 1 00 Macgame_FM.qxd 2/21/03 5:02 PM Page v This book is dedicated to the memory of Jeff Bilicki. 00 Macgame_FM.qxd 2/21/03 5:02 PM Page vi Foreword o you want to be a Mac game programmer? This book is a great place to start. SWhether you are interested in how games work, want to learn to write share- ware games, or want to become the next great commercial game developer, this book will show you some of the key technologies and techniques required to make your dream game. Through the years, I’ve worked on many Mac games, from Duke Nukem 3D to Tomb Raider and Unreal Tournament. When I began writing Mac games in 1985, I would have loved to have access to a reference like this one. In those early years, Mac game developers learned tricks from anywhere they could find them—late nights pouring over Apple documentation, swapping ideas with other developers, and a lot of trial and error. As president of Westlake Interactive, I’m often asked by people how to break into the Mac game business. My first answer is to get experience any way you can. Anything from writing simple 2D games to working on flashy demos will show potential employers that you have the drive and knowledge to make it in the game business. Mac game programmers are incredibly difficult to find these days, so the market for people with the interest and talent to work in the industry is very strong. Writing Macintosh games can be some of the most exciting, challenging, and rewarding work a programmer can tackle. You have in your hands the perfect start- ing point to enter the world of game programming; with hard work and determina- tion, you’ll be well on your way to writing that next great Mac game! Glenda Adams President, Westlake Interactive May 2002 00 Macgame_FM.qxd 2/21/03 5:02 PM Page vii Acknowledgments t pains me to admit it, but I did not single-handedly create this book. Many people Ihelped make this book a reality, and I’d like to thank them here. First, I’d like to thank my editors at Premier Press—and I worked with many editors on this book. I would like to thank my original acquisitions editor, Jody Kennen, for discovering me and for putting the book deal together. I would also like to thank my original project editor, Brian Thomasson, for his help as I wrote the first chapters of this book. He helped me get up to speed quickly on writing the chapters. While I was writing this book, both Jody and Brian left to take other positions. Emi Smith became my new acquisitions editor. I need to thank Emi for gathering all the permissions necessary to include all the programs on the CD-ROM and for putting up with delays in the project. Thanks to my copy editor, Alice Martina Smith, for cleaning up some of my grammar and for bringing up questions that made the text of this book easier for you to understand. I would also like to thank my Tech Editor, Darrell Walisser, for reviewing all the source code in this book and making sure it all runs well. Next, I owe a huge Thank You to Carlos Camacho, the editor of the Mac game pro- gramming site iDevGames, for two reasons. First, he let me write some articles for his site, which is how Premier Press found me. Without his Web site, I wouldn’t be writ- ing this book. Second, he donated artwork, music, and sound effects for the CD- ROM that accompanies this book. His donation will make it easier for you to make your own games, improving the book in the process. I had many programming questions as I wrote the code for the book. I would like to thank everyone who answered my questions on various mailing lists and message boards. I don’t have a list of individuals who helped, but you know who you are. 00 Macgame_FM.qxd 2/21/03 5:02 PM Page viii The CD-ROM that is with this book comes packed with development tools, games, source code, and game assets. I would like to thank everyone who graciously let me include their programs on the CD-ROM. You have helped make this book as good as it can possibly be. Finally, I want to thank my family for helping keep me sane while I wrote the book. To my parents Stan and Mary, my brothers Dave and Steve, my sister Kathy, her hus- band John, and my nephews Zachary and Christian, thank you. 00 Macgame_FM.qxd 2/21/03 5:02 PM Page ix About the Author When he graduated with a computer science degree from John Carroll University, Mark Szymczyk set out on a career in software development. After bouncing around among various corporate programming positions, he decided to go into game development, starting his own shareware game company, Black Apple Software. While working on his first game, he wrote some game development arti- cles for the iDevGames Web site, which led to a request to write this book. Now that the book is completed, he can finish up work on his first game. Mark currently lives in the Cleveland, Ohio area. When he’s away from his Mac, he enjoys playing basketball, listening to underground music from the 80s and 90s, and hanging out with his nephews. 00 Macgame_FM.qxd 2/21/03 5:02 PM Page x x Contents at a Glance Introduction . xxviii Chapter 1 Introduction to the Mac and to Game Programming . 1 Chapter 2 Project Builder . 27 Chapter 3 C++ for C Programmers. 59 Chapter 4 Introduction to Macintosh Graphics . 77 Chapter 5 Tiles and Scrolling. 113 Chapter 6 Animation. 155 Chapter 7 InputSprocket. 197 Chapter 8 HID Manager . 233 Chapter 9 Reading the Keyboard and Mouse Plus Event Handling . 297 00 Macgame_FM.qxd 2/21/03 5:02 PM Page xi Contents at a Glance xi Chapter 10 Sound. 345 Chapter 11 Physics . 391 Chapter 12 Beginning Artificial Intelligence . 455 Chapter 13 Pathfinding . 491 Chapter 14 DrawSprocket . 545 Chapter 15 Files . 585 Chapter 16 Putting It All Together . 639 Chapter 17 Understanding the Game’s Source Code . 675 Chapter 18 Optimization . 717 Chapter 19 Game Development Tips . 763 Appendix A Installing Programs from the CD-ROM . 797 Appendix B Game Development Resources . 801 Appendix C CD-ROM Contents . 813 00 Macgame_FM.qxd 2/21/03 5:02 PM Page xii xii Contents Foreword . vi Letter from the Series Editor. xxxi Introduction . xxxiii Chapter 1 Introduction to the Mac and to Game Programming . 1 A Brief History of the Mac OS . 2 Mac Programming Models . 6 Mac Toolbox . 6 Carbon . 7 Cocoa . 10 Programming with Carbon . 11 What to Place in Your System Folder. 11 What to Place in Your Compiler . 12 A Description of the Game We Will Develop. 12 Elements of a Computer Game . 12 Player Input . 13 Challenges . 13 Graphics . 13 Sound . 14 Physics . 14 Game Event Loop .