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