简要分析一下代码:
两个线程:
1.主线程(Main Looper)
处理Activity的生命周期(onCreate,onResume,onPause)和获取用户的输入(Sensor,TouchEvent)
2.GLSurfaceView线程
Renderer接口中注册的回调方法,显示界面,处理用户的输入
两个线程需要同步GameState对象和相应的用户输入的内存池
-
package com.badlogic.androidgames.framework.impl;
-
-
import javax.microedition.khronos.egl.EGLConfig;
-
import javax.microedition.khronos.opengles.GL10;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.opengl.GLSurfaceView;
-
import android.opengl.GLSurfaceView.Renderer;
-
import android.os.Bundle;
-
import android.os.PowerManager;
-
import android.os.PowerManager.WakeLock;
-
import android.view.Window;
-
import android.view.WindowManager;
-
-
import com.badlogic.androidgames.framework.Audio;
-
import com.badlogic.androidgames.framework.FileIO;
-
import com.badlogic.androidgames.framework.Game;
-
import com.badlogic.androidgames.framework.Graphics;
-
import com.badlogic.androidgames.framework.Input;
-
import com.badlogic.androidgames.framework.Screen;
-
-
public abstract class GLGame extends Activity implements Game, Renderer {
-
enum GLGameState {
-
Initialized,
-
Running,
-
Paused,
-
Finished,
-
Idle
-
}
-
-
GLSurfaceView glView;
-
GLGraphics glGraphics;
-
Audio audio;
-
Input input;
-
FileIO fileIO;
-
Screen screen;
-
GLGameState state = GLGameState.Initialized;
-
Object stateChanged = new Object();//同步对象锁
-
long startTime = System.nanoTime();
-
WakeLock wakeLock;
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
-
WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
glView = new GLSurfaceView(this);
-
glView.setRenderer(this);//设置GLSurfaceView对应的回调接口
-
setContentView(glView);
-
-
glGraphics = new GLGraphics(glView);
-
fileIO = new AndroidFileIO(getAssets());
-
audio = new AndroidAudio(this);
-
input = new AndroidInput(this, glView, 1, 1);
-
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
-
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
-
}
-
-
public void onResume() {
-
super.onResume();
-
glView.onResume();//启动GLSurfaceView线程
-
wakeLock.acquire();
-
}
-
-
//启动GLSurfaceView线程时调用
-
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
-
glGraphics.setGL(gl);
-
-
synchronized(stateChanged) {
-
if(state == GLGameState.Initialized)
-
screen = getStartScreen();
-
state = GLGameState.Running;
-
screen.resume();
-
startTime = System.nanoTime();
-
}
-
}
-
-
-
public void onSurfaceChanged(GL10 gl, int width, int height) {
-
}
-
-
//GLSurfaceView线程中as often as possible的被调用
-
public void onDrawFrame(GL10 gl) {
-
GLGameState state = null;
-
-
synchronized(stateChanged) {
-
state = this.state;
-
}
-
-
if(state == GLGameState.Running) {
-
float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
-
startTime = System.nanoTime();
-
-
screen.update(deltaTime);//处理用户的事件
-
screen.present(deltaTime);//显示当前的屏幕
-
}
-
-
if(state == GLGameState.Paused) {
-
screen.pause(); //设定相应的screen的状态为Paused,为了在以后处在Running状态的时候进行显示
-
synchronized(stateChanged) {
-
this.state = GLGameState.Idle;
-
stateChanged.notifyAll();//通知主线程状态已经设定完毕
-
}
-
}
-
-
if(state == GLGameState.Finished) {
-
screen.pause();
-
screen.dispose();
-
synchronized(stateChanged) {
-
this.state = GLGameState.Idle;
-
stateChanged.notifyAll();
-
}
-
}
-
}
-
-
-
public void onPause() {
-
synchronized(stateChanged) {
-
if(isFinishing())
-
state = GLGameState.Finished;
-
else
-
state = GLGameState.Paused;
-
while(true) {
-
try {
-
stateChanged.wait();//本线程(Main Looper)等待,直到GLSurfaceView线程完成界面的状态设定
-
break;
-
} catch(InterruptedException e) {
-
}
-
}
-
}
-
wakeLock.release();
-
glView.onPause(); //停止GLSurfaceView线程
-
super.onPause();
-
}
-
-
public GLGraphics getGLGraphics() {
-
return glGraphics;
-
}
-
-
-
public Input getInput() {
-
return input;
-
}
-
-
-
public FileIO getFileIO() {
-
return fileIO;
-
}
-
-
-
public Graphics getGraphics() {
-
throw new IllegalStateException("We are using OpenGL!");
-
}
-
-
-
public Audio getAudio() {
-
return audio;
-
}
-
-
//设置一个Screen为当前的要显示的,GLSurfaceView将根据这个进行事件处理和显示
-
public void setScreen(Screen screen) {
-
if (screen == null)
-
throw new IllegalArgumentException("Screen must not be null");
-
this.screen.pause();
-
this.screen.dispose();
-
screen.resume();
-
screen.update(0);
-
this.screen = screen;
-
}
-
-
-
public Screen getCurrentScreen() {
-
return screen;
-
}
-
}
阅读(1792) | 评论(2) | 转发(0) |