Iphone Game Development
Total Page:16
File Type:pdf, Size:1020Kb
iPhone Game Development Paul Zirkle and Joe Hogue Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo iPhone Game Development by Paul Zirkle and Joe Hogue Copyright © 2010 Paul Zirkle and Joe Hogue. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editor: Andy Oram Indexer: Lucie Haskins Production Editor: Loranah Dimant Cover Designer: Karen Montgomery Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreader: Loranah Dimant Illustrator: Robert Romano Printing History: November 2009: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. iPhone Game Development, the image of a roadrunner, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-15985-6 [M] 1256658809 Table of Contents Preface . vii 1. Introduction to the iPhone . ... ......................................... 1 Apple Developer Account and Downloading the SDK 1 Application Bundles 2 Xcode and Interface Builder 4 Views and Controllers 13 Loading Devices 22 Certificates and Profiles 22 Xcode Configuration 24 Objective-C Primer 25 Classes 27 Instantiation 31 Messaging 31 Member Variables 32 Memory Management 33 Constructors and Destructors 33 Interface Builder Integration 34 Mixed C++ and Objective-C 34 Conclusion 34 2. Game Engine Anatomy . ............................................ 35 Application Framework 35 Main Loop 36 Game State Manager 37 State Machine 38 Graphics Engine 41 Texturing 41 Animation 46 Physics Engine 51 Audio Engine 56 iii Player Input 58 Game Logic 61 Conclusion 68 3. The Framework . ... .................................................. 69 Game State Management 70 Implementation 71 Removing the Status Bar 73 The App Delegate 73 Frames per Second 75 Event Handling 76 The Resource Manager 78 Texture Format 78 Sound Format 78 Deploying Resources 79 Management 79 The Render Engine 80 GLESGameState 81 Textures 82 Font Textures 83 Example State 85 The Sound Engine 87 Example State 88 The Data Store 89 Storing Simple Data 89 Storing Complex Data 90 Example State 90 The Skeleton Application 93 Conclusion 95 4. 2D Game Engine . ................................................. 97 Game Design 97 Feature List 98 User Input Scheme 99 Learning Curve 100 Tile Engine 102 Unique Tiles 102 Drawing Tiles 104 TileWorld Class 105 Animation 110 Animation and Sprite Classes 111 Physics 121 Entities 121 iv | Table of Contents Entity-to-World 122 Special Tiles 126 Entity-to-Entity 126 Level 1 Implementation 127 gsEmuLevel 127 TileWorld 128 Main Character 128 Emu Chicks 131 Emu Mother 137 Game Logic 139 Sound 140 Level 2 Implementation 141 gsLionLevel 141 TileWorld 142 McGuffin 143 Main Character 143 Lion Entities 144 Game Logic 151 Sound 152 Level 3 Implementation 152 gsMazeLevel 153 TileWorld 154 Buttons 155 Doors 156 Cat and Mouse 157 User Input 157 Game Logic 158 Sounds 164 Level 4 Implementation 164 gsRiverLevel 165 TileWorld 165 Crocodiles 167 Logs 168 Tom 169 User Input 171 Game Logic 172 Sounds 175 Game State Serialization 175 Initialize Storage 175 End Game 176 Modify UI 179 Conclusion 180 Table of Contents | v 5. 3D Games . ...................................................... 181 GLESGameState3D Class 182 POWERVR 184 Sprite3D 185 Accelerometer 186 3D Game Design 187 Graphics 187 Input 189 Camera 190 Logic 190 Implementation 190 Camera 192 Skybox 194 Input 196 Rings 204 Particle System 209 Logic 217 Best Times 219 End Game 222 Conclusion 224 6. Considerations for Game Design . 225 Resource Management 225 User Input Design 226 Networking 227 Third-Party Code 227 Libraries 227 Middleware 228 Open Source Games 228 App Store 229 Conclusion 230 Appendix: References . 231 Index . 233 vi | Table of Contents Preface If you’re reading this Preface, you might be trying to decide whether this book is right for you. So, we’ll explain what we intend to cover and who we expect will get the most out of reading the book. But we’ll start with why you should even listen to us. The Authors We’re two programmers who have been buddies in the Los Angeles area for a long time and have been making mobile games since 2003. Between the two of us we have de- veloped and ported close to a hundred titles for cell phones, including Ninja Gaiden, Castlevania: Order of Shadows, Dance Dance Revolution: Mobius, and Contra 4, among many, many others. Paul currently works for Konami, and occasionally gives lectures about game programming at USC. Joe works at EA and recently published a game for the iPhone. Paul wrote most of the theory, while Joe cooked up some great example projects. Our Goal We expect there is—scratch that—we know there is a lot of interest in the iPhone as a new game platform. The high-end hardware promises plenty of power and memory for graphics and content, while the unique touch screen and gyroscope interface pose an interesting challenge that encourages innovative game designs. Add to that the open market of the iTunes App Store, which promises easy entry for independent developers as well as accurate revenue tracking for serious publishers, and it’s no surprise that the iPhone is making a huge splash in the mobile space. Our goal was to write a book that can put iPhone game development in the reach of any programmer out there, in a format that suits both beginning and advanced game programmers. This book is not a course on Objective-C, though it will contain a primer. This book is not an exhaustive study of the iPhone SDK, as we will use only the functionality needed to make our games. This book is not a five-year course on game programming vii technology. However, this book is the source for all you need to know not only to build the example games we have provided but also to build your own. By the end of the book, you will have learned all of the core concepts of creating a game engine, and how to apply it to the iPhone. You will learn enough Objective-C to utilize the iPhone SDK to make a 2D game. You will have been introduced to OpenGL ES on the iPhone for making 3D games. And finally, you will be familiar with the certification process so that making the last plunge into the App Store is much less scary. Prerequisites To get the most out of this book, you will need to have some programming knowledge. The iPhone uses Objective-C, but most people are more familiar with C/C++ or Java, so the Objective-C primer in Chapter 1 will help familiarize you with the basics. You will also need a Mac. The iPhone Software Development Kit (SDK) is available only for OS X, so a trip to the Apple store may be in your future if you are serious about developing for the iPhone. Fortunately, the base model Mac Mini has plenty of power to develop for the iPhone, and you can use your existing monitor and keyboard setup. Meanwhile, once you have a Mac, the Xcode Integrated Development Environment (IDE) is free. But we’re getting ahead of ourselves. The third requirement is not 100% necessary, but strongly recommended: an iPhone or iPod Touch. Although we use the term iPhone throughout the book, games made for the iPhone will also work on iPod Touch. Some people will undoubtedly try to create an iPhone game using only the iPhone Simulator. It may be possible to do this, and even get it placed into the App Store, but we strongly recommend that you develop and test on the device itself. After all, what good is a game you can’t play yourself? Beyond those three requirements, everything else you need you can either download or learn. We are working on useful classes and examples at https://sourceforge.net/ projects/iphonegamebook/, where you will also find the source code for all examples. Audience As we mentioned earlier, we expect you to have some basic programming knowledge. That aside, all kinds of programmers are interested in the iPhone. Developers of all levels are going to be reading up to try their skill. You might not have any game programming experience and will need some basic theory on game engines and user-interface design. Or perhaps you have plenty of experience making video games on other platforms and just need to become familiar with the iPhone SDK and build process, along with some touch-screen concepts. You may also be interested in advanced topics, such as how to write portable code for cross-platform games and what middleware solutions currently exist. Either way, this book has got you covered. viii | Preface Organization of This Book Chapter 1, Introduction to the iPhone, gets you set up to build interfaces and write code in Objective-C, including logistics such as setting up a developer account with Apple. Chapter 2, Game Engine Anatomy, introduces the elements of game logic and good design that will let you write a maintainable and enjoyable game application.