`

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

 
阅读更多

关于用户事件的处理是比较复杂的一个部分,在上一篇过后,剩下的关于Audio,Graphics,FileIO的部分就显得比较简单了

首先来看AndroidFileIO,这里的File主要存在于2个地方,一个是SD卡上,另一个是assets文件夹中

 

public class AndroidFileIO implements FileIO {
	AssetManager assets;
	String externalStoragePath;
	
	public AndroidFileIO(AssetManager assets){
		this.assets=assets;
		this.externalStoragePath=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
	}
	
	@Override
	public InputStream readAsset(String fileName) throws IOException {
		return assets.open(fileName);
	}

	@Override
	public InputStream readFile(String fileName) throws IOException {
		return new FileInputStream(externalStoragePath+fileName);
	}

	@Override
	public OutputStream writeFile(String fileName) throws IOException {
		return new FileOutputStream(externalStoragePath+fileName);
	}

}

 

再看AndroidGraphics的实现 主要关系到2个类,AndroidGraphics和AndroidPixmap,他们分别是之前给出抽象中Graphics和Pixmap的实现

 

public class AndroidPixmap implements Pixmap {
	Bitmap bitmap;
	PixmapFormat format;
	
	public AndroidPixmap(Bitmap bitmap, PixmapFormat format){
		this.bitmap=bitmap;
		this.format=format;
	}
	
	@Override
	public void dispose() {
		bitmap.recycle();
	}

	@Override
	public PixmapFormat getFormat() {
		return format;
	}

	@Override
	public int getWidth() {
		return bitmap.getWidth();
	}

	@Override
	public int getHeight() {
		return bitmap.getHeight();
	}

}

 

 

public class AndroidGraphics implements Graphics {
	AssetManager assets;
	Bitmap frameBuffer;
	Canvas canvas;
	Paint paint;
	Rect srcRect = new Rect();
	Rect dstRect = new Rect();

	public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
		this.assets = assets;
		this.frameBuffer = frameBuffer;
		canvas = new Canvas(frameBuffer);
		paint = new Paint();
	}

	@Override
	public void clear(int color) {
		canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8,
				(color & 0xff));
	}

	@Override
	public void drawLine(int x, int y, int x2, int y2, int color) {
		paint.setColor(color);
		canvas.drawLine(x, y, x2, y2, paint);
	}

	@Override
	public void drawPixel(int x, int y, int color) {
		paint.setColor(color);
		canvas.drawPoint(x, y, paint);
	}

	@Override
	public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY,
			int srcWidth, int srcHeight) {
		srcRect.left=srcX;
		srcRect.top=srcY;
		srcRect.right=srcX+srcWidth-1;
		srcRect.bottom=srcY+srcHeight-1;
		
		dstRect.left=x;
		dstRect.top=y;
		dstRect.right=x+srcWidth-1;
		dstRect.bottom=y+srcHeight-1;
		
		canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, srcRect, dstRect, paint);
	}

	@Override
	public void drawPixmap(Pixmap pixmap, int x, int y) {
		canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y, paint);

	}

	@Override
	public void drawRect(int x, int y, int width, int height, int color) {
		paint.setColor(color);
		paint.setStyle(Style.FILL);
		canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
	}

	@Override
	public int getHeight() {
		return frameBuffer.getHeight();
	}

	@Override
	public int getWidth() {
		return frameBuffer.getWidth();
	}

	@Override
	public Pixmap newPixmap(String fileName, PixmapFormat format) {
		Config config = null;
		if (format == PixmapFormat.RGB565) {
			config = Config.RGB_565;
		} else if (format == PixmapFormat.ARGB84444) {
			config = Config.ARGB_4444;
		} else {
			config = Config.ARGB_8888;
		}

		Options options = new Options();
		options.inPreferredConfig = config;

		InputStream in = null;
		Bitmap bitmap = null;
		try {
			in = assets.open(fileName);
			bitmap = BitmapFactory.decodeStream(in);
			if (bitmap == null) {
				throw new RuntimeException("Couldn't load bitmap from asset '"
						+ fileName + "'");
			}
		} catch (IOException e) {
			throw new RuntimeException("Couldn't load bitmap from asset '"
					+ fileName + "'");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}

		if (bitmap.getConfig() == Config.RGB_565) {
			format = PixmapFormat.RGB565;
		} else if (bitmap.getConfig() == Config.ARGB_4444) {
			format = PixmapFormat.ARGB84444;
		} else {
			format = PixmapFormat.ARGB8888;
		}

		return new AndroidPixmap(bitmap, format);
	}

}
分享到:
评论

相关推荐

    Beginning Android Games.pdf

    Beginning Android Games.pdf

    Beginning Android Games 3rd Edition.pdf

    Beginning Android Games, Third Edition gives you everything you need to branch out and write your own Android games for a variety of hardware. Do you have an awesome idea for the next break-through ...

    Beginning Android Games

    "Beginning Android Games, Second Edition offers everything you need to join the ranks of successful Android game developers, including Android tablet game app development considerations. You'll start...

    Beginning Android Games 3rd Edition

    Android. While some of the material may be old news for you, there are still a lot of tips and hints contained here that should make reading this book worthwhile. Android is a strange beast at times, ...

    Beginning Android Games - Mario Zechner

    你会开始与游戏设计基础和编程的基础,然后逐步实现建立自己的基本的游戏引擎和游戏可玩。这会给你一切你需要另辟蹊径,写自己的Andr​​oid游戏。 潜在的用户群和现有的高性能装置,使机器人的广泛有志游戏开发商...

    Beginning Android Games(Apress,3ed,2015)

    Beginning Android Games, Third Edition gives you everything you need to branch out and write your own Android games for a variety of hardware. Do you have an awesome idea for the next break-through ...

    Beginning Android Games 2012

    Beginning Android Games 2012 (新版)

    Beginning Android Games [3rd Edition,2016]

    Beginning Android Games English | 6 Jan. 2017 | ISBN: 1484204735 | 636 Pages | PDF | 11.87 MB Learn all of the basics needed to join the ranks of successful Android game developers. You'll start with...

    Beginning Android Games, 2nd Edition

    Beginning Android Games, 2nd Edition

    beginning android games

    beginning android games

    android.开发书籍 高清PDF Beginning Android Games

    android.开发书籍 高清PDF Beginning Android Games

    Beginning Android Games(3rd) 无水印pdf 0分

    Beginning Android Games(3rd) 英文无水印pdf 第3版 pdf使用FoxitReader和PDF-XChangeViewer测试可以打开

    Beginning Android 4 Games Development

    Beginning Android 4 Games Development offers everything you need to join the ranks of successful Android game developers. You'll start with game design fundamentals and programming basics, and then ...

    Beginning Android Games( Android 游戏开发入门)

    你会开始与游戏设计基础和编程的基础,然后逐步实现建立自己的基本的游戏引擎和游戏可玩。这会给你一切你需要另辟蹊径,写自己的Android游戏。 这本书将指导制作为Android平台的几个示例游戏的过程,并涉及广泛的...

    Beginning Android

    Beginning Android Beginning Android Beginning Android

    Beginning Android Games 1

    * Android平台的基础,适用于那些在游戏中基本面 * 2D和3D游戏的设计和他们在Android平台上的成功实施 你将学到什么 *如何开发你的第一个Android应用程序的设置和使用的开发工具 *在Android平台中的游戏编程的基础...

    Beginning Android Games 2

    * Android平台的基础,适用于那些在游戏中基本面 * 2D和3D游戏的设计和他们在Android平台上的成功实施 你将学到什么 *如何开发你的第一个Android应用程序的设置和使用的开发工具 *在Android平台中的游戏编程的基础...

    Android 4 游戏开发入门(Beginning Android 4 Games Development)

    Beginning Android 4 Games Development offers everything you need to join the ranks of successful Android game developers. You'll start with game design fundamentals and programming basics, and then ...

Global site tag (gtag.js) - Google Analytics