Android

 

Embed or link this publication

Popular Pages


p. 1

488 chapter 9 super jumper a 2d opengl es game get started with game apps development for the android platform android games mario zechner beginning

[close]

p. 2

488 chapter 9 super jumper a 2d opengl es game for your convenience apress has placed some of the front matter material after the index please use the bookmarks and contents at a glance links to access them.

[close]

p. 3

contents at a glance contents v about the author xii about the technical reviewer xiii acknowledgments xiv introduction xv chapter 1 android the new kid on the block 1 chapter 2 first steps with the android sdk 25 chapter 3 game development 101 51 chapter 4 android for game developers 103 chapter 5 an android game development framework 185 chapter 6 mr nom invades android 229 chapter 7 opengl es a gentle introduction 269 chapter 8 2d game programming tricks 351 chapter 9 super jumper a 2d opengl es game 429 chapter 10 opengl es going 3d 489 chapter 11 3d programming tricks 525 chapter 12 droid invaders the grand finale 577 chapter 13 publishing your game 625 chapter 14 what s next 637 index 641 iv

[close]

p. 4

introduction hi there and welcome to the world of android game development my name is mario i ll be your guide for the next fourteen chapters you came here to learn about game development on android and i hope to be the person who enables 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 and a scary thing called opengl es based on all this knowledge we ll develop three different games one even being 3d game programming can be easy if you know what you re doing therefore i ve tried to present the material in a way that not only gives you helpful code snippets to reuse but 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 a word about the target audience this book is aimed first and foremost at complete beginners in game programming you don t need any prior knowledge on the subject matter i ll walk you through all the basics however i need to assume a little knowledge on your end about java if you feel rusty on the matter i d suggest refreshing your memory by reading the online edition of 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 the intermediate-level game programmer that wants to get her 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 that should make reading this book worthwhile android is a strange beast at times and this book should be considered your battle guide how this book is organized 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 chapters we ll build up a reusable code base so i d suggest going through the chapters in sequence more experienced readers can of course skip certain sections they feel confident with just make sure to read through the code listings of sections you skim over a little so you will understand how the classes and interfaces are used in subsequent more advanced sections xv

[close]

p. 5

introduction getting the source code 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 also the process of copying code from the book s text to eclipse can introduce errors robert the book s technical reviewer and i 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 i created a google code project that offers you the following · · · the complete source code and assets licensed under the gpl version 3 available from the project s subversion repository a quickstart guide showing you how to import the projects into eclipse in textual form and a video demonstration for the same an issue tracker that allows you to report any errors you find either in the book itself or in the code accompanying the book once you file an issue in the issue tracker i can incorporate any fixes in the subversion repository this way you ll always have an up-to-date hopefully error-free version of this book s code from which other readers can benefit as well a discussion group that is free for everybody to join and discuss the contents of the book i ll be on there as well of course · for each chapter that contains code there s an equivalent eclipse project in the subversion repository the projects do not depend on each other as we ll iteratively improve some of the framework classes over the course of the book each project therefore stands on its own the code for both chapters 5 and 6 is contained in the ch06-mrnom project the google code project can be found at http code.google.com/p/beginning-androidgames xvi

[close]

p. 6

1 chapter 1 android the new kid on the block as a kid of the early nineties i naturally grew up with my trusty nintendo game boy i spent countless hours helping mario rescue the princess getting the highest score in tetris and racing my friends in rc pro-am via link cable i took this awesome piece of hardware with me everywhere and every time i could my passion for games made me want to create my own worlds and share them with my friends i started programming on the pc but soon found out that i couldn t transfer my little masterpieces to the game boy i continued being an enthusiastic programmer but over time my interest in actually playing video games faded also my game boy broke fast forward to 2010 smartphones are becoming the new mobile gaming platforms of the era competing with classic dedicated handheld systems such as the nintendo ds or the playstation portable that caught my interest again and i started investigating which mobile platforms would be suitable for my development needs apple s ios seemed like a good candidate to start coding games for however i quickly realized that the system was not open that i d be able to share my work with others only if apple allowed it and that i d need a mac to develop for the ios and then i found android i immediately fell in love with android its development environment works on all the major platforms no strings attached it has a vibrant developer community happy to help you with any problem you encounter as well as comprehensive documentation i can share my games with anyone without having to pay a fee to do so and if i want to monetize my work i can easily publish my latest and greatest innovation to a global market with millions of users in a matter of minutes the only thing i was left with was actually figuring out how to write games for android and how to transfer my pc game development knowledge to this new system in the following chapters i want to share my experience with you and get you started with android game development this is of course a rather selfish plan i want to have more games to play on the go let s start by getting to know our new friend android 1

[close]

p. 7

2 chapter 1 android the new kid on the block a brief history of android android was first publicly noticed in 2005 when google acquired a small startup called android inc this fueled speculation that google wanted to enter the mobile space in 2008 the release of version 1.0 of android put an end to all speculation and android became the new challenger on the mobile market since then it s been battling it out with already established platforms such as ios then called iphone os and blackberry and its chances of winning look rather good because android is open source handset manufacturers have a low barrier of entry when using the new platform they can produce devices for all price segments modifying android itself to accommodate the processing power of a specific device android is therefore not limited to high-end devices but can also be deployed to lowbudget devices thus reaching a wider audience a crucial ingredient for android s success was the formation of the open handset alliance oha in late 2007 the oha includes companies such as htc qualcomm motorola and nvidia which collaborate to develop open standards for mobile devices although android s core is developed mainly by google all the oha members contribute to its source in one form or another android itself is a mobile operating system and platform based on the linux kernel version 2.6 and is freely available for commercial and noncommercial use many members of the oha build custom versions of android for their devices with modified user interfaces uis for example htc s htc sense and motorola s motoblur the open source nature of android also enables hobbyists to create and distribute their own versions of android these are usually called mods firmwares or roms the most prominent rom at the time of this writing was developed by a fellow known as cyanogen and is aimed at bringing the latest and greatest improvements to all sorts of android devices since its release in 2008 android has received seven version updates all code-named after desserts with the exception of android 1.1 which is irrelevant nowadays each version has added new functionality to the android platform that has relevance in one way or another for game developers version 1.5 cupcake added support for including native libraries in android applications which were previously restricted to being written in pure java native code can be very beneficial in situations where performance is of upmost concern version 1.6 donut introduced support for different screen resolutions we will revisit this fact a couple of times in this book because it has some impact on how we approach writing games for android with version 2.0 Éclair came support for multi-touch screens and version 2.2 froyo added just-in-time jit compilation to the dalvik virtual machine vm which powers all the java applications on android the jit speeds up the execution of android applications considerably depending on the scenario up to a factor of five at the time of this writing the latest version is 2.3 called gingerbread it adds a new concurrent garbage collector to the dalvik vm if you haven t noticed yet android applications are written in java a special version of android targeted at tablets is also being released in 2011 it is called honeycomb and represents version 3.0 of android honeycomb is not meant to

[close]

p. 8

chapter 1 android the new kid on the block 3 run on phones at this point however some features of honeycomb will be ported to the main line of android at the time of this writing android 3.0 is not available to the public and no devices on the market are running it android 2.3 can be installed on many devices using custom roms the only handset using gingerbread is the nexus s a developer phone sold by google directly fragmentation the great flexibility of android comes at a price companies that opt to develop their own user interfaces have to play catch-up with the fast pace at which new versions of android are released this can lead to handsets not older than a few months becoming outdated really fast as carriers and handset manufacturers refuse to create updates that incorporate the improvements of new android versions the big bogeyman called fragmentation is a result of this process fragmentation has many faces for the end user it means being unable to install and use certain applications and features because of being stuck on an old android version for developers it means that some care has to be taken when creating applications that should work on all versions of android while applications written for earlier versions of android will usually run fine on newer versions the reverse is not true some features added in newer android versions are of course not available on older versions such as multi-touch support developers are thus forced to create separate code paths for different versions of android but fear not although this sounds terrifying it turns out that the measures that have to be taken are minimal most often you can even completely forget about the whole issue and pretend there s only a single version of android as game developers we re less concerned with differences in apis and more concerned about hardware capabilities this is a different form of fragmentation which is also a problem for platforms such as ios albeit not as pronounced throughout this book i will cover the relevant fragmentation issues that might get in your way while you develop your next game for android the role of google although android is officially the brainchild of the open handset alliance google is the clear leader when it comes to implementing android itself as well as providing the necessary ecosystem for android to grow the android open source project google s efforts are summarized under the name android open source project most of the code is licensed under apache license 2 a very open and nonrestrictive license compared to other open source licenses such as the gnu general public license gpl everyone is free to use this source code to build their own systems however systems that are claimed to be android compatible first have to pass the android compatibility

[close]

p. 9

4 chapter 1 android the new kid on the block program a process ensuring baseline compatibility with third-party applications written by developers like us compatible systems are allowed to participate in the android ecosystem which also includes the android market the android market the android market was opened to the public in october 2008 by google it s an online software store that enables users to find and install third-party applications the market is generally accessible only through the market application on a device this situation will change in the near future according to google which promises the deployment of a desktop-based online store accessible via the browser the market allows third-party developers to publish their applications either for free or as paid applications paid applications are available for purchase in only about 30 countries selling applications as a developer is possible in a slightly smaller number table 1­1 shows you the countries in which apps can be bought and sold table 1­1 purchase and selling options per country country australia austria belgium brazil canada czech republic denmark finland france germany hong kong hungary india ireland user can purchase apps yes yes yes yes yes yes yes yes yes yes yes yes yes yes developer can sell apps yes yes yes yes yes no yes yes yes yes yes yes yes yes

[close]

p. 10

chapter 1 android the new kid on the block 5 country israel italy japan mexico netherlands new zealand norway pakistan poland portugal russia singapore south korea spain sweden switzerland taiwan united kingdom united states user can purchase apps yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes developer can sell apps yes yes yes yes yes yes yes no no yes yes yes yes yes yes yes yes yes yes users get access to the market after setting up a google account applications can be bought only via credit card at the moment buyers can decide to return an application within 15 minutes from the time of purchasing it and will receive a full refund previously the refund time window was 24 hours the recent change to 15 minutes has not been well received by end users developers need to register an android developer account with google for a one-time fee of $25 in order to be able to publish applications on the market after successful

[close]

p. 11

6 chapter 1 android the new kid on the block registration a developer can immediately start to publish a new application in a matter of minutes the android market has no approval process but relies on a permission system a user is presented with a set of permissions needed by an application before the installation of the program these permissions handle access to phone services networking access access to the secure digital sd card and so on only after a user has approved these permissions is the application installed the system relies on the user doing the right thing on the pc especially on windows systems this concept didn t work out too well on android it seems to have worked so far only a few of applications have been pulled from the market because of malicious behavior to sell applications a developer has to additionally register a google checkout merchant account which is free of charge all financial business is handled through this account challenges device seeding and google i/o in an ongoing effort to draw more developers to the android platform google started to hold challenges the first challenge called the android developer challenge adc was launched in 2008 offering relatively high cash prices for the winning projects the adc was carried out in the subsequent year and was again a huge success in terms of developer participation there was no adc in 2010 which can probably be attributed to android now having a considerable developer base and thus not needing any further actions to get new developers on board google also started a device-seeding program in early 2010 each developer who had one or more applications on the market with more than 5,000 downloads and an average user rating of 3.5 stars or above received a brand new motorola droid motorola milestone or nexus one phone this was a very well-received action within the developer community although it was initially met with disbelief many considered the email notifications that came out of the blue to be an elaborate hoax fortunately the promotion turned out to be a reality and thousands of devices were sent to developers across the planet a great move by google to keep its third-party developers happy and make them stick with the platform and to potentially attract new developers google also provides the special android dev phone adp for developers the first adp was a version of the t-mobile g1 also known as htc dream the next iteration called adp 2 was a variation of the htc magic google also released its own phone in the form of the nexus one available to end users although initially not released as an adp it was considered by many as the successor to the adp 2 google eventually stopped selling the nexus one to end users and it is now available for shipment only to partners and developers at the end of 2010 the latest adp was released this samsung device running android 2.3 gingerbread is called the nexus s adps can be bought via the android market which requires you to have a developer account the nexus s can be bought via a separate google site at www.google.com/phone.

[close]

p. 12

chapter 1 android the new kid on the block 7 the annual google i/o conference is an event every android developer looks forward to each year at google i/o the latest and greatest google technologies and projects are revealed among which android has gained a special place in recent years google i/o usually features multiple sessions on android-related topics which are also available as videos on youtube s google developers channel android s features and architecture android is not just another linux distribution for mobile devices while you develop for android you re not all that likely to meet the linux kernel itself the developer-facing side of android is a platform that abstracts away the underlying linux kernel and is programmed via java from a high-level view android possesses several nice features an application framework providing a rich set of apis to create various types of applications it also allows the reuse and replacement of components provided by the platform and third-party applications the dalvik virtual machine which is responsible for running applications on android a set of graphics libraries for 2d and 3d programming media support for common audio video and image formats such as ogg vorbis mp3 mpeg-4 h.264 and png there s even a specialized api for playing back sound effects which will come in handy in our game development adventures apis for accessing peripherals such as the camera global positioning system gps compass accelerometer touch screen trackball and keyboard note that not all android devices have all of these peripherals hardware fragmentation in action there s of course a lot more to android than the few features i just mentioned for our game development needs these features are the most relevant though android s architecture is composed of a stack of components and each component builds on the components in the layer below it figure 1­1 gives an overview of android s major components.

[close]

p. 13

8 chapter 1 android the new kid on the block figure 1­1 android architecture overview the kernel starting from the bottom of the stack you can see that the linux kernel provides the basic drivers for the hardware components additionally the kernel is responsible for such mundane things as memory and process management networking and so on the runtime and dalvik the android runtime is built on top of the kernel and is responsible for spawning and running android applications each android application is run in its own process with its own dalvik virtual machine dalvik runs programs in the dex bytecode format usually you transform common java .class files to the dex format via a special tool called dx that is provided by the software development kit the dex format is designed to have a smaller memory footprint compared to classic java .class files this is achieved by heavy compression tables and merging of multiple .class files the dalvik virtual machine interfaces with the core libraries which provide the basic functionality exposed to java programs the core libraries provide some but not all of

[close]

p. 14

chapter 1 android the new kid on the block 9 the classes available in java se through the use of a subset of the apache harmony java implementation this also means that there s no swing or abstract window toolkit awt available nor any classes that can be found in java me however with some care you can still use many of the third-party libraries available for java se on dalvik before android 2.2 froyo all bytecode was interpreted froyo introduces a tracing jit compiler which compiles parts of the bytecode to machine code on the fly this increases the performance of computationally intensive applications considerably the jit compiler can use cpu features specifically tailored for special computations such as a dedicated floating point unit fpu dalvik also has an integrated garbage collector gc it s a mark-and-sweep nongenerational gc that has the tendency to drive developers a tad bit mad at times with some attention to details you can peacefully coexist with the gc in your day-today game development though the latest android release 2.3 has an improved concurrent gc which relieves some of the pain we ll investigate gc issues in more detail later in the book each application running in an instance of the dalvik vm has a total of 16mb to 24mb of heap memory available we ll have to keep that in mind as we juggle our image and audio resources system libraries besides the core libraries which provide some java se functionality there s also a set of native c/c libraries that build the basis for the application framework located in the next layer of figure 1­1 these system libraries are mostly responsible for the computationally heavy tasks such as graphics rendering audio playback and database access which would not be so well suited for the dalvik virtual machine the apis are wrapped via java classes in the application framework which we ll exploit when we start writing our games we ll abuse the following libraries in one form or another skia graphics library skia this software renderer for 2d graphics is used for rendering the ui of android applications we ll use it to draw our first 2d game opengl for embedded systems opengl es this is the industry standard for hardware-accelerated graphics rendering opengl es 1.0 and 1.1 are exposed in java on all versions of android opengl es 2.0 which brings shaders to the table is supported from only android 2.2 froyo onward it should be mentioned that the java bindings for opengl es 2.0 are incomplete and lack a few vital methods also the emulator and most of the older devices that still make up a considerable share of the market do not support opengl es 2.0 we ll be concerned with opengl es 1.0 and 1.1 to stay compatible as much as possible.

[close]

p. 15

10 chapter 1 android the new kid on the block opencore this is a media playback and recording library for audio and video it supports a good mix of formats such as ogg vorbis mp3 h.264 mpeg-4 and so on we ll be mostly concerned with the audio portion which is not directly exposed to the java side but wrapped in a couple of classes and services freetype this is a library to load and render bitmap and vector fonts most notably the truetype format freetype supports the unicode standard including right-to-left glyph rendering for arabic and similar peculiarities sadly this is not entirely true for the java side which to this point does not support arabic typography as with opencore freetype is not directly exposed to the java side but is wrapped in a couple of convenient classes these system libraries cover a lot of ground for game developers and perform most of the heavy lifting for us they are the reason why we can write our games in plain old java note although the capabilities of dalvik are usually more than sufficient for our purposes at times you might need more performance this can be the case for very complex physics simulations or heavy 3d calculations for which we would usually resort to writing native code i do not cover this aspect in this book a couple of open source libraries for android already exist that can help you stay on the java side of things see http code.google.com/p/libgdx for an example also worth noting is the excellent book pro android games by vladimir silva apress 2009 which goes into depth about interfacing with native code in the context of game programming the application framework the application framework ties together the system libraries and the runtime creating the user side of android the framework manages applications and provides an elaborate framework within which applications operate developers create applications for this framework via a set of java apis that cover such areas as ui programming background services notifications resource management peripheral access and so on all core applications provided out of the box by android such as the mail client are written with these apis applications whether they are uis or background services can communicate their capabilities to other applications this communication enables an application to reuse components of other applications a simple example is an application that needs to take a photo and then perform some operations on it the application queries the system for a component of another application that provides this service the first application can then reuse the component for example a built-in camera application or photo gallery this significantly lowers the burden on programmers and also enables you to customize a plethora of aspects of android s behavior.

[close]

Comments

no comments yet

YOUBLISHER
About
What Others Say
Sitemap
Impressum

PUBLISHERS
Login
Signup
Tutorials
FAQ
Support

BUSINESS
Overview
Advertising
Support

DEVELOPERS
API

LEGAL
Report a Copyright Violation
Copyright FAQ
Terms of Use
Privacy Policy