Mario Zechner,
J. F. DiMarzio and
Robert Green
3rd ed.
Mario Zechner
Graz, Steiermark, Austria
J. F. DiMarzio
Celebration, Florida, USA
Robert Green
Portland, Oregon, USA
Any source code or other supplementary materials referenced by the author in this text are available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter.
ISBN 978-1-4842-0473-3
e-ISBN 978-1-4842-0472-6
DOI 10.1007/978-1-4842-0472-6
Library of Congress Control Number: 2016961227
© Mario Zechner, J.F. DiMarzio and Robert Green 2016
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
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 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.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Printed on acid-free paper
Dedicated to my idols, Mom and Dad, and my love, Stefanie.
—Mario Zechner
Dedicated to my children and everyone involved in helping make this book happen. Go Sox.
—J.F. DiMarzio
Dedicated to my family and everyone who has helped us along the way.
—Robert Green
Hi there, and welcome to the world of Android game development. You came here to learn about game development on Android, and we hope to be the people who enable you to realize your ideas.
Together we’ll cover quite a range of materials and topics: Android basics, audio and graphics programming, a little math and physics, OpenGL ES, an intro to the Android Native Development Kit (NDK), and, finally, publishing, marketing, and making money from your game. Based on all this knowledge, we’ll develop three different games, one of which is even 3D.
Game programming can be easy if you know what you’re doing. Therefore, we’ve tried to present the material in a way that not only gives you helpful code snippets to reuse, but also actually shows you the big picture of game development. Understanding the underlying principles is the key to tackling ever more complex game ideas. You’ll not only be able to write games similar to the ones developed over the course of this book, but you’ll also be equipped with enough knowledge to go to the Web or the bookstore and take on new areas of game development on your own.
This book is aimed first and foremost at complete beginners in game programming. You don’t need any prior knowledge on the subject matter; we’ll walk you through all of the basics. However, we need to assume a little knowledge on your end about Java. If you feel rusty on the matter, we’d suggest refreshing your memory by reading Thinking in Java by Bruce Eckel (Prentice Hall, 2006), an excellent introductory text on the programming language. Other than that, there are no other requirements. No prior exposure to Android or Eclipse is necessary!
This book is also aimed at intermediate-level game programmers who want to get their hands dirty with Android. While some of the material may be old news for you, there are still a lot of tips and hints contained here that should make reading this book worthwhile. Android is a strange beast at times, and this book should be considered your battle guide.
This book takes an iterative approach in that we’ll slowly but surely work our way from the absolute basics to the esoteric heights of hardware-accelerated game programming goodness. Over the course of the book, we’ll build up a reusable code base that you can use as the foundation for most types of games.
If you’re reading this book purely as a learning exercise, we suggest going through the chapters in sequence, starting from Chapter 1 . Each chapter builds off of the previous chapter, which makes for a good learning experience.
If you’re reading this book with the intent to publish a new game at the end, we highly recommend you skip to Chapter 14 and learn about designing your game to be marketable and make money, then come back to the beginning and begin development.
Of course, more experienced readers can skip certain sections they feel confident with. Just make sure to read through the code listings of sections you skim over so you will understand how the classes and interfaces are used in subsequent, more advanced sections.
This book is fully self-contained; all the code necessary to run the examples and games is included. However, copying the listings from the book to Eclipse is error prone, and games do not consist of code alone, but also have assets that you can’t easily copy out of the book. We took great care to ensure that all the listings in this book are error free, but the gremlins are always hard at work.
To make this a smooth ride, you can download the source code and related assets from the Apress website. For more details, see the book’s product page at www.apress.com/9781484204733 . The download file will offer you the following:
The complete source code and assets available from the project’s Subversion repository. The code is licensed under the Apache License 2.0 and hence is free to use in commercial and noncommercial projects. The assets are licensed under the Creative Commons BY-SA 3.0. You can use and modify them for your commercial projects, but you have to put your assets under the same license!
For each chapter that contains code, there’s an equivalent Android Studio project. The projects do not depend on each other, as we’ll iteratively improve some of the framework classes over the course of the book. Therefore, each project stands on its own. The code for both Chapters 5 and 6 is contained in the ch06-mrnom project.
Should you have any questions or comments — or even spot a mistake you think we should know about — you can contact Mario Zechner by registering an account and posting at http://badlogicgames.com/forum/viewforum.php?f=21 , or Robert Green by visiting www.rbgrn.net/contact .
We prefer being contacted through the forums. That way other readers benefit as well, as they can look up already answered questions or contribute to the discussion!
I’d like to thank the excellent team at Apress, who made this book possible. Specifically, I’d like to thank Candace English, Adam Heath, Matthew Moodie, Damon Larson, James Compton, Christine Ricketts, Tom Welsh, Bill McManus, and many more good souls who have worked on this book.
Thanks to Robert Green for being a great co-author and for living through the hell of early Android versions with me.
Another big thanks to my splendid team of contributors, who carried on the libgdx effort while I was busy writing this book. I’d especially like to thank my friend Nathan Sweet — the months we spent together in San Francisco and Graz were some of the most memorable times in my life; Ruben Garat and Ariel Coppes from Gemserk, who make sure we stay on target with libgdx; Justin Shapcott, who keeps cleaning up my mess in the libgdx repository; Søren Nielsen, one of the most knowledgeable artists I’ve ever had the pleasure to work with, who’s a helping hand in the libgdx IRC channel; and, finally, all the folks on the libgdx IRC channel and the forums, whom I consider to be extended family.
Last but not least, thanks to Mom and Dad for being folks to look up to, and to my love, Stefanie, who put up with all the long nights alone in bed, as well as my grumpiness. Lyrt!
—Mario Zechner
I’d like to thank Mark, Steve, Paul, and the team at Apress for making this book happen. I’d also like to thank Stacey, Katrina, and the agents and team at Studio B for handling the hard part of the process for over 15 years. Finally, I’d like to thank Christian, Sophia, Giovanni, Jennifer, Katie, Sarah, Joseph, and the rest of my family for being patient and understanding throughout.
—J.F. DiMarzio
I’d like to thank Mario for answering so many questions and helping me work through a few tricky bugs over the years. Ryan Foss is an artist and programmer I worked with to build several games and is truly an exceptional person. Scott Lembcke of Howling Moon Software helped me through some difficult programming problems. Zach Wendt, organizer of the Minneapolis IGDA, always hung out late after meetings and talked tech with me. Thanks to Dmitri Salcedo, who’s been sticking with me through a tough year and helping us to achieve more than we ever have before. Finally, I thank all of the original licensees of BatteryTech SDK, who believed in the product before anyone else. Thank you all for your support and help; I couldn’t have made it without you.
—Robert Green
Mario Zechner has been programming games since age 12. Over the years, he has created many prototypes and finished games on various platforms. He’s the creator of libgdx, an Android game development framework used by many top-grossing Android games. Before he joined his current employer, Mario was the tech-lead at a mobile gaming startup in San Francisco. Mario’s current day job involves working in the fields of information extraction and retrieval, visualization, and machine learning. You can meet Mario at conferences, where he gives talks and workshops on game programming, read his blog at www.badlogicgames.com , or follow him on Twitter @badlogicgames .
J. F. DiMarzio is a seasoned Android developer and author. He began developing games in Basic on the TRS-80 Color Computer II in 1984. Since then, he has worked in the technology departments of companies such as the U.S. Department of Defense and the Walt Disney Company. He has been developing on the Android platform since the beta release of version .03, and he has published two professional applications and one game on the Android Marketplace. DiMarzio is also an accomplished author. Over the last 15 years, he has released 12 books, including Android: A Programmer’s Guide . His books have been translated into four languages and published worldwide. DiMarzio’s writing style is very easy to read and understand, which makes the information in the topics that he presents more retainable.
Robert Green is the founder of the game studio Battery Powered Games in Portland, Oregon. He has developed over a dozen Android games, including Deadly Chambers , Antigen , Wixel , Light Racer , and Light Racer 3D . Before diving full time into mobile video game development and publishing, Robert worked for software companies in Minneapolis and Chicago, including IBM Interactive. Robert’s current focus is cross-platform game development and high-performance mobile gaming with the BatteryTech SDK. Robert often updates his personal blog with game programming tidbits at www.rbgrn.net . You can check out his professional game development software at www.batterypoweredgames.com .
Paul Trebilcox-Ruiz is a software engineer at Sphero (they make awesome robots that you can control from your phone) in Boulder, Colorado, and a computer science graduate from California State University, Fresno. His main interests lie in the Android platform, Android TV, and wearable computing. He also actively participates in hackathons in the Denver – Boulder area, writes an Android technical blog, and presents to Meetups in the area.