AndEngine Learning (II): How the AndEngine engine runs

Source: Internet
Author: User

By analyzing the logic principles of general games and reading the AndEngine source code, we have a certain understanding of the general architecture of the Games and how AndEngine implements the principles of this architecture, summary!

General game main logic: (in the main thread of the game)

[Java] <span style = "font-size: 16px;"> while (true)
{
User interaction listener (user input)
Logical operation
Draw Screen
} </Span>
<Span style = "font-size: 16px;"> while (true)
{
User interaction listener (user input)
Logical operation
Draw Screen
} </Span>
If you write it in a simple way, there will be a very serious problem, that is, the effects of running the game on machines with different configurations are different, because the thread is continuously running, different CPUs affect logical operations, and different GPUs affect the rendering efficiency. Therefore, on machines with different configurations, the number of times of running cyclically is different. Assume that an genie will go through 10 Px in one frame, and 10 or 100 frames will be run on different machines per second, then the genie will appear on different machines, and the moving distance in the same time is different! This is very serious! Therefore, the main logic has the following improvements,


[Java] <span style = "font-size: 16px;"> while (true)
{
StartTime = currentTime;
User input listener
Logical operation
EndTime = currentTime;

DeltaTime = endTime-startTime;
If (deltaTime <FRAME_LENGTH_PER_SECOND)
{
Sleep (deltaTime );
}

Draw Screen
} </Span>
<Span style = "font-size: 16px;"> while (true)
{
StartTime = currentTime;
User input listener
Logical operation
EndTime = currentTime;

DeltaTime = endTime-startTime;
If (deltaTime <FRAME_LENGTH_PER_SECOND)
{
Sleep (deltaTime );
}

Draw Screen
} </Span>
Note that currentTime is the current time of the retrieved system. deltaTime is the time used for user input listening and logical operation. The value FRAME_LENGTH_PER_SECOND is very important, this is the time we want to use each frame (of course, in milliseconds, that is, the number of frames per second/we want the game to run on different machines per second, for example, if we want the game to be kept within 30 frames on all machines, FRAME_LENGTH_PER_SECOND = 1000/30 = 33 ms (ms). Of course, we think that the screen rendering process is fast, if dual buffering is used, the content to be drawn in the logic operation is drawn to the buffer zone, and the screen drawing process is equivalent to a copy of the buffer zone to the screen, and the process will be very fast!

Shuai's separation line www.2cto.com ============================================ ========================================================== ==============================

Next we will introduce how AndEngine implements the dead loop of the main thread!

There is an internal class UpdateThread class in the Engine class,


[Java] <span style = "font-size: 16px;"> private class UpdateThread extends Thread
{
// ================================================ ==================================
// Constants
// ================================================ ==================================
 
// ================================================ ==================================
// Fields
// ================================================ ==================================
 
// ================================================ ==================================
// Constructors
// ================================================ ==================================
 
Public UpdateThread ()
{
Super (UpdateThread. class. getSimpleName ());
}
 
// ================================================ ==================================
// Getter & Setter
// ================================================ ==================================
 
// ================================================ ==================================
// Methods for/from SuperClass/Interfaces
// ================================================ ==================================
 
@ Override
Public void run ()
{
Android. OS. Process. setThreadPriority (Engine. this. mEngineOptions. getUpdateThreadPriority ());
Try
{
While (true)
{
Engine. this. onTickUpdate ();
}
}
Catch (final InterruptedException e)
{
Debug. d (this. getClass (). getSimpleName () + "interrupted. Don't worry-this"
+ E. getClass (). getSimpleName () + "is most likely expected! ", E );
This. interrupt ();
}
}
 
// ================================================ ==================================
// Methods
// ================================================ ==================================
 
// ================================================ ==================================
// Inner and Anonymous Classes
// ================================================ ==================================
} </Span>
<Span style = "font-size: 16px;"> private class UpdateThread extends Thread
{
// ================================================ ==================================
// Constants
// ================================================ ==================================

// ================================================ ==================================
// Fields
// ================================================ ==================================

// ================================================ ==================================
// Constructors
// ================================================ ==================================

Public UpdateThread ()
{
Super (UpdateThread. class. getSimpleName ());
}

// ================================================ ==================================
// Getter & Setter
// ================================================ ==================================

// ================================================ ==================================
// Methods for/from SuperClass/Interfaces
// ================================================ ==================================

@ Override
Public void run ()
{
Android. OS. Process. setThreadPriority (Engine. this. mEngineOptions. getUpdateThreadPriority ());
Try
{
While (true)
{
Engine. this. onTickUpdate ();
}
}
Catch (final InterruptedException e)
{
Debug. d (this. getClass (). getSimpleName () + "interrupted. Don't worry-this"
+ E. getClass (). getSimpleName () + "is most likely expected! ", E );
This. interrupt ();
}
}

// ================================================ ==================================
// Methods
// ================================================ ==================================

// ================================================ ==================================
// Inner and Anonymous Classes
// ================================================ ==================================
} </Span> now, in its run () method, it is the main loop of the game, the main thread is started in the Engine Constructor (you can check the Code)

The problem arises. This is only the implementation of our general game engine, which has different configuration machine running efficiency issues. Of course, we can rely on TimerHandler for approximate control, but it is not reliable, so what should we do if we want the engine to control the number of frames? In fact, AndEngine has already been implemented for us, that is, the LimitedFPSEngine class. From the name, the function of this class is the same as the number of frames we want to control! The following is the onUpdate method in the LimitedFPSEngine class. The source code has carefully explained the principle of controlling the number of frames,


[Java] <span style = "font-size: 16px;"> @ Override
Public void onUpdate (final long pNanosecondsElapsed) throws InterruptedException
{
Final long preferredFrameLengthNanoseconds = this. mPreferredFrameLengthNanoseconds;
Final long deltaFrameLengthNanoseconds = preferredFrameLengthNanoseconds-pNanosecondsElapsed;
 
If (deltaFrameLengthNanoseconds <= 0)
{
Super. onUpdate (pNanosecondsElapsed );
}
Else
{
Final int sleepTimeMilliseconds = (int) (deltaFrameLengthNanoseconds/NANOSECONDS_PER_MILLISECOND );
 
Thread. sleep (sleepTimeMilliseconds );
Super. onUpdate (pNanosecondsElapsed + deltaFrameLengthNanoseconds );
}
} </Span>
<Span style = "font-size: 16px;"> @ Override
Public void onUpdate (final long pNanosecondsElapsed) throws InterruptedException
{
Final long preferredFrameLengthNanoseconds = this. mPreferredFrameLengthNanoseconds;
Final long deltaFrameLengthNanoseconds = preferredFrameLengthNanoseconds-pNanosecondsElapsed;

If (deltaFrameLengthNanoseconds <= 0)
{
Super. onUpdate (pNanosecondsElapsed );
}
Else
{
Final int sleepTimeMilliseconds = (int) (deltaFrameLengthNanoseconds/NANOSECONDS_PER_MILLISECOND );

Thread. sleep (sleepTimeMilliseconds );
Super. onUpdate (pNanosecondsElapsed + deltaFrameLengthNanoseconds );
}
} </Span> LimitedFPSEngine is a subclass of Engine. In its onUpdate method, it also calls the onUpdate method of the parent class (Engine, the onupdate method of the Engine performs some operations required for each frame (the update of Scene is recursive)

 


This article mainly records the running principle of AndEngine. The next article will record some bonding points between AndEngine and our Android games.

It is not a great god, and it is inevitable that there will be omissions. If you think there is something wrong with my summary, please make a picture! 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.