More iPhone Cool Projects Cool Developers Reveal the Details of Their Cooler Apps and Discuss Their iPad Development Experiences ■ ■ ■ Danton Chin Ben Britten Smith Claus Höfele Chuck Smith Ben Kazez David Smith Saul Mora Arne de Vries Leon Palm Joost van de Wijgerd Scott Penberthy i More iPhone Cool Projects: Cool Developers Reveal the Details of Their Cooler Apps and Discuss Their iPad Development Experiences Copyright © 2010 by Danton Chin, Claus Höfele, Ben Kazez, Saul Mora, Leon Palm, Scott Penberthy, Ben Britten Smith, Chuck Smith, David Smith, Arne de Vries, and Joost van de Wijgerd 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 (pbk): 978-1-4302-2922-3 ISBN-13 (electronic): 978-1-4302-2923-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. Publisher and President: Paul Manning Lead Editor: Clay Andres Development Editors: Douglas Pundick, Matthew Moodie, and Brian MacDonald Technical Reviewer: Ben Britten Smith Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, 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 Coordinating Editors: Candace English and Debra Kelly Copy Editor: Katie Stence Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit 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 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 www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii My gratitude and thanks to Mom and Dad (they would have liked this), to my wife Carol for everything over the years, and to our wonderful and artistically, musically talented son Tim. Also, thanks to Robert and Elizabeth Bergenheim, Elise Falkinburg and Errol Frankel, and their wonderfully intelligent sons, John and James Frankel. —Danton Chin Thanks to my wife and daughters, who inspire me daily to help make the world a better place. And for letting me buy that awesome new MacBook Pro (you know which one). —Saul Mora To my mother, who taught me to pursue excellence, and to my father, who always inspired creativity in me. Also to Andrea Zemenides, for being cute and little. —Leon Palm To the beautiful women in my family: Lisa, Julia, and Taylor. Taylor insists I mention our dog, Jack, and Snickers too. —Scott Penberthy To my lovely wife Leonie. —Ben Britten Smith To my parents, who were always there for me and gave me the joy of reading. Also to my professor, Dr. Gene Chase, who always brought an incredible amount of enthusiasm to everything he taught, and made computer science inspiring. —Chuck Smith To my wife and best friend, Lauren. —David Smith iii Contents at a Glance ■Contents at a Glance...................................................................................................iv ■Contents.......................................................................................................................v ■Preface.........................................................................................................................x ■Acknowledgments......................................................................................................xi ■Introduction...............................................................................................................xii Danton Chin....................................................................................................................1(cid:2) ■Chapter 1: Using Concurrency to Improve the Responsiveness of iPhone and iPad Applications...............................................................3(cid:2) Claus Höfele.................................................................................................................57(cid:2) ■Chapter 2: Your Own Content Pipeline: Importing 3D Art Assets into Your iPhone Game............................................................................59(cid:2) Ben Kazez....................................................................................................................89(cid:2) ■Chapter 3: How FlightTrack Uses External Data Providers to Power This Best-Selling Travel App........................................................91(cid:2) Saul Mora, Jr.............................................................................................................107(cid:2) ■Chapter 4: Write Better Code and Save Time with Unit Testing..............................109(cid:2) Leon Palm..................................................................................................................135(cid:2) ■Chapter 5: Fun with Computer Vision: Face Recognition with OpenCV on the iPhone...........................................................................137(cid:2) Scott Penberthy.........................................................................................................161(cid:2) ■Chapter 6: How to Use OpenGL Fonts without Losing Your Mind............................163(cid:2) Ben Britten Smith.......................................................................................................189(cid:2) ■Chapter 7: Game Development with Unity..............................................................191(cid:2) Chuck Smith...............................................................................................................249(cid:2) ■Chapter 8: Cocos2d for iPhone and iPad; It Is Easier than You Think....................251(cid:2) David Smith................................................................................................................287(cid:2) ■Chapter 9: Creating an Audio-Centric App for the iPhone with AVAudioPlayer..............................................................................289(cid:2) Joost van de Wijgerd and Arne de Vries....................................................................309(cid:2) ■Chapter 10: Implementing Push Notifications at eBuddy.......................................311(cid:2) ■Index.......................................................................................................................577 iv Contents ■Contents at a Glance.......................................................................................iv(cid:2) ■Contents..........................................................................................................v(cid:2) ■Preface............................................................................................................x(cid:2) ■Acknowledgments..........................................................................................xi(cid:2) ■Introduction...................................................................................................xii(cid:2) Danton Chin........................................................................................................1(cid:2) ■Chapter 1: Using Concurrency to Improve the Responsiveness of iPhone and iPad Applications....................................................3(cid:2) (cid:2) Prepare for Concurrency.........................................................................................................................................3 (cid:2) Non-Responsive User Interfaces.............................................................................................................................6 (cid:2) Building the Interestingness User Interface.......................................................................................................7 (cid:2) Adding A JSON Parsing Framework to the Interestingness App......................................................................10 (cid:2) Composing a RESTful Request for a List of Interestingness Images...............................................................10 (cid:2) Using the RESTful Request and the JSON Parser to Parse the Response.......................................................12 (cid:2) Implementing the UITableViewDataSource Protocol Methods to Display the Results.....................................14 (cid:2) Concurrency Landscape.......................................................................................................................................16 (cid:2) Considerations When Using Concurrent Solutions................................................................................................17 (cid:2) Concurrency with NSThread and NSObject..........................................................................................................19 (cid:2) Concurrency with Operation Objects....................................................................................................................24 (cid:2) NSOperationQueue...........................................................................................................................................25 (cid:2) NSOperation and NSInvocationOperation........................................................................................................26 (cid:2) Concurrency with Operation Objects and Blocks..................................................................................................45 (cid:2) Blocks..............................................................................................................................................................46 (cid:2) Summary..............................................................................................................................................................53 (cid:2) Resources.............................................................................................................................................................54 (cid:2) Apple and Apple-related News........................................................................................................................54 (cid:2) Apple Documentation.......................................................................................................................................54 v ■ CONTENTS (cid:2) Blocks and Grand Central Dispatch.................................................................................................................55 (cid:2) General.............................................................................................................................................................56 (cid:2) JSON................................................................................................................................................................56 (cid:2) POSIX Threads.................................................................................................................................................56 Claus Höfele......................................................................................................57(cid:2) ■Chapter 2: Your Own Content Pipeline: Importing 3D Art Assets into Your iPhone Game.....................................................59(cid:2) (cid:2) Starting an iPhone Game......................................................................................................................................59 (cid:2) Why Write Your Own Tools?..................................................................................................................................60 (cid:2) Creating a Flexible Content Pipeline.....................................................................................................................61 (cid:2) The Tools Problem...........................................................................................................................................61 (cid:2) Data Exchange vs. In-Game File Formats........................................................................................................63 (cid:2) Outline of the Example Code.................................................................................................................................64 (cid:2) Exporting 3D Models.............................................................................................................................................65 (cid:2) Reading FBX files.............................................................................................................................................66 (cid:2) Traversing the Scene Contents.............................................................................................................................69 (cid:2) Distinguishing between Different Types of Scene Nodes.....................................................................................70 (cid:2) OpenGL Triangle Data......................................................................................................................................71 (cid:2) Converting FBX Models into Triangle Data......................................................................................................73 (cid:2) Converting Triangle Data into an In-Game Format..........................................................................................76 (cid:2) Handling Textures.................................................................................................................................................79 (cid:2) Image Compression vs. Texture Compression.................................................................................................79 (cid:2) Imagination’s PVRTC Format...........................................................................................................................80 (cid:2) Reading PNG Images.......................................................................................................................................82 (cid:2) Converting Images into the PVRTC Format......................................................................................................84 (cid:2) Rendering the Converted Data on the iPhone.......................................................................................................86 (cid:2) Running the Converter Tool.............................................................................................................................86 (cid:2) Creating the iPhone Project.............................................................................................................................86 (cid:2) Summary..............................................................................................................................................................87 Ben Kazez.........................................................................................................89(cid:2) ■Chapter 3: How FlightTrack Uses External Data Providers to Power This Best-Selling Travel App........................................91(cid:2) (cid:2) Choosing a Data Source........................................................................................................................................92 (cid:2) API Design........................................................................................................................................................93 (cid:2) Data Coverage and Accuracy...........................................................................................................................94 (cid:2) Economics.......................................................................................................................................................95 (cid:2) Trials................................................................................................................................................................96 (cid:2) Source-Driven User Interface Design....................................................................................................................96 (cid:2) Challenges.......................................................................................................................................................97 (cid:2) Techniques from FlightTrack...........................................................................................................................98 (cid:2) Design Patterns for Data Source Consumption.....................................................................................................99 (cid:2) Direct-Client Consumption...............................................................................................................................99 (cid:2) Server-Intermediary Consumption.................................................................................................................100 (cid:2) Data-Driven Cocoa App Architecture..................................................................................................................101 (cid:2) Data Model Design.........................................................................................................................................101 (cid:2) Connecting Data to UI....................................................................................................................................102 vi ■ CONTENTS (cid:2) Choosing an Approach...................................................................................................................................104 (cid:2) Release!..............................................................................................................................................................105 (cid:2) FlightTrack Today...............................................................................................................................................105 Saul Mora, Jr..................................................................................................107(cid:2) ■Chapter 4: Write Better Code and Save Time with Unit Testing..................109(cid:2) (cid:2) Mock Objects.................................................................................................................................................120 (cid:2) Testing Your Core Data Models......................................................................................................................127 (cid:2) Summary............................................................................................................................................................134 Leon Palm.......................................................................................................135(cid:2) ■Chapter 5: Fun with Computer Vision: Face Recognition with OpenCV on the iPhone........................................................137(cid:2) (cid:2) What Is Computer Vision?...................................................................................................................................140 (cid:2) Why do Computer Vision on an iPhone?.............................................................................................................141 (cid:2) Your Project: Creating a Face Detector...............................................................................................................142 (cid:2) Setting Up OpenCV.........................................................................................................................................142 (cid:2) Setting Up XCode...........................................................................................................................................145 (cid:2) Adding Image Conversion Functions..............................................................................................................147 (cid:2) Creating a Simple GUI....................................................................................................................................149 (cid:2) Loading Images from the Photo Library.........................................................................................................151 (cid:2) Loading the Haar Cascades...........................................................................................................................152 (cid:2) Performing Face Detection............................................................................................................................154 (cid:2) Bonus.............................................................................................................................................................156 (cid:2) Performance Tweaking.......................................................................................................................................157 (cid:2) Going Further......................................................................................................................................................159 (cid:2) Summary............................................................................................................................................................160 Scott Penberthy..............................................................................................161(cid:2) ■Chapter 6: How to Use OpenGL Fonts without Losing Your Mind................163(cid:2) (cid:2) History.................................................................................................................................................................164 (cid:2) Terminology........................................................................................................................................................165 (cid:2) Pragmatic Fontery...............................................................................................................................................167 (cid:2) fCookie................................................................................................................................................................167 (cid:2) Creating a Font’s Texture Atlas...........................................................................................................................168 (cid:2) Texture Mapping............................................................................................................................................171 (cid:2) Opening Your App..........................................................................................................................................173 (cid:2) The Fontery Classes............................................................................................................................................173 (cid:2) APGlyph..........................................................................................................................................................174 (cid:2) APChar...........................................................................................................................................................175 (cid:2) APText............................................................................................................................................................177 (cid:2) Putting It All Together.........................................................................................................................................179 (cid:2) Setting Up the Display.........................................................................................................................................181 (cid:2) Creating Your Fortune.........................................................................................................................................182 (cid:2) Displaying the Fortune........................................................................................................................................184 (cid:2) Summary............................................................................................................................................................187 vii ■ CONTENTS Ben Britten Smith...........................................................................................189(cid:2) ■Chapter 7: Game Development with Unity...................................................191(cid:2) (cid:2) What Is Unity?.....................................................................................................................................................192 (cid:2) Why Use Unity?...................................................................................................................................................193 (cid:2) Exploring the Unity Interface...............................................................................................................................195 (cid:2) The Scene View.............................................................................................................................................196 (cid:2) The Game View..............................................................................................................................................197 (cid:2) The Project View............................................................................................................................................198 (cid:2) The Hierarchy View........................................................................................................................................198 (cid:2) The Inspector View........................................................................................................................................198 (cid:2) How the Pipeline Flows..................................................................................................................................198 (cid:2) The Transform: Everybody Has One...............................................................................................................199 (cid:2) Meshes, Renderers, and Materials................................................................................................................200 (cid:2) Importing Assets............................................................................................................................................204 (cid:2) Custom Scripting...........................................................................................................................................205 (cid:2) Playing Your Game.........................................................................................................................................208 (cid:2) Coroutines Not Updates.................................................................................................................................209 (cid:2) The Game View..............................................................................................................................................210 (cid:2) Adding Lights.................................................................................................................................................213 (cid:2) Using the iPhone as a Game Pad...................................................................................................................214 (cid:2) Your Game..........................................................................................................................................................215 (cid:2) Adding a Base to Work From.........................................................................................................................216 (cid:2) The Main Character........................................................................................................................................218 (cid:2) Inputs and Colliders.......................................................................................................................................224 (cid:2) Your First Design Iteration.............................................................................................................................228 (cid:2) Adding More Touchable Objects....................................................................................................................230 (cid:2) Prefabs...........................................................................................................................................................231 (cid:2) Animations.....................................................................................................................................................232 (cid:2) Animation Import Settings.............................................................................................................................233 (cid:2) Interacting with Something Besides the Floor...............................................................................................237 (cid:2) User Interface................................................................................................................................................240 (cid:2) Multiple Cameras...........................................................................................................................................241 (cid:2) 3D Objects As GUI Items................................................................................................................................243 (cid:2) Building for Your Device................................................................................................................................247 (cid:2) Summary............................................................................................................................................................248 Chuck Smith...................................................................................................249(cid:2) ■Chapter 8: Cocos2d for iPhone and iPad; It Is Easier than You Think.........251(cid:2) (cid:2) Origins of Cocos2d..............................................................................................................................................251 (cid:2) Why Use Cocos2d?.............................................................................................................................................252 (cid:2) Getting Started with Cocos2d.............................................................................................................................252 (cid:2) Installing Cocos2d..........................................................................................................................................254 (cid:2) Configuring Sample Code..............................................................................................................................254 (cid:2) Installing the XCode Project Templates.........................................................................................................255 (cid:2) Starting a New Project...................................................................................................................................256 (cid:2) Introduction to Video Poker.................................................................................................................................257 (cid:2) Making a Scene..................................................................................................................................................258 (cid:2) Creating a Game Menu..................................................................................................................................260 viii ■ CONTENTS (cid:2) Game Logic.........................................................................................................................................................261 (cid:2) Card...............................................................................................................................................................262 (cid:2) Deck...............................................................................................................................................................264 (cid:2) SimpleHand....................................................................................................................................................266 (cid:2) I Like the Sprites in You.................................................................................................................................270 (cid:2) Putting It All together.....................................................................................................................................272 (cid:2) Events: Making It Interactive.........................................................................................................................275 (cid:2) Adding Sound......................................................................................................................................................280 (cid:2) Supporting the iPad............................................................................................................................................281 (cid:2) Further Exploring cocos2d..................................................................................................................................286 David Smith....................................................................................................287(cid:2) ■Chapter 9: Creating an Audio-Centric App for the iPhone with AVAudioPlayer.......................................................289(cid:2) (cid:2) Design.................................................................................................................................................................290 (cid:2) Designing for Your Target User......................................................................................................................290 (cid:2) Our Design Process........................................................................................................................................291 (cid:2) Implementation...................................................................................................................................................295 (cid:2) Example Project..................................................................................................................................................296 (cid:2) Getting Started...............................................................................................................................................296 (cid:2) Setting Up the UI............................................................................................................................................298 (cid:2) Coding the Audio Player.................................................................................................................................302 (cid:2) Summary............................................................................................................................................................307 Joost van de Wijgerd and Arne de Vries.........................................................309(cid:2) ■Chapter 10: Implementing Push Notifications at eBuddy............................311(cid:2) (cid:2) Introduction to eBuddy........................................................................................................................................311 (cid:2) The eBuddy Messenger.................................................................................................................................312 (cid:2) The eBuddy iPhone Application.....................................................................................................................312 (cid:2) Apple Push Notification Service..........................................................................................................................313 (cid:2) The Communication Flow..............................................................................................................................314 (cid:2) The Client Implementation.............................................................................................................................314 (cid:2) The eBuddy Push Implementation......................................................................................................................317 (cid:2) Client / Server Protocol..................................................................................................................................317 (cid:2) Server to APNS...............................................................................................................................................318 (cid:2) Fitting the Parts Together..............................................................................................................................322 (cid:2) Changes along the Way......................................................................................................................................323 (cid:2) Introducing eBuddy Pro.................................................................................................................................323 (cid:2) Extending the Beta Program..........................................................................................................................324 (cid:2) Summary............................................................................................................................................................325 ■Index............................................................................................................577 ix
Description: