001    /*
002     * The FML Forge Mod Loader suite.
003     * Copyright (C) 2012 cpw
004     *
005     * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or any later version.
007     *
008     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
009     * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
010     *
011     * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
012     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
013     */
014    
015    package net.minecraft.src;
016    
017    import java.awt.Graphics2D;
018    import java.awt.image.BufferedImage;
019    import java.awt.image.ImageObserver;
020    
021    import org.lwjgl.opengl.GL11;
022    
023    import cpw.mods.fml.client.FMLClientHandler;
024    import cpw.mods.fml.client.FMLTextureFX;
025    
026    /**
027     * A texture override for animations, it takes a vertical image of
028     * texture frames and constantly rotates them in the texture.
029     */
030    public class ModTextureAnimation extends FMLTextureFX
031    {
032        private final int tickRate;
033        private byte[][] images;
034        private int index = 0;
035        private int ticks = 0;
036    
037        private String targetTex = null;
038        private BufferedImage imgData = null;
039    
040        public ModTextureAnimation(int icon, int target, BufferedImage image, int tickCount)
041        {
042            this(icon, 1, target, image, tickCount);
043        }
044    
045        public ModTextureAnimation(int icon, int size, int target, BufferedImage image, int tickCount)
046        {
047            this(icon, size, (target == 0 ? "/terrain.png" : "/gui/items.png"), image, tickCount);
048        }
049    
050        public ModTextureAnimation(int icon, int size, String target, BufferedImage image, int tickCount)
051        {
052            super(icon);
053            RenderEngine re = FMLClientHandler.instance().getClient().renderEngine;
054    
055            targetTex = target;
056            tileSize = size;
057            tileImage = re.getTexture(target);
058    
059            tickRate = tickCount;
060            ticks = tickCount;
061            imgData = image;
062        }
063    
064        @Override
065        public void setup()
066        {
067            super.setup();
068    
069            int sWidth  = imgData.getWidth();
070            int sHeight = imgData.getHeight();
071            int tWidth  = tileSizeBase;
072            int tHeight = tileSizeBase;
073    
074    
075            int frames = (int)Math.floor((double)(sHeight / sWidth));
076    
077            if (frames < 1)
078            {
079                throw new IllegalArgumentException(String.format("Attempted to create a TextureAnimation with no complete frames: %dx%d", sWidth, sHeight));
080            }
081            else
082            {
083                images = new byte[frames][];
084                BufferedImage image = imgData;
085    
086                if (sWidth != tWidth)
087                {
088                    BufferedImage b = new BufferedImage(tWidth, tHeight * frames, 6);
089                    Graphics2D g = b.createGraphics();
090                    g.drawImage(imgData, 0, 0, tWidth, tHeight * frames, 0, 0, sWidth, sHeight, (ImageObserver)null);
091                    g.dispose();
092                    image = b;
093                }
094    
095                for (int frame = 0; frame < frames; frame++)
096                {
097                    int[] pixels = new int[tileSizeSquare];
098                    image.getRGB(0, tHeight * frame, tWidth, tHeight, pixels, 0, tWidth);
099                    images[frame] = new byte[tileSizeSquare << 2];
100    
101                    for (int i = 0; i < pixels.length; i++)
102                    {
103                        int i4 = i * 4;
104                        images[frame][i4 + 0] = (byte)(pixels[i] >> 16 & 255);
105                        images[frame][i4 + 1] = (byte)(pixels[i] >> 8  & 255);
106                        images[frame][i4 + 2] = (byte)(pixels[i] >> 0  & 255);
107                        images[frame][i4 + 3] = (byte)(pixels[i] >> 24 & 255);
108                    }
109                }
110            }
111        }
112    
113        public void onTick()
114        {
115            if (++ticks >= tickRate)
116            {
117                if (++index >= images.length)
118                {
119                    index = 0;
120                }
121    
122                imageData = images[index];
123                ticks = 0;
124            }
125        }
126    
127        public void bindImage(RenderEngine renderEngine)
128        {
129            GL11.glBindTexture(GL11.GL_TEXTURE_2D, tileImage);
130        }
131    
132        // TODO: REMOVE THIS - just for you dan200
133        @Deprecated
134        public void func_783_a()
135        {
136            onTick();
137        }
138    
139    }