iPhone Games Projects DAVE MARK, SERIES EDITOR PJ CABRERA JOACHIM BONDO AARON FOTHERGILL BRIAN GREENSTONE OLIVIER HENNESSY MIKE KASPRZAK MIKE LEE RICHARD ZITO MATTHEW AITKEN CLAYTON KANE iPhone Games Projects Copyright © 2009 by PJ Cabrera, Joachim Bondo, Aaron Fothergill, Brian Greenstone, Olivier Hennessy, Mike Kasprzak, Mike Lee, Richard Zito, Matthew Aitken, Clayton Kane All rights reserved. No part of this work 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 the prior written permission of the copyright owner and the publisher. ISBN-13 (paperback): 978-1-4302-1968-2 ISBN-13 (electronic): 978-1-4302-1969-9 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Clay Andres Developmental Editor: Douglas Pundick Lead Author and Technical Reviewer: PJ Cabrera Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager | Production Director: Grace Wong Copy Editors: Kim Wimpsett, Marilyn Smith Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor | Interior Designer: Diana Van Winkle Proofreader: Nancy Bell Indexer: BIM Indexing & Proofreading Services Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www. springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. To my parents, Pedro and Cecilia: LQM —PJ Cabrera To my brother Adam, who does 50% of the work and gets 0.01% of the mentions ; ) —Aaron Fothergill To my teammates, past and present, who have made me the engineer I am —Mike Lee Contents at a Glance Foreword ..................................................................... xiii About the Lead Author and Technical Reviewer .................................xv Acknowledgments ...........................................................xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix JOACHIM BONDO CHAPTER 1 Simplify the User Interface for Complex Games: Chess, the Deep Green Way .................................. 3 PJ CABRERA CHAPTER 2 Responsive Social Gaming with RESTful Web Services ........21 AARON FOTHERGILL CHAPTER 3 Rapid Game Development Using (Mostly) Standard C ........61 BRIAN GREENSTONE CHAPTER 4 Brian Greenstone’s Jedi Master List for Game Optimization ...101 OLIVIER HENNESSY AND CLAYTON KANE CHAPTER 5 Starting with a Game Design Document: A Methodology for Success ................................131 MICHAEL KASPRZAK CHAPTER 6 Multiplatform Game Development: iPhone Games for Linux and Windows ......................155 MIKE LEE CHAPTER 7 Code Optimization with Mike Lee, the “World’s Toughest Programmer” ........................191 RICHARD ZITO AND MATTHEW AITKEN CHAPTER 8 Networked Games: Choosing the Right Option ...............215 INDEX ...................................................................... 251 v Contents Foreword ..................................................................... xiii About the Lead Author and Technical Reviewer .................................xv Acknowledgments ...........................................................xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix JOACHIM BONDO CHAPTER 1 Simplify the User Interface for Complex Games: Chess, the Deep Green Way ......................3 Once Upon a Time . . . ..........................................4 Why Should We Care About Simplicity? .........................7 How Simplicity Was Achieved ..................................7 Distilling the Essence ......................................8 Pushing the Pixels .........................................9 Letting the User Focus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Drilling Down ............................................12 Less Is More ..............................................13 Empowering the User ....................................15 Making the User Smile ...................................16 Summary .....................................................17 PJ CABRERA CHAPTER 2 Responsive Social Gaming with RESTful Web Services ..................... 21 Social Networking Games and the iPhone .....................22 Creating the Prototype High-Score Web Service ................24 Creating the High-Scores Rails App .......................24 Using RESTful XML Web Services ..........................27 Displaying High Scores on the iPhone .........................29 Creating the User Interface ...............................30 Connecting to Web Services ..............................36 Parsing XML .............................................42 Displaying the High Scores in the Table View ..............46 vii viii CONTENTS Submitting High Scores to the Web Service ....................49 Submitting User Achievements ................................52 Introducing ObjectiveResource ................................53 Using ObjectiveResource .................................53 Submitting High Scores with ObjectiveResource ..........57 Summary .....................................................57 AARON FOTHERGILL CHAPTER 3 Rapid Game Development Using (Mostly) Standard C .................................... 61 Getting Started ...............................................64 Creating the Project ...........................................66 Addressing the Save Game Problem ...........................74 How to Save .............................................74 How to Save the Easy Way ................................75 Space Hike: The App ..........................................81 The Render Loop and Basic Organization .................81 Game Logic Overview ....................................83 Drawing and Handling the Game .........................84 Enhancing the Game .....................................97 Summary .....................................................98 BRIAN GREENSTONE CHAPTER 4 Brian Greenstone’s Jedi Master List for Game Optimization ....................... 101 Memory Matters ............................................ 102 Parlez-vous C? .............................................. 103 Straight C Is Best ....................................... 103 Cocoa vs. Core Foundation ............................. 105 Compiler Optimizations ..................................... 105 The Thumb Instruction Set ............................. 106 Compiler Optimization Level ........................... 108 Optimizing Function Calls .............................. 108 Audio Optimizations ........................................ 109 Streaming Music Playback .............................. 109 Sound Effects with OpenAL ............................ 111 OpenGL Optimizations ...................................... 112 Construct an Efficient OpenGL Draw Context. . . . . . . . . . . . 112 Avoid State Changes ................................... 114 CONTENTS ix Reducing Texture Sizes ................................. 116 Using Compressed Textures ............................ 117 Geometry Data Reduction .............................. 118 Limit the Quantity of Draw Calls ........................ 120 Performance Tools .......................................... 121 Using Instruments ..................................... 121 Using Shark ............................................ 123 Summary ................................................... 127 OLIVIER HENNESSY AND CLAYTON KANE CHAPTER 5 Starting with a Game Design Document: A Methodology for Success ................... 131 The Game Vision ............................................ 132 The Game Design Document ................................ 133 Title ................................................... 133 Game Summary ........................................ 133 Game Detail ........................................... 134 Game Setting .......................................... 134 Game System/Engine .................................. 134 Game Play: Controls and User Interface ................. 136 Level Map ............................................. 137 Aesthetic Design ....................................... 138 Title and Information Screens .......................... 139 Sound Effects .......................................... 139 From Vision to Reality ....................................... 140 Tips for Creating Realistic Artificial Intelligence .......... 140 Challenges of Designing for the iPhone ................. 141 Game Development Tips .................................... 144 Solitaire Top 3 .......................................... 144 Backgammon .......................................... 145 Pool ................................................... 146 Kaleidoscope .......................................... 147 Shake N’ Break ......................................... 148 Bikini Hunt ............................................. 149 YoYo ................................................... 150 Apache Lander ......................................... 151 Summary ................................................... 152 x CONTENTS MICHAEL KASPRZAK CHAPTER 6 Multiplatform Game Development: iPhone Games for Linux and Windows ......... 155 The Development of Smiles: A Collection of Puzzle Games ... 156 What Are Cross-Platform and Portability? .................... 160 Why Write Portable Code? .............................. 160 Why Not Write Portable Code? .......................... 161 Portability from the Ground Up. ............................. 162 The Classic Game Loop ................................. 162 A Practical Game Loop ................................. 163 Frames and Refresh Rates .............................. 165 Work and Draw Frame Code ............................ 165 Cooperating with an Event-Driven Operating System ........ 167 Preparing to Track Touches ............................. 167 Tracking Touches ....................................... 167 Simulating Touch and Release Events in a Game Loop ... 170 Frame Skipping ............................................. 172 Creating a Unix System Time Library .................... 173 Using the UnixTime Library for Frame Skipping ......... 177 Tilt and Touch Physics Sample ............................... 179 The Game Code for the Physics Simulation Sample ...... 180 Further Portability Considerations ...................... 186 Summary ................................................... 188 MIKE LEE CHAPTER 7 Code Optimization with Mike Lee, the “World’s Toughest Programmer” ........... 191 Iteration 1: Particle Effects ................................... 192 The Big Picture .............................................. 194 Iteration 2: Smoke and Mirrors .............................. 195 Premature Optimization .................................... 197 Build Efficiency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Code Efficiency ........................................ 199 Algorithmic Efficiency .................................. 201 Iteration 3: Shark Attack Time ............................... 202 Level-Headed Performance ................................. 204 Iteration 4: Increasingly Clever Optimizations ................ 206 Application-Specific Optimization ........................... 207 Summary ................................................... 211 CONTENTS xi RICHARD ZITO AND MATTHEW AITKEN CHAPTER 8 Networked Games: Choosing the Right Option ... 215 Multiplayer Networking Options ............................ 217 Communication Is Key ................................. 218 Say “Bonjour” to Local Network Gaming ................. 224 Drawing to Screen ..................................... 235 Tic-Tac-Toe, an Example ..................................... 237 Summary ................................................... 249 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Foreword iPhone games are hot, hot, hot! As I write this, there are more than 40,000 apps on the App Store, of which nearly 9,000 are in the Games category, the largest category by far. The next largest category is Entertainment, at just over 5,000 apps. There are nearly 40% more games on the App Store than any other kind of app. Games is not only the biggest category on the App Store, but it is also the best-selling cat- egory. During the promotion of its one billionth download, Apple provided a list of all-time most popular apps. Of the top-20 all-time paid apps, more than 14 were games. Many of these apps were on the top-10 paid apps list at one time or another during the nine months the App Store had been in business. Many are still on the top-100 list. These sold thousands of copies a day in their time, with daily revenue between a few thousand to tens of thou- sands of dollars. With such great numbers, it is understandable why the interest in developing iPhone games is so high. It is probably the reason you are reading this foreword. You couldn’t have picked up this book at a better time. If you want to get in on the fun and potential profit of making iPhone games, the time to get started is now, and this book is your ticket there! This book contains lots of information from master independent iPhone game developers— information that is hard to find anywhere else. Some of the authors of this book are respon- sible for all-time popular games: (cid:0) (cid:78)(cid:0)Brian Greenstone developed Enigmo and Cro-Mag Rally. (cid:0) (cid:78)(cid:0)Aaron Fothergill developed Flick Fishing. (cid:0) (cid:78)(cid:0)Mike Lee developed the original Tap Tap Revolution, the most downloaded game in App Store history. This book also features a chapter by award-winning game developer Mike Kasprzak, finalist for Best Mobile Game in the Game Developers Conference’s Independent Games Festival Mobile 2009. Other authors, including Richard Zito, Joachim Bondo, and Olivier Hennessy, have received glowing reviews and accolades for their games. The content in this book is phenomenal. The authors provide a variety of points of view and different approaches to iPhone development, showing you how iPhone games are made with various technologies. You will gain knowledge of how to optimize your game using iPhone SDK tools such as Instruments and Shark, as well as optimization tricks that the masters have xiii
Description: