我想通过将场景渲染到RenderTexture来捕获屏幕,我试图在MotionStreakExample中执行此操作并且一切正常,但是当我将代码复制到我的项目时,我有这张图片(可能有纹理有些麻烦...我无法理解):
@Override
public Engine onCreateEngine(EngineOptions pEngineOptions) {
return new Engine(pEngineOptions) {
private boolean mRenderTextureInitialized;
private RenderTexture mRenderTextures ;
private Sprite mRenderTextureSprites ;
@Override
public void onDrawFrame(final GLState pGLState) throws InterruptedException {
final boolean firstFrame = !this.mRenderTextureInitialized;
if(firstFrame) {
this.initRenderTextures(pGLState);
this.mRenderTextureInitialized = true;
}
final int surfaceWidth = WIDTH*2;
final int surfaceHeight = HEIGHT;
this.mRenderTextures.begin(pGLState, false, true);
{
/* Draw current frame. */
super.onDrawFrame(pGLState);
/* Draw previous frame with reduced alpha. */
}
this.mRenderTextures.end(pGLState);
{
pGLState.pushProjectionGLMatrix();
pGLState.orthoProjectionGLMatrixf(0, surfaceWidth, 0, surfaceHeight, -1, 1);
{
this.mRenderTextureSprites.setAlpha(1);
this.mRenderTextureSprites.onDraw(pGLState, this.mCamera);
}
pGLState.popProjectionGLMatrix();
}
if (needToSave)
{
needToSave = false;
FSHelper.saveBitmapToFile(this.mRenderTextures.getBitmap(pGLState), SAVED_PATH+"/test.png");
}
/* Flip RenderTextures. */
}
private void initRenderTextures(final GLState pGLState) {
final int surfaceWidth = WIDTH*2;
final int surfaceHeight = HEIGHT;
final VertexBufferObjectManager vertexBufferObjectManager = mEngine.getVertexBufferObjectManager();
this.mRenderTextures = new RenderTexture(mEngine.getTextureManager(), surfaceWidth, surfaceHeight);
this.mRenderTextures.init(pGLState);
final ITextureRegion renderTextureATextureRegion = TextureRegionFactory.extractFromTexture(this.mRenderTextures);
this.mRenderTextureSprites = new Sprite(0, 0, renderTextureATextureRegion, vertexBufferObjectManager);
}
};
}
PS。这个红色背景的十字架是我场景中的一个精灵。
答案 0 :(得分:1)
Andengine已经拥有截取屏幕截图的基础设施,您尝试过使用它吗?看看这个例子: https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/ScreenCaptureExample.java
答案 1 :(得分:1)
我找到了答案! 纹理是动态加载的,在第一帧我根本没有加载纹理,因为我在那里有一个选择对话框并在它之后创建所有纹理。每当纹理发生变化时,我都需要调用initRenderTextures。