AndEngine之DEMO学习(三)SpriteExample

package org.andengine.examples;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;

public class SpriteExample extends SimpleBaseGameActivity {

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private ITexture mTexture;
	private ITextureRegion mFaceTextureRegion;

	@Override
	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	public void onCreateResources() {
		try {
			this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
				@Override
				public InputStream open() throws IOException {
					return getAssets().open("gfx/face_box.png");
				}
			});

			this.mTexture.load();
			this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
		} catch (IOException e) {
			Debug.e(e);
		}
	}

	@Override
	public Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		/* Calculate the coordinates for the face, so its centered on the camera. */
		final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
		final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

		/* Create the face and add it to the scene. */
		final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
		scene.attachChild(face);

		return scene;
	}

}


      这是AndEngine的第三个例子,在屏幕的最中间绘制了一个精灵,精灵的纹理来源于assetes文件夹中的图片资源文件。这回的例子带来了几个新的概念,Texture“纹理”、TextureRegion“纹理范围”、Sprite“精灵”。其中精灵是一个矩形图形(继承于RectangularShape,与Rectangular一样)并且是TextureRegion的载体,可以这样认为RectangularShape为精灵提供了矩形,TextureRegion为精灵提供了纹理映射,Texture为提供了覆盖物,扩展阅读“纹理映射”。

      先看Sprite,他继承RectangularShape拥有矩形形状的特征,拥有ITextureRegion成员变量用于将Texture正确映射在矩形中。同样他拥有一个缓存对象HighPerformanceSpriteVertexBufferObject(高性能精灵顶点缓存对象),此对象实现了接口ISpriteVertexBufferObject:

public interface ISpriteVertexBufferObject extends IVertexBufferObject {

 //更新颜色
 public void onUpdateColor(final Sprite pSprite);
 //更新顶点
 public void onUpdateVertices(final Sprite pSprite);
 //更新纹理坐标(纹理映射)
 public void onUpdateTextureCoordinates(final Sprite pSprite);
}

     意味着他的缓存数据mBufferData中不但拥有矩形中的顶点和颜色,还拥有纹理映射到矩形上的坐标,这个坐标的来源正是ITextureRegion提供的U、V、U2、V2坐标。

     再看看Texture,这个可以说是Engine中最重要的资源,他专门提供了onCreateResources的办法让我们在其中建立需要的资源,并且提供了TextureManager对资源进行统一管理。我们只需要提供资源的来源,这里是实现一个IInputStreamOpener类,让管理器在需要的时候能够获得他。建立完成后记得调用mTexture.load();这样管理器会在渲染线程中加载纹理到硬件,并且得到索引资源的ID。

     Texture管理的结构虽然很复杂,其实只需要记住一个过程就好了:

     Engine.onDrawFrame  中调用this.mTextureManager.updateTextures(pGLState);

     mTextureManager.updateTextures  中会从硬件加载或卸载textures资源。加载资源时调用Texture.loadToHardware

     Texture.loadToHardware   中创建了mHardwareTextureID并且与纹理绑定,继续调用实现类中的writeTextureToHardware

     XXXXTexture.writeTextureToHardware    具体的纹理实现,会将纹理数据写入硬件

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值