카테고리 없음

Android Game Framework Part 3

Soul-Learner 2012. 5. 3. 16:07

Android Game Framework Part 3

AppManager를 추가하여 리소스(이미지)와 GameView의 참조를 전달해 줄 수 있도록 작성한 예

GameActivity.java

package game.framework;


import android.app.Activity;

import android.os.Bundle;


public class GameActivity extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(new GameView(this));

    }

}


GameView.java

package game.framework;


import android.content.*;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.view.*;


public class GameView extends SurfaceView implements SurfaceHolder.Callback {


GameViewThread gameThread;

public GameView(Context context) {

super(context);

getHolder().addCallback(this);

setFocusable(true);

AppManager.getInstance().setGameView(this);

AppManager.getInstance().setResources(getResources());

gameThread = new GameViewThread(getHolder(), this);

}

public void update() {

}

@Override

protected void onDraw(Canvas canvas) {

Bitmap icon = AppManager.getInstance().getBitmap(R.drawable.ic_launcher);

canvas.drawColor(Color.BLACK);

canvas.drawBitmap(icon, 10, 10, null);

}


@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}


@Override

public void surfaceCreated(SurfaceHolder holder) {

gameThread.setRunning(true);

gameThread.start();

}


@Override

public void surfaceDestroyed(SurfaceHolder holder) {

boolean retry = true;

gameThread.setRunning(false);

while(retry){

try{

gameThread.join();

retry = false;

}catch(InterruptedException ie){

}

}

}

}


GameViewThread.java

package game.framework;


import android.graphics.*;

import android.view.*;


public class GameViewThread extends Thread {


private SurfaceHolder surfaceHolder;

private GameView gameView;

private boolean m_run;

public GameViewThread(SurfaceHolder surfaceHolder, GameView gameView) {

this.surfaceHolder = surfaceHolder;

this.gameView = gameView;

}

public void setRunning(boolean run) {

this.m_run = run;

}


@Override

public void run() {

Canvas canvas = null;

while(m_run) {

canvas = null;

try{

gameView.update();

canvas = surfaceHolder.lockCanvas(null);

synchronized (surfaceHolder) {

gameView.onDraw(canvas);

}

}catch(Exception ex){

}finally {

if(canvas != null) {

surfaceHolder.unlockCanvasAndPost(canvas);

}

}

}

}

}


AppManager.java

package game.framework;


import android.content.res.*;

import android.graphics.*;


public class AppManager {


private GameView gameView;

private Resources resources;

void setGameView(GameView gameView) {

this.gameView = gameView;

}

void setResources(Resources resources ){

this.resources = resources;

}


public GameView getGameView() {

return gameView;

}


public Resources getResources() {

return resources;

}

public Bitmap getBitmap(int resId) {

return BitmapFactory.decodeResource(getResources(), resId);

}

private static AppManager instance;

public static AppManager getInstance() {

if(instance==null) instance = new AppManager();

return instance;

}

}