If you think the Android project TinyVoxel listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.toet.TinyVoxel.Shaders;
//www.java2s.comimport com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.toet.TinyVoxel.Config;
import com.toet.TinyVoxel.Debug.LogHandler;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Kajos on 3-1-14.
*/publicclass ShaderManager {
privatestatic ShaderManager instance;
publicstatic ShaderManager get() {
if (instance == null)
instance = new ShaderManager();
return instance;
}
publicstaticvoid refresh() {
instance = null;
}
private Map<String, ShaderProgram> programs = new HashMap<String, ShaderProgram>();
private ShaderProgram init(String frag, String vert) {
LogHandler.log("Init frag: " + frag);
LogHandler.log("Init vert: " + vert);
FileHandle vFile = Gdx.files.internal(vert);
FileHandle fFile = Gdx.files.internal(frag);
if (!vFile.exists() || !fFile.exists()) {
LogHandler.throwEx("Shaders file not found!: " + frag + ", " + vert);
}
ShaderProgram sh = new ShaderProgram(vFile, fFile);
if (sh.isCompiled() == false) {
LogHandler.throwEx("Frag: " + frag + " Vert: " + vert + "--" + sh.getLog());
}
if (Config.ENABLE_ERROR_HANDLER) {
LogHandler.log("Frag: " + frag + " Vert: " + vert);
LogHandler.log(sh.getLog());
}
//ErrorHandler.log(sh.getLog());
sh.pedantic = false;
String name = frag + "|" + vert;
programs.remove(name);
programs.put(name, sh);
return sh;
}
publicvoid reloadAll() {
String[] keys = programs.keySet().toArray(new String[0]);
for (int i = 0; i < keys.length; i++) {
String val[] = keys[i].split("\\|");
init(val[0], val[1]);
}
}
public ShaderProgram getShader(String frag, String vert) {
ShaderProgram sh = programs.get(frag + "|" + vert);
if (sh != null)
return sh;
elsereturn init(frag, vert);
}
publicstaticvoid disposeAll() {
if (instance == null)
return;
for (ShaderProgram sh : instance.programs.values()) {
sh.dispose();
}
}
}