Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1495039
  • 博文数量: 218
  • 博客积分: 6394
  • 博客等级: 准将
  • 技术积分: 2563
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-08 15:33
个人简介

持之以恒

文章分类

全部博文(218)

文章存档

2013年(8)

2012年(2)

2011年(21)

2010年(55)

2009年(116)

2008年(16)

分类: 嵌入式

2011-11-18 11:56:08

简要分析一下代码:
两个线程:
1.主线程(Main Looper)
   处理Activity的生命周期(onCreate,onResume,onPause)和获取用户的输入(Sensor,TouchEvent)
2.GLSurfaceView线程
   Renderer接口中注册的回调方法,显示界面,处理用户的输入
两个线程需要同步GameState对象和相应的用户输入的内存池

  1. package com.badlogic.androidgames.framework.impl;

  2. import javax.microedition.khronos.egl.EGLConfig;
  3. import javax.microedition.khronos.opengles.GL10;

  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.opengl.GLSurfaceView;
  7. import android.opengl.GLSurfaceView.Renderer;
  8. import android.os.Bundle;
  9. import android.os.PowerManager;
  10. import android.os.PowerManager.WakeLock;
  11. import android.view.Window;
  12. import android.view.WindowManager;

  13. import com.badlogic.androidgames.framework.Audio;
  14. import com.badlogic.androidgames.framework.FileIO;
  15. import com.badlogic.androidgames.framework.Game;
  16. import com.badlogic.androidgames.framework.Graphics;
  17. import com.badlogic.androidgames.framework.Input;
  18. import com.badlogic.androidgames.framework.Screen;

  19. public abstract class GLGame extends Activity implements Game, Renderer {
  20.     enum GLGameState {
  21.         Initialized,
  22.         Running,
  23.         Paused,
  24.         Finished,
  25.         Idle
  26.     }
  27.     
  28.     GLSurfaceView glView;
  29.     GLGraphics glGraphics;
  30.     Audio audio;
  31.     Input input;
  32.     FileIO fileIO;
  33.     Screen screen;
  34.     GLGameState state = GLGameState.Initialized;
  35.     Object stateChanged = new Object();//同步对象锁
  36.     long startTime = System.nanoTime();
  37.     WakeLock wakeLock;
  38.     public void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  41.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  42.                              WindowManager.LayoutParams.FLAG_FULLSCREEN);
  43.         glView = new GLSurfaceView(this);
  44.         glView.setRenderer(this);//设置GLSurfaceView对应的回调接口
  45.         setContentView(glView);
  46.         
  47.         glGraphics = new GLGraphics(glView);
  48.         fileIO = new AndroidFileIO(getAssets());
  49.         audio = new AndroidAudio(this);
  50.         input = new AndroidInput(this, glView, 1, 1);
  51.         PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
  52.         wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
  53.     }
  54.     
  55.     public void onResume() {
  56.         super.onResume();
  57.         glView.onResume();//启动GLSurfaceView线程
  58.         wakeLock.acquire();
  59.     }
  60.     
  61.      //启动GLSurfaceView线程时调用
  62.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  63.         glGraphics.setGL(gl);
  64.         
  65.         synchronized(stateChanged) {
  66.             if(state == GLGameState.Initialized)
  67.                 screen = getStartScreen();
  68.             state = GLGameState.Running;
  69.             screen.resume();
  70.             startTime = System.nanoTime();
  71.         }
  72.     }
  73.     
  74.     
  75.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  76.     }

  77.      //GLSurfaceView线程中as often as possible的被调用
  78.     public void onDrawFrame(GL10 gl) {
  79.         GLGameState state = null;
  80.         
  81.         synchronized(stateChanged) {
  82.             state = this.state;
  83.         }
  84.         
  85.         if(state == GLGameState.Running) {
  86.             float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
  87.             startTime = System.nanoTime();
  88.             
  89.             screen.update(deltaTime);//处理用户的事件
  90.             screen.present(deltaTime);//显示当前的屏幕
  91.         }
  92.         
  93.         if(state == GLGameState.Paused) {
  94.             screen.pause(); //设定相应的screen的状态为Paused,为了在以后处在Running状态的时候进行显示
  95.             synchronized(stateChanged) {
  96.                 this.state = GLGameState.Idle;
  97.                 stateChanged.notifyAll();//通知主线程状态已经设定完毕
  98.             }
  99.         }
  100.         
  101.         if(state == GLGameState.Finished) {
  102.             screen.pause();
  103.             screen.dispose();
  104.             synchronized(stateChanged) {
  105.                 this.state = GLGameState.Idle;
  106.                 stateChanged.notifyAll();
  107.             }
  108.         }
  109.     }
  110.     
  111.      
  112.     public void onPause() {
  113.         synchronized(stateChanged) {
  114.             if(isFinishing())
  115.                 state = GLGameState.Finished;
  116.             else
  117.                 state = GLGameState.Paused;
  118.             while(true) {
  119.                 try {
  120.                     stateChanged.wait();//本线程(Main Looper)等待,直到GLSurfaceView线程完成界面的状态设定
  121.                     break;
  122.                 } catch(InterruptedException e) {
  123.                 }
  124.             }
  125.         }
  126.         wakeLock.release();
  127.         glView.onPause(); //停止GLSurfaceView线程
  128.         super.onPause();
  129.     }
  130.     
  131.     public GLGraphics getGLGraphics() {
  132.         return glGraphics;
  133.     }
  134.     
  135.     
  136.     public Input getInput() {
  137.         return input;
  138.     }

  139.     
  140.     public FileIO getFileIO() {
  141.         return fileIO;
  142.     }

  143.     
  144.     public Graphics getGraphics() {
  145.         throw new IllegalStateException("We are using OpenGL!");
  146.     }

  147.     
  148.     public Audio getAudio() {
  149.         return audio;
  150.     }

  151.      //设置一个Screen为当前的要显示的,GLSurfaceView将根据这个进行事件处理和显示
  152.     public void setScreen(Screen screen) {
  153.         if (screen == null)
  154.             throw new IllegalArgumentException("Screen must not be null");
  155.         this.screen.pause();
  156.         this.screen.dispose();
  157.         screen.resume();
  158.         screen.update(0);
  159.         this.screen = screen;
  160.     }

  161.     
  162.     public Screen getCurrentScreen() {
  163.         return screen;
  164.     }
  165. }
阅读(1792) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

GilBert19872011-11-20 14:46:40

☆彼岸★花开: 加油啊↖(^ω^)↗.....
谢谢,加油

☆彼岸★花开2011-11-19 19:10:11

加油啊↖(^ω^)↗