From 2d8098112b89646bd96fb9158c98345e9128e010 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Mon, 17 Jun 2013 02:12:21 +0200 Subject: [PATCH] SpriteManager using TextureAtlas - not ready yet --- .../renderer/sublayers/SpriteManager.java | 100 ++++++++++++++++++ .../renderer/sublayers/TextureAtlas.java | 29 ++--- 2 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 src/org/oscim/renderer/sublayers/SpriteManager.java diff --git a/src/org/oscim/renderer/sublayers/SpriteManager.java b/src/org/oscim/renderer/sublayers/SpriteManager.java new file mode 100644 index 00000000..baa7646b --- /dev/null +++ b/src/org/oscim/renderer/sublayers/SpriteManager.java @@ -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 . + */ +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 { + + public class Sprite extends Inlist{ + + 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); + +} \ No newline at end of file diff --git a/src/org/oscim/renderer/sublayers/TextureAtlas.java b/src/org/oscim/renderer/sublayers/TextureAtlas.java index 8c282539..a0eea2bd 100644 --- a/src/org/oscim/renderer/sublayers/TextureAtlas.java +++ b/src/org/oscim/renderer/sublayers/TextureAtlas.java @@ -59,26 +59,13 @@ */ package org.oscim.renderer.sublayers; -import java.util.HashMap; - import org.oscim.utils.pool.Inlist; import android.graphics.Bitmap; -public class TextureAtlas{ +public class TextureAtlas extends Inlist { - public static class SpriteManager{ - TextureAtlas atlas; - HashMap items; - public Rect getRegion(Object item){ - return items.get(item); - } - - public Rect addItem(int width, int height){ - return atlas.getRegion(width, height); - } - } /** Allocated slots */ public Slot mSlots; @@ -97,7 +84,7 @@ public class TextureAtlas{ int mUsed; /** Texture identity (OpenGL) */ - int mId; + int id; /** Atlas data */ Bitmap mData; @@ -116,7 +103,7 @@ public class TextureAtlas{ public int x, y, w, h; } - private TextureAtlas(int width, int height, int depth) { + TextureAtlas(int width, int height, int depth) { mWidth = width; mHeight = height; mDepth = depth; @@ -197,15 +184,15 @@ public class TextureAtlas{ // merge for (slot = mSlots; slot.next != null;) { - Slot next = slot.next; + Slot nextSlot = slot.next; - if (slot.y == next.y) { - slot.w += next.w; + if (slot.y == nextSlot.y) { + slot.w += nextSlot.w; // erease 'next' slot - slot.next = next.next; + slot.next = nextSlot.next; } else { - slot = next; + slot = nextSlot; } }