ebook img

Advanced Android 4 Games PDF

299 Pages·2012·6.322 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Advanced Android 4 Games

Advanced Android 4 Games ■ ■ ■ Vladimir Silva i Advanced Android 4 Games Copyright © 2012 by Vladimir Silva 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-4059-4 ISBN 978-1-4302-4060-0 (eBook) 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: James Markham Technical Reviewer: Jim Graham and Vikram Goyal 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: Jessica Belanger Copy Editor: Kimberly Burton Compositor: MacPS, LLC 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/. To all Android developers out there. The future of the platform is in your hands. Contents at a Glance Contents .............................................................................................................. v About the Author ............................................................................................... xxi About the Technical Reviewers ....................................................................... xxii Acknowledgment ............................................................................................. xiii Introduction ..................................................................................................... xiii ■Chapter 1: Welcome to the World of the Little Green Robot ............................ 1 ■Chapter 2: Gaming Tricks for Phones or Tablets ........................................... 19 ■Chapter 3: More Gaming Tricks with OpenGL and JNI ................................... 59 ■Chapter 4: Efficient Graphics with OpenGL ES 2.0 ....................................... 113 ■Chapter 5: 3D Shooters for Doom ................................................................ 145 ■Chapter 6: 3D Shooters for Quake ............................................................... 193 ■Chapter 7: 3D Shooters for Quake II ............................................................. 233 ■Appendix: Deployment and Compilation Tips .............................................. 261(cid:2) Index ............................................................................................................... 271 iv Contents Contents at a Glance .......................................................................................... iv(cid:2) About the Author. .............................................................................................. xxi(cid:2) About the Technical Reviewers ....................................................................... xxii(cid:2) Acknowledgment ............................................................................................ xxiii(cid:2) Introduction .................................................................................................... xxiii(cid:2) ■Chapter 1: Welcome to the World of the Little Green Robot ............................ 1 Setting Up Your Machine ........................................................................................................................................ 1 Download and Install the SDK ................................................................................................................................. 2 Configure your Eclipse ............................................................................................................................................ 2 Installing the Native Development Kit ..................................................................................................................... 9 NDK Install ....................................................................................................................................................... 10 Install Cygwin .................................................................................................................................................. 10 Creating an Android Emulator ............................................................................................................................... 10 Creating an AVD ............................................................................................................................................... 11 Configuring a Real Device ..................................................................................................................................... 15 Summary .............................................................................................................................................................. 17 Gaming Tricks for Phones or Tablets ................................................................ 19 Compiling Native Code in Android ........................................................................................................................ 19 Creating the Android Project with Native Support ........................................................................................... 21 Application Architecture .................................................................................................................................. 23 Compiling the Shared Library ............................................................................................................................... 34 Testing the App on a Device ................................................................................................................................. 35 Java Wrappers for C/C++ Event Handling ............................................................................................................ 38 Handling Audio Independently ......................................................................................................................... 38 Cascading Video Events ................................................................................................................................... 43 Multitouch Tricks .................................................................................................................................................. 48 MultiTouchGesture ........................................................................................................................................... 48 MultiTouchScreen ............................................................................................................................................ 50 TestActivity ...................................................................................................................................................... 52 Bluetooth Controllers (Zeemote) ........................................................................................................................... 53 Summary .............................................................................................................................................................. 58 v ■ CONTENTS More Gaming Tricks with OpenGL and JNI ....................................................... 59 The Power of Mobile Devices ................................................................................................................................ 60 OpenGL the Java Way ........................................................................................................................................... 61 Creating a Project ............................................................................................................................................ 62 Java Main Activity ............................................................................................................................................ 67 Surface View .................................................................................................................................................... 68 GL Thread ......................................................................................................................................................... 71 Cube Renderer ................................................................................................................................................. 74 Cube Class ....................................................................................................................................................... 77 OpenGL the Native Way ........................................................................................................................................ 80 Main Activity .................................................................................................................................................... 82 Native Interface Class ...................................................................................................................................... 84 Changes to the Original Sample ...................................................................................................................... 85 Native Cube Renderer ...................................................................................................................................... 87 Native Cube ...................................................................................................................................................... 93 Compiling and Running the Sample ................................................................................................................. 94 Scaling Video Buffers with Hybrid OpenGL ES ...................................................................................................... 99 Why Use Hybrid Scaling? ................................................................................................................................. 99 Initializing the Surface ................................................................................................................................... 102 Drawing into the Texture ............................................................................................................................... 103 What Happens when the Image is not a Power of Two ................................................................................. 106 Summary ............................................................................................................................................................ 111 Efficient Graphics with OpenGL ES 2.0 ........................................................... 113 OpenGL ES 2.0 and Android ................................................................................................................................ 113 Shaders .......................................................................................................................................................... 114 GLSL ............................................................................................................................................................... 115 Anatomy of a Shader ..................................................................................................................................... 117 Invoking OpenGL ES 2.0 in Android ..................................................................................................................... 120 Project Icosahedron ............................................................................................................................................ 121 Reviewing the Shape ..................................................................................................................................... 121 Tackling the Project ....................................................................................................................................... 123 Native Icosahedron ........................................................................................................................................ 133 Adding Swipe and Multi-Touch Pinch for Zooming ............................................................................................. 139 Compiling and Running ....................................................................................................................................... 143 Summary ............................................................................................................................................................ 144 3D Shooters for Doom ..................................................................................... 145 The Sky Is the Limit with the Java/C Power Combo ........................................................................................... 146 Bringing Doom to a Mobile Device ...................................................................................................................... 147 Game Architecture for Doom .............................................................................................................................. 149 Java Main Activity ............................................................................................................................................... 151 Creation Handler ............................................................................................................................................ 152 Game Layout .................................................................................................................................................. 152 Menu and Selection Handlers ........................................................................................................................ 155 Key and Touch Event Handlers ...................................................................................................................... 157 Native Callback Handlers ............................................................................................................................... 158 Navigation Controls ........................................................................................................................................ 162 Handling Audio Independently of the Format ...................................................................................................... 164 vi ■ CONTENTS Native Interface Class ......................................................................................................................................... 165 Callback Listener ........................................................................................................................................... 165 Native Methods .............................................................................................................................................. 166 C to Java Callbacks ........................................................................................................................................ 167 Native Layer ........................................................................................................................................................ 169 Native Method Implementations .................................................................................................................... 170 Original Game Changes. ................................................................................................................................. 181 Compiling Doom with the NDK ............................................................................................................................ 187 Testing Doom in the Emulator ............................................................................................................................ 189 Summary ............................................................................................................................................................ 192 3D Shooters for Quake .................................................................................... 193 A Little About the Quake Jargon ......................................................................................................................... 193 The Power of Code Reuse: Quake in Less Than 500 Lines of New Code ............................................................1 95 OpenGL Is Not the Same as OpenGL ES ......................................................................................................... 195 Is the Software Renderer a Possible Solution? ................................................................................................... 197 NanoGL: The Live Saver ................................................................................................................................. 197 Quake for Android Architecture .......................................................................................................................... 202 Java OpenGL Renderer Architecture .............................................................................................................. 202 Handling Audio Independently of the Format ................................................................................................. 207 Handling Keyboard Events ............................................................................................................................. 210 Handling Touch Events .................................................................................................................................. 212 Game Startup Activity .................................................................................................................................... 214 Changes Required to the Native Quake Engine .................................................................................................. 216 Video Handler Changes .................................................................................................................................. 216 Handling Pitch and Yaw ................................................................................................................................. 219 Handling Forward and Side Movement .......................................................................................................... 220 Audio Handler Changes .................................................................................................................................. 220 Fixing the Game Loop .................................................................................................................................... 223 Running on a Device ........................................................................................................................................... 225 Summary ............................................................................................................................................................ 232 3D Shooters for Quake II ................................................................................. 233 Reviewing the Code ............................................................................................................................................ 233 Escaping the Shackles of the Java Heap ............................................................................................................ 234 Taming the Mighty Quake II Engine .................................................................................................................... 235 Code Reusability ............................................................................................................................................ 236 What to Do When Fatal Errors Occur ............................................................................................................. 245 OpenGL Immediate Mode Issues ................................................................................................................... 248 Video Handlers ............................................................................................................................................... 248 Building Quake II with the NDK ........................................................................................................................... 252 Running on the Device or Emulator .................................................................................................................... 255 Quake II Performance on Multiple Devices ......................................................................................................... 258 Summary ............................................................................................................................................................ 260 Deployment and Compilation Tips .................................................................. 261 Signing Your Application ..................................................................................................................................... 261 Creating a Key Store ...................................................................................................................................... 262 Signing the Application .................................................................................................................................. 262 JNI Cheat Sheet .................................................................................................................................................. 267 vii ■ CONTENTS JNI Method Syntax ......................................................................................................................................... 268 Loading a Java Class as Global Reference .................................................................................................... 268 Converting a Java Array to a C array ............................................................................................................. 268 Invoking Java Within C (Callbacks) ................................................................................................................ 269 Final Thoughts .................................................................................................................................................... 269 Index ............................................................................................................... 271 viii About the Author Vladimir Silva was born in Quito, Ecuador. He received a system’s analyst degree from Ecuador’s Army Polytechnic Institute in 1994. The same year, he came to the United States as an exchange student pursuing a master’s degree in computer science at Middle Tennessee State University. After graduation, he joined the IBM WebAhead technology think tank. His interests include grid computing, neural networks, and artificial intelligence. He also holds numerous IT certifications, including Oracle Certified Professional (OCP), Microsoft Certified Solution Developer (MCSD), and Microsoft Certified Professional (MCP). He has written many technical articles on security and grid computing for IBM developerWorks. ix About the Technical Reviewers Vikram Goyal is the author of several Apress books and works from home on several web sites. He currently lives in Brisbane, Australia. James Graham was born in Alabama and grew up in Fort Walton Beach, Florida, and San Antonio, Texas. He received a BS in electronics with a specialty in telecommunications from Texas A&M University in 1988. He has been an associate network engineer for the Network Design group at Amoco Corporation and an intelligence systems analyst for the US Air Force Special Operations Command. In addition to being an Android developer, he is the author of the highly-rated freeware hurricane tracking program, JStrack. x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.