You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.4 KiB
114 lines
2.4 KiB
package mightypork.rogue.texture;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import mightypork.rogue.AppAccess;
|
|
import mightypork.rogue.AppAdapter;
|
|
import mightypork.rogue.bus.events.ResourceLoadRequest;
|
|
import mightypork.utils.math.coord.Rect;
|
|
|
|
import org.newdawn.slick.opengl.Texture;
|
|
|
|
|
|
/**
|
|
* Texture loader and quad registry
|
|
*
|
|
* @author MightyPork
|
|
*/
|
|
public class TextureBank extends AppAdapter {
|
|
|
|
public TextureBank(AppAccess app) {
|
|
super(app);
|
|
}
|
|
|
|
private final HashMap<String, DeferredTexture> textures = new HashMap<String, DeferredTexture>();
|
|
|
|
private final HashMap<String, TxQuad> quads = new HashMap<String, TxQuad>();
|
|
|
|
private DeferredTexture lastTx;
|
|
|
|
|
|
/**
|
|
* Load a {@link Texture} from resource
|
|
*
|
|
* @param key texture key
|
|
* @param resourcePath texture resource path
|
|
*/
|
|
public void loadTexture(String key, String resourcePath)
|
|
{
|
|
DeferredTexture tx = new DeferredTexture(resourcePath);
|
|
bus().queue(new ResourceLoadRequest(tx));
|
|
|
|
textures.put(key, tx);
|
|
lastTx = tx;
|
|
}
|
|
|
|
|
|
/**
|
|
* Create a {@link TxQuad} in a texture
|
|
*
|
|
* @param quadKey quad key
|
|
* @param textureKey texture key
|
|
* @param quad quad rectangle (absolute pixel coordinates) *
|
|
*/
|
|
public void makeQuad(String quadKey, String textureKey, Rect quad)
|
|
{
|
|
DeferredTexture tx = textures.get(textureKey);
|
|
if (tx == null) throw new RuntimeException("Texture with key " + textureKey + " not defined!");
|
|
|
|
TxQuad txquad = tx.getQuad(quad);
|
|
|
|
quads.put(quadKey, txquad);
|
|
}
|
|
|
|
|
|
/**
|
|
* Create a {@link TxQuad} in the last loaded texture
|
|
*
|
|
* @param quadKey quad key
|
|
* @param quad quad rectangle (absolute pixel coordinates)
|
|
*/
|
|
public void makeQuad(String quadKey, Rect quad)
|
|
{
|
|
DeferredTexture tx = lastTx;
|
|
if (tx == null) throw new RuntimeException("There's no texture loaded yet, can't define quads!");
|
|
|
|
TxQuad txquad = tx.getQuad(quad);
|
|
|
|
quads.put(quadKey, txquad);
|
|
}
|
|
|
|
|
|
/**
|
|
* Get a {@link TxQuad} for key
|
|
*
|
|
* @param key quad key
|
|
* @return the quad
|
|
*/
|
|
public TxQuad getTxQuad(String key)
|
|
{
|
|
TxQuad q = quads.get(key);
|
|
|
|
if (q == null) throw new RuntimeException("There's no quad called " + key + "!");
|
|
|
|
return q;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get a loaded {@link Texture}
|
|
*
|
|
* @param key texture key
|
|
* @return the texture
|
|
*/
|
|
public Texture getTexture(String key)
|
|
{
|
|
Texture t = textures.get(key);
|
|
|
|
if (t == null) throw new RuntimeException("There's no texture called " + key + "!");
|
|
|
|
return t;
|
|
}
|
|
|
|
}
|
|
|