Path: blob/master/SLICK_HOME/src/org/newdawn/slick/opengl/DeferredTexture.java
1461 views
package org.newdawn.slick.opengl;12import java.io.IOException;3import java.io.InputStream;45import org.newdawn.slick.loading.DeferredResource;6import org.newdawn.slick.loading.LoadingList;78/**9* A texture proxy that can be used to load a texture at a later date while still10* allowing elements to reference it11*12* @author kevin13*/14public class DeferredTexture extends TextureImpl implements DeferredResource {15/** The stream to read the texture from */16private InputStream in;17/** The name of the resource to load */18private String resourceName;19/** True if the image should be flipped */20private boolean flipped;21/** The filter to apply to the texture */22private int filter;23/** The texture we're proxying for */24private TextureImpl target;25/** The color to be transparent */26private int[] trans;2728/**29* Create a new deferred texture30*31* @param in The input stream from which to read the texture32* @param resourceName The name to give the resource33* @param flipped True if the image should be flipped34* @param filter The filter to apply35* @param trans The colour to defined as transparent36*/37public DeferredTexture(InputStream in, String resourceName, boolean flipped, int filter, int[] trans) {38this.in = in;39this.resourceName = resourceName;40this.flipped = flipped;41this.filter = filter;42this.trans = trans;4344LoadingList.get().add(this);45}4647/**48* @see org.newdawn.slick.loading.DeferredResource#load()49*/50public void load() throws IOException {51boolean before = InternalTextureLoader.get().isDeferredLoading();52InternalTextureLoader.get().setDeferredLoading(false);53target = InternalTextureLoader.get().getTexture(in, resourceName, flipped, filter, trans);54InternalTextureLoader.get().setDeferredLoading(before);55}5657/**58* Check if the target has been obtained already59*/60private void checkTarget() {61if (target == null) {62try {63load();64LoadingList.get().remove(this);65return;66} catch (IOException e) {67throw new RuntimeException("Attempt to use deferred texture before loading and resource not found: "+resourceName);68}69}70}7172/**73* @see org.newdawn.slick.opengl.TextureImpl#bind()74*/75public void bind() {76checkTarget();7778target.bind();79}8081/**82* @see org.newdawn.slick.opengl.TextureImpl#getHeight()83*/84public float getHeight() {85checkTarget();8687return target.getHeight();88}8990/**91* @see org.newdawn.slick.opengl.TextureImpl#getImageHeight()92*/93public int getImageHeight() {94checkTarget();95return target.getImageHeight();96}9798/**99* @see org.newdawn.slick.opengl.TextureImpl#getImageWidth()100*/101public int getImageWidth() {102checkTarget();103return target.getImageWidth();104}105106/**107* @see org.newdawn.slick.opengl.TextureImpl#getTextureHeight()108*/109public int getTextureHeight() {110checkTarget();111return target.getTextureHeight();112}113114/**115* @see org.newdawn.slick.opengl.TextureImpl#getTextureID()116*/117public int getTextureID() {118checkTarget();119return target.getTextureID();120}121122/**123* @see org.newdawn.slick.opengl.TextureImpl#getTextureRef()124*/125public String getTextureRef() {126checkTarget();127return target.getTextureRef();128}129130/**131* @see org.newdawn.slick.opengl.TextureImpl#getTextureWidth()132*/133public int getTextureWidth() {134checkTarget();135return target.getTextureWidth();136}137138/**139* @see org.newdawn.slick.opengl.TextureImpl#getWidth()140*/141public float getWidth() {142checkTarget();143return target.getWidth();144}145146/**147* @see org.newdawn.slick.opengl.TextureImpl#release()148*/149public void release() {150checkTarget();151target.release();152}153154/**155* @see org.newdawn.slick.opengl.TextureImpl#setAlpha(boolean)156*/157public void setAlpha(boolean alpha) {158checkTarget();159target.setAlpha(alpha);160}161162/**163* @see org.newdawn.slick.opengl.TextureImpl#setHeight(int)164*/165public void setHeight(int height) {166checkTarget();167target.setHeight(height);168}169170/**171* @see org.newdawn.slick.opengl.TextureImpl#setTextureHeight(int)172*/173public void setTextureHeight(int texHeight) {174checkTarget();175target.setTextureHeight(texHeight);176}177178/**179* @see org.newdawn.slick.opengl.TextureImpl#setTextureID(int)180*/181public void setTextureID(int textureID) {182checkTarget();183target.setTextureID(textureID);184}185186/**187* @see org.newdawn.slick.opengl.TextureImpl#setTextureWidth(int)188*/189public void setTextureWidth(int texWidth) {190checkTarget();191target.setTextureWidth(texWidth);192}193194/**195* @see org.newdawn.slick.opengl.TextureImpl#setWidth(int)196*/197public void setWidth(int width) {198checkTarget();199target.setWidth(width);200}201202/**203* @see org.newdawn.slick.opengl.TextureImpl#getTextureData()204*/205public byte[] getTextureData() {206checkTarget();207return target.getTextureData();208}209210/**211* @see org.newdawn.slick.loading.DeferredResource#getDescription()212*/213public String getDescription() {214return resourceName;215}216217/**218* @see org.newdawn.slick.opengl.Texture#hasAlpha()219*/220public boolean hasAlpha() {221checkTarget();222return target.hasAlpha();223}224}225226227