TextureAtlasSprite .java

net.minecraft.client.renderer.texture.TextureAtlasSprite (class): members: clearFramesTextureData, setLocationNameCompass, interpolatedFrameData, minU.

net.minecraft.client.renderer.textureclass433 linesclient14.1 KB

API Surface (28)

TextureAtlasSprite makeAtlasSprite(ResourceLocation spriteResourceLocation)
void setLocationNameClock(String clockName)
void setLocationNameCompass(String compassName)
void initSprite(int inX, int inY, int originInX, int originInY, boolean rotatedIn)
void copyFrom(TextureAtlasSprite atlasSpirit)
int getOriginX()
int getOriginY()
int getIconWidth()
int getIconHeight()
float getMinU()
float getMaxU()
float getInterpolatedU(double u)
float getMinV()
float getMaxV()
float getInterpolatedV(double v)
String getIconName()
void updateAnimation()
int[][] getFrameTextureData(int index)
int getFrameCount()
void setIconWidth(int newWidth)
void setIconHeight(int newHeight)
void loadSprite(BufferedImage[] images, AnimationMetadataSection meta)
void generateMipmaps(int level)
String call()
void clearFramesTextureData()
boolean hasAnimationMetadata()
void setFramesTextureData(List<int[][]> newFramesTextureData)
String toString()

Fields & Constants (17)

iconName
framesTextureData
interpolatedFrameData
animationMetadata
rotated
originX
originY
width
height
minU
maxU
minV
maxV
frameCounter
tickCounter
locationNameClock
locationNameCompass

String literals (13)

"builtin/clock"
"builtin/compass"
"broken aspect ratio and not an animation"
"invalid frameindex "
"Generating mipmaps for frame"
"Frame being iterated"
"Frame index"
"Frame sizes"
"TextureAtlasSprite{name=\'"
", frameCount="
", rotated="
", height="
", width="

Full Source

TextureAtlasSprite.java433 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.client.renderer.texture;
2
3
import com.google.common.collect.Lists;
4
import java.awt.image.BufferedImage;
5
import java.io.IOException;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.concurrent.Callable;
9
import net.minecraft.client.resources.data.AnimationFrame;
10
import net.minecraft.client.resources.data.AnimationMetadataSection;
11
import net.minecraft.crash.CrashReport;
12
import net.minecraft.crash.CrashReportCategory;
13
import net.minecraft.util.ReportedException;
14
import net.minecraft.util.ResourceLocation;
15
16
public class TextureAtlasSprite
17
{
18
    private final String iconName;
19
    protected List<int[][]> framesTextureData = Lists.<int[][]>newArrayList();
20
    protected int[][] interpolatedFrameData;
21
    private AnimationMetadataSection animationMetadata;
22
    protected boolean rotated;
23
    protected int originX;
24
    protected int originY;
25
    protected int width;
26
    protected int height;
27
    private float minU;
28
    private float maxU;
29
    private float minV;
30
    private float maxV;
31
    protected int frameCounter;
32
    protected int tickCounter;
33
    private static String locationNameClock = "builtin/clock";
34
    private static String locationNameCompass = "builtin/compass";
35
36
    protected TextureAtlasSprite(String spriteName)
37
    {
38
        this.iconName = spriteName;
39
    }
40
41
    protected static TextureAtlasSprite makeAtlasSprite(ResourceLocation spriteResourceLocation)
42
    {
43
        String s = spriteResourceLocation.toString();
44
        return (TextureAtlasSprite)(locationNameClock.equals(s) ? new TextureClock(s) : (locationNameCompass.equals(s) ? new TextureCompass(s) : new TextureAtlasSprite(s)));
45
    }
46
47
    public static void setLocationNameClock(String clockName)
48
    {
49
        locationNameClock = clockName;
50
    }
51
52
    public static void setLocationNameCompass(String compassName)
53
    {
54
        locationNameCompass = compassName;
55
    }
56
57
    public void initSprite(int inX, int inY, int originInX, int originInY, boolean rotatedIn)
58
    {
59
        this.originX = originInX;
60
        this.originY = originInY;
61
        this.rotated = rotatedIn;
62
        float f = (float)(0.009999999776482582D / (double)inX);
63
        float f1 = (float)(0.009999999776482582D / (double)inY);
64
        this.minU = (float)originInX / (float)((double)inX) + f;
65
        this.maxU = (float)(originInX + this.width) / (float)((double)inX) - f;
66
        this.minV = (float)originInY / (float)inY + f1;
67
        this.maxV = (float)(originInY + this.height) / (float)inY - f1;
68
    }
69
70
    public void copyFrom(TextureAtlasSprite atlasSpirit)
71
    {
72
        this.originX = atlasSpirit.originX;
73
        this.originY = atlasSpirit.originY;
74
        this.width = atlasSpirit.width;
75
        this.height = atlasSpirit.height;
76
        this.rotated = atlasSpirit.rotated;
77
        this.minU = atlasSpirit.minU;
78
        this.maxU = atlasSpirit.maxU;
79
        this.minV = atlasSpirit.minV;
80
        this.maxV = atlasSpirit.maxV;
81
    }
82
83
    /**
84
     * Returns the X position of this icon on its texture sheet, in pixels.
85
     */
86
    public int getOriginX()
87
    {
88
        return this.originX;
89
    }
90
91
    /**
92
     * Returns the Y position of this icon on its texture sheet, in pixels.
93
     */
94
    public int getOriginY()
95
    {
96
        return this.originY;
97
    }
98
99
    /**
100
     * Returns the width of the icon, in pixels.
101
     */
102
    public int getIconWidth()
103
    {
104
        return this.width;
105
    }
106
107
    /**
108
     * Returns the height of the icon, in pixels.
109
     */
110
    public int getIconHeight()
111
    {
112
        return this.height;
113
    }
114
115
    /**
116
     * Returns the minimum U coordinate to use when rendering with this icon.
117
     */
118
    public float getMinU()
119
    {
120
        return this.minU;
121
    }
122
123
    /**
124
     * Returns the maximum U coordinate to use when rendering with this icon.
125
     */
126
    public float getMaxU()
127
    {
128
        return this.maxU;
129
    }
130
131
    /**
132
     * Gets a U coordinate on the icon. 0 returns uMin and 16 returns uMax. Other arguments return in-between values.
133
     */
134
    public float getInterpolatedU(double u)
135
    {
136
        float f = this.maxU - this.minU;
137
        return this.minU + f * (float)u / 16.0F;
138
    }
139
140
    /**
141
     * Returns the minimum V coordinate to use when rendering with this icon.
142
     */
143
    public float getMinV()
144
    {
145
        return this.minV;
146
    }
147
148
    /**
149
     * Returns the maximum V coordinate to use when rendering with this icon.
150
     */
151
    public float getMaxV()
152
    {
153
        return this.maxV;
154
    }
155
156
    /**
157
     * Gets a V coordinate on the icon. 0 returns vMin and 16 returns vMax. Other arguments return in-between values.
158
     */
159
    public float getInterpolatedV(double v)
160
    {
161
        float f = this.maxV - this.minV;
162
        return this.minV + f * ((float)v / 16.0F);
163
    }
164
165
    public String getIconName()
166
    {
167
        return this.iconName;
168
    }
169
170
    public void updateAnimation()
171
    {
172
        ++this.tickCounter;
173
174
        if (this.tickCounter >= this.animationMetadata.getFrameTimeSingle(this.frameCounter))
175
        {
176
            int i = this.animationMetadata.getFrameIndex(this.frameCounter);
177
            int j = this.animationMetadata.getFrameCount() == 0 ? this.framesTextureData.size() : this.animationMetadata.getFrameCount();
178
            this.frameCounter = (this.frameCounter + 1) % j;
179
            this.tickCounter = 0;
180
            int k = this.animationMetadata.getFrameIndex(this.frameCounter);
181
182
            if (i != k && k >= 0 && k < this.framesTextureData.size())
183
            {
184
                TextureUtil.uploadTextureMipmap((int[][])this.framesTextureData.get(k), this.width, this.height, this.originX, this.originY, false, false);
185
            }
186
        }
187
        else if (this.animationMetadata.isInterpolate())
188
        {
189
            this.updateAnimationInterpolated();
190
        }
191
    }
192
193
    private void updateAnimationInterpolated()
194
    {
195
        double d0 = 1.0D - (double)this.tickCounter / (double)this.animationMetadata.getFrameTimeSingle(this.frameCounter);
196
        int i = this.animationMetadata.getFrameIndex(this.frameCounter);
197
        int j = this.animationMetadata.getFrameCount() == 0 ? this.framesTextureData.size() : this.animationMetadata.getFrameCount();
198
        int k = this.animationMetadata.getFrameIndex((this.frameCounter + 1) % j);
199
200
        if (i != k && k >= 0 && k < this.framesTextureData.size())
201
        {
202
            int[][] aint = (int[][])this.framesTextureData.get(i);
203
            int[][] aint1 = (int[][])this.framesTextureData.get(k);
204
205
            if (this.interpolatedFrameData == null || this.interpolatedFrameData.length != aint.length)
206
            {
207
                this.interpolatedFrameData = new int[aint.length][];
208
            }
209
210
            for (int l = 0; l < aint.length; ++l)
211
            {
212
                if (this.interpolatedFrameData[l] == null)
213
                {
214
                    this.interpolatedFrameData[l] = new int[aint[l].length];
215
                }
216
217
                if (l < aint1.length && aint1[l].length == aint[l].length)
218
                {
219
                    for (int i1 = 0; i1 < aint[l].length; ++i1)
220
                    {
221
                        int j1 = aint[l][i1];
222
                        int k1 = aint1[l][i1];
223
                        int l1 = (int)((double)((j1 & 16711680) >> 16) * d0 + (double)((k1 & 16711680) >> 16) * (1.0D - d0));
224
                        int i2 = (int)((double)((j1 & 65280) >> 8) * d0 + (double)((k1 & 65280) >> 8) * (1.0D - d0));
225
                        int j2 = (int)((double)(j1 & 255) * d0 + (double)(k1 & 255) * (1.0D - d0));
226
                        this.interpolatedFrameData[l][i1] = j1 & -16777216 | l1 << 16 | i2 << 8 | j2;
227
                    }
228
                }
229
            }
230
231
            TextureUtil.uploadTextureMipmap(this.interpolatedFrameData, this.width, this.height, this.originX, this.originY, false, false);
232
        }
233
    }
234
235
    public int[][] getFrameTextureData(int index)
236
    {
237
        return (int[][])this.framesTextureData.get(index);
238
    }
239
240
    public int getFrameCount()
241
    {
242
        return this.framesTextureData.size();
243
    }
244
245
    public void setIconWidth(int newWidth)
246
    {
247
        this.width = newWidth;
248
    }
249
250
    public void setIconHeight(int newHeight)
251
    {
252
        this.height = newHeight;
253
    }
254
255
    public void loadSprite(BufferedImage[] images, AnimationMetadataSection meta) throws IOException
256
    {
257
        this.resetSprite();
258
        int i = images[0].getWidth();
259
        int j = images[0].getHeight();
260
        this.width = i;
261
        this.height = j;
262
        int[][] aint = new int[images.length][];
263
264
        for (int k = 0; k < images.length; ++k)
265
        {
266
            BufferedImage bufferedimage = images[k];
267
268
            if (bufferedimage != null)
269
            {
270
                if (k > 0 && (bufferedimage.getWidth() != i >> k || bufferedimage.getHeight() != j >> k))
271
                {
272
                    throw new RuntimeException(String.format("Unable to load miplevel: %d, image is size: %dx%d, expected %dx%d", new Object[] {Integer.valueOf(k), Integer.valueOf(bufferedimage.getWidth()), Integer.valueOf(bufferedimage.getHeight()), Integer.valueOf(i >> k), Integer.valueOf(j >> k)}));
273
                }
274
275
                aint[k] = new int[bufferedimage.getWidth() * bufferedimage.getHeight()];
276
                bufferedimage.getRGB(0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), aint[k], 0, bufferedimage.getWidth());
277
            }
278
        }
279
280
        if (meta == null)
281
        {
282
            if (j != i)
283
            {
284
                throw new RuntimeException("broken aspect ratio and not an animation");
285
            }
286
287
            this.framesTextureData.add(aint);
288
        }
289
        else
290
        {
291
            int j1 = j / i;
292
            int k1 = i;
293
            int l = i;
294
            this.height = this.width;
295
296
            if (meta.getFrameCount() > 0)
297
            {
298
                Iterator iterator = meta.getFrameIndexSet().iterator();
299
300
                while (iterator.hasNext())
301
                {
302
                    int i1 = ((Integer)iterator.next()).intValue();
303
304
                    if (i1 >= j1)
305
                    {
306
                        throw new RuntimeException("invalid frameindex " + i1);
307
                    }
308
309
                    this.allocateFrameTextureData(i1);
310
                    this.framesTextureData.set(i1, getFrameTextureData(aint, k1, l, i1));
311
                }
312
313
                this.animationMetadata = meta;
314
            }
315
            else
316
            {
317
                List<AnimationFrame> list = Lists.<AnimationFrame>newArrayList();
318
319
                for (int l1 = 0; l1 < j1; ++l1)
320
                {
321
                    this.framesTextureData.add(getFrameTextureData(aint, k1, l, l1));
322
                    list.add(new AnimationFrame(l1, -1));
323
                }
324
325
                this.animationMetadata = new AnimationMetadataSection(list, this.width, this.height, meta.getFrameTime(), meta.isInterpolate());
326
            }
327
        }
328
    }
329
330
    public void generateMipmaps(int level)
331
    {
332
        List<int[][]> list = Lists.<int[][]>newArrayList();
333
334
        for (int i = 0; i < this.framesTextureData.size(); ++i)
335
        {
336
            final int[][] aint = (int[][])this.framesTextureData.get(i);
337
338
            if (aint != null)
339
            {
340
                try
341
                {
342
                    list.add(TextureUtil.generateMipmapData(level, this.width, aint));
343
                }
344
                catch (Throwable throwable)
345
                {
346
                    CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Generating mipmaps for frame");
347
                    CrashReportCategory crashreportcategory = crashreport.makeCategory("Frame being iterated");
348
                    crashreportcategory.addCrashSection("Frame index", Integer.valueOf(i));
349
                    crashreportcategory.addCrashSectionCallable("Frame sizes", new Callable<String>()
350
                    {
351
                        public String call() throws Exception
352
                        {
353
                            StringBuilder stringbuilder = new StringBuilder();
354
355
                            for (int[] aint1 : aint)
356
                            {
357
                                if (stringbuilder.length() > 0)
358
                                {
359
                                    stringbuilder.append(", ");
360
                                }
361
362
                                stringbuilder.append(aint1 == null ? "null" : Integer.valueOf(aint1.length));
363
                            }
364
365
                            return stringbuilder.toString();
366
                        }
367
                    });
368
                    throw new ReportedException(crashreport);
369
                }
370
            }
371
        }
372
373
        this.setFramesTextureData(list);
374
    }
375
376
    private void allocateFrameTextureData(int index)
377
    {
378
        if (this.framesTextureData.size() <= index)
379
        {
380
            for (int i = this.framesTextureData.size(); i <= index; ++i)
381
            {
382
                this.framesTextureData.add((int[][])null);
383
            }
384
        }
385
    }
386
387
    private static int[][] getFrameTextureData(int[][] data, int rows, int columns, int p_147962_3_)
388
    {
389
        int[][] aint = new int[data.length][];
390
391
        for (int i = 0; i < data.length; ++i)
392
        {
393
            int[] aint1 = data[i];
394
395
            if (aint1 != null)
396
            {
397
                aint[i] = new int[(rows >> i) * (columns >> i)];
398
                System.arraycopy(aint1, p_147962_3_ * aint[i].length, aint[i], 0, aint[i].length);
399
            }
400
        }
401
402
        return aint;
403
    }
404
405
    public void clearFramesTextureData()
406
    {
407
        this.framesTextureData.clear();
408
    }
409
410
    public boolean hasAnimationMetadata()
411
    {
412
        return this.animationMetadata != null;
413
    }
414
415
    public void setFramesTextureData(List<int[][]> newFramesTextureData)
416
    {
417
        this.framesTextureData = newFramesTextureData;
418
    }
419
420
    private void resetSprite()
421
    {
422
        this.animationMetadata = null;
423
        this.setFramesTextureData(Lists.<int[][]>newArrayList());
424
        this.frameCounter = 0;
425
        this.tickCounter = 0;
426
    }
427
428
    public String toString()
429
    {
430
        return "TextureAtlasSprite{name=\'" + this.iconName + '\'' + ", frameCount=" + this.framesTextureData.size() + ", rotated=" + this.rotated + ", x=" + this.originX + ", y=" + this.originY + ", height=" + this.height + ", width=" + this.width + ", u0=" + this.minU + ", u1=" + this.maxU + ", v0=" + this.minV + ", v1=" + this.maxV + '}';
431
    }
432
}
433

Deobfuscated MCP 1.8.9 source of TextureAtlasSprite from Marcelektro/MavenMCP-1.8.9 (net/minecraft/client/renderer/texture/TextureAtlasSprite.java), stored verbatim.