| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import net.minecraft.block.material.MapColor;
|
| 6 | import net.minecraft.client.renderer.GlStateManager;
|
| 7 | import net.minecraft.client.renderer.Tessellator;
|
| 8 | import net.minecraft.client.renderer.WorldRenderer;
|
| 9 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 10 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 11 | import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
| 12 | import net.minecraft.util.ResourceLocation;
|
| 13 | import net.minecraft.util.Vec4b;
|
| 14 | import net.minecraft.world.storage.MapData;
|
| 15 |
|
| 16 | public class MapItemRenderer
|
| 17 | {
|
| 18 | private static final ResourceLocation mapIcons = new ResourceLocation("textures/map/map_icons.png");
|
| 19 | private final TextureManager textureManager;
|
| 20 | private final Map<String, MapItemRenderer.Instance> loadedMaps = Maps.<String, MapItemRenderer.Instance>newHashMap();
|
| 21 |
|
| 22 | public MapItemRenderer(TextureManager textureManagerIn)
|
| 23 | {
|
| 24 | this.textureManager = textureManagerIn;
|
| 25 | }
|
| 26 |
|
| 27 | /**
|
| 28 | * Updates a map texture
|
| 29 | */
|
| 30 | public void updateMapTexture(MapData mapdataIn)
|
| 31 | {
|
| 32 | this.getMapRendererInstance(mapdataIn).updateMapTexture();
|
| 33 | }
|
| 34 |
|
| 35 | public void renderMap(MapData mapdataIn, boolean p_148250_2_)
|
| 36 | {
|
| 37 | this.getMapRendererInstance(mapdataIn).render(p_148250_2_);
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Returns {@link net.minecraft.client.gui.MapItemRenderer.Instance MapItemRenderer.Instance} with given map data
|
| 42 | */
|
| 43 | private MapItemRenderer.Instance getMapRendererInstance(MapData mapdataIn)
|
| 44 | {
|
| 45 | MapItemRenderer.Instance mapitemrenderer$instance = (MapItemRenderer.Instance)this.loadedMaps.get(mapdataIn.mapName);
|
| 46 |
|
| 47 | if (mapitemrenderer$instance == null)
|
| 48 | {
|
| 49 | mapitemrenderer$instance = new MapItemRenderer.Instance(mapdataIn);
|
| 50 | this.loadedMaps.put(mapdataIn.mapName, mapitemrenderer$instance);
|
| 51 | }
|
| 52 |
|
| 53 | return mapitemrenderer$instance;
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * Clears the currently loaded maps and removes their corresponding textures
|
| 58 | */
|
| 59 | public void clearLoadedMaps()
|
| 60 | {
|
| 61 | for (MapItemRenderer.Instance mapitemrenderer$instance : this.loadedMaps.values())
|
| 62 | {
|
| 63 | this.textureManager.deleteTexture(mapitemrenderer$instance.location);
|
| 64 | }
|
| 65 |
|
| 66 | this.loadedMaps.clear();
|
| 67 | }
|
| 68 |
|
| 69 | class Instance
|
| 70 | {
|
| 71 | private final MapData mapData;
|
| 72 | private final DynamicTexture mapTexture;
|
| 73 | private final ResourceLocation location;
|
| 74 | private final int[] mapTextureData;
|
| 75 |
|
| 76 | private Instance(MapData mapdataIn)
|
| 77 | {
|
| 78 | this.mapData = mapdataIn;
|
| 79 | this.mapTexture = new DynamicTexture(128, 128);
|
| 80 | this.mapTextureData = this.mapTexture.getTextureData();
|
| 81 | this.location = MapItemRenderer.this.textureManager.getDynamicTextureLocation("map/" + mapdataIn.mapName, this.mapTexture);
|
| 82 |
|
| 83 | for (int i = 0; i < this.mapTextureData.length; ++i)
|
| 84 | {
|
| 85 | this.mapTextureData[i] = 0;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | private void updateMapTexture()
|
| 90 | {
|
| 91 | for (int i = 0; i < 16384; ++i)
|
| 92 | {
|
| 93 | int j = this.mapData.colors[i] & 255;
|
| 94 |
|
| 95 | if (j / 4 == 0)
|
| 96 | {
|
| 97 | this.mapTextureData[i] = (i + i / 128 & 1) * 8 + 16 << 24;
|
| 98 | }
|
| 99 | else
|
| 100 | {
|
| 101 | this.mapTextureData[i] = MapColor.mapColorArray[j / 4].getMapColor(j & 3);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | this.mapTexture.updateDynamicTexture();
|
| 106 | }
|
| 107 |
|
| 108 | private void render(boolean noOverlayRendering)
|
| 109 | {
|
| 110 | int i = 0;
|
| 111 | int j = 0;
|
| 112 | Tessellator tessellator = Tessellator.getInstance();
|
| 113 | WorldRenderer worldrenderer = tessellator.getWorldRenderer();
|
| 114 | float f = 0.0F;
|
| 115 | MapItemRenderer.this.textureManager.bindTexture(this.location);
|
| 116 | GlStateManager.enableBlend();
|
| 117 | GlStateManager.tryBlendFuncSeparate(1, 771, 0, 1);
|
| 118 | GlStateManager.disableAlpha();
|
| 119 | worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX);
|
| 120 | worldrenderer.pos((double)((float)(i + 0) + f), (double)((float)(j + 128) - f), -0.009999999776482582D).tex(0.0D, 1.0D).endVertex();
|
| 121 | worldrenderer.pos((double)((float)(i + 128) - f), (double)((float)(j + 128) - f), -0.009999999776482582D).tex(1.0D, 1.0D).endVertex();
|
| 122 | worldrenderer.pos((double)((float)(i + 128) - f), (double)((float)(j + 0) + f), -0.009999999776482582D).tex(1.0D, 0.0D).endVertex();
|
| 123 | worldrenderer.pos((double)((float)(i + 0) + f), (double)((float)(j + 0) + f), -0.009999999776482582D).tex(0.0D, 0.0D).endVertex();
|
| 124 | tessellator.draw();
|
| 125 | GlStateManager.enableAlpha();
|
| 126 | GlStateManager.disableBlend();
|
| 127 | MapItemRenderer.this.textureManager.bindTexture(MapItemRenderer.mapIcons);
|
| 128 | int k = 0;
|
| 129 |
|
| 130 | for (Vec4b vec4b : this.mapData.mapDecorations.values())
|
| 131 | {
|
| 132 | if (!noOverlayRendering || vec4b.func_176110_a() == 1)
|
| 133 | {
|
| 134 | GlStateManager.pushMatrix();
|
| 135 | GlStateManager.translate((float)i + (float)vec4b.func_176112_b() / 2.0F + 64.0F, (float)j + (float)vec4b.func_176113_c() / 2.0F + 64.0F, -0.02F);
|
| 136 | GlStateManager.rotate((float)(vec4b.func_176111_d() * 360) / 16.0F, 0.0F, 0.0F, 1.0F);
|
| 137 | GlStateManager.scale(4.0F, 4.0F, 3.0F);
|
| 138 | GlStateManager.translate(-0.125F, 0.125F, 0.0F);
|
| 139 | byte b0 = vec4b.func_176110_a();
|
| 140 | float f1 = (float)(b0 % 4 + 0) / 4.0F;
|
| 141 | float f2 = (float)(b0 / 4 + 0) / 4.0F;
|
| 142 | float f3 = (float)(b0 % 4 + 1) / 4.0F;
|
| 143 | float f4 = (float)(b0 / 4 + 1) / 4.0F;
|
| 144 | worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX);
|
| 145 | float f5 = -0.001F;
|
| 146 | worldrenderer.pos(-1.0D, 1.0D, (double)((float)k * -0.001F)).tex((double)f1, (double)f2).endVertex();
|
| 147 | worldrenderer.pos(1.0D, 1.0D, (double)((float)k * -0.001F)).tex((double)f3, (double)f2).endVertex();
|
| 148 | worldrenderer.pos(1.0D, -1.0D, (double)((float)k * -0.001F)).tex((double)f3, (double)f4).endVertex();
|
| 149 | worldrenderer.pos(-1.0D, -1.0D, (double)((float)k * -0.001F)).tex((double)f1, (double)f4).endVertex();
|
| 150 | tessellator.draw();
|
| 151 | GlStateManager.popMatrix();
|
| 152 | ++k;
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | GlStateManager.pushMatrix();
|
| 157 | GlStateManager.translate(0.0F, 0.0F, -0.04F);
|
| 158 | GlStateManager.scale(1.0F, 1.0F, 1.0F);
|
| 159 | GlStateManager.popMatrix();
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|