summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/model/IHLBlockRenderer.java
blob: 81efbd93d505415f9500773576f7b4c3b6dbbed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ihl.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ihl.handpump.BlockWithCoordinates;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.World;

@SideOnly(value=Side.CLIENT)
public class IHLBlockRenderer {
	
	private RenderBlocks renderBlocks;
    private final Map<BlockWithCoordinates, Integer> renderCache = new HashMap<BlockWithCoordinates, Integer>();
	public static IHLBlockRenderer instance;
    
    public IHLBlockRenderer()
    {
    	renderBlocks=RenderBlocks.getInstance();
    	instance=this;
    }
    
    public void refreshDisplayLists(int diplayLists, List<BlockWithCoordinates> bwc, ChunkCache iBlockAccess) 
    {
        if (bwc==null || bwc.isEmpty())
        {
            return;
        }
       	renderBlocks.blockAccess=iBlockAccess;
       	GL11.glNewList(diplayLists, 4864 /*GL_COMPILE*/);
		Iterator<BlockWithCoordinates> bwci = bwc.iterator();
		while(bwci.hasNext())
		{
			BlockWithCoordinates bwce = bwci.next();
	       	renderBlock(bwce);
		}
       	GL11.glEndList();
    }
    
    
    public int getBlockDisplayLists(BlockWithCoordinates bwc, World world) {
        if (bwc.block == null)
        {
            return -1;
        }
        if(renderCache.containsKey(bwc))
        {
        	return renderCache.get(bwc);
        }
        int diplayLists = GLAllocation.generateDisplayLists(1);
        if(renderBlocks.blockAccess==null || !renderBlocks.blockAccess.equals(world))
        {
        	renderBlocks.blockAccess=world;
        }
       	GL11.glNewList(diplayLists, 4864 /*GL_COMPILE*/);
       	renderBlock(bwc);
       	GL11.glEndList();
        renderCache.put(bwc, diplayLists);
        return diplayLists;
    }
    
    private void renderBlock(BlockWithCoordinates bwc)
    {
       	Tessellator tessellator = Tessellator.instance;
       	tessellator.startDrawingQuads();
       	renderBlocks.renderBlockByRenderType(bwc.block, bwc.x, bwc.y, bwc.z);
       	tessellator.draw();
    }
}