`

《Beginning Android Games》给出基本框架的实现(1)

阅读更多

实现的部分代码比较多 所以分成几个部分 首先从最主题的Game实现来看

public abstract class AndroidGame extends Activity implements Game {
	AndroidFastRenderView renderView;
	Graphics graphics;
	Audio audio;
	Input input;
	FileIO fileIO;
	Screen screen;
	WakeLock wakeLock;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		boolean isLandspace=getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE;
		int frameBufferWidth=isLandspace?480:320;
		int frameBufferHeight=isLandspace?320:480;
		Bitmap frameBuffer=Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
		
		float scaleX=(float)frameBufferWidth/getWindowManager().getDefaultDisplay().getWidth();
		float scaleY=(float)frameBufferHeight/getWindowManager().getDefaultDisplay().getHeight();
		
		renderView=new AndroidFastRenderView(this, frameBuffer);
		graphics=new AndroidGraphics(getAssets(),frameBuffer);
		fileIO=new AndroidFileIO(getAssets());
		audio=new AndroidAudio(this);
		input=new AndroidInput(this, renderView, scaleX, scaleY);
		screen=getStartScreen();
		setContentView(renderView);
		
		PowerManager powerManager=(PowerManager)getSystemService(Context.POWER_SERVICE);
		wakeLock=powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
	}

	@Override
	protected void onResume() {
		super.onResume();
		wakeLock.acquire();
		screen.resume();
		renderView.resume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		wakeLock.release();
		renderView.pause();
		screen.pause();
		
		if(isFinishing()){
			screen.dispose();
		}
	}

	@Override
	public Audio getAudio() {
		return audio;
	}

	@Override
	public Screen getCurrentScreen() {
		return screen;
	}

	@Override
	public FileIO getFileIO() {
		return fileIO;
	}

	@Override
	public Graphics getGraphics() {
		return graphics;
	}

	@Override
	public Input getInput() {
		return input;
	}

	@Override
	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;
	}

}

 所有的实现都采用了AndroidXXX这样的名字 而AndroidGame是Game的实现 从代码中 也可以看到AndroidInput,AndroidAudio等 在后面会一一给出具体实现

 

在AndroidGame中有两个之前没有见到的类 一个是AndroidFastRenderView 另一个是WakeLock,WakeLock是Android系统自带的类,用于控制屏幕唤醒的锁,并不属于游戏框架,而AndroidFastRenderView则是非常重要的一个类了,之前所说的游戏主循环,其实也是在这个类中得到的体现,下面是AndroidFastRenderView的实现

public class AndroidFastRenderView extends SurfaceView implements Runnable{
	AndroidGame game;
	Bitmap framebuffer;
	Thread renderThread=null;
	SurfaceHolder holder;
	volatile boolean running=false;
	
	public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer){
		super(game);
		this.game=game;
		this.framebuffer=framebuffer;
		this.holder=getHolder();
	}
	
	public void resume(){
		running=true;
		renderThread=new Thread(this);
		renderThread.start();
	}
	
	public void pause(){
		running=false;
		while(true){
			try{
				renderThread.join();
				break;
			} catch(InterruptedException e){
				//retry
			}
		}
	}
	
	@Override
	public void run() {
		Rect dstRect=new Rect();
		long startTime=System.nanoTime();
		while(running){
			if(!holder.getSurface().isValid()){
				continue;
			}
			float deltaTime=(System.nanoTime()-startTime)/1000000000.0f;
			startTime=System.nanoTime();
			
			game.getCurrentScreen().update(deltaTime);
			game.getCurrentScreen().present(deltaTime);
			
			Canvas canvas=holder.lockCanvas();
			canvas.getClipBounds(dstRect);
			canvas.drawBitmap(framebuffer, null, dstRect, null);
			holder.unlockCanvasAndPost(canvas);
		}
	}

}

 Game中具体Input Audio等的实现会在后面给出

分享到:
评论
1 楼 binlovexuelu 2012-02-24  
顶!!!!!!!!!!!!

相关推荐

Global site tag (gtag.js) - Google Analytics