프레임워크 전체코드

from Android 2013. 8. 14. 13:27


package com.framework;


import android.content.res.Resources;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;


public class AppManager {

private static AppManager s_instance;

//프로그램 전역에서 접근 가능한 싱글턴 패턴으로 정의

public static AppManager getInstance(){

if(s_instance == null){

s_instance = new AppManager();

}

return s_instance;

}

//객체를 저장하고 불러오는 메소드들을 생성.

private Resources m_resource;

private GameView m_gameView;

void setGameView(GameView _gameView){

m_gameView = _gameView;

}

void setResource(Resources _resource){

m_resource = _resource;

}

public GameView getGameView(){

return m_gameView;

}

public Resources getResource(){

return m_resource;

}

public Bitmap getBitmap(int r){

return BitmapFactory.decodeResource(m_resource, r);

}


}

package com.framework;


import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.view.KeyEvent;

import android.view.MotionEvent;


public class CreditState implements IState {

Bitmap android;

int x;

int y;

@Override

public void Init() {

android = AppManager.getInstance().getBitmap(R.drawable.layer3);

}

@Override

public void Destory() {

// TODO Auto-generated method stub

}

@Override

public void Update() {

// TODO Auto-generated method stub

}

@Override

public void Render(Canvas canvas) {

canvas.drawBitmap(android, x, y, null);

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

return true;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

return true;

}

}

package com.framework;


import android.content.Context;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;


public class GameView extends SurfaceView implements SurfaceHolder.Callback {

private GameViewThread m_thread;

private IState m_state;

private GraphicObject m_Image; //그래픽오브젝트로 추가

private SpriteAnimation m_spr; //스프라이드애니메이션으로 추가

public GameView(Context context){

super(context);

setFocusable(true);

AppManager.getInstance().setGameView(this);

AppManager.getInstance().setResource(getResources());

getHolder().addCallback(this);

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

//m_Image = new GraphicObject(AppManager.getInstance().getBitmap(R.drawable.background));//

ChangeGameState(new IntroState());

m_spr = new SpriteAnimation(BitmapFactory.decodeResource(getResources(), R.drawable.robosprite));

m_spr.InitSpriteData(80, 90, 24, 14);

}

public void ChangeGameState(IState _state){

if(m_state!=null)

m_state.Destory();

_state.Init();

m_state = _state;

}

@Override

public void onDraw(Canvas canvas){

/*

m_state.Render(canvas);

m_Image.Draw(canvas);//그래픽 오브젝트로 추가*/

canvas.drawColor(Color.WHITE);

m_spr.Draw(canvas);

}

@Override

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

}

@Override

public void surfaceCreated(SurfaceHolder arg0){

m_thread.setRunning(true);

m_thread.start();

}

@Override

public void surfaceDestroyed(SurfaceHolder arg0){

boolean retry = true;

m_thread.setRunning(false);

while(retry){

try{

//스레드를 중지

m_thread.join();

retry=false;

}

catch(InterruptedException e){

}

}

}

public void Update(){

long GameTime = System.currentTimeMillis(); //현재시간을 밀리초단위로 구함

m_spr.Update(GameTime);

}


@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

m_state.onKeyDown(keyCode, event);

return true;

}


@Override

public boolean onTouchEvent(MotionEvent event) {

AppManager.getInstance().getGameView().ChangeGameState(new CreditState());

return true;

}

}

package com.framework;


import android.graphics.Bitmap;

import android.graphics.Canvas;


public class GraphicObject {

protected Bitmap m_bitmap;

protected int m_x;

protected int m_y;

public GraphicObject(Bitmap bitmap){

m_bitmap = bitmap;

m_x=0;

m_y=0;

}

public void Draw(Canvas canvas){

canvas.drawBitmap(m_bitmap, m_y, m_y, null);

}

public void setPosition(int x, int y){

m_x = x;

m_y = y;

}

public int GetX(){

return m_x;

}

public int GetY(){

return m_y;

}

}

package com.framework;


import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.view.KeyEvent;

import android.view.MotionEvent;


public class IntroState implements IState {

Bitmap icon;

int x;

int y;

@Override

public void Init() {

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

}

@Override

public void Destory() {

}

@Override

public void Update() {

}

@Override

public void Render(Canvas canvas) {

canvas.drawBitmap(icon, x, y, null);

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

return true;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

return true;

}

}

package com.framework;


import android.graphics.Canvas;

import android.view.KeyEvent;

import android.view.MotionEvent;


public interface IState {

public void Init();

public void Destory();

public void Update();

public void Render(Canvas canvas);

public boolean onKeyDown(int keyCode, KeyEvent event);

public boolean onTouchEvent(MotionEvent event);

}

package com.framework;


import android.app.Activity;

import android.os.Bundle;


public class MainActivity extends Activity {


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new GameView(this));

}

}

package com.framework;


import java.util.HashMap;


import android.content.Context;

import android.media.AudioManager;

import android.media.SoundPool;


public class SoundManager {

private SoundPool m_SoundPool; //안드로이드에서 지원하는 사운드풀

private HashMap m_SoundPoolMap; //사운드 id를 저장할 해시맵

private AudioManager m_AudioManager; //골라먹는 재미가 있는 사운드 매니져

private Context m_Activity; //어플의 컨텍스트 값

//멤버변수 초기화

public void Init(Context context){

m_SoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); //사운드풀 생성

m_SoundPoolMap = new HashMap(); //해시맵 생성

//오디오매니져의 핸들값을 가져옴(아래가..)

m_AudioManager = (AudioManager)context.getSystemService

(Context.AUDIO_SERVICE);

m_Activity = context; //액티비티 핸들값 가져옴

}

//싱글턴 패턴 작성

private static SoundManager s_instance;

public static SoundManager getInstance(){

if(s_instance == null){

s_instance = new SoundManager();

}

return s_instance;

//사운드 추가

public void addSound(int index, int soundID){

int id = m_SoundPool.load(m_Activity, soundID, 1);//해시맵에서 사운드 로드

m_SoundPoolMap.put(index, id);

}

//사운드 재생

public void play(int index){

float streamVolume = m_AudioManager.getStreamVolume

(AudioManager.STREAM_MUSIC);

streamVolume = streamVolume / m_AudioManager.getStreamMaxVolume

(AudioManager.STREAM_MUSIC);

m_SoundPool.play((Integer)m_SoundPoolMap.get(index),

streamVolume, streamVolume, 1, 0, 1f);

}

//사운드 반복 재생

public void playLooped(int index){

float streamVolume = m_AudioManager.getStreamVolume

(AudioManager.STREAM_MUSIC);

streamVolume = streamVolume / m_AudioManager.getStreamMaxVolume

(AudioManager.STREAM_MUSIC);

m_SoundPool.play((Integer)m_SoundPoolMap.get(index), 

streamVolume, streamVolume, 1, -1, 1f); 

}

}

package com.framework;


import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Rect;

import android.text.style.UpdateAppearance;


public class SpriteAnimation extends GraphicObject {

private Rect m_Rect; //사각영역

private int m_fps; //초당 프레임

private int m_MaxFrame; //프레임개수

private int m_CurrentFrame; //최근프레임

private int m_SPriteWidth;

private int m_SpriteHeight;

private long m_FrameTimer;

public SpriteAnimation(Bitmap bitmap){

super(bitmap);

//멤버변수 초기와

m_Rect = new Rect(0,0,0,0);

m_FrameTimer = 0;

m_CurrentFrame = 0;

}

public void InitSpriteData(int width, int height, int fps, int frameCount){

//얻어온 값들을 스프라이트 애니메이션에 대입

m_SpriteHeight = height;

m_SPriteWidth = width;

m_Rect.top = 0;

m_Rect.bottom = m_SpriteHeight;

m_Rect.left = 0;

m_Rect.right = m_SPriteWidth;

m_fps = 1000 / fps; //밀리세컨드라서

m_MaxFrame = frameCount;

}


@Override

public void Draw(Canvas canvas) {

//사각형 영역에 맞춰 새로 정의한 Draw(), onDrow아님;;

Rect dest = new Rect(m_x, m_y, m_x + m_SPriteWidth, m_y + m_SpriteHeight);

canvas.drawBitmap(m_bitmap, m_Rect, dest, null);

}

public void Update(long GameTime){

//시간의 흐름에 따른 스프라이트 애니메이션 재생

if(GameTime > m_FrameTimer + m_fps){

m_FrameTimer = GameTime;

m_CurrentFrame += 1;

if(m_CurrentFrame >= m_MaxFrame){

m_CurrentFrame = 0;

}

}

//다음 프레임으로 넘어가기(스프라이드 이미지에서 다음 사각영역으로 넘어가기)

m_Rect.left = m_CurrentFrame * m_SPriteWidth;

m_Rect.right = m_Rect.left + m_SPriteWidth;

}

}


.... 그러하다...

,