/** <p> * Flushes the batch and realigns the real matrix on the GPU. Subsequent draws won't need adjustment and will be slightly * faster as long as the transform matrix is not {@link #setTransformMatrix(Matrix4) changed}. * </p> * <p> * Note: The real transform matrix <em>must</em> be invertible. If a singular matrix is detected, GdxRuntimeException will be * thrown. * </p> * @see SpriteBatch#flush() */ public void flushAndSyncTransformMatrix () { flush(); if (adjustNeeded) { // vertices flushed, safe now to replace matrix haveIdentityRealMatrix = checkIdt(virtualMatrix); if (!haveIdentityRealMatrix && virtualMatrix.det() == 0) throw new GdxRuntimeException("Transform matrix is singular, can't sync"); adjustNeeded = false; super.setTransformMatrix(virtualMatrix); } }
@Override public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise) { if (!adjustNeeded) { super.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation, clockwise); } else { drawAdjusted(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation, clockwise); } }
private void drawAdjusted (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) { // v must be flipped drawAdjustedUV(region.texture, x, y, originX, originY, width, height, scaleX, scaleY, rotation, region.u, region.v2, region.u2, region.v, false, false); }
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()} or {@link #end()}. */ public void setTransformMatrix (Affine2 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { virtualMatrix.setAsAffine(transform); if (isDrawing()) { adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { adjustAffine.set(realMatrix).inv().mul(transform); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
private void drawAdjusted (Texture texture, float[] spriteVertices, int offset, int count) { if (!drawing) throw new IllegalStateException("CpuSpriteBatch.begin must be called before draw."); if (texture != lastTexture) switchTexture(texture); Affine2 t = adjustAffine; int copyCount = Math.min(vertices.length - idx, count); do { count -= copyCount; while (copyCount > 0) { float x = spriteVertices[offset]; float y = spriteVertices[offset + 1]; vertices[idx] = t.m00 * x + t.m01 * y + t.m02; // x vertices[idx + 1] = t.m10 * x + t.m11 * y + t.m12; // y vertices[idx + 2] = spriteVertices[offset + 2]; // color vertices[idx + 3] = spriteVertices[offset + 3]; // u vertices[idx + 4] = spriteVertices[offset + 4]; // v idx += Sprite.VERTEX_SIZE; offset += Sprite.VERTEX_SIZE; copyCount -= Sprite.VERTEX_SIZE; } if (count > 0) { super.flush(); copyCount = Math.min(vertices.length, count); } } while (count > 0); }
public void create () { Batch batch = new CpuSpriteBatch(); // batch = new SpriteBatch(); stage = new Stage(new ExtendViewport(500, 500), batch); Gdx.input.setInputProcessor(stage); texture = new Texture("data/bobargb8888-32x32.png"); texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(texture)); for (int i = 0; i < NUM_GROUPS; i++) { Group group = createActorGroup(drawable); stage.addActor(group); } }
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()} or {@link #end()}. */ public void setTransformMatrix (Affine2 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { virtualMatrix.setAsAffine(transform); if (isDrawing()) { adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { adjustAffine.set(realMatrix).inv().mul(transform); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
private void drawAdjusted (Texture texture, float[] spriteVertices, int offset, int count) { if (!drawing) throw new IllegalStateException("CpuSpriteBatch.begin must be called before draw."); if (texture != lastTexture) switchTexture(texture); Affine2 t = adjustAffine; int copyCount = Math.min(vertices.length - idx, count); do { count -= copyCount; while (copyCount > 0) { float x = spriteVertices[offset]; float y = spriteVertices[offset + 1]; vertices[idx] = t.m00 * x + t.m01 * y + t.m02; // x vertices[idx + 1] = t.m10 * x + t.m11 * y + t.m12; // y vertices[idx + 2] = spriteVertices[offset + 2]; // color vertices[idx + 3] = spriteVertices[offset + 3]; // u vertices[idx + 4] = spriteVertices[offset + 4]; // v idx += Sprite.VERTEX_SIZE; offset += Sprite.VERTEX_SIZE; copyCount -= Sprite.VERTEX_SIZE; } if (count > 0) { super.flush(); copyCount = Math.min(vertices.length, count); } } while (count > 0); }
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()}. */ @Override public void setTransformMatrix (Matrix4 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { if (isDrawing()) { virtualMatrix.setAsAffine(transform); adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { tmpAffine.set(transform); adjustAffine.set(realMatrix).inv().mul(tmpAffine); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
@Override public void draw (TextureRegion region, float width, float height, Affine2 transform) { if (!adjustNeeded) { super.draw(region, width, height, transform); } else { drawAdjusted(region, width, height, transform); } }
/** <p> * Flushes the batch and realigns the real matrix on the GPU. Subsequent draws won't need adjustment and will be slightly * faster as long as the transform matrix is not {@link #setTransformMatrix(Matrix4) changed}. * </p> * <p> * Note: The real transform matrix <em>must</em> be invertible. If a singular matrix is detected, GdxRuntimeException will be * thrown. * </p> * @see SpriteBatch#flush() */ public void flushAndSyncTransformMatrix () { flush(); if (adjustNeeded) { // vertices flushed, safe now to replace matrix haveIdentityRealMatrix = checkIdt(virtualMatrix); if (!haveIdentityRealMatrix && virtualMatrix.det() == 0) throw new GdxRuntimeException("Transform matrix is singular, can't sync"); adjustNeeded = false; super.setTransformMatrix(virtualMatrix); } }
switchTexture(region.texture); else if (idx == vertices.length) super.flush();
private void drawAdjusted (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) { // v must be flipped drawAdjustedUV(region.texture, x, y, originX, originY, width, height, scaleX, scaleY, rotation, region.u, region.v2, region.u2, region.v, false, false); }
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()}. */ @Override public void setTransformMatrix (Matrix4 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { if (isDrawing()) { virtualMatrix.setAsAffine(transform); adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { tmpAffine.set(transform); adjustAffine.set(realMatrix).inv().mul(tmpAffine); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
@Override public void draw (Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight) { if (!adjustNeeded) { super.draw(texture, x, y, srcX, srcY, srcWidth, srcHeight); } else { drawAdjusted(texture, x, y, 0, 0, srcWidth, srcHeight, 1, 1, 0, srcX, srcY, srcWidth, srcHeight, false, false); } }
/** <p> * Flushes the batch and realigns the real matrix on the GPU. Subsequent draws won't need adjustment and will be slightly * faster as long as the transform matrix is not {@link #setTransformMatrix(Matrix4) changed}. * </p> * <p> * Note: The real transform matrix <em>must</em> be invertible. If a singular matrix is detected, GdxRuntimeException will be * thrown. * </p> * @see SpriteBatch#flush() */ public void flushAndSyncTransformMatrix () { flush(); if (adjustNeeded) { // vertices flushed, safe now to replace matrix haveIdentityRealMatrix = checkIdt(virtualMatrix); if (!haveIdentityRealMatrix && virtualMatrix.det() == 0) throw new GdxRuntimeException("Transform matrix is singular, can't sync"); adjustNeeded = false; super.setTransformMatrix(virtualMatrix); } }
switchTexture(region.texture); else if (idx == vertices.length) super.flush();
@Override public void draw (Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2) { if (!adjustNeeded) { super.draw(texture, x, y, width, height, u, v, u2, v2); } else { drawAdjustedUV(texture, x, y, 0, 0, width, height, 1, 1, 0, u, v, u2, v2, false, false); } }
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()} or {@link #end()}. */ public void setTransformMatrix (Affine2 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { virtualMatrix.setAsAffine(transform); if (isDrawing()) { adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { adjustAffine.set(realMatrix).inv().mul(transform); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
@Override public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) { if (!adjustNeeded) { super.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation); } else { drawAdjusted(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation); } }