You are previewing Beginning Android Games.
O'Reilly logo
Beginning Android Games

Book Description

Beginning Android Games offers everything you need to join the ranks of successful Android game developers. You'll start with game design fundamentals and programming basics, and then progress towards creating your own basic game engine and playable games. This will give you everything you need to branch out and write your own Android games.

The potential user base and the wide array of available high-performance devices makes Android an attractive target for aspiring game developers. Do you have an awesome idea for the next break-through mobile gaming title? Beginning Android Games will help you kick-start your project.

The book will guide you through the process of making several example games for the Android platform, and involves a wide range of topics:

  • The fundamentals of game development

  • The Android platform basics to apply those fundamentals in the context of making a game

  • The design of 2D and 3D games and their successful implementation on the Android platform

What you'll learn

  • How to set up and use the development tools for developing your first Android application

  • The fundamentals of game programming in the context of the Android platform

  • How to use the Android's APIs for graphics (Canvas, OpenGL ES 1.0/1.1), audio, and user input to reflect those fundamentals

  • How to develop two 2D games from scratch, based on the Canvas API and OpenGL ES.

  • How to create a full-featured 3D game

  • How to publish your games, get crash reports, and support your users

  • How to complete your own playable 2D OpenGL games

Who this book is for

This book is for people with a basic knowledge of Java who want to write games on the Android platform. It also offers information for experienced game developers about the pitfalls and peculiarities of the platform.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Introduction
    1. A Word About the Target Audience
    2. How This Book Is Organized
    3. Getting the Source Code
  6. 1. Android, the New Kid on the Block
    1. 1.1. A Brief History of Android
    2. 1.2. Fragmentation
    3. 1.3. The Role of Google
      1. 1.3.1. The Android Open Source Project
      2. 1.3.2. The Android Market
      3. 1.3.3. Challenges, Device Seeding, and Google I/O
    4. 1.4. Android's Features and Architecture
      1. 1.4.1. The Kernel
      2. 1.4.2. The Runtime and Dalvik
      3. 1.4.3. System Libraries
      4. 1.4.4. The Application Framework
    5. 1.5. The Software Development Kit
    6. 1.6. The Developer Community
    7. 1.7. Devices, Devices, Devices!
      1. 1.7.1. Hardware
      2. 1.7.2. First Gen, Second Gen, Next Gen
        1. 1.7.2.1. In the Beginning: First Generation
        2. 1.7.2.2. More Power: Second Generation
        3. 1.7.2.3. The Future: Next Generation
        4. 1.7.2.4. Game Controllers
    8. 1.8. Mobile Gaming Is Different
      1. 1.8.1. A Gaming Machine in Every Pocket
      2. 1.8.2. Always Connected
      3. 1.8.3. Casual and Hardcore
      4. 1.8.4. Big Market, Small Developers
    9. 1.9. Summary
  7. 2. First Steps with the Android SDK
    1. 2.1. Setting Up the Development Environment
      1. 2.1.1. Setting Up the JDK
      2. 2.1.2. Setting Up the Android SDK
      3. 2.1.3. Installing Eclipse
      4. 2.1.4. Installing the ADT Eclipse Plug-In
      5. 2.1.5. A Quick Tour of Eclipse
    2. 2.2. Hello World, Android Style
      1. 2.2.1. Creating the Project
      2. 2.2.2. Exploring the Project
      3. 2.2.3. Writing the Application Code
    3. 2.3. Running and Debugging Android Applications
      1. 2.3.1. Connecting a Device
      2. 2.3.2. Creating an Android Virtual Device
      3. 2.3.3. Running an Application
      4. 2.3.4. Debugging an Application
      5. 2.3.5. LogCat and DDMS
      6. 2.3.6. Using ADB
    4. 2.4. Summary
  8. 3. Game Development 101
    1. 3.1. Genres: To Each One's Taste
      1. 3.1.1. Causal Games
      2. 3.1.2. Puzzle Games
      3. 3.1.3. Action and Arcade Games
      4. 3.1.4. Tower-Defense Games
      5. 3.1.5. Innovation
    2. 3.2. Game Design: The Pen Is Mightier Than the Code
      1. 3.2.1. Core Game Mechanics
      2. 3.2.2. A Story and an Art Style
      3. 3.2.3. Screens and Transitions
    3. 3.3. Code: The Nitty-Gritty Details
      1. 3.3.1. Application and Window Management
      2. 3.3.2. Input
      3. 3.3.3. File I/O
      4. 3.3.4. Audio
        1. 3.3.4.1. The Physics of Sound
        2. 3.3.4.2. Recording and Playback
        3. 3.3.4.3. Audio Quality and Compression
        4. 3.3.4.4. In Practice
      5. 3.3.5. Graphics
        1. 3.3.5.1. Of Rasters, Pixels, and Framebuffers
        2. 3.3.5.2. Vsync and Double-Buffering
        3. 3.3.5.3. What Is Color?
        4. 3.3.5.4. Color Models
        5. 3.3.5.5. Encoding Colors Digitally
        6. 3.3.5.6. Image Formats and Compression
        7. 3.3.5.7. Alpha Compositing and Blending
        8. 3.3.5.8. In Practice
      6. 3.3.6. The Game Framework
        1. 3.3.6.1. The Game and Screen Interfaces
        2. 3.3.6.2. A Simple Example
        3. 3.3.6.3. FrameRate-Independent Movement
    4. 3.4. Summary
  9. 4. Android for Game Developers
    1. 4.1. Defining an Android Application: The Manifest File
      1. 4.1.1. The <manifest> Element
      2. 4.1.2. The <application> Element
      3. 4.1.3. The <activity> Element
      4. 4.1.4. The <uses-permission> Element
      5. 4.1.5. The <uses-feature> Element
      6. 4.1.6. The <uses-sdk> Element
      7. 4.1.7. Android Game Project Setup in Ten Easy Steps
      8. 4.1.8. Defining the Icon of Your Game
    2. 4.2. Android API Basics
      1. 4.2.1. Creating a Test Project
        1. 4.2.1.1. The AndroidBasicsStarter Activity
        2. 4.2.1.2. Starting Activities Programmatically
        3. 4.2.1.3. Creating the Test Activities
      2. 4.2.2. The Activity Life Cycle
        1. 4.2.2.1. In Theory
        2. 4.2.2.2. In Practice
      3. 4.2.3. Input Device Handling
        1. 4.2.3.1. Getting (Multi-)Touch Events
        2. 4.2.3.2. Processing Single-Touch Events
        3. 4.2.3.3. Processing Multitouch Events
          1. 4.2.3.3.1. Pointer IDs and Indices
          2. 4.2.3.3.2. The Action Mask and More Event Types
          3. 4.2.3.3.3. In Practice
        4. 4.2.3.4. Processing Key Events
        5. 4.2.3.5. Reading the Accelerometer State
      4. 4.2.4. File Handling
        1. 4.2.4.1. Reading Assets
        2. 4.2.4.2. Accessing the External Storage
      5. 4.2.5. Audio Programming
        1. 4.2.5.1. Setting the Volume Controls
      6. 4.2.6. Playing Sound Effects
      7. 4.2.7. Streaming Music
      8. 4.2.8. Basic Graphics Programming
        1. 4.2.8.1. Using Wake Locks
        2. 4.2.8.2. Going Full-Screen
        3. 4.2.8.3. Continuous Rendering in the UI Thread
        4. 4.2.8.4. Getting the Screen Resolution (and Coordinate Systems)
        5. 4.2.8.5. Drawing Simple Shapes
          1. 4.2.8.5.1. Drawing Pixels
          2. 4.2.8.5.2. Drawing Lines
          3. 4.2.8.5.3. Drawing Rectangles
          4. 4.2.8.5.4. Drawing Circles
          5. 4.2.8.5.5. Putting It All Together
        6. 4.2.8.6. Using Bitmaps
          1. 4.2.8.6.1. Loading and Examining Bitmaps
          2. 4.2.8.6.2. Disposing of Bitmaps
          3. 4.2.8.6.3. Drawing Bitmaps
          4. 4.2.8.6.4. Putting It All Together
        7. 4.2.8.7. Rendering Text
          1. 4.2.8.7.1. Loading Fonts
          2. 4.2.8.7.2. Drawing Text with a Font
          3. 4.2.8.7.3. Text Alignment and Boundaries
          4. 4.2.8.7.4. Putting It All Together
        8. 4.2.8.8. Continuous Rendering with SurfaceView
          1. 4.2.8.8.1. Motivation
          2. 4.2.8.8.2. SurfaceHolder and Locking
          3. 4.2.8.8.3. Surface Creation and Validity
          4. 4.2.8.8.4. Putting It All Together
    3. 4.3. Best Practices
    4. 4.4. Summary
  10. 5. An Android Game Development Framework
    1. 5.1. Plan of Attack
    2. 5.2. The AndroidFileIO Class
    3. 5.3. AndroidAudio, AndroidSound, and AndroidMusic: Crash, Bang, Boom!
    4. 5.4. AndroidInput and AccelerometerHandler
      1. 5.4.1. AccelerometerHandler: Which Side Is Up?
      2. 5.4.2. The Pool Class: Because Reuse is Good for You!
      3. 5.4.3. KeyboardHandler: Up, Up, Down, Down, Left, Right . . .
      4. 5.4.4. Touch Handlers
        1. 5.4.4.1. The TouchHandler Interface
        2. 5.4.4.2. The SingleTouchHandler Class
        3. 5.4.4.3. The MultiTouchHandler
      5. 5.4.5. AndroidInput: The Great Coordinator
    5. 5.5. AndroidGraphics and AndroidPixmap: Double Rainbow
      1. 5.5.1. Handling Different Screen Sizes and Resolutions
        1. 5.5.1.1. Density
        2. 5.5.1.2. Aspect Ratio
        3. 5.5.1.3. Coping with Different Aspect Ratios
        4. 5.5.1.4. A Simpler Solution
        5. 5.5.1.5. The Implementation
      2. 5.5.2. AndroidPixmap: Pixels for the People
      3. 5.5.3. AndroidGraphics: Serving Our Drawing Needs
      4. 5.5.4. AndroidFastRenderView: Loop, Strech, Loop, Stretch
    6. 5.6. AndroidGame: Tying Everything Together
    7. 5.7. Summary
  11. 6. Mr. Nom Invades Android
    1. 6.1. Creating the Assets
    2. 6.2. Setting Up the Project
    3. 6.3. MrNomGame: The Main Activity
      1. 6.3.1. Assets: A Convenient Asset Store
      2. 6.3.2. Settings: Keeping Track of User Choices and High Scores
      3. 6.3.3. LoadingScreen: Fetching the Assets from Disk
    4. 6.4. The Main Menu Screen
    5. 6.5. The HelpScreen Class(es)
    6. 6.6. The High-Scores Screen
      1. 6.6.1. Rendering Numbers: An Excursion
      2. 6.6.2. Implementing the Screen
    7. 6.7. Abstracting...
      1. 6.7.1. Abstracting the World of Mr. Nom: Model, View, Controller
        1. 6.7.1.1. The Stain Class
        2. 6.7.1.2. The Snake and SnakePart Classes
        3. 6.7.1.3. The World Class
          1. 6.7.1.3.1. Time-Based Movement of Mr. Nom
          2. 6.7.1.3.2. Placing Stains
          3. 6.7.1.3.3. Determining When the Game Is Over
          4. 6.7.1.3.4. Implementing the World Class
      2. 6.7.2. The GameScreen Class
    8. 6.8. Summary
  12. 7. OpenGL ES: A Gentle Introduction
    1. 7.1. What Is OpenGL ES and Why Should I Care?
      1. 7.1.1. The Programming Model: An Analogy
      2. 7.1.2. Projections
      3. 7.1.3. Normalized Device Space and the Viewport
      4. 7.1.4. Matrices
      5. 7.1.5. The Rendering Pipeline
    2. 7.2. Before We Begin
    3. 7.3. GLSurfaceView: Making Things Easy Since 2008
    4. 7.4. GLGame: Implementing the Game Interface
    5. 7.5. Look Mom, I Got a Red Triangle!
      1. 7.5.1. Defining the Viewport
      2. 7.5.2. Defining the Projection Matrix
        1. 7.5.2.1. Matrix Modes and Active Matrices
        2. 7.5.2.2. Orthographic Projection with glOrthof
        3. 7.5.2.3. A Helpful Snippet
      3. 7.5.3. Specifying Triangles
        1. 7.5.3.1. A Small NIO Buffer Digression
        2. 7.5.3.2. Sending Vertices to OpenGL ES
      4. 7.5.4. Putting It Together
    6. 7.6. Specifying Per Vertex Color
    7. 7.7. Texture Mapping: Wallpapering Made Easy
      1. 7.7.1. Texture Coordinates
      2. 7.7.2. Uploading Bitmaps
      3. 7.7.3. Texture Filtering
      4. 7.7.4. Disposing of Textures
      5. 7.7.5. A Helpful Snippet
      6. 7.7.6. Enabling Texturing
      7. 7.7.7. Putting It Together
      8. 7.7.8. A Texture Class
    8. 7.8. Indexed Vertices: Because Reuse Is Good for You
      1. 7.8.1. Putting It Together
      2. 7.8.2. A Vertices Class
    9. 7.9. Alpha Blending: I Can See Through You
    10. 7.10. More Primitives: Points, Lines, Strips, and Fans
    11. 7.11. 2D Transformations: Fun with the Model-View Matrix
      1. 7.11.1. World and Model Space
      2. 7.11.2. Matrices Again
      3. 7.11.3. An First Example Using Translation
        1. 7.11.3.1. Bob Becomes a Class
        2. 7.11.3.2. Putting It Together
      4. 7.11.4. More Transformations
        1. 7.11.4.1. Rotation
        2. 7.11.4.2. Scaling
        3. 7.11.4.3. Combining Transformations
    12. 7.12. Optimizing for Performance
      1. 7.12.1. Measuring Frame Rate
      2. 7.12.2. The Curious Case of the Hero on Android 1.5
      3. 7.12.3. What's Making My OpenGL ES Rendering So Slow?
      4. 7.12.4. Removing Unnecessary State Changes
      5. 7.12.5. Reducing Texture Size Means Fewer Pixels to Be Fetched
      6. 7.12.6. Reducing Calls to OpenGL ES/JNI Methods
      7. 7.12.7. The Concept of Binding Vertices
      8. 7.12.8. In Closing
    13. 7.13. Summary
  13. 8. 2D Game Programming Tricks
    1. 8.1. Before We Begin
    2. 8.2. In the Beginning There Was the Vector
      1. 8.2.1. Working with Vectors
      2. 8.2.2. A Little Trigonometry
      3. 8.2.3. Implementing a Vector Class
      4. 8.2.4. A Simple Usage Example
    3. 8.3. A Little Physics in 2D
      1. 8.3.1. Newton and Euler, Best Friends Forever
      2. 8.3.2. Force and Mass
      3. 8.3.3. Playing Around, Theoretically
      4. 8.3.4. Playing Around, Practically
    4. 8.4. Collision Detection and Object Representation in 2D
      1. 8.4.1. Bounding Shapes
      2. 8.4.2. Constructing Bounding Shapes
      3. 8.4.3. Game Object Attributes
      4. 8.4.4. Broad-Phase and Narrow-Phase Collision Detection
        1. 8.4.4.1. Narrow Phase
          1. 8.4.4.1.1. Circle Collision
          2. 8.4.4.1.2. Rectangle Collision
          3. 8.4.4.1.3. Circle/Rectangle Collision
          4. 8.4.4.1.4. Putting It All Together
        2. 8.4.4.2. Broad Phase
      5. 8.4.5. An Elaborate Example
        1. 8.4.5.1. GameObject, DynamicGameObject, and Cannon
        2. 8.4.5.2. The Spatial Hash Grid
        3. 8.4.5.3. Putting It All Together
    5. 8.5. A Camera in 2D
      1. 8.5.1. The Camera2D Class
      2. 8.5.2. An Example
    6. 8.6. Texture Atlas: Because Sharing Is Caring
      1. 8.6.1. An Example
    7. 8.7. Texture Regions, Sprites, and Batches: Hiding OpenGL ES
      1. 8.7.1. The TextureRegion Class
      2. 8.7.2. The SpriteBatcher Class
        1. 8.7.2.1. Using the SpriteBatcher Class
        2. 8.7.2.2. Measuring Performance
        3. 8.7.2.3. Working Around a Bug in FloatBuffer
    8. 8.8. Sprite Animation
      1. 8.8.1. The Animation Class
      2. 8.8.2. An Example
    9. 8.9. Summary
  14. 9. Super Jumper: A 2D OpenGL ES Game
    1. 9.1. Core Game Mechanics
    2. 9.2. A Backstory and Art Style
    3. 9.3. Screens and Transitions
    4. 9.4. Defining the Game World
    5. 9.5. Creating the Assets
      1. 9.5.1. The UI Elements
      2. 9.5.2. Handling Text with Bitmap Fonts
      3. 9.5.3. The Game Elements
      4. 9.5.4. Texture Atlas to the Rescue
      5. 9.5.5. Music and Sound
    6. 9.6. Implementing Super Jumper
      1. 9.6.1. The Assets Class
      2. 9.6.2. The Settings Class
      3. 9.6.3. The Main Activity
      4. 9.6.4. The Font Class
      5. 9.6.5. GLScreen
      6. 9.6.6. The Main Menu Screen
      7. 9.6.7. The Help Screens
      8. 9.6.8. The High-Scores Screen
      9. 9.6.9. The Simulation Classes
        1. 9.6.9.1. The Spring Class
        2. 9.6.9.2. The Coin Class
        3. 9.6.9.3. The Castle Class
        4. 9.6.9.4. The Squirrel Class
        5. 9.6.9.5. The Platform Class
        6. 9.6.9.6. The Bob Class
        7. 9.6.9.7. The World Class
          1. 9.6.9.7.1. Generating the World
          2. 9.6.9.7.2. Updating the World
          3. 9.6.9.7.3. Collision Detection and Response
          4. 9.6.9.7.4. Game Over, Buddy!
      10. 9.6.10. The Game Screen
        1. 9.6.10.1. Updating the GameScreen
        2. 9.6.10.2. Rendering the GameScreen
        3. 9.6.10.3. Finishing Touches
      11. 9.6.11. The WorldRenderer Class
    7. 9.7. To Optimize or Not to Optimize
    8. 9.8. Summary
  15. 10. OpenGL ES: Going 3D
    1. 10.1. Before We Begin
    2. 10.2. Vertices in 3D
      1. 10.2.1. Vertices3: Storing 3D Positions
      2. 10.2.2. An Example
    3. 10.3. Perspective Projection: The Closer, the Bigger
    4. 10.4. Z-buffer: Bringing Order into Chaos
      1. 10.4.1. Fixing the Last Example
      2. 10.4.2. Blending: There's Nothing Behind You
      3. 10.4.3. Z-buffer Precision and Z-fighting
    5. 10.5. Defining 3D Meshes
      1. 10.5.1. A Cube: Hello World in 3D
      2. 10.5.2. An Example
    6. 10.6. Matrices and Transformations Again
      1. 10.6.1. The Matrix Stack
      2. 10.6.2. Hierarchical Systems with the Matrix Stack
        1. 10.6.2.1. A Simple Crate Solar System
        2. 10.6.2.2. The HierarchicalObject Class
        3. 10.6.2.3. Putting It All Together
      3. 10.6.3. A Simple Camera System
    7. 10.7. Summary
  16. 11. 3D Programming Tricks
    1. 11.1. Before We Begin
    2. 11.2. Vectors in 3D
    3. 11.3. Lighting in OpenGL ES
      1. 11.3.1. How Lighting Works
      2. 11.3.2. Light Sources
      3. 11.3.3. Materials
      4. 11.3.4. How OpenGL ES Calculates Lighting: Vertex Normals
      5. 11.3.5. In Practice
        1. 11.3.5.1. Enabling and Disabling Lighting
        2. 11.3.5.2. Specifying Light Sources
          1. 11.3.5.2.1. Ambient Light
          2. 11.3.5.2.2. Point Lights
          3. 11.3.5.2.3. Directional Lights
        3. 11.3.5.3. Specifying Materials
        4. 11.3.5.4. Specifying Normals
        5. 11.3.5.5. Putting it All Together
      6. 11.3.6. Some Notes on Lighting in OpenGL ES
    4. 11.4. Mipmapping
    5. 11.5. Simple Cameras
      1. 11.5.1. The First-Person or Euler Camera
      2. 11.5.2. An Euler Camera Example
      3. 11.5.3. A Look-At Camera
    6. 11.6. Loading Models
      1. 11.6.1. The Wavefront OBJ Format
      2. 11.6.2. Implementing an OBJ Loader
      3. 11.6.3. Using the OBJ Loader
      4. 11.6.4. Some Notes on Loading Models
    7. 11.7. A Little Physics in 3D
    8. 11.8. Collision Detection and Object Representation in 3D
      1. 11.8.1. Bounding Shapes in 3D
      2. 11.8.2. Bounding Sphere Overlap Testing
      3. 11.8.3. GameObject3D and DynamicGameObject3D
    9. 11.9. Summary
  17. 12. Droid Invaders: the Grand Finale
    1. 12.1. Core Game Mechanics
    2. 12.2. A Backstory and Art Style
    3. 12.3. Screens and Transitions
    4. 12.4. Defining the Game World
    5. 12.5. Creating the Assets
      1. 12.5.1. The UI Assets
      2. 12.5.2. The Game Assets
      3. 12.5.3. Sound and Music
    6. 12.6. Plan of Attack
    7. 12.7. The Assets Class
    8. 12.8. The Settings Class
    9. 12.9. The Main Activity
    10. 12.10. The Main Menu Screen
    11. 12.11. The Settings Screen
    12. 12.12. The Simulation Classes
      1. 12.12.1. The Shield Class
      2. 12.12.2. The Shot Class
      3. 12.12.3. The Ship Class
      4. 12.12.4. The Invader Class
      5. 12.12.5. The World Class
    13. 12.13. The GameScreen Class
    14. 12.14. The WorldRender Class
    15. 12.15. Optimizations
    16. 12.16. Summary
  18. 13. Publishing Your Game
    1. 13.1. A Word on Testing
    2. 13.2. Becoming a Registered Developer
    3. 13.3. Sign Your Game's APK
    4. 13.4. Putting Your Game on the Market
      1. 13.4.1. Uploading Assets
      2. 13.4.2. Listing Details
      3. 13.4.3. Publishing Options
      4. 13.4.4. Publish!
      5. 13.4.5. Marketing
    5. 13.5. The Developer Console
    6. 13.6. Summary
  19. 14. What's Next?
    1. 14.1. Getting Social
    2. 14.2. Location Awareness
    3. 14.3. Multiplayer Functionality
    4. 14.4. OpenGL ES 2.0 and More
    5. 14.5. Frameworks and Engines
    6. 14.6. Resources on the Web
    7. 14.7. Closing Words