Beginning Android 4 Games Development Mario Zechner Robert Green
Contents Contents at a Glance About the Authors Acknowledgments Introduction iv xii xiii xiv Chapter 1: Android, the New Kid on the Block... 1 A Brief History of Android 2 Fragmentation 3 The Role of Google 4 The Android Open Source Project 4 The Android Market 4 Challenges, Device Seeding, and Google I/O 5 Android's Features and Architecture 6 The Kernel 7 The Runtime and Dalvik 7 System Libraries 8 The Application Framework 9 The Software Development Kit 10 The Developer Community 11 Devices, Devices, Devices! 12 Hardware 12 The Range of Devices 14 Compatibility Across All Devices 19 Mobile Gaming Is Different 20 A Gaming Machine in Every Pocket 20 Always Connected 21 Casual and Hardcore 22 Big Market, Small Developers 22 Summary 23 Chapter 2: First Steps with the Android SDK 25 Setting Up the Development Environment 25 Setting Up the JDK 26 Setting Up the Android SDK 26 V
CONTEXTS Installing Eclipse 28 Installing the ADT Eclipse Plug-In 28 A Quick Tour of Eclipse 30 Helpful Eclipse Shortcuts 32 Hello World, Android Style 33 Creating the Project 33 Exploring the Project 34 Writing the Application Code 36 Running and Debugging Android Applications 39 Connecting a Device 39 Creating an Android Virtual Device 39 Running an Application 41 Debugging an Application 44 LogCatand DDMS 48 Using ADB 50 Summary 51 Chapter 3: Game Development 101 53 Genres: To Each One's Taste 54 Casual Games 54 Puzzle Games 56 Action and Arcade Games 58 Tower-Defense Games 61 Innovation 62 Game Design: The Pen Is Mightier Than the Code 63 Core Game Mechanics 64 A Story and an Art Style 66 Screens and Transitions 67 Code: The Nitty-Gritty Details 73 Application and Window Management 74 Input 75 File I/O 79 Audio 79 Graphics 84 The Game Framework 97 Summary 105 Chapter 4: Android for Game Developers 107 Defining an Android Application: The Manifest File 108 The <manifest> Element 109 The <application> Element 110 The <activity> Element 111 The <uses-permission> Element, 113 The <uses-feature> Element 114 The <uses-sdk> Element 116 Android Game Project Setup in Ten Easy Steps 117 Market Filters 119 Defining the Icon of Your Game 119 Android API Basics 121 vl
CONTENTS Creating a Test Project 121 The Activity Life Cycle 125 Input Device Handling 132 File Handling 152 Audio Programming 158 Playing Sound Effects 159 Streaming Music 163 Basic Graphics Programming 167 Best Practices 192 Summary 193 Chapter 5: An Android Game Development Framework 195 Plan of Attack 195 The AndroidFilelO Class 196 AndroidAudio, AndroidSound, and AndroidMusic: Crash, Bang, Boom! 197 Androidlnput and AccelerometerHandler 202 AccelerometerHandler: Which Side Is Up? 202 CompassHandler 204 The Pool Class: Because Reuse Is Good for You! 205 KeyboardHandler: Up, Up, Down, Down, Left, Right 207 Touch Handlers 211 Androidlnput: The Great Coordinator 219 AndroidGraphics and AndroidPixmap: Double Rainbow 221 Handling Different Screen Sizes and Resolutions 221 AndroidPixmap: Pixels for the People 226 AndroidGraphics: Serving Our Drawing Needs 227 AndroidFastRenderView: Loop, Stretch, Loop, Stretch 231 AndroidGame: Tying Everything Together 234 Summary 238 Chapter 6: Mr. Norn Invades Android 239 Creating the Assets 239 Setting Up the Project 241 MrNomGame: The Main Activity 242 Assets: A Convenient Asset Store 242 Settings: Keeping Track of User Choices and High Scores 243 LoadingScreen: Fetching the Assets from Disk 246 The Main Menu Screen 247 The HelpScreen Class(es) 251 The High-Scores Screen 253 Rendering Numbers: An Excursion 253 Implementing the Screen 255 Abstracting 257 Abstracting the World of Mr. Norn: Model, View, Controller 258 The GameScreen Class 270 Summary 277 Chapter 7: OpenGL ES: A Gentle Introduction 279 What is OpenGL ES and Why Should I Care? 279 The Programming Model: An Analogy 280 Vii
Projections Normalized Device Space and the Viewport Matrices The Rendering Pipeline Before We Begin GLSurfaceView: Making Things Easy Since 2008 GLGame: Implementing the Game Interface Look Mom, I Got a Red Triangle! Defining the Viewport Defining the Projection Matrix Specifying Triangles Putting It Together Specifying Per Vertex Color Texture Mapping: Wallpapering Made Easy Texture Coordinates Uploading Bitmaps Texture Filtering Disposing of Textures A Helpful Snippet Enabling Texturing Putting It Together A Texture Class Indexed Vertices: Because Re-use is Good for You Putting It Together A Vertices Class Alpha Blending: I Can See Through You 329 More Primitives: Points, Lines, Strips, and Fans 2D Transformations: Fun with the Model-View Matrix World and Model Space Matrices Again An Initial Example Using Translation More Transformations Optimizing for Performance Measuring Frame Rate The Curious Case of the Hero on Android 1.5 What's Making My OpenGL ES Rendering So Slow? v Removing Unnecessary State Changes 349 Reducing Texture Size Means Fewer Pixels to be Fetched Reducing Calls to OpenGL ES/JNI Methods The Concept of Binding Vertices InClosing Summary Chapter 8:2D Game Programming Tricks 282 284 284 285 286 287 290 297 298 298 302 306 309 313 313 315 316 317 318 318 318 321 323 324 326 333 334 334 335 336 341 345 345 347 347 351 352 352 356 '. 356 Before We Begin 357 In the Beginning... There Was the Vector, 358 Working with Vectors 359 A Little Trigonometry 361 Implementing a Vector Class 363 357 Vlil
CONTENTS A Simple Usage Example 366 A Little Physics in 2D 371 Newton and Euler, Best Friends Forever 371 Force and Mass 372 Playing Around, Theoretically 373 Playing Around, Practically 374 Collision Detection and Object Representation in 2D 378 Bounding Shapes 379 Constructing Bounding Shapes 381 Game Object Attributes 383 Broad-Phase and Narrow-Phase Collision Detection 384 An Elaborate Example 391 A Camera in 2D 404 The Camera2D Class 407 An Example, 409 Texture Atlas: Because Sharing Is Caring 410 An Example 412 Texture Regions, Sprites, and Batches: Hiding OpenGL ES 416 The TextureRegion Class 417 The SpriteBatcher Class, 418 Sprite Animation 427 The Animation Class 428 An Example 429 Summary 433 Chapter 9: Super Jumper: A 2D OpenGL ES Game. 435 Core Game Mechanics 435 A Backstory and Art Style 436 Screens and Transitions 437 Defining the Game World 438 Creating the Assets 441 The Ul Elements 441 Handling Text with Bitmap Fonts 443 The Game Elements 445 Texture Atlas to the Rescue 447 Music and Sound 448 Implementing Super Jumper 449 The Assets Class 450 The Settings Class 453 The Main Activity 454 The Font Class 456 GLScreen 457 The Main Menu Screen 458 The Help Screens 461 The High-Scores Screen 463 The Simulation Classes 466 The Game Screen 481 The WorldRenderer Class 489 Ix
CONTENTS To Optimize or Not to Optimize 492 Summary 493 Chapter 10: OpenGL ES: Going 3D 495 Before We Begin 495 Vertices in 3D 496 Vertices3: Storing 3D Positions 496 An Example 498 Perspective Projection: The Closer, the Bigger 502 Z-buffer: Bringing Order into Chaos 505 Fixing the Last Example 506 Blending: There's Nothing Behind You 507 Z-buffer Precision and Z-fighting 510 Defining 3D Meshes 511 A Cube: Hello World in 3D 512 An Example 514 Matrices and Transformations Again, 517 The Matrix Stack 518 Hierarchical Systems with the Matrix Stack 520 A Simple Camera System 527 Summary 531 Chapter 11:3D Programming Tricks 533 Before We Begin 533 Vectors in 3D 534 Lighting in OpenGL ES 538 How Lighting Works 538 Light Sources 540 Materials 541 How OpenGL ES Calculates Lighting: Vertex Normals 542 In Practice 542 Some Notes on Lighting in OpenGL ES 556 Mipmapping 557 Simple Cameras 561 The First-Person or Euler Camera 562 A Euler Camera Example 565 A Look-At Camera 571 Loading Models 573 The Wavefront OBJ Format 573 Implementing an OBJ Loader 574 Using the OBJ Loader 579 Some Notes on Loading Models 579 A Little Physics in 3D 580 Collision Detection and Object Representation in 3D 581 Bounding Shapes in 3D 581 Bounding Sphere Overlap Testing 582 Game0bject3D and DynamicGameObject3D 583 Summary 584 X
CONTENTS Chapter 12: Droid Invaders: The Grand Finale 587 Core Game Mechanics 587 A Backstory and Art Style 589 Screens and Transitions 590 Defining the Game World 591 Creating the Assets 593 The Ul Assets 593 The Game Assets 594 Sound and Music 596 Plan of Attack 596 The Assets Class 597 The Settings Class 600 The Main Activity 601 The Main Menu Screen 602 The Settings Screen 604 The Simulation Classes 607 The Shield Class 608 The Shot Class 608 The Ship Class 609 The Invader Class 611 The World Class 614 The GameScreen Class 620 TheWorldRender Class 626 Optimizations 632 Summary 633 Chapter 13: Publishing Your Game... 635 A Word on Testing 635 Becoming a Registered Developer 636 Signing Your Game's APK 637 Putting Your Game on the Market 642 Uploading Assets 643 Product Details 644 Publishing Options 644 Publish! 645 Marketing 645 The Developer Console 645 Summary 646 Chapter 14: What's Next? 647 Getting Social 647 Location Awareness 647 Multiplayer Functionality 648 OpenGL ES 2.0 and More 648 Frameworks and Engines 648 Resources on the Web 650 Closing Words 651 Index 653 xl