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 cpw.mods.fml.client; 016 017 import java.util.BitSet; 018 import java.util.HashMap; 019 import java.util.logging.Level; 020 021 import cpw.mods.fml.common.FMLCommonHandler; 022 import cpw.mods.fml.common.FMLLog; 023 024 /** 025 * @author cpw 026 * 027 */ 028 public class SpriteHelper 029 { 030 private static HashMap<String, BitSet> spriteInfo = new HashMap<String, BitSet>(); 031 032 private static void initMCSpriteMaps() { 033 BitSet slots = 034 SpriteHelper.toBitSet( 035 "0000000000000000" + 036 "0000000000110000" + 037 "0000000000100000" + 038 "0000000001100000" + 039 "0000000000000000" + 040 "0000000000000000" + 041 "0000000000000000" + 042 "0000000000000000" + 043 "0000000000000000" + 044 "0000000000000000" + 045 "0000000000000000" + 046 "0000000000011111" + 047 "0000000000000000" + 048 "0000000001111100" + 049 "0000000001111000" + 050 "0000000000000000"); 051 spriteInfo.put("/terrain.png", slots); 052 053 slots = SpriteHelper.toBitSet( 054 "0000000000000000" + 055 "0000000000000000" + 056 "0000000000000000" + 057 "0000000000000000" + 058 "0000000000000000" + 059 "0000000000000000" + 060 "0000000000000000" + 061 "0000000000000000" + 062 "0000000000000000" + 063 "0111110000000000" + 064 "1111111010000000" + 065 "0111111110000000" + 066 "0111111111111001" + 067 "1111111111111111" + 068 "0000011111111111" + 069 "0000000000000000"); 070 spriteInfo.put("/gui/items.png", slots); 071 } 072 /** 073 * Register a sprite map for ModTextureStatic, to allow for other mods to override 074 * your sprite page. 075 * 076 * 077 */ 078 public static void registerSpriteMapForFile(String file, String spriteMap) { 079 if (spriteInfo.size() == 0) { 080 initMCSpriteMaps(); 081 } 082 if (spriteInfo.containsKey(file)) { 083 FMLCommonHandler.instance().getFMLLogger().finer(String.format("Duplicate attempt to register a sprite file %s for overriding -- ignoring",file)); 084 return; 085 } 086 spriteInfo.put(file, toBitSet(spriteMap)); 087 } 088 089 public static int getUniqueSpriteIndex(String path) 090 { 091 if (!spriteInfo.containsKey("/terrain.png")) 092 { 093 initMCSpriteMaps(); 094 } 095 096 BitSet slots = spriteInfo.get(path); 097 098 if (slots == null) 099 { 100 Exception ex = new Exception(String.format("Invalid getUniqueSpriteIndex call for texture: %s", path)); 101 FMLLog.log(Level.SEVERE, ex, "A critical error has been detected with sprite overrides"); 102 FMLCommonHandler.instance().raiseException(ex,"Invalid request to getUniqueSpriteIndex",true); 103 } 104 105 int ret = getFreeSlot(slots); 106 107 if (ret == -1) 108 { 109 Exception ex = new Exception(String.format("No more sprite indicies left for: %s", path)); 110 FMLLog.log(Level.SEVERE, ex, "There are no sprite indicies left for %s", path); 111 FMLCommonHandler.instance().raiseException(ex,"No more sprite indicies left", true); 112 } 113 return ret; 114 } 115 116 public static BitSet toBitSet(String data) 117 { 118 BitSet ret = new BitSet(data.length()); 119 for (int x = 0; x < data.length(); x++) 120 { 121 ret.set(x, data.charAt(x) == '1'); 122 } 123 return ret; 124 } 125 126 public static int getFreeSlot(BitSet slots) 127 { 128 int next=slots.nextSetBit(0); 129 slots.clear(next); 130 return next; 131 } 132 133 public static int freeSlotCount(String textureToOverride) 134 { 135 return spriteInfo.get(textureToOverride).cardinality(); 136 } 137 138 }