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 具體的紋理實現,會將紋理數據寫入硬件