AndEngine Learning (III): bonding between AndEngine and Android

Source: Internet
Author: User

AndEngine provides an easy-to-use interface for Android!

As we all know, almost every Android app or game will have Activity (except for some services or Reciver, of course ...), As a game, Activity is even more essential. The reason is very simple: the game needs to interact with users, and the game needs to present pictures. All these require Activity support...

Look at the package structure of AndEngine. In/ui/activity/, we will find BaseActivity, BaseGameActivity, and SimpleBaseGameActivity...

These are the Activity encapsulation for us in AndEngine. The inheritance relationship is

BaseActivity-> BaseGameActivity-> SimpleBaseGameActivity

Of course, the Child classes inherited from BaseGameActivity are not only SimpleBaseGameActivity! Looking at AndEngineExamples (an example officially provided for us), we will find that almost all examples are inherited from SimpleBaseGameActivity.

 


In BaseActivity, only some methods for Asynchronously loading resources are defined, and engine-related processing is not performed.

So, let's look at BaseGameActivity. We can find that BaseGameActivity is the most basic class to use AndEngine in Android! There are two important points: Engine and Render. This is the most important part. Let's take a look at it separately!

Engine construction:

Of course, it is in the onCreate () of the Activity lifecycle method, that is, when we inherit the BaseGameActivity or its subclass, whether we override or not override its onCreate (), ps: if it is rewritten, super is called. onCreate () will call the onCreate () method of BaseGameActivity, which provides us with a mechanism to construct the Engine. Engine Construction


[Java] this. mEngine = this. onCreateEngine (this. mEngineOptions );
This. mEngine = this. onCreateEngine (this. mEngineOptions); by calling onCreateEngine (), if you try to use AndEngine to write some small demos, you will know clearly that it inherits from BaseGameActivity or its subclass, all three methods must be implemented. onCreateEngine () is one of them (of course, it may also be onCreateEngineOptions (). If we use the default Engine instead of other engines, you just need to configure the engine parameters ).

Use of Render:

Another important part is the game's rendering! AndEngine uses OpenGL to call the OpenGL part of Android. The basic usage of OpenGL in Android is as follows:

1. to construct a class implemented in GLSurfaceView, you must have a custom Render class (see figure 2) for filling in the Activity (using the setContentView () method, ps: GLSurfaceView is also a subclass of View)

2. Customize a class implemented in GLSurfaceView. Render to provide rendering operations.

View the last line of the onCreate () method in BaseGameActivity source code.

[Java] this. onSetContentView ();
This. onSetContentView (); let's take a look at the implementation of onSetContentView ().


[Java] protected void onSetContentView ()
{
This. mRenderSurfaceView = new RenderSurfaceView (this );
This. mRenderSurfaceView. setRenderer (this. mEngine, this );
 
This. setContentView (this. mRenderSurfaceView, BaseGameActivity. createSurfaceViewLayoutParams ());
}
Protected void onSetContentView ()
{
This. mRenderSurfaceView = new RenderSurfaceView (this );
This. mRenderSurfaceView. setRenderer (this. mEngine, this );

This. setContentView (this. mRenderSurfaceView, BaseGameActivity. createSurfaceViewLayoutParams ());
} Haha, it's the same as our analysis!

 


After analyzing the two most important parts of the BaseGameActivity, you will understand why we only need to inherit from the BaseGameActivity or its subclass, so we can use the Engine and draw!

 


Now, let's summarize it here today! In the future, I will enrich the engine updates and drawing principles!

Good morning, everyone. Thank you! (Your shoot is the starting point for me! Thank you !)


From the oneRain88 Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.