Book Image

Mastering AndEngine Game Development

By : Maya Posch
Book Image

Mastering AndEngine Game Development

By: Maya Posch

Overview of this book

AndEngine is a popular and easy-to-use game framework, best suited for Android game development. After learning the basics of creating an Android game using AndEngine it's time you move beyond the basics to explore further. For this you need to understand the theory behind many of the technologies AndEngine uses. This book aims to provide all the skills and tools you need to learn more about Android game development using AndEngine. With this book you will get a quick overview of the basics of AndEngine and Android application development. From there, you will learn how to use 3D models in a 2D scene, render a visual representation of a scene's objects, and create interaction between these objects. You will explore frame-based animations and learn to use skeletal animations. As the book progresses, you will be guided through exploring all the relevant aspects of rendering graphics with OpenGL ES, generating audio using OpenSL ES and OpenAL, making the best use of Android's network API, implementing anti-aliasing algorithms, shaders, dynamic lighting and much more. With all this, you will be ready to enhance the look and feel of your game with its user interface, sound effects and background music. After an in-depth study of 2D and 3D worlds and multi-player implementations, you will be a master in AndEngine and Android game development.
Table of Contents (21 chapters)
Mastering AndEngine Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the scene


To finish this basic application, we will be implementing a simple scene that merely creates a single sprite. For this, we will use the Scene object created in the code shown earlier. This object forms the root of the scene tree to which all the objects, such as sprites in the scene, attach. Before we can create the sprite, we need to set up the bitmap logic to load and handle textures. As we know, a sprite is more or less just a bitmap image. To display it, we first fetch the required image from the application's resources. Probably, you have already preloaded these in the onCreateResources() function, a procedure that, in general, is a very good idea for reducing load times later on in the application in order to prevent slowdowns.

An efficient way of using textures with OpenGL ES is to have only a single texture loaded at any given time. Since any given scene is likely to have more than one texture, we have to merge our textures into a single large texture. Here, we have two options:

  • Texture atlas

  • Texture packer

The texture atlas is the traditional approach, whereby you create a single, blank texture and place smaller textures on it. The texture atlas must have dimensions of powers of 2, and is limited in size by the memory buffer of the graphics processor of the device. Generally, sticking to an upper limit of 1,024 for either dimension is advisable.

The texture packer is a relatively new approach. It uses an external application to create a texture atlas, which is then output together with a XML file that maps the textures on the atlas. The advantages of this method are less code in the resource loading section and a more visual approach to the atlas creation process. The TexturePack APIs are then used within the AndEngine application to load the XML file. It is important to note here that starting with the GLES2-AnchorCenter branch of AndEngine, the TexturePack API extension (AndEngineTexturePackerExtension) is integrated, and thus available by default.

To access the individual textures in the texture atlas, we create texture region instances, which are a mapping of a specific region of the said atlas. Once all is said and done, these texture regions are essentially our textures, as we use them within a scene. After the loading stage of the resources and the creation of the atlas and texture regions, we can pretty much ignore these details and use the texture regions without considering the implementation.

For the sample application that we are developing in this chapter, we will use the former approach of creating the texture atlas in code, as we have only a single sprite to display. Briefly, we first create the atlas:

this.mFaceTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "gfx/helloworld.png");
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(
                  this.mFaceTexture);

This code loads a PNG file called helloworld.png from the assets folder of the Android application into a texture that we use as the texture atlas. Next, we create a texture region out of it, as we'll need this to actually reference the texture. Then we load the texture atlas into the memory buffer of the video processor:

this.mFaceTexture.load();

Leaving the onCreateResources() function, we move on to onPopulateScene(), in which we basically add sprites to the scene, one sprite in this particular case. After setting the background color of the Scene object to a nice shade of gray using the RGB values in floating-point format (three times 0.8f), we determine the center of the camera's view:

final float centerX = mCameraWidth / 2;
final float centerY = mCameraHeight / 2;

Finally, we create the sprite using the texture region we created before, and add it to the scene:

final Sprite sprite = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
pScene.attachChild(sprite);

When we run the application, we see the following appear on the screen:

That's it! We're now ready to build and launch the application. You should look at the sample project (AndEngineOnTour) for this chapter and try to get it running. We will be building upon it in the coming chapters.