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 cpw.mods.fml.client.FMLClientHandler;
022    import cpw.mods.fml.client.FMLTextureFX;
023    
024    /**
025     * A texture override for animations, it takes a vertical image of 
026     * texture frames and constantly rotates them in the texture.
027     */
028    public class ModTextureAnimation extends FMLTextureFX
029    {
030        private final int tickRate;
031        private byte[][] images;
032        private int index = 0;
033        private int ticks = 0;
034        
035        private String targetTex = null;
036        private BufferedImage imgData = null;
037        
038        public ModTextureAnimation(int icon, int target, BufferedImage image, int tickCount)
039        {
040            this(icon, 1, target, image, tickCount);
041        }
042    
043        public ModTextureAnimation(int icon, int size, int target, BufferedImage image, int tickCount)    
044        {
045            this(icon, size, (target == 0 ? "/terrain.png" : "/gui/items.png"), image, tickCount);
046        }
047        
048        public ModTextureAnimation(int icon, int size, String target, BufferedImage image, int tickCount)
049        {
050            super(icon);
051            RenderEngine re = FMLClientHandler.instance().getClient().renderEngine;
052            
053            targetTex = target;
054            tileSize = size;
055            tileImage = re.getTexture(target);
056            
057            tickRate = tickCount;
058            ticks = tickCount;
059            imgData = image;
060        }
061        
062        @Override
063        public void setup()
064        {
065            super.setup();
066            
067            int sWidth  = imgData.getWidth();
068            int sHeight = imgData.getHeight();
069            int tWidth  = tileSizeBase;
070            int tHeight = tileSizeBase;
071            
072            
073            int frames = (int)Math.floor((double)(sHeight / sWidth));
074    
075            if (frames < 1)
076            {
077                throw new IllegalArgumentException(String.format("Attempted to create a TextureAnimation with no complete frames: %dx%d", sWidth, sHeight));
078            }
079            else
080            {
081                images = new byte[frames][];
082                BufferedImage image = imgData;
083                
084                if (sWidth != tWidth)
085                {
086                    BufferedImage b = new BufferedImage(tWidth, tHeight * frames, 6);
087                    Graphics2D g = b.createGraphics();
088                    g.drawImage(imgData, 0, 0, tWidth, tHeight * frames, 0, 0, sWidth, sHeight, (ImageObserver)null);
089                    g.dispose();
090                    image = b;
091                }
092    
093                for (int frame = 0; frame < frames; frame++)
094                {
095                    int[] pixels = new int[tileSizeSquare];
096                    image.getRGB(0, tHeight * frame, tWidth, tHeight, pixels, 0, tWidth);
097                    images[frame] = new byte[tileSizeSquare << 2];
098    
099                    for (int i = 0; i < pixels.length; i++)
100                    {
101                        int i4 = i * 4;
102                        images[frame][i4 + 0] = (byte)(pixels[i] >> 16 & 255);
103                        images[frame][i4 + 1] = (byte)(pixels[i] >> 8  & 255);
104                        images[frame][i4 + 2] = (byte)(pixels[i] >> 0  & 255);
105                        images[frame][i4 + 3] = (byte)(pixels[i] >> 24 & 255);
106                    }
107                }
108            }
109        }
110        
111        public void func_783_a()
112        {
113            if (++ticks >= tickRate)
114            {
115                if (++index >= images.length)
116                {
117                    index = 0;
118                }
119    
120                imageData = images[index];
121                ticks = 0;
122            }
123        }
124    }