From e7f849004745340d07c14af57ec95efe540d6337 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 1 Jul 2013 13:10:53 -0400 Subject: [PATCH] add initial methods --- .../gdx/graphics/BillboardSpriteBatch.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/com/blarg/gdx/graphics/BillboardSpriteBatch.java b/src/com/blarg/gdx/graphics/BillboardSpriteBatch.java index bb92861..e2c390a 100644 --- a/src/com/blarg/gdx/graphics/BillboardSpriteBatch.java +++ b/src/com/blarg/gdx/graphics/BillboardSpriteBatch.java @@ -1,5 +1,10 @@ package com.blarg.gdx.graphics; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.g3d.decals.Decal; +import com.badlogic.gdx.graphics.g3d.decals.DecalBatch; +import com.badlogic.gdx.utils.Array; + /*** *

* Wrapper over libgdx's included {@link DecalBatch} with automatic easy management of @@ -8,5 +13,74 @@ package com.blarg.gdx.graphics; *

*/ public class BillboardSpriteBatch { + static final int CAPACITY_INCREMENT = 128; + Array sprites; + int pointer; + boolean hasBegun; + DecalBatch decalBatch; + + public BillboardSpriteBatch() { + sprites = new Array(true, CAPACITY_INCREMENT, Decal.class); + pointer = 0; + addNewSprites(CAPACITY_INCREMENT); + + hasBegun = false; + decalBatch = null; + } + + public void begin(DecalBatch decalBatch) { + if (hasBegun) + throw new IllegalStateException("Cannot be called within an existing begin/end block."); + if (decalBatch == null) + throw new IllegalArgumentException(); + + this.decalBatch = decalBatch; + hasBegun = true; + pointer = 0; + } + + public void flush() { + if (!hasBegun) + throw new IllegalStateException("Cannot call outside of a begin/end block."); + + // TODO: render decals with DecalBatch + + pointer = 0; + } + + public void end() { + if (!hasBegun) + throw new IllegalStateException("Must call begin() first."); + + flush(); + + hasBegun = false; + decalBatch = null; // don't need to hold on to this particular reference anymore + } + + + private void increaseCapacity() { + int newCapacity = sprites.items.length + CAPACITY_INCREMENT; + sprites.ensureCapacity(newCapacity); + addNewSprites(CAPACITY_INCREMENT); + } + + private void addNewSprites(int count) { + for (int i = 0; i < count; ++i) + sprites.add(Decal.newDecal(new TextureRegion())); + } + + private int getRemainingSpace() { + return sprites.size - pointer; + } + + private Decal nextUsable() { + if (getRemainingSpace() == 0) + increaseCapacity(); + + Decal usable = sprites.items[pointer]; + pointer++; + return usable; + } }