SpriteManager using TextureAtlas - not ready yet

This commit is contained in:
Hannes Janetzek 2013-06-17 02:12:21 +02:00
parent 569ad7764d
commit 2d8098112b
2 changed files with 108 additions and 21 deletions

View File

@ -0,0 +1,100 @@
/*
* Copyright 2013
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.renderer.sublayers;
import org.oscim.renderer.sublayers.TextureAtlas.Rect;
import org.oscim.utils.pool.Inlist;
import android.graphics.Canvas;
public abstract class SpriteManager<T> {
public class Sprite extends Inlist<Sprite>{
public Sprite(T i, TextureAtlas a, Rect r) {
atlas = a;
rect = r;
item = i;
}
T item;
TextureAtlas atlas;
Rect rect;
}
TextureAtlas mAtlas;
TextureAtlas curAtlas;
Sprite items;
protected final Canvas mCanvas = new Canvas();
protected TextureItem mTexture;
public SpriteManager() {
mTexture = TextureItem.get(true);
//mTexture.ownBitmap = true;
mAtlas = new TextureAtlas(
TextureItem.TEXTURE_WIDTH,
TextureItem.TEXTURE_HEIGHT,
32);
mCanvas.setBitmap(mTexture.bitmap);
}
public Sprite getRegion(T item) {
//return items.get(item);
for (Sprite t = items; t != null; t = t.next)
if (t.item == item)
return t;
return null;
}
public void clear(){
TextureItem.releaseAll(mTexture);
mAtlas.clear();
items = null;
//mTexture.bitmap.eraseColor(Color.TRANSPARENT);
mTexture = TextureItem.get(true);
mCanvas.setBitmap(mTexture.bitmap);
}
public TextureItem getTextures(){
return mTexture;
}
public Sprite addItem(T item, int width, int height) {
Rect r = mAtlas.getRegion(width, height);
if (r == null) {
//create new atlas
return null;
}
Sprite sprite = new Sprite(item, mAtlas, r);
items = Inlist.append(items, sprite);
draw(item, r);
return sprite;
}
abstract void draw(T item, Rect r);
}

View File

@ -59,26 +59,13 @@
*/ */
package org.oscim.renderer.sublayers; package org.oscim.renderer.sublayers;
import java.util.HashMap;
import org.oscim.utils.pool.Inlist; import org.oscim.utils.pool.Inlist;
import android.graphics.Bitmap; import android.graphics.Bitmap;
public class TextureAtlas{ public class TextureAtlas extends Inlist<TextureAtlas> {
public static class SpriteManager{
TextureAtlas atlas;
HashMap<Object, Rect> items;
public Rect getRegion(Object item){
return items.get(item);
}
public Rect addItem(int width, int height){
return atlas.getRegion(width, height);
}
}
/** Allocated slots */ /** Allocated slots */
public Slot mSlots; public Slot mSlots;
@ -97,7 +84,7 @@ public class TextureAtlas{
int mUsed; int mUsed;
/** Texture identity (OpenGL) */ /** Texture identity (OpenGL) */
int mId; int id;
/** Atlas data */ /** Atlas data */
Bitmap mData; Bitmap mData;
@ -116,7 +103,7 @@ public class TextureAtlas{
public int x, y, w, h; public int x, y, w, h;
} }
private TextureAtlas(int width, int height, int depth) { TextureAtlas(int width, int height, int depth) {
mWidth = width; mWidth = width;
mHeight = height; mHeight = height;
mDepth = depth; mDepth = depth;
@ -197,15 +184,15 @@ public class TextureAtlas{
// merge // merge
for (slot = mSlots; slot.next != null;) { for (slot = mSlots; slot.next != null;) {
Slot next = slot.next; Slot nextSlot = slot.next;
if (slot.y == next.y) { if (slot.y == nextSlot.y) {
slot.w += next.w; slot.w += nextSlot.w;
// erease 'next' slot // erease 'next' slot
slot.next = next.next; slot.next = nextSlot.next;
} else { } else {
slot = next; slot = nextSlot;
} }
} }