mirror of
https://github.com/GTNewHorizons/Chisel.git
synced 2026-11-25 06:34:35 +00:00
* make threadlocals final * minor code fixes * fix custom block renderers leaking world * remove now useless threadlocal cleaning * fix memory leak from rendererOld by just deleting the field * fix ThreadLocals to work on dedicated server
164 lines
6.6 KiB
Java
164 lines
6.6 KiB
Java
package team.chisel.client.render;
|
|
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.client.renderer.RenderBlocks;
|
|
import net.minecraft.client.renderer.texture.IIconRegister;
|
|
import net.minecraft.util.IIcon;
|
|
import net.minecraft.util.MathHelper;
|
|
import net.minecraft.world.IBlockAccess;
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
import org.apache.commons.lang3.tuple.Triple;
|
|
|
|
import com.cricketcraft.chisel.api.carving.CarvingUtils;
|
|
import com.cricketcraft.chisel.api.rendering.TextureType;
|
|
import com.cricketcraft.chisel.api.rendering.TextureType.AbstractSubmapManager;
|
|
|
|
import cpw.mods.fml.relauncher.FMLLaunchHandler;
|
|
import cpw.mods.fml.relauncher.Side;
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
import team.chisel.ctmlib.RenderBlocksCTM;
|
|
import team.chisel.ctmlib.TextureSubmap;
|
|
|
|
public class SubmapManagerCombinedCTM extends SubmapManagerBase {
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
private class RenderBlocksCombinedCTM extends RenderBlocksCTM {
|
|
|
|
@Override
|
|
public void renderFaceXNeg(Block block, double x, double y, double z, IIcon icon) {
|
|
setIcons(rType, x, y, z, ForgeDirection.WEST);
|
|
super.renderFaceXNeg(block, x, y, z, inWorld ? submapSmall.getSubIcon(0, 0) : icon);
|
|
}
|
|
|
|
@Override
|
|
public void renderFaceXPos(Block block, double x, double y, double z, IIcon icon) {
|
|
setIcons(rType, x, y, z, ForgeDirection.EAST);
|
|
super.renderFaceXPos(block, x, y, z, inWorld ? submapSmall.getSubIcon(0, 0) : icon);
|
|
}
|
|
|
|
@Override
|
|
public void renderFaceYNeg(Block block, double x, double y, double z, IIcon icon) {
|
|
setIcons(rType, x, y, z, ForgeDirection.DOWN);
|
|
super.renderFaceYNeg(block, x, y, z, inWorld ? submapSmall.getSubIcon(0, 0) : icon);
|
|
}
|
|
|
|
@Override
|
|
public void renderFaceYPos(Block block, double x, double y, double z, IIcon icon) {
|
|
setIcons(rType, x, y, z, ForgeDirection.UP);
|
|
super.renderFaceYPos(block, x, y, z, inWorld ? submapSmall.getSubIcon(0, 0) : icon);
|
|
}
|
|
|
|
@Override
|
|
public void renderFaceZNeg(Block block, double x, double y, double z, IIcon icon) {
|
|
setIcons(rType, x, y, z, ForgeDirection.NORTH);
|
|
super.renderFaceZNeg(block, x, y, z, inWorld ? submapSmall.getSubIcon(0, 0) : icon);
|
|
}
|
|
|
|
@Override
|
|
public void renderFaceZPos(Block block, double x, double y, double z, IIcon icon) {
|
|
setIcons(rType, x, y, z, ForgeDirection.SOUTH);
|
|
super.renderFaceZPos(block, x, y, z, inWorld ? submapSmall.getSubIcon(0, 0) : icon);
|
|
}
|
|
|
|
private void setIcons(TextureType type, double x, double y, double z, ForgeDirection side) {
|
|
int ix = MathHelper.floor_double(x);
|
|
int iy = MathHelper.floor_double(y);
|
|
int iz = MathHelper.floor_double(z);
|
|
if (type == TextureType.V4 || type == TextureType.V9) {
|
|
submap = (TextureSubmap) TextureType
|
|
.getVIcon(rType, SubmapManagerCombinedCTM.this.submap, ix, iy, iz, side.ordinal());
|
|
submapSmall = (TextureSubmap) TextureType
|
|
.getVIcon(rType, SubmapManagerCombinedCTM.this.smallSubmap, ix, iy, iz, side.ordinal());
|
|
} else {
|
|
submap = (TextureSubmap) TextureType
|
|
.getRIcon(rType, SubmapManagerCombinedCTM.this.submap, ix, iy, iz, side.ordinal());
|
|
submapSmall = (TextureSubmap) TextureType
|
|
.getRIcon(rType, SubmapManagerCombinedCTM.this.smallSubmap, ix, iy, iz, side.ordinal());
|
|
}
|
|
}
|
|
}
|
|
|
|
private static class Submap extends TextureSubmap {
|
|
|
|
private final TextureSubmap[][] submap;
|
|
|
|
public Submap(IIcon base, int wh, TextureSubmap[][] submap) {
|
|
super(base, wh, wh);
|
|
this.submap = submap;
|
|
}
|
|
|
|
@Override
|
|
public void texturesStitched() {
|
|
for (int i = 0; i < icons.length; i++) {
|
|
for (int j = 0; j < icons[i].length; j++) {
|
|
icons[i][j] = submap[i][j];
|
|
submap[i][j].texturesStitched();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private final ThreadLocal<RenderBlocksCombinedCTM> renderBlocksThreadLocal = FMLLaunchHandler.side()
|
|
.isClient() ? ThreadLocal.withInitial(RenderBlocksCombinedCTM::new) : null;
|
|
|
|
private TextureSubmap submap, smallSubmap;
|
|
private final int size;
|
|
private final String texturePath;
|
|
private final int meta;
|
|
private IIcon defaultIcon;
|
|
private final TextureType rType;
|
|
|
|
public SubmapManagerCombinedCTM(int meta, String texturePath, TextureType rType) {
|
|
assert rType == TextureType.R16 || rType == TextureType.R9
|
|
|| rType == TextureType.R4
|
|
|| rType == TextureType.V4
|
|
|| rType == TextureType.V9 : "Not a valid random type!";
|
|
|
|
this.meta = meta;
|
|
this.texturePath = texturePath;
|
|
this.size = Integer.parseInt(
|
|
rType.name()
|
|
.substring(1)); // >.>
|
|
this.rType = rType;
|
|
}
|
|
|
|
@Override
|
|
public IIcon getIcon(int side, int meta) {
|
|
return defaultIcon;
|
|
}
|
|
|
|
@Override
|
|
@SideOnly(Side.CLIENT)
|
|
public RenderBlocks createRenderContext(RenderBlocks rendererOld, Block block, IBlockAccess world) {
|
|
RenderBlocksCombinedCTM rb = renderBlocksThreadLocal.get();
|
|
rb.setRenderBoundsFromBlock(block);
|
|
return rb;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
@Override
|
|
@SideOnly(Side.CLIENT)
|
|
public void registerIcons(String modName, Block block, IIconRegister register) {
|
|
IIcon base = register.registerIcon(modName + ":" + texturePath);
|
|
int wh = (int) Math.sqrt(size);
|
|
TextureSubmap[][] submaps = new TextureSubmap[wh][wh];
|
|
TextureSubmap[][] submapsSmall = new TextureSubmap[wh][wh];
|
|
for (int i = 0; i < size; i++) {
|
|
AbstractSubmapManager manager = (AbstractSubmapManager) TextureType.CTMX
|
|
.createManagerFor(CarvingUtils.getDefaultVariationFor(block, meta, 0), texturePath + "-" + i);
|
|
manager.registerIcons(modName, block, register);
|
|
Object cached = manager.getCachedObject();
|
|
Triple<IIcon, TextureSubmap, TextureSubmap> triple = (Triple<IIcon, TextureSubmap, TextureSubmap>) cached;
|
|
submaps[i % wh][i / wh] = triple.getMiddle();
|
|
submapsSmall[i % wh][i / wh] = triple.getRight();
|
|
if (i == 0) {
|
|
defaultIcon = triple.getRight()
|
|
.getBaseIcon();
|
|
}
|
|
}
|
|
submap = new Submap(base, wh, submaps);
|
|
smallSubmap = new Submap(base, wh, submapsSmall);
|
|
}
|
|
}
|