    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.client;
    014
    015import java.awt.Dimension;
    016import java.awt.image.BufferedImage;
    017import java.io.IOException;
    018import java.io.InputStream;
    019import java.util.Map;
    020
    021import javax.imageio.ImageIO;
    022
    023import org.lwjgl.opengl.ContextCapabilities;
    024import org.lwjgl.opengl.GLContext;
    025
    026import com.google.common.collect.Maps;
    027
    028import cpw.mods.fml.common.FMLLog;
    029
    030import net.minecraft.client.Minecraft;
    031import net.minecraft.client.renderer.RenderEngine;
    032
    033public class TextureFXManager
    034{
    035    private static final TextureFXManager INSTANCE = new TextureFXManager();
    036
    037    private Minecraft client;
    038
    039    private Map<Integer,TextureHolder> texturesById = Maps.newHashMap();
    040    private Map<String, TextureHolder> texturesByName = Maps.newHashMap();
    041
    042    private TextureHelper helper;
    043
    044    void setClient(Minecraft client)
    045    {
    046        this.client = client;
    047    }
    048    public BufferedImage loadImageFromTexturePack(RenderEngine renderEngine, String path) throws IOException
    049    {
    050        InputStream image=client.texturePackList.getSelectedTexturePack().getResourceAsStream(path);
    051        if (image==null) {
    052            throw new RuntimeException(String.format("The requested image path %s is not found",path));
    053        }
    054        BufferedImage result=ImageIO.read(image);
    055        if (result==null)
    056        {
    057            throw new RuntimeException(String.format("The requested image path %s appears to be corrupted",path));
    058        }
    059        return result;
    060    }
    061
    062    public static TextureFXManager instance()
    063    {
    064        return INSTANCE;
    065    }
    066
    067    public void fixTransparency(BufferedImage loadedImage, String textureName)
    068    {
    069        if (textureName.matches("^/mob/.*_eyes.*.png$"))
    070        {
    071            for (int x = 0; x < loadedImage.getWidth(); x++) {
    072                for (int y = 0; y < loadedImage.getHeight(); y++) {
    073                    int argb = loadedImage.getRGB(x, y);
    074                    if ((argb & 0xff000000) == 0 && argb != 0) {
    075                        loadedImage.setRGB(x, y, 0);
    076                    }
    077                }
    078            }
    079        }
    080    }
    081    public void bindTextureToName(String name, int index)
    082    {
    083        TextureHolder holder = new TextureHolder();
    084        holder.textureId = index;
    085        holder.textureName = name;
    086        texturesById.put(index,holder);
    087        texturesByName.put(name,holder);
    088    }
    089
    090    public void setTextureDimensions(int index, int j, int k)
    091    {
    092        TextureHolder holder = texturesById.get(index);
    093        if (holder == null)
    094        {
    095            return;
    096        }
    097        holder.x = j;
    098        holder.y = k;
    099    }
    100
    101    private class TextureHolder {
    102        private int textureId;
    103        private String textureName;
    104        private int x;
    105        private int y;
    106    }
    107
    108    public Dimension getTextureDimensions(String texture)
    109    {
    110        return texturesByName.containsKey(texture) ? new Dimension(texturesByName.get(texture).x, texturesByName.get(texture).y) : new Dimension(1,1);
    111    }
    112
    113
    114    public TextureHelper getHelper()
    115    {
    116        if (helper == null)
    117        {
    118            ContextCapabilities capabilities = GLContext.getCapabilities();
    119            boolean has43 = false;
    120            try
    121            {
    122                has43 = capabilities.getClass().getField("GL_ARB_copy_image").getBoolean(capabilities);
    123            }
    124            catch (Exception e)
    125            {
    126                e.printStackTrace();
    127                // NOOP - LWJGL needs updating
    128                FMLLog.info("Forge Mod Loader has detected an older LWJGL version, new advanced texture animation features are disabled");
    129            }
    130//            if (has43 && Boolean.parseBoolean(System.getProperty("fml.useGL43","true")))
    131//            {
    132//                FMLLog.info("Using the new OpenGL 4.3 advanced capability for animations");
    133//                helper = new OpenGL43TextureHelper();
    134//            }
    135//            else
    136            {
    137                FMLLog.info("Not using advanced OpenGL 4.3 advanced capability for animations : OpenGL 4.3 is %s", has43 ? "available" : "not available");
    138                helper = new CopySubimageTextureHelper();
    139            }
    140        }
    141        return helper;
    142    }
    143}