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