    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.HashMap;
    006    import java.util.Iterator;
    007    import java.util.Map;
    008    import org.lwjgl.opengl.GL11;
    009
    010    @SideOnly(Side.CLIENT)
    011    public class RenderManager
    012    {
    013        /** A map of entity classes and the associated renderer. */
    014        public Map entityRenderMap = new HashMap();
    015
    016        /** The static instance of RenderManager. */
    017        public static RenderManager instance = new RenderManager();
    018
    019        /** Renders fonts */
    020        private FontRenderer fontRenderer;
    021        public static double renderPosX;
    022        public static double renderPosY;
    023        public static double renderPosZ;
    024        public RenderEngine renderEngine;
    025        public ItemRenderer itemRenderer;
    026
    027        /** Reference to the World object. */
    028        public World worldObj;
    029
    030        /** Rendermanager's variable for the player */
    031        public EntityLiving livingPlayer;
    032        public float playerViewY;
    033        public float playerViewX;
    034
    035        /** Reference to the GameSettings object. */
    036        public GameSettings options;
    037        public double viewerPosX;
    038        public double viewerPosY;
    039        public double viewerPosZ;
    040        public static boolean field_85095_o = false;
    041
    042        private RenderManager()
    043        {
    044            this.entityRenderMap.put(EntitySpider.class, new RenderSpider());
    045            this.entityRenderMap.put(EntityCaveSpider.class, new RenderSpider());
    046            this.entityRenderMap.put(EntityPig.class, new RenderPig(new ModelPig(), new ModelPig(0.5F), 0.7F));
    047            this.entityRenderMap.put(EntitySheep.class, new RenderSheep(new ModelSheep2(), new ModelSheep1(), 0.7F));
    048            this.entityRenderMap.put(EntityCow.class, new RenderCow(new ModelCow(), 0.7F));
    049            this.entityRenderMap.put(EntityMooshroom.class, new RenderMooshroom(new ModelCow(), 0.7F));
    050            this.entityRenderMap.put(EntityWolf.class, new RenderWolf(new ModelWolf(), new ModelWolf(), 0.5F));
    051            this.entityRenderMap.put(EntityChicken.class, new RenderChicken(new ModelChicken(), 0.3F));
    052            this.entityRenderMap.put(EntityOcelot.class, new RenderOcelot(new ModelOcelot(), 0.4F));
    053            this.entityRenderMap.put(EntitySilverfish.class, new RenderSilverfish());
    054            this.entityRenderMap.put(EntityCreeper.class, new RenderCreeper());
    055            this.entityRenderMap.put(EntityEnderman.class, new RenderEnderman());
    056            this.entityRenderMap.put(EntitySnowman.class, new RenderSnowMan());
    057            this.entityRenderMap.put(EntitySkeleton.class, new RenderSkeleton());
    058            this.entityRenderMap.put(EntityWitch.class, new RenderWitch());
    059            this.entityRenderMap.put(EntityBlaze.class, new RenderBlaze());
    060            this.entityRenderMap.put(EntityZombie.class, new RenderZombie());
    061            this.entityRenderMap.put(EntitySlime.class, new RenderSlime(new ModelSlime(16), new ModelSlime(0), 0.25F));
    062            this.entityRenderMap.put(EntityMagmaCube.class, new RenderMagmaCube());
    063            this.entityRenderMap.put(EntityPlayer.class, new RenderPlayer());
    064            this.entityRenderMap.put(EntityGiantZombie.class, new RenderGiantZombie(new ModelZombie(), 0.5F, 6.0F));
    065            this.entityRenderMap.put(EntityGhast.class, new RenderGhast());
    066            this.entityRenderMap.put(EntitySquid.class, new RenderSquid(new ModelSquid(), 0.7F));
    067            this.entityRenderMap.put(EntityVillager.class, new RenderVillager());
    068            this.entityRenderMap.put(EntityIronGolem.class, new RenderIronGolem());
    069            this.entityRenderMap.put(EntityLiving.class, new RenderLiving(new ModelBiped(), 0.5F));
    070            this.entityRenderMap.put(EntityBat.class, new RenderBat());
    071            this.entityRenderMap.put(EntityDragon.class, new RenderDragon());
    072            this.entityRenderMap.put(EntityEnderCrystal.class, new RenderEnderCrystal());
    073            this.entityRenderMap.put(EntityWither.class, new RenderWither());
    074            this.entityRenderMap.put(Entity.class, new RenderEntity());
    075            this.entityRenderMap.put(EntityPainting.class, new RenderPainting());
    076            this.entityRenderMap.put(EntityItemFrame.class, new RenderItemFrame());
    077            this.entityRenderMap.put(EntityArrow.class, new RenderArrow());
    078            this.entityRenderMap.put(EntitySnowball.class, new RenderSnowball(Item.snowball.getIconFromDamage(0)));
    079            this.entityRenderMap.put(EntityEnderPearl.class, new RenderSnowball(Item.enderPearl.getIconFromDamage(0)));
    080            this.entityRenderMap.put(EntityEnderEye.class, new RenderSnowball(Item.eyeOfEnder.getIconFromDamage(0)));
    081            this.entityRenderMap.put(EntityEgg.class, new RenderSnowball(Item.egg.getIconFromDamage(0)));
    082            this.entityRenderMap.put(EntityPotion.class, new RenderSnowball(154));
    083            this.entityRenderMap.put(EntityExpBottle.class, new RenderSnowball(Item.expBottle.getIconFromDamage(0)));
    084            this.entityRenderMap.put(EntityLargeFireball.class, new RenderFireball(2.0F));
    085            this.entityRenderMap.put(EntitySmallFireball.class, new RenderFireball(0.5F));
    086            this.entityRenderMap.put(EntityWitherSkull.class, new RenderWitherSkull());
    087            this.entityRenderMap.put(EntityItem.class, new RenderItem());
    088            this.entityRenderMap.put(EntityXPOrb.class, new RenderXPOrb());
    089            this.entityRenderMap.put(EntityTNTPrimed.class, new RenderTNTPrimed());
    090            this.entityRenderMap.put(EntityFallingSand.class, new RenderFallingSand());
    091            this.entityRenderMap.put(EntityMinecart.class, new RenderMinecart());
    092            this.entityRenderMap.put(EntityBoat.class, new RenderBoat());
    093            this.entityRenderMap.put(EntityFishHook.class, new RenderFish());
    094            this.entityRenderMap.put(EntityLightningBolt.class, new RenderLightningBolt());
    095            Iterator var1 = this.entityRenderMap.values().iterator();
    096
    097            while (var1.hasNext())
    098            {
    099                Render var2 = (Render)var1.next();
    100                var2.setRenderManager(this);
    101            }
    102        }
    103
    104        public Render getEntityClassRenderObject(Class par1Class)
    105        {
    106            Render var2 = (Render)this.entityRenderMap.get(par1Class);
    107
    108            if (var2 == null && par1Class != Entity.class)
    109            {
    110                var2 = this.getEntityClassRenderObject(par1Class.getSuperclass());
    111                this.entityRenderMap.put(par1Class, var2);
    112            }
    113
    114            return var2;
    115        }
    116
    117        public Render getEntityRenderObject(Entity par1Entity)
    118        {
    119            return this.getEntityClassRenderObject(par1Entity.getClass());
    120        }
    121
    122        /**
    123         * Caches the current frame's active render info, including the current World, RenderEngine, GameSettings and
    124         * FontRenderer settings, as well as interpolated player position, pitch and yaw.
    125         */
    126        public void cacheActiveRenderInfo(World par1World, RenderEngine par2RenderEngine, FontRenderer par3FontRenderer, EntityLiving par4EntityLiving, GameSettings par5GameSettings, float par6)
    127        {
    128            this.worldObj = par1World;
    129            this.renderEngine = par2RenderEngine;
    130            this.options = par5GameSettings;
    131            this.livingPlayer = par4EntityLiving;
    132            this.fontRenderer = par3FontRenderer;
    133
    134            if (par4EntityLiving.isPlayerSleeping())
    135            {
    136                int x = MathHelper.floor_double(par4EntityLiving.posX);
    137                int y = MathHelper.floor_double(par4EntityLiving.posY);
    138                int z = MathHelper.floor_double(par4EntityLiving.posZ);
    139                Block block = Block.blocksList[par1World.getBlockId(x, y, z)];
    140
    141                if (block != null && block.isBed(par1World, x, y, z, par4EntityLiving))
    142                {
    143                    int var9 = block.getBedDirection(par1World, x, y, z);;
    144                    this.playerViewY = (float)(var9 * 90 + 180);
    145                    this.playerViewX = 0.0F;
    146                }
    147            }
    148            else
    149            {
    150                this.playerViewY = par4EntityLiving.prevRotationYaw + (par4EntityLiving.rotationYaw - par4EntityLiving.prevRotationYaw) * par6;
    151                this.playerViewX = par4EntityLiving.prevRotationPitch + (par4EntityLiving.rotationPitch - par4EntityLiving.prevRotationPitch) * par6;
    152            }
    153
    154            if (par5GameSettings.thirdPersonView == 2)
    155            {
    156                this.playerViewY += 180.0F;
    157            }
    158
    159            this.viewerPosX = par4EntityLiving.lastTickPosX + (par4EntityLiving.posX - par4EntityLiving.lastTickPosX) * (double)par6;
    160            this.viewerPosY = par4EntityLiving.lastTickPosY + (par4EntityLiving.posY - par4EntityLiving.lastTickPosY) * (double)par6;
    161            this.viewerPosZ = par4EntityLiving.lastTickPosZ + (par4EntityLiving.posZ - par4EntityLiving.lastTickPosZ) * (double)par6;
    162        }
    163
    164        /**
    165         * Will render the specified entity at the specified partial tick time. Args: entity, partialTickTime
    166         */
    167        public void renderEntity(Entity par1Entity, float par2)
    168        {
    169            double var3 = par1Entity.lastTickPosX + (par1Entity.posX - par1Entity.lastTickPosX) * (double)par2;
    170            double var5 = par1Entity.lastTickPosY + (par1Entity.posY - par1Entity.lastTickPosY) * (double)par2;
    171            double var7 = par1Entity.lastTickPosZ + (par1Entity.posZ - par1Entity.lastTickPosZ) * (double)par2;
    172            float var9 = par1Entity.prevRotationYaw + (par1Entity.rotationYaw - par1Entity.prevRotationYaw) * par2;
    173            int var10 = par1Entity.getBrightnessForRender(par2);
    174
    175            if (par1Entity.isBurning())
    176            {
    177                var10 = 15728880;
    178            }
    179
    180            int var11 = var10 % 65536;
    181            int var12 = var10 / 65536;
    182            OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float)var11 / 1.0F, (float)var12 / 1.0F);
    183            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    184            this.renderEntityWithPosYaw(par1Entity, var3 - renderPosX, var5 - renderPosY, var7 - renderPosZ, var9, par2);
    185        }
    186
    187        /**
    188         * Renders the specified entity with the passed in position, yaw, and partialTickTime. Args: entity, x, y, z, yaw,
    189         * partialTickTime
    190         */
    191        public void renderEntityWithPosYaw(Entity par1Entity, double par2, double par4, double par6, float par8, float par9)
    192        {
    193            Render var10 = null;
    194
    195            try
    196            {
    197                var10 = this.getEntityRenderObject(par1Entity);
    198
    199                if (var10 != null && this.renderEngine != null)
    200                {
    201                    if (field_85095_o)
    202                    {
    203                        try
    204                        {
    205                            this.func_85094_b(par1Entity, par2, par4, par6, par8, par9);
    206                        }
    207                        catch (Throwable var17)
    208                        {
    209                            throw new ReportedException(CrashReport.func_85055_a(var17, "Rendering entity hitbox in world"));
    210                        }
    211                    }
    212
    213                    try
    214                    {
    215                        var10.doRender(par1Entity, par2, par4, par6, par8, par9);
    216                    }
    217                    catch (Throwable var16)
    218                    {
    219                        throw new ReportedException(CrashReport.func_85055_a(var16, "Rendering entity in world"));
    220                    }
    221
    222                    try
    223                    {
    224                        var10.doRenderShadowAndFire(par1Entity, par2, par4, par6, par8, par9);
    225                    }
    226                    catch (Throwable var15)
    227                    {
    228                        throw new ReportedException(CrashReport.func_85055_a(var15, "Post-rendering entity in world"));
    229                    }
    230                }
    231            }
    232            catch (Throwable var18)
    233            {
    234                CrashReport var12 = CrashReport.func_85055_a(var18, "Rendering entity in world");
    235                CrashReportCategory var13 = var12.func_85058_a("Entity being rendered");
    236                par1Entity.func_85029_a(var13);
    237                CrashReportCategory var14 = var12.func_85058_a("Renderer details");
    238                var14.addCrashSection("Assigned renderer", var10);
    239                var14.addCrashSection("Location", CrashReportCategory.func_85074_a(par2, par4, par6));
    240                var14.addCrashSection("Rotation", Float.valueOf(par8));
    241                var14.addCrashSection("Delta", Float.valueOf(par9));
    242                throw new ReportedException(var12);
    243            }
    244        }
    245
    246        private void func_85094_b(Entity par1Entity, double par2, double par4, double par6, float par8, float par9)
    247        {
    248            GL11.glDepthMask(false);
    249            GL11.glDisable(GL11.GL_TEXTURE_2D);
    250            GL11.glDisable(GL11.GL_LIGHTING);
    251            GL11.glDisable(GL11.GL_CULL_FACE);
    252            GL11.glDisable(GL11.GL_BLEND);
    253            GL11.glPushMatrix();
    254            Tessellator var10 = Tessellator.instance;
    255            var10.startDrawingQuads();
    256            var10.setColorRGBA(255, 255, 255, 32);
    257            double var11 = (double)(-par1Entity.width / 2.0F);
    258            double var13 = (double)(-par1Entity.width / 2.0F);
    259            double var15 = (double)(par1Entity.width / 2.0F);
    260            double var17 = (double)(-par1Entity.width / 2.0F);
    261            double var19 = (double)(-par1Entity.width / 2.0F);
    262            double var21 = (double)(par1Entity.width / 2.0F);
    263            double var23 = (double)(par1Entity.width / 2.0F);
    264            double var25 = (double)(par1Entity.width / 2.0F);
    265            double var27 = (double)par1Entity.height;
    266            var10.addVertex(par2 + var11, par4 + var27, par6 + var13);
    267            var10.addVertex(par2 + var11, par4, par6 + var13);
    268            var10.addVertex(par2 + var15, par4, par6 + var17);
    269            var10.addVertex(par2 + var15, par4 + var27, par6 + var17);
    270            var10.addVertex(par2 + var23, par4 + var27, par6 + var25);
    271            var10.addVertex(par2 + var23, par4, par6 + var25);
    272            var10.addVertex(par2 + var19, par4, par6 + var21);
    273            var10.addVertex(par2 + var19, par4 + var27, par6 + var21);
    274            var10.addVertex(par2 + var15, par4 + var27, par6 + var17);
    275            var10.addVertex(par2 + var15, par4, par6 + var17);
    276            var10.addVertex(par2 + var23, par4, par6 + var25);
    277            var10.addVertex(par2 + var23, par4 + var27, par6 + var25);
    278            var10.addVertex(par2 + var19, par4 + var27, par6 + var21);
    279            var10.addVertex(par2 + var19, par4, par6 + var21);
    280            var10.addVertex(par2 + var11, par4, par6 + var13);
    281            var10.addVertex(par2 + var11, par4 + var27, par6 + var13);
    282            var10.draw();
    283            GL11.glPopMatrix();
    284            GL11.glEnable(GL11.GL_TEXTURE_2D);
    285            GL11.glEnable(GL11.GL_LIGHTING);
    286            GL11.glEnable(GL11.GL_CULL_FACE);
    287            GL11.glDisable(GL11.GL_BLEND);
    288            GL11.glDepthMask(true);
    289        }
    290
    291        /**
    292         * World sets this RenderManager's worldObj to the world provided
    293         */
    294        public void set(World par1World)
    295        {
    296            this.worldObj = par1World;
    297        }
    298
    299        public double getDistanceToCamera(double par1, double par3, double par5)
    300        {
    301            double var7 = par1 - this.viewerPosX;
    302            double var9 = par3 - this.viewerPosY;
    303            double var11 = par5 - this.viewerPosZ;
    304            return var7 * var7 + var9 * var9 + var11 * var11;
    305        }
    306
    307        /**
    308         * Returns the font renderer
    309         */
    310        public FontRenderer getFontRenderer()
    311        {
    312            return this.fontRenderer;
    313        }
    314    }