Beginning Android 4 Games Development Copyright © 2011 by Mario Zechner and Robert Green 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-43023987-1 ISBN-13 (electronic): 978-1-4302-3988-8 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image, we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The images of the Android Robot (01 / Android Robot) are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. Android and all Android and Google-based marks are trademarks or registered trademarks of Google, Inc., in the U.S. and other countries. Apress Media, L.L.C. is not affiliated with Google, Inc., and this book was written without endorsement from Google, Inc. 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. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Gary Schwartz Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Morgan Engel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Adam Heath Copy Editor: Chandra Clarke Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: SPi Global 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/bulk-sales. 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. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book's source code, go to http://www.apress.com/source-code/. Dedicated to our idols, fans, families, and loved ones Contents at a Glance Contents About the Authors Acknowledgments Introduction Chapter 1: Android, the New Kid on the Block Chapter 2: First Steps with the Android SDK Chapter 3: Game Development 101 Chapter 4: Android for Game Developers Chapter 5: An Android Game Development Framework Chapter 6: Mr. Nom Invades Android Chapter 7: OpenGL ES: A Gentle Introduction Chapter 8: 2D Game Programming Tricks Chapter 9: Super Jumper: A 2D OpenGL ES Game Chapter 10: OpenGL ES: Going 3D Chapter 11: 3D Programming Tricks Chapter 12: Droid Invaders: The Grand Finale Chapter 13: Publishing Your Game Chapter 14: What's Next? Index Contents Contents at a Glance About the Authors Acknowledgments Introduction Chapter 1: Android, the New Kid on the Block A Brief History of Android Fragmentation The Role of Google The Android Open Source Project The Android Market Challenges, Device Seeding, and Google I/O Android's Features and Architecture The Kernel The Runtime and Dalvik System Libraries The Application Framework The Software Development Kit The Developer Community Devices, Devices, Devices! Hardware The Range of Devices Compatibility Across All Devices Mobile Gaming Is Different A Gaming Machine in Every Pocket Always Connected Casual and Hardcore Big Market, Small Developers Summary Chapter 2: First Steps with the Android SDK Setting Up the Development Environment Setting Up the JDK Setting Up the Android SDK Installing Eclipse Installing the ADT Eclipse Plug-In A Quick Tour of Eclipse Helpful Eclipse Shortcuts Hello World, Android Style Creating the Project Exploring the Project Writing the Application Code Running and Debugging Android Applications Connecting a Device Creating an Android Virtual Device Running an Application Debugging an Application LogCat and DDMS Using ADB Summary Chapter 3: Game Development 101 Genres: To Each One's Taste Casual Games Puzzle Games Action and Arcade Games Tower-Defense Games Innovation Game Design: The Pen Is Mightier Than the Code Core Game Mechanics A Story and an Art Style Screens and Transitions Code: The Nitty-Gritty Details Application and Window Management Input File I/O Audio Graphics The Game Framework Summary Chapter 4: Android for Game Developers Defining an Android Application: The Manifest File The <manifest> Element The Application Element The <activity> Element The <uses-permission> Element The <uses-feature> Element The <uses-sdk> Element Android Game Project Setup in Ten Easy Steps Market Filters Defining the Icon of Your Game Android API Basics Creating a Test Project The Activity Life Cycle Input Device Handling File Handling Audio Programming Playing Sound Effects Streaming Music Basic Graphics Programming Best Practices Summary Chapter 5: An Android Game Development Framework Plan of Attack The AndroidFileIO Class AndroidAudio, AndroidSound, and AndroidMusic: Crash, Bang, Boom! AndroidInput and AccelerometerHandler AccelerometerHandler: Which Side Is Up? CompassHandler The Pool Class: Because Reuse Is Good for You! KeyboardHandler: Up, Up, Down, Down, Left, Right … Touch Handlers The MultiTouchHandler AndroidInput: The Great Coordinator AndroidGraphics and AndroidPixmap: Double Rainbow Handling Different Screen Sizes and Resolutions AndroidPixmap: Pixels for the People AndroidGraphics: Serving Our Drawing Needs AndroidFastRenderView: Loop, Stretch, Loop, Stretch AndroidGame: Tying Everything Together Summary Chapter 6: Mr. Nom Invades Android Creating the Assets Setting Up the Project MrNomGame: The Main Activity Assets: A Convenient Asset Store Settings: Keeping Track of User Choices and High Scores LoadingScreen: Fetching the Assets from Disk The Main Menu Screen The HelpScreenClass(es) The High-Scores Screen Rendering Numbers: An Excursion Implementing the Screen Abstracting… Abstracting the World of Mr. Nom: Model, View, Controller The GameScreen Class Summary Chapter 7: OpenGL ES: A Gentle Introduction What is OpenGL ES and Why Should I Care? The Programming Model: An Analogy 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
Description: