1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package org.plissken.texture;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class TextureManager {
private static TextureManager instance = null;
private static Context context;
private static HashMap<String, Integer> texList;
private static int[] texId;
private static int currentId;
private TextureManager(){
texList = new HashMap<String, Integer>();
}
public static void init(Context c, GL10 gl, int maxTex){
context = c;
texId = new int[maxTex];
gl.glGenTextures(maxTex, texId, 0);
}
public final synchronized static TextureManager getInstance(){
if(instance == null)
instance = new TextureManager();
return instance;
}
public int getTextureByName(String name) throws IOException{
if(!texList.containsKey(name)){
//Not found, try to load it
InputStream is = null;
if(context == null){
throw new IOException();
}
try {
is = context.getAssets().open("textures/"+name);
Bitmap bitmap;
bitmap = BitmapFactory.decodeStream(is);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, ++currentId, bitmap, 0);
bitmap.recycle();
} catch (Exception e) {
e.printStackTrace();
}
//Save it
texList.put(name, currentId);
return currentId;
}else{
return texList.get(name);
}
}
} |
Partager