View Javadoc
1   package org.newdawn.slick.opengl;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   
6   import org.newdawn.slick.loading.DeferredResource;
7   import org.newdawn.slick.loading.LoadingList;
8   
9   import javax.annotation.Nullable;
10  
11  /**
12   * A texture proxy that can be used to load a texture at a later date while still
13   * allowing elements to reference it
14   *
15   * @author kevin
16   */
17  public class DeferredTexture extends TextureImpl implements DeferredResource {
18      /** The stream to read the texture from */
19      private final InputStream in;
20      /** The name of the resource to load */
21      private final String resourceName;
22      /** True if the image should be flipped */
23      private final boolean flipped;
24      /** The filter to apply to the texture */
25      private final int filter;
26      /** The texture we're proxying for */
27      private TextureImpl target;
28      /** The color to be transparent */
29      private final int[] trans;
30  
31      /**
32       * Create a new deferred texture
33       *
34       * @param in The input stream from which to read the texture
35       * @param resourceName The name to give the resource
36        * @param flipped True if the image should be flipped
37       * @param filter The filter to apply
38       * @param trans The colour to defined as transparent
39       */
40      public DeferredTexture(InputStream in, String resourceName, boolean flipped, int filter, @Nullable int[] trans) {
41          this.in = in;
42          this.resourceName = resourceName;
43          this.flipped = flipped;
44          this.filter = filter;
45          this.trans = trans;
46  
47          LoadingList.get().add(this);
48      }
49  
50      /**
51       * @see org.newdawn.slick.loading.DeferredResource#load()
52       */
53      public void load() throws IOException {
54          boolean before = InternalTextureLoader.get().isDeferredLoading();
55          InternalTextureLoader.get().setDeferredLoading(false);
56          target = InternalTextureLoader.get().getTexture(in, resourceName, flipped, filter, trans);
57          InternalTextureLoader.get().setDeferredLoading(before);
58      }
59  
60      /**
61       * Check if the target has been obtained already
62       */
63      private void checkTarget() {
64          if (target == null) {
65              try {
66                  load();
67                  LoadingList.get().remove(this);
68              } catch (IOException e) {
69                  throw new RuntimeException("Attempt to use deferred texture before loading and resource not found: "+resourceName);
70              }
71          }
72      }
73  
74      /**
75       * @see org.newdawn.slick.opengl.TextureImpl#bind()
76       */
77      public void bind() {
78          checkTarget();
79  
80          target.bind();
81      }
82  
83      /**
84       * @see org.newdawn.slick.opengl.TextureImpl#getHeight()
85       */
86      public float getHeight() {
87          checkTarget();
88  
89          return target.getHeight();
90      }
91  
92      /**
93       * @see org.newdawn.slick.opengl.TextureImpl#getImageHeight()
94       */
95      public int getImageHeight() {
96          checkTarget();
97          return target.getImageHeight();
98      }
99  
100     /**
101      * @see org.newdawn.slick.opengl.TextureImpl#getImageWidth()
102      */
103     public int getImageWidth() {
104         checkTarget();
105         return target.getImageWidth();
106     }
107 
108     /**
109      * @see org.newdawn.slick.opengl.TextureImpl#getTextureHeight()
110      */
111     public int getTextureHeight() {
112         checkTarget();
113         return target.getTextureHeight();
114     }
115 
116     /**
117      * @see org.newdawn.slick.opengl.TextureImpl#getTextureID()
118      */
119     public int getTextureID() {
120         checkTarget();
121         return target.getTextureID();
122     }
123 
124     /**
125      * @see org.newdawn.slick.opengl.TextureImpl#getTextureRef()
126      */
127     public String getTextureRef() {
128         checkTarget();
129         return target.getTextureRef();
130     }
131 
132     /**
133      * @see org.newdawn.slick.opengl.TextureImpl#getTextureWidth()
134      */
135     public int getTextureWidth() {
136         checkTarget();
137         return target.getTextureWidth();
138     }
139 
140     /**
141      * @see org.newdawn.slick.opengl.TextureImpl#getWidth()
142      */
143     public float getWidth() {
144         checkTarget();
145         return target.getWidth();
146     }
147 
148     /**
149      * @see org.newdawn.slick.opengl.TextureImpl#release()
150      */
151     public void release() {
152         checkTarget();
153         target.release();
154     }
155 
156     /**
157      * @see org.newdawn.slick.opengl.TextureImpl#setHeight(int)
158      */
159     public void setHeight(int height) {
160         checkTarget();
161         target.setHeight(height);
162     }
163 
164     /**
165      * @see org.newdawn.slick.opengl.TextureImpl#setTextureHeight(int)
166      */
167     public void setTextureHeight(int texHeight) {
168         checkTarget();
169         target.setTextureHeight(texHeight);
170     }
171 
172     /**
173      * @see org.newdawn.slick.opengl.TextureImpl#setTextureID(int)
174      */
175     public void setTextureID(int textureID) {
176         checkTarget();
177         target.setTextureID(textureID);
178     }
179 
180     /**
181      * @see org.newdawn.slick.opengl.TextureImpl#setTextureWidth(int)
182      */
183     public void setTextureWidth(int texWidth) {
184         checkTarget();
185         target.setTextureWidth(texWidth);
186     }
187 
188     /**
189      * @see org.newdawn.slick.opengl.TextureImpl#setWidth(int)
190      */
191     public void setWidth(int width) {
192         checkTarget();
193         target.setWidth(width);
194     }
195 
196     /**
197      * @see org.newdawn.slick.opengl.TextureImpl#getTextureData()
198      */
199     public byte[] getTextureData() {
200         checkTarget();
201         return target.getTextureData();
202     }
203 
204     /**
205      * @see org.newdawn.slick.loading.DeferredResource#getDescription()
206      */
207     public String getDescription() {
208         return resourceName;
209     }
210 
211     /**
212      * @see org.newdawn.slick.opengl.Texture#hasAlpha()
213      */
214     public boolean hasAlpha() {
215         checkTarget();
216         return target.hasAlpha();
217     }
218     
219     /**
220      * @see org.newdawn.slick.opengl.Texture#setTextureFilter(int)
221      */
222     public void setTextureFilter(int textureFilter) {
223         checkTarget();
224         target.setTextureFilter(textureFilter);
225     }
226 }