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
13
14
15
16
17 public class DeferredTexture extends TextureImpl implements DeferredResource {
18
19 private final InputStream in;
20
21 private final String resourceName;
22
23 private final boolean flipped;
24
25 private final int filter;
26
27 private TextureImpl target;
28
29 private final int[] trans;
30
31
32
33
34
35
36
37
38
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
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
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
76
77 public void bind() {
78 checkTarget();
79
80 target.bind();
81 }
82
83
84
85
86 public float getHeight() {
87 checkTarget();
88
89 return target.getHeight();
90 }
91
92
93
94
95 public int getImageHeight() {
96 checkTarget();
97 return target.getImageHeight();
98 }
99
100
101
102
103 public int getImageWidth() {
104 checkTarget();
105 return target.getImageWidth();
106 }
107
108
109
110
111 public int getTextureHeight() {
112 checkTarget();
113 return target.getTextureHeight();
114 }
115
116
117
118
119 public int getTextureID() {
120 checkTarget();
121 return target.getTextureID();
122 }
123
124
125
126
127 public String getTextureRef() {
128 checkTarget();
129 return target.getTextureRef();
130 }
131
132
133
134
135 public int getTextureWidth() {
136 checkTarget();
137 return target.getTextureWidth();
138 }
139
140
141
142
143 public float getWidth() {
144 checkTarget();
145 return target.getWidth();
146 }
147
148
149
150
151 public void release() {
152 checkTarget();
153 target.release();
154 }
155
156
157
158
159 public void setHeight(int height) {
160 checkTarget();
161 target.setHeight(height);
162 }
163
164
165
166
167 public void setTextureHeight(int texHeight) {
168 checkTarget();
169 target.setTextureHeight(texHeight);
170 }
171
172
173
174
175 public void setTextureID(int textureID) {
176 checkTarget();
177 target.setTextureID(textureID);
178 }
179
180
181
182
183 public void setTextureWidth(int texWidth) {
184 checkTarget();
185 target.setTextureWidth(texWidth);
186 }
187
188
189
190
191 public void setWidth(int width) {
192 checkTarget();
193 target.setWidth(width);
194 }
195
196
197
198
199 public byte[] getTextureData() {
200 checkTarget();
201 return target.getTextureData();
202 }
203
204
205
206
207 public String getDescription() {
208 return resourceName;
209 }
210
211
212
213
214 public boolean hasAlpha() {
215 checkTarget();
216 return target.hasAlpha();
217 }
218
219
220
221
222 public void setTextureFilter(int textureFilter) {
223 checkTarget();
224 target.setTextureFilter(textureFilter);
225 }
226 }