WarpPI/engine-gpu/src/main/java/it/cavallium/warppi/gui/graphicengine/impl/jogl/JOGLSkin.java

76 lines
1.8 KiB
Java
Raw Normal View History

2018-09-22 10:29:40 +02:00
package it.cavallium.warppi.gui.graphicengine.impl.jogl;
import java.io.IOException;
2017-11-19 22:58:37 +01:00
import java.nio.file.Files;
import java.nio.file.Paths;
import com.jogamp.opengl.GLException;
import com.jogamp.opengl.util.texture.Texture;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Skin;
2018-09-22 10:29:40 +02:00
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLRenderer.OpenedTextureData;
2018-09-22 10:29:40 +02:00
public class JOGLSkin implements Skin {
public Texture t;
public int w;
public int h;
private String texturePath;
private boolean initialized = false;
private boolean isResource;
2018-05-12 21:18:29 +02:00
2018-09-22 11:17:30 +02:00
JOGLSkin(final GraphicEngine d, final String file) throws IOException {
load(file);
}
@Override
2018-09-22 11:17:30 +02:00
public void load(final String file) throws IOException {
2018-05-12 21:18:29 +02:00
final boolean isResource = !Files.exists(Paths.get(file));
2018-09-22 11:17:30 +02:00
if (isResource && this.getClass().getClassLoader().getResource(file) == null)
throw new IOException("File '" + file + "' not found!");
texturePath = file;
this.isResource = isResource;
}
@Override
2018-09-22 11:17:30 +02:00
public void initialize(final GraphicEngine d) {
try {
2018-09-22 10:29:40 +02:00
final OpenedTextureData i = JOGLRenderer.openTexture(texturePath, isResource);
t = JOGLRenderer.importTexture(i.f, i.deleteOnExit);
2017-11-19 22:58:37 +01:00
w = i.w;
h = i.h;
2018-09-22 10:29:40 +02:00
((JOGLEngine) d).registerTexture(t);
initialized = true;
} catch (GLException | IOException e) {
e.printStackTrace();
System.exit(1);
}
}
@Override
2018-09-22 11:17:30 +02:00
public void use(final GraphicEngine d) {
if (!initialized)
initialize(d);
2018-09-22 10:29:40 +02:00
final JOGLRenderer r = (JOGLRenderer) d.getRenderer();
r.useTexture(t, w, h);
}
2017-09-15 23:24:12 +02:00
@Override
public boolean isInitialized() {
return initialized;
}
2018-04-02 00:47:33 +02:00
@Override
public int getSkinWidth() {
return w;
}
@Override
public int getSkinHeight() {
return h;
}
}