andEngine結構:
雙線程:業(yè)務線程、渲染線程,,使用ReentrantLock實現(xiàn)兩個線程的交替執(zhí)行,。
渲染線程:使用GLSurfaceView提供的渲染功能,內置渲染繪圖線程,。
第一步:RenderSurfaceView為繼承于GLSurfaceView的GL渲染視圖,,BaseGameActivity作為繼承于Activity的活動,通過setContentView(RenderSurfaceView)將視圖加入活動中,。
BaseGameActivity中主要方法:
- @Override
- protected void onCreate(final Bundle pSavedInstanceState) {
- if(BuildConfig.DEBUG) {
- Debug.d(this.getClass().getSimpleName() ".onCreate" " @(Thread: '" Thread.currentThread().getName() "')");
- }
-
- super.onCreate(pSavedInstanceState);
- //先處于暫停狀態(tài)
- this.mGamePaused = true;
- //通過EngineOption定義,,創(chuàng)建Engine
- this.mEngine = this.onCreateEngine(this.onCreateEngineOptions());
- //應用EngineOption定義到Activity
- this.applyEngineOptions();
- //添加RenderSurfaceView到Activity
- this.onSetContentView();
- }
-
- protected void onSetContentView() {
- this.mRenderSurfaceView = new RenderSurfaceView(this);
- this.mRenderSurfaceView.setRenderer(this.mEngine, this);
-
- this.setContentView(this.mRenderSurfaceView, BaseGameActivity.createSurfaceViewLayoutParams());
- }
第二步:將渲染器EngineRenderer的加入RenderSurfaceView中,BaseGameActivity實現(xiàn)IRendererListener接口監(jiān)聽EngineRenderer,。
EngineRenderer中的主要方法:
- @Override
- public void onSurfaceCreated(final GL10 pGL, final EGLConfig pEGLConfig) {
- synchronized(GLState.class) {
- final RenderOptions renderOptions = this.mEngine.getEngineOptions().getRenderOptions();
- this.mGLState.reset(renderOptions, this.mConfigChooser, pEGLConfig);
-
- // TODO Check if available and make available through EngineOptions-RenderOptions
- // GLES20.glEnable(GLES20.GL_POLYGON_SMOOTH);
- // GLES20.glHint(GLES20.GL_POLYGON_SMOOTH_HINT, GLES20.GL_NICEST);
- // GLES20.glEnable(GLES20.GL_LINE_SMOOTH);
- // GLES20.glHint(GLES20.GL_LINE_SMOOTH_HINT, GLES20.GL_NICEST);
- // GLES20.glEnable(GLES20.GL_POINT_SMOOTH);
- // GLES20.glHint(GLES20.GL_POINT_SMOOTH_HINT, GLES20.GL_NICEST);
-
- //2D不需要深度
- this.mGLState.disableDepthTest();
- this.mGLState.enableBlend();
- this.mGLState.setDitherEnabled(renderOptions.isDithering());
-
- /* Enabling culling doesn't really make sense, because triangles are never drawn 'backwards' on purpose. */
- // this.mGLState.enableCulling();
- // GLES20.glFrontFace(GLES20.GL_CCW);
- // GLES20.glCullFace(GLES20.GL_BACK);
-
- //回調BaseGameActivity中onSurfaceCreated
- if(this.mRendererListener != null) {
- this.mRendererListener.onSurfaceCreated(this.mGLState);
- }
- }
- }
-
- @Override
- public void onSurfaceChanged(final GL10 pGL, final int pWidth, final int pHeight) {
- //確定因為和GL的顯示區(qū)域大小
- this.mEngine.setSurfaceSize(pWidth, pHeight);
- GLES20.glViewport(0, 0, pWidth, pHeight);
- this.mGLState.loadProjectionGLMatrixIdentity();
-
- //回調BaseGameActivity中onSurfaceChanged
- if(this.mRendererListener != null) {
- this.mRendererListener.onSurfaceChanged(this.mGLState, pWidth, pHeight);
- }
- }
-
- @Override
- public void onDrawFrame(final GL10 pGL) {
- synchronized(GLState.class) {
- if (this.mMultiSampling && this.mConfigChooser.isCoverageMultiSampling()) {
- final int GL_COVERAGE_BUFFER_BIT_NV = 0x8000;
- GLES20.glClear(GL_COVERAGE_BUFFER_BIT_NV);
- }
-
- try {
- //調用引擎中的方法
- this.mEngine.onDrawFrame(this.mGLState);
- } catch (final InterruptedException e) {
- Debug.e("GLThread interrupted!", e);
- }
- }
- }
-
Engine中的主要方法:
- public void onDrawFrame(final GLState pGLState) throws InterruptedException {
- final EngineLock engineLock = this.mEngineLock;
-
- //繪制過程中為引擎加鎖,,等待繪制完成
- engineLock.lock();
- try {
- engineLock.waitUntilCanDraw();
-
- //更新頂點緩存,去除Unloaded的緩存
- this.mVertexBufferObjectManager.updateVertexBufferObjects(pGLState);
- //更新紋理,,加載紋理到硬件
- this.mTextureManager.updateTextures(pGLState);
- //更新字體,,繪制所有文字
- this.mFontManager.updateFonts(pGLState);
-
- //調用mDrawHandlers中注冊的所有需要繪制的IDrawHandler的onDraw方法
- this.onUpdateDrawHandlers(pGLState, this.mCamera);
- //逐層調用Scene中IEntity的onDraw方法
- this.onDrawScene(pGLState, this.mCamera);
-
- engineLock.notifyCanUpdate();
- } finally {
- engineLock.unlock();
- }
- }
-
業(yè)務線程:Engine的初始化函數(shù)中啟動業(yè)務執(zhí)行線程UpdateThread,為注冊到引擎中,、或注冊到引擎包含的實體中的IUpdateHandler接口執(zhí)行onUpdate()方法,,完成業(yè)務的更新操作。
UpdateThread中主要方法:
- @Override
- public void run() {
- android.os.Process.setThreadPriority(this.mEngine.getEngineOptions().getUpdateThreadPriority());
- try {
- while(true) {
- this.mRunnableHandler.onUpdate(0);
- this.mEngine.onTickUpdate();
- }
- } catch (final InterruptedException e) {
- if(BuildConfig.DEBUG) {
- Debug.d(this.getClass().getSimpleName() " interrupted. Don't worry - this " e.getClass().getSimpleName() " is most likely expected!", e);
- }
- this.interrupt();
- }
- }
Engine中的主要方法:
- void onTickUpdate() throws InterruptedException {
- //如果引擎正在運行,,更新所有IUpdateHandler,,否則Thread.sleep(16);什么也不做
- if(this.mRunning) {
- //從上傳更新已經(jīng)度過的時間(毫秒)
- final long secondsElapsed = this.getNanosecondsElapsed();
-
- this.mEngineLock.lock();
- try {
- //如果引擎已經(jīng)被摧毀,,拋出EngineDestroyedException異常
- this.throwOnDestroyed();
-
- //更新業(yè)務
- this.onUpdate(secondsElapsed);
-
- //如果引擎已經(jīng)被摧毀,拋出EngineDestroyedException異常
- this.throwOnDestroyed();
-
- this.mEngineLock.notifyCanDraw();
- this.mEngineLock.waitUntilCanUpdate();
- } finally {
- this.mEngineLock.unlock();
- }
- } else {
- this.mEngineLock.lock();
- try {
- this.throwOnDestroyed();
-
- this.mEngineLock.notifyCanDraw();
- this.mEngineLock.waitUntilCanUpdate();
- } finally {
- this.mEngineLock.unlock();
- }
-
- Thread.sleep(16);
- }
- }
-
- public void onUpdate(final long pNanosecondsElapsed) throws InterruptedException {
- final float pSecondsElapsed = pNanosecondsElapsed * TimeConstants.SECONDS_PER_NANOSECOND;
-
- this.mSecondsElapsedTotal = pSecondsElapsed;
- this.mLastTick = pNanosecondsElapsed;
-
- //更新觸摸事件控制
- this.mTouchController.onUpdate(pSecondsElapsed);
- //更新注冊的IUpdateHandler
- this.onUpdateUpdateHandlers(pSecondsElapsed);
- //更新屏幕
- this.onUpdateScene(pSecondsElapsed);
- }
Entity中的主要方法:
- @Override
- public final void onUpdate(final float pSecondsElapsed) {
- if(!this.mIgnoreUpdate) {
- this.onManagedUpdate(pSecondsElapsed);
- }
- }
-
- /**
- * 對實體的onUpdate方法的實際處理函數(shù)
- *
- * @param pSecondsElapsed 間隔秒數(shù)
- */
- protected void onManagedUpdate(final float pSecondsElapsed) {
- //本身注冊的實體修改器的onUpdate
- if(this.mEntityModifiers != null) {
- this.mEntityModifiers.onUpdate(pSecondsElapsed);
- }
- //本身注冊IUpdateHandler對象的onUpdate
- if(this.mUpdateHandlers != null) {
- this.mUpdateHandlers.onUpdate(pSecondsElapsed);
- }
-
- //調用子實體的onUpdate方法
- if((this.mChildren != null) && !this.mChildrenIgnoreUpdate) {
- final SmartList<IEntity> entities = this.mChildren;
- final int entityCount = entities.size();
- for(int i = 0; i < entityCount; i ) {
- entities.get(i).onUpdate(pSecondsElapsed);
- }
- }
- }
andEngine繼承關系:
|