You are previewing Beginning iPhone Games Development.
O'Reilly logo
Beginning iPhone Games Development

Book Description

iPhone games are hot! Just look at the numbers. Games make up over 25 percent of total apps and over 70 percent of the most popular apps. Surprised? Of course not! Most of us have filled our iPhone or iPod touch with games, and many of us hope to develop the next best-selling, most talked-about game.

You've probably already read and mastered Beginning iPhone 3 Development; Exploring the iPhone SDK, the best-selling, the second edition of Apress's highly acclaimed introduction to the iPhone and iPod touch by developers Dave Mark and Jeff LaMarche. This book is the game-specific equivalent, providing you with the same easy-to-follow, step-by-step approach, more deep technical insights, and that familiar friendly style.

While games are all about fun, at the same time, they're serious business. With this book, you're going to roll up your sleeves and get your hands dirty with some hardcore coding. While you may have written games before, this book will take you further, immersing you in the following topics:

  • Game graphics and animation with UIKit, Quartz, Core Animation, and OpenGL ES

  • Game audio with OpenAL, MediaPlayer Framework, AV Foundation, and AudioSession

  • Game networking with GameKit, Bonjour, and Internet sharing

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Introduction
  6. 1. A Revolutionary Gaming Platform: Games for Everyone, Anytime, Anywhere
    1. 1.1. The Ever-Present iPhone
    2. 1.2. Mass Appeal—There's a Gamer Born Every Minute
    3. 1.3. User Interfaces—Death of the D-Pad
    4. 1.4. Connectivity—Plays Well with Others
    5. 1.5. User Data—This Time It's Personal
    6. 1.6. Device Performance—A Multimedia Powerhouse
    7. 1.7. Dev Kit? You're Holding It!
    8. 1.8. Innovation—Good Things Come from Small Developers
    9. 1.9. Summary
  7. 2. Developing iPhone Games: Peeking Inside the iPhone Toolbox
    1. 2.1. Development Tools and Environment
    2. 2.2. UIKit
    3. 2.3. Quartz 2D and Core Animation
    4. 2.4. OpenGL ES
    5. 2.5. Audio APIs
    6. 2.6. Networking
    7. 2.7. Summary
  8. 3. Moving Images on a Small Screen—UIKit Controls
    1. 3.1. A Quick Introduction to Cocoa Touch
      1. 3.1.1. The Objective-C Language
        1. 3.1.1.1. A Brief Objective-C Example
        2. 3.1.1.2. Using an Objective-C Class
        3. 3.1.1.3. Memory Management in Objective-C
      2. 3.1.2. Cocoa Touch and the UIKit Framework
        1. 3.1.2.1. Introducing UIView
        2. 3.1.2.2. The Frame Property
        3. 3.1.2.3. The Background Color and Alpha Properties
        4. 3.1.2.4. The Center Property
    2. 3.2. Building a Simple Game
      1. 3.2.1. Creating an Xcode Project
      2. 3.2.2. Creating the IVBricker User Interface
        1. 3.2.2.1. Examining the Main Window File
        2. 3.2.2.2. Examining the View Controller File
        3. 3.2.2.3. Setting View Properties
        4. 3.2.2.4. Adding Components to the View
        5. 3.2.2.5. Adding Code to the View Controller
        6. 3.2.2.6. Connecting a Property with a Component
      3. 3.2.3. Snazzy Graphics Make the Grade
        1. 3.2.3.1. Adding the Image
        2. 3.2.3.2. The Illusion of Motion
        3. 3.2.3.3. Rocking and Grooving with User Input
        4. 3.2.3.4. Handling Accelerometer Input
        5. 3.2.3.5. Handling Touchscreen Input
        6. 3.2.3.6. When Objects Collide
        7. 3.2.3.7. Failure Conditions
        8. 3.2.3.8. Winning Conditions
        9. 3.2.3.9. Loading and Displaying Images Programmatically
        10. 3.2.3.10. Creating the Grid of Bricks
        11. 3.2.3.11. Detecting image View Collisions
        12. 3.2.3.12. Changing the Timer to CADisplayLink
      4. 3.2.4. The End?
    3. 3.3. Application Delegate Events
      1. 3.3.1. Application Termination
      2. 3.3.2. Application Interruptions
      3. 3.3.3. Low Memory Warnings
    4. 3.4. Saving and Loading Game State
      1. 3.4.1. Managing Memory with a Custom Image Loader
    5. 3.5. Animating Images
      1. 3.5.1. Using the UIImageView Animation Properties
      2. 3.5.2. Using NSTimer for Animation
      3. 3.5.3. Using CADisplayLink for Animation
    6. 3.6. Summary
  9. 4. She Shoots, She Hits, She Scores!
    1. 4.1. Quartz 2D Game Overview
    2. 4.2. Every Artist Needs a Canvas
    3. 4.3. Your First Graphic with Quartz 2D
      1. 4.3.1. Saving and Restoring the Context
      2. 4.3.2. Adding Color
    4. 4.4. Sprites
      1. 4.4.1. Creating the Sprite Class
      2. 4.4.2. Using the Sprite Class
    5. 4.5. Which Way Is Up?
      1. 4.5.1. Changing to Landscape Orientation
      2. 4.5.2. Centering the Origin
    6. 4.6. Vector Art
      1. 4.6.1. Creating the VectorSprite Class
      2. 4.6.2. Using the VectorSprite Class
    7. 4.7. Flipbook Animations
      1. 4.7.1. Creating the AtlasSprite Class
      2. 4.7.2. Modifying the Sprite Class
      3. 4.7.3. Using the AtlasSprite Class
    8. 4.8. Heads-Up Displays
      1. 4.8.1. Creating the TextSprite Class
      2. 4.8.2. Using the TextSprite Class
    9. 4.9. Asteroids Game Architecture
      1. 4.9.1. The Asteroids Game Loop
      2. 4.9.2. The Asteroids Model
      3. 4.9.3. The Asteroids View
      4. 4.9.4. The Asteroids Game Controller
    10. 4.10. Conclusion
  10. 5. Flipping Out and Sweeping Away with Core Animation
    1. 5.1. Core Animation Sample Project Overview
    2. 5.2. Animating UIViews
      1. 5.2.1. Simple Movement
      2. 5.2.2. Animation Curves
      3. 5.2.3. Reverse and Repeat
      4. 5.2.4. Delay, Ease-In, and Ease-Out
      5. 5.2.5. UIView Transforms
      6. 5.2.6. UIView Transitions
    3. 5.3. Animating Core Animation Layers
      1. 5.3.1. Implicit Animations
      2. 5.3.2. Timing Functions
      3. 5.3.3. Layer Animation Transitions
        1. 5.3.3.1. Fade
        2. 5.3.3.2. Move In, Push, and Reveal
    4. 5.4. Summary
  11. 6. OpenGL Basics: Wrapping Your Head Around the OpenGL API
    1. 6.1. What Is OpenGL ES and Why Do I Care?
    2. 6.2. Understanding the 3D World
    3. 6.3. Matrix Basics: Taking the Red Pill
      1. 6.3.1. Bringing It All Together
      2. 6.3.2. Matrix Types
        1. 6.3.2.1. The Model and the Model Matrix
        2. 6.3.2.2. The View Matrix
        3. 6.3.2.3. The Projection
        4. 6.3.2.4. The Viewport
      3. 6.3.3. Stateful API
    4. 6.4. Rendering Basics
    5. 6.5. The Basic Game Template
    6. 6.6. Wrapping the CAEAGLLayer in a View: EAGLView
      1. 6.6.1. First Steps: The Init Method
      2. 6.6.2. Frame Buffers, Render Buffers, and Depth Buffers
      3. 6.6.3. Seeing into the OpenGL World
      4. 6.6.4. Drawing and Rendering
    7. 6.7. How to Draw Stuff with OpenGL
      1. 6.7.1. The Scene and Mesh Objects
      2. 6.7.2. Pushing and Popping Matrixes
      3. 6.7.3. Putting Your Objects in the Scene
        1. 6.7.3.1. Moving Objects
        2. 6.7.3.2. Scaling Objects
        3. 6.7.3.3. Spinning Objects
      4. 6.7.4. Defining Your Object in 3D Space
        1. 6.7.4.1. Vertex and Color Information
        2. 6.7.4.2. Understanding Triangles
    8. 6.8. The Game Loop and the Timer
    9. 6.9. The Input Controller
    10. 6.10. The App Delegate
    11. 6.11. Summary
  12. 7. Putting It Together: Making a Game in OpenGL
    1. 7.1. Space Rocks! Game Design
    2. 7.2. Getting Started with the Template
    3. 7.3. Rotation Makes the World Go 'Round
    4. 7.4. 3D Point Upgrade
    5. 7.5. Adding Buttons
      1. 7.5.1. Creating a Button Object
      2. 7.5.2. Working with Vertex Data
      3. 7.5.3. Storing Buttons
      4. 7.5.4. Detecting Touches
      5. 7.5.5. Wiring Up the Buttons
    6. 7.6. Building a Better Spaceship
      1. 7.6.1. Going Mobile
      2. 7.6.2. Adding the Spaceship
      3. 7.6.3. Adding and Removing Scene Objects
      4. 7.6.4. Falling Off the Edge of the Screen
    7. 7.7. Space Rocks!
    8. 7.8. Adding Missiles
      1. 7.8.1. Firing Missiles
      2. 7.8.2. Removing Unwanted Missiles
    9. 7.9. Making Nicer Buttons
    10. 7.10. Collision Detection
      1. 7.10.1. What Is a Collision?
      2. 7.10.2. Collision-Detection Techniques
        1. 7.10.2.1. Optimized Collision Detection
        2. 7.10.2.2. Radius-to-Radius Collision Check
    11. 7.11. Collisions on the Rocks
      1. 7.11.1. Centroid and Radius
      2. 7.11.2. Colliders and Collidees
        1. 7.11.2.1. Take the Red Pill: The Transform Matrix
        2. 7.11.2.2. The Collider Object
        3. 7.11.2.3. The Collision Controller Object
        4. 7.11.2.4. Back to the Scene Controller
        5. 7.11.2.5. Scene Object Updates
      3. 7.11.3. Collision Checking Redux
    12. 7.12. Summary
  13. 8. The Next Steps: Atlases, Sprites, and Particles—Oh My!
    1. 8.1. Textures and Texture Atlases
      1. 8.1.1. What Is a Texture Anyway, and Why Do I Care?
      2. 8.1.2. Getting Image Data into OpenGL
      3. 8.1.3. Binding Textures
      4. 8.1.4. UV Is Not Bad for Your Skin
      5. 8.1.5. You Get a Textured Quad!
      6. 8.1.6. Say Hello to the Texture Atlas
      7. 8.1.7. Switching the Old and Busted for the New Hotness
      8. 8.1.8. A Nicer User Interface
      9. 8.1.9. Colors with Textures
    2. 8.2. Sprite Animation
      1. 8.2.1. Frame Rate Independence
      2. 8.2.2. Animations for Everything
    3. 8.3. From 2D to 3D
      1. 8.3.1. It's Only One More D—What's the Big Deal?
      2. 8.3.2. Where Do 3D Models Come From?
      3. 8.3.3. From Modeler to Screen
      4. 8.3.4. What Is Normal?
      5. 8.3.5. Standardizing on GL_TRIANGLES
      6. 8.3.6. Textures Plus Models
      7. 8.3.7. Shadows Define Shape
      8. 8.3.8. Depth Buffer and Face Culling
      9. 8.3.9. Collision Detection Tweaks
    4. 8.4. Particle Systems Add Life to Your Game
      1. 8.4.1. What Is a Particle System?
      2. 8.4.2. A Lot of Random Numbers
      3. 8.4.3. The Nitty-Gritty of a Particle System: Particles
      4. 8.4.4. Particle Emitters and You
      5. 8.4.5. Tuning Our System
      6. 8.4.6. Particle Systems for Everything
      7. 8.4.7. What the Future Holds: Shaders
    5. 8.5. Summary
  14. 9. Introduction to Core Audio
    1. 9.1. Audio Services Provided by Core Audio
      1. 9.1.1. Audio Units
      2. 9.1.2. Audio File Services
      3. 9.1.3. Audio File Stream Services
      4. 9.1.4. Audio Conversion Services
      5. 9.1.5. Extended Audio File Services
      6. 9.1.6. Audio Session Services
      7. 9.1.7. System Sound Services
      8. 9.1.8. Audio Queue Services
      9. 9.1.9. AVFoundation
      10. 9.1.10. OpenAL
    2. 9.2. The Core Audio Frameworks
    3. 9.3. Codecs and File Formats
      1. 9.3.1. Codecs Supported by Core Audio
      2. 9.3.2. File Formats Supported by Core Audio
      3. 9.3.3. Using afconvert to Convert Between Formats
      4. 9.3.4. Hardware-Accelerated Codecs: Restricted and Unrestricted Codec Groups
      5. 9.3.5. Codec and File Format Suggestions
    4. 9.4. Alerts and Vibration: Introducing System Sound Services
      1. 9.4.1. The Case Against System Sound Services for General Sound Effects
      2. 9.4.2. A System Sound Services Example
      3. 9.4.3. A Comment on Asynchronous Playback
    5. 9.5. Setting Policies for Your Audio: Introducing Audio Session Services
      1. 9.5.1. Boilerplate Code and Procedures for Using Audio Session Services
      2. 9.5.2. Detecting Hardware and Getting Properties
    6. 9.6. Easy Audio Playback in Objective-C with AVFoundation
    7. 9.7. Mission Complete...but Our Princess Is in Another Castle!
  15. 10. Making Noise with OpenAL
    1. 10.1. OpenAL Overview
      1. 10.1.1. What OpenAL Supports
      2. 10.1.2. Some History of OpenAL
    2. 10.2. My Story and Goals for Audio Coverage
    3. 10.3. Roadmap for the Audio Coverage
    4. 10.4. Setting Up Basic Sound in OpenAL
      1. 10.4.1. Setting Up an Audio Session
      2. 10.4.2. Opening a Device
        1. 10.4.2.1. Advanced OpenAL Tip: Specifying OpenAL Devices
      3. 10.4.3. Creating a Context
        1. 10.4.3.1. Advanced Tip: Using Attribute Lists
      4. 10.4.4. Activating the Context
      5. 10.4.5. Generating Sound Sources
      6. 10.4.6. Generating Data Buffers
      7. 10.4.7. Loading Sound Data from Files
      8. 10.4.8. Submitting Sound Data to OpenAL Data Buffers
        1. 10.4.8.1. Advanced OpenAL Tip: The alBufferDataStatic Extension
      9. 10.4.9. Attaching a Data Buffer to a Sound Source
      10. 10.4.10. Playing Sound
      11. 10.4.11. Shutting Down and Cleaning Up
        1. 10.4.11.1. Cow Launched!
    5. 10.5. Exposing Flaws and the Missing Details
      1. 10.5.1. Adding More Sounds
      2. 10.5.2. Problems to Notice
      3. 10.5.3. OpenAL Error Checking
      4. 10.5.4. Audio Session Interruptions
      5. 10.5.5. OpenAL Extensions for iPhone OS
      6. 10.5.6. Performance Notes
      7. 10.5.7. OpenAL Source Limits: "It's a Secret to Everybody"
    6. 10.6. Sound Resource Manager: Fixing the Design
      1. 10.6.1. Overview of the Resource Manager
      2. 10.6.2. Initial Cleanup
      3. 10.6.3. The Sound File Database (Cache System)
      4. 10.6.4. OpenAL Source Management (Reserving and Recycling)
      5. 10.6.5. Integration with Space Rocks!
        1. 10.6.5.1. SoundDidFinishPlaying Callbacks
        2. 10.6.5.2. Final Integration Steps for Space Rocks!
      6. 10.6.6. Handling When All Available Sources Are Exhausted
      7. 10.6.7. Final Demo Embellishments
    7. 10.7. Save Point Reached
  16. 11. 3D Audio—Turning Noise into Game Sounds
    1. 11.1. The Design of OpenAL: Sources, Buffers, and Listeners
    2. 11.2. Limits of 3D Audio in OpenAL
    3. 11.3. Integrating the Listener into Space Rocks!
      1. 11.3.1. Creating the Listener Class
      2. 11.3.2. Picking the Designated Driver
    4. 11.4. Adding Positions to Sounds
      1. 11.4.1. Handling Initial Positioning on Creation
      2. 11.4.2. Disabling Distance Attenuation
        1. 11.4.2.1. Working Around an alDistanceModelBug: Disabling Attenuation on a Per-Source Basis
    5. 11.5. Listener Orientation
      1. 11.5.1. Right-Handed Coordinate Systems and the Right-Hand Rule
      2. 11.5.2. Unit Circle, Polar-to-Rectangular Coordinates, Phase Shifting, and Trigonometric Identities
      3. 11.5.3. Integration into Space Rocks!
    6. 11.6. Source Direction and Cones
      1. 11.6.1. Inner Cone, Outer Cone, and Transitional Zone
      2. 11.6.2. Implementation Time
    7. 11.7. Velocity and the Doppler Effect
      1. 11.7.1. Velocities and Scaling Factors
      2. 11.7.2. Doppler Effect Example Time
    8. 11.8. Distance Attenuation
      1. 11.8.1. Attenuation Models
        1. 11.8.1.1. Inverse Distance
        2. 11.8.1.2. Inverse Distance Clamped
        3. 11.8.1.3. Linear Distance
        4. 11.8.1.4. Linear Distance Clamped
        5. 11.8.1.5. Exponential Distance
        6. 11.8.1.6. Exponential Distance Clamped
        7. 11.8.1.7. Distance Attenuation Parameters
        8. 11.8.1.8. Picking a Distance Model
      2. 11.8.2. Back to Space Rocks!
    9. 11.9. Using Relative Sound Properties to Selectively Disable 3D Effects
    10. 11.10. Achievement Unlocked: Use All OpenAL 3D Features
  17. 12. Streaming: Thumping, Pulse-Quickening Game Excitement
    1. 12.1. Music and Beyond
    2. 12.2. iPod Music Library (Media Player Framework)
      1. 12.2.1. Playing iPod Music in Space Rocks!
      2. 12.2.2. Adding a Media Item Picker
      3. 12.2.3. Shake It! (Easy Accelerometer Shake Detection)
    3. 12.3. Audio Streaming
      1. 12.3.1. AVFoundation-Based Background Music for Space Rocks!
        1. 12.3.1.1. The Playback Sound Controller
        2. 12.3.1.2. Integration into Space Rocks!
      2. 12.3.2. OpenAL Buffer Queuing Introduction
        1. 12.3.2.1. Initialization
        2. 12.3.2.2. Unqueuing
        3. 12.3.2.3. Queuing
        4. 12.3.2.4. Buffer Underrun
        5. 12.3.2.5. Pausing
        6. 12.3.2.6. Fast Forwarding and Rewinding
        7. 12.3.2.7. Startup Number of Buffers and Filling the Queue
        8. 12.3.2.8. How to Choose the Number and Size of Buffers
      3. 12.3.3. OpenAL-Based Background Music for Space Rocks!
        1. 12.3.3.1. A New Buffer Data Class for Streaming
        2. 12.3.3.2. Unqueue the Processed Buffers
        3. 12.3.3.3. Queue a New Buffer If Necessary
        4. 12.3.3.4. Handle a Buffer Underrun
        5. 12.3.3.5. Handle EOF and Finished Playing
        6. 12.3.3.6. OpenALSoundController Changes
        7. 12.3.3.7. BBSceneController Integration
        8. 12.3.3.8. Analysis: Number of Buffers and Buffer Sizes
        9. 12.3.3.9. Star Power Ready!
      4. 12.3.4. OpenAL Speech for Space Rocks!
        1. 12.3.4.1. EWSoundSourceObject: Finishing the implementation
        2. 12.3.4.2. BBSceneController: Adding a New UFO
        3. 12.3.4.3. UFO: Taunting and Callbacks
        4. 12.3.4.4. The alBufferDataStatic Crashing Bug
          1. 12.3.4.4.1. Work-Arounds
      5. 12.3.5. Audio Queue Services Based Background Music for Space Rocks!
        1. 12.3.5.1. Rewind and Looping
        2. 12.3.5.2. Audio Session Interruptions
      6. 12.3.6. Perfect Full Combo!
    4. 12.4. Audio Capture
      1. 12.4.1. Audio Capture APIs
      2. 12.4.2. AVFoundation: File Recording with AVAudioRecorder
      3. 12.4.3. OpenAL: Capture Oscilloscope
        1. 12.4.3.1. The Capture APIs
        2. 12.4.3.2. ALC Device Enumeration
        3. 12.4.3.3. Capturing Audio
    5. 12.5. Back to OpenGL
      1. 12.5.1. Vertex Buffer Objects
      2. 12.5.2. Some Notes on OpenGL and OpenAL Optimization
    6. 12.6. The End of the Audio Road
  18. 13. Networking for iPhone Games: Introduction
    1. 13.1. Meet the Network
      1. 13.1.1. Network Interfaces
      2. 13.1.2. TCP/IP
      3. 13.1.3. Bonjour
    2. 13.2. iPhone SDK and Networking
      1. 13.2.1. Sockets and Connections
      2. 13.2.2. BSD Socket API
      3. 13.2.3. CFNetwork
      4. 13.2.4. NSNetServices
      5. 13.2.5. GameKit
    3. 13.3. Summary
  19. 14. Going Head to Head
    1. 14.1. Hello Pong!
    2. 14.2. Using Peer Picker to Find a Human Opponent
      1. 14.2.1. What Does It Look Like?
      2. 14.2.2. How Does It Work?
    3. 14.3. Making the Connection
    4. 14.4. Sending and Receiving Messages
      1. 14.4.1. Rolling the Dice
      2. 14.4.2. Ready...Set...Go!
      3. 14.4.3. Hits and Misses
      4. 14.4.4. The Paddle Comes Alive
    5. 14.5. Game Over: Handling Disconnects
    6. 14.6. Summary
  20. 15. Party Time
    1. 15.1. 8 × 3 = ?
      1. 15.1.1. Starting Point
      2. 15.1.2. Where Are We Going?
      3. 15.1.3. What's in the Structure?
    2. 15.2. Making Connections
      1. 15.2.1. Introducing the Connection and Stream Objects
      2. 15.2.2. Connection Initialization
      3. 15.2.3. Closing and Cleanup
      4. 15.2.4. Reading Data
      5. 15.2.5. Writing Data
      6. 15.2.6. Handling Stream Events
      7. 15.2.7. The Complete Picture
    3. 15.3. Socket Servers
      1. 15.3.1. The SocketServer Class
      2. 15.3.2. Socket Server Initialization
      3. 15.3.3. Publishing via Bonjour
      4. 15.3.4. Starting and Stopping
    4. 15.4. Finding Servers via Bonjour
      1. 15.4.1. Looking for Servers
      2. 15.4.2. Connecting to Servers
      3. 15.4.3. Final Details
    5. 15.5. Implementing the Game Client
      1. 15.5.1. Tracing the Logic
      2. 15.5.2. Choosing the Network Message Format
      3. 15.5.3. Making It Talk
      4. 15.5.4. Hooking It Up
    6. 15.6. Implementing the Game Server
      1. 15.6.1. Managing Players
      2. 15.6.2. Laying It Out
      3. 15.6.3. Initialization
      4. 15.6.4. Players Joining and Leaving
      5. 15.6.5. Starting and Stopping Game Rounds
      6. 15.6.6. Collecting and Processing Answers
      7. 15.6.7. Hooking It Up
    7. 15.7. Summary
  21. 16. Connecting with the Outside World
    1. 16.1. Challenges
      1. 16.1.1. Au Revoir, Bonjour!
      2. 16.1.2. No GameKit Peer-to-Peer for You!
      3. 16.1.3. Server, interrupted
      4. 16.1.4. Lag
      5. 16.1.5. It's All Greek to Me...
      6. 16.1.6. And a Bunch More
    2. 16.2. Basics of Online Game Play
      1. 16.2.1. Client/Server Games
      2. 16.2.2. Connecting to a Game Server Without Bonjour
      3. 16.2.3. Peer-to-Peer Games
      4. 16.2.4. Something About Reinventing the Wheel
      5. 16.2.5. Like a Drop in the Ocean
    3. 16.3. Making Games More Social
      1. 16.3.1. Sharing High Scores Online
      2. 16.3.2. Chasing Ghosts
      3. 16.3.3. Chatting
    4. 16.4. Summary
  22. 17. Putting It All Together: Now Comes the Fun Part
    1. 17.1. What We've Covered
    2. 17.2. Some Game Design Tips
    3. 17.3. Wrapping It Up