diff --git a/ClassicalSharp/Model/ChickenModel.cs b/ClassicalSharp/Model/ChickenModel.cs index 18e4aadfa..b5e79b1ce 100644 --- a/ClassicalSharp/Model/ChickenModel.cs +++ b/ClassicalSharp/Model/ChickenModel.cs @@ -17,7 +17,8 @@ namespace ClassicalSharp.Model { LeftWing = MakeWing( -0.25f, -0.1875f ); RightWing = MakeWing( 0.1875f, 0.25f ); - DefaultTexId = graphics.CreateTexture( "chicken.png" ); + if( cache.ChickenTexId <= 0 ) + cache.ChickenTexId = graphics.CreateTexture( "chicken.png" ); } ModelPart MakeHead() { @@ -61,7 +62,7 @@ namespace ClassicalSharp.Model { protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.ChickenTexId : p.MobTextureId; graphics.BindTexture( texId ); graphics.AlphaTest = true; @@ -75,10 +76,6 @@ namespace ClassicalSharp.Model { DrawRotate( 0.1875f, 0.6875f, 0, 0, 0, Math.Abs( p.rightArmXRot ), RightWing ); } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - } - ModelPart Head, Head2, Head3, Torso, LeftLeg, RightLeg, LeftWing, RightWing; } } \ No newline at end of file diff --git a/ClassicalSharp/Model/CreeperModel.cs b/ClassicalSharp/Model/CreeperModel.cs index a75406b9b..277fca294 100644 --- a/ClassicalSharp/Model/CreeperModel.cs +++ b/ClassicalSharp/Model/CreeperModel.cs @@ -15,7 +15,8 @@ namespace ClassicalSharp.Model { LeftLegBack = MakeLeg( -0.25f, 0, 0.125f, 0.375f ); RightLegBack = MakeLeg( 0, 0.25f, 0.125f, 0.375f ); - DefaultTexId = graphics.CreateTexture( "creeper.png" ); + if( cache.CreeperTexId <= 0 ) + cache.CreeperTexId = graphics.CreateTexture( "creeper.png" ); } ModelPart MakeHead() { @@ -44,7 +45,7 @@ namespace ClassicalSharp.Model { protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.CreeperTexId : p.MobTextureId; graphics.BindTexture( texId ); DrawRotate( 0, 1.125f, 0, -p.PitchRadians, 0, 0, Head ); @@ -56,10 +57,6 @@ namespace ClassicalSharp.Model { graphics.AlphaTest = true; } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - } - ModelPart Head, Torso, LeftLegFront, RightLegFront, LeftLegBack, RightLegBack; } } \ No newline at end of file diff --git a/ClassicalSharp/Model/IModel.cs b/ClassicalSharp/Model/IModel.cs index e4c138aa2..e153a2784 100644 --- a/ClassicalSharp/Model/IModel.cs +++ b/ClassicalSharp/Model/IModel.cs @@ -42,9 +42,8 @@ namespace ClassicalSharp.Model { protected abstract void DrawPlayerModel( Player p ); - public abstract void Dispose(); - - public int DefaultTexId; + public virtual void Dispose() { + } protected FastColour col; protected VertexPos3fTex2fCol4b[] vertices; diff --git a/ClassicalSharp/Model/ModelCache.cs b/ClassicalSharp/Model/ModelCache.cs index 8e43bc124..cc8d758d2 100644 --- a/ClassicalSharp/Model/ModelCache.cs +++ b/ClassicalSharp/Model/ModelCache.cs @@ -16,13 +16,15 @@ namespace ClassicalSharp.Model { public void InitCache() { vertices = new VertexPos3fTex2fCol4b[384]; - vb = game.Graphics.CreateDynamicVb( VertexFormat.Pos3fTex2fCol4b, vertices.Length ); + vb = api.CreateDynamicVb( VertexFormat.Pos3fTex2fCol4b, vertices.Length ); cache["humanoid"] = new PlayerModel( game ); } internal int vb; internal VertexPos3fTex2fCol4b[] vertices; Dictionary cache = new Dictionary(); + internal int ChickenTexId, CreeperTexId, PigTexId, SheepTexId, + SkeletonTexId, SpiderTexId, ZombieTexId, SheepFurTexId, HumanoidTexId; public IModel GetModel( string modelName ) { IModel model; @@ -72,7 +74,16 @@ namespace ClassicalSharp.Model { foreach( var entry in cache ) { entry.Value.Dispose(); } - game.Graphics.DeleteDynamicVb( vb ); + api.DeleteDynamicVb( vb ); + api.DeleteTexture( ref ChickenTexId ); + api.DeleteTexture( ref CreeperTexId ); + api.DeleteTexture( ref PigTexId ); + api.DeleteTexture( ref SheepTexId ); + api.DeleteTexture( ref SkeletonTexId ); + api.DeleteTexture( ref SpiderTexId ); + api.DeleteTexture( ref ZombieTexId ); + api.DeleteTexture( ref SheepFurTexId ); + api.DeleteTexture( ref HumanoidTexId ); } } } diff --git a/ClassicalSharp/Model/PigModel.cs b/ClassicalSharp/Model/PigModel.cs index f149ec4bf..de7aff27d 100644 --- a/ClassicalSharp/Model/PigModel.cs +++ b/ClassicalSharp/Model/PigModel.cs @@ -15,7 +15,8 @@ namespace ClassicalSharp.Model { LeftLegBack = MakeLeg( -0.3125f, -0.0625f, 0.3125f, 0.5625f ); RightLegBack = MakeLeg( 0.0625f, 0.3125f, 0.3125f, 0.5625f ); - DefaultTexId = graphics.CreateTexture( "pig.png" ); + if( cache.PigTexId <= 0 ) + cache.PigTexId = graphics.CreateTexture( "pig.png" ); } ModelPart MakeHead() { @@ -44,7 +45,7 @@ namespace ClassicalSharp.Model { protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.PigTexId : p.MobTextureId; graphics.BindTexture( texId ); DrawRotate( 0, 0.75f, -0.375f, -p.PitchRadians, 0, 0, Head ); @@ -56,10 +57,6 @@ namespace ClassicalSharp.Model { graphics.AlphaTest = true; } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - } - ModelPart Head, Torso, LeftLegFront, RightLegFront, LeftLegBack, RightLegBack; } } \ No newline at end of file diff --git a/ClassicalSharp/Model/PlayerModel.cs b/ClassicalSharp/Model/PlayerModel.cs index b6b30cc45..1026e3c46 100644 --- a/ClassicalSharp/Model/PlayerModel.cs +++ b/ClassicalSharp/Model/PlayerModel.cs @@ -39,7 +39,7 @@ namespace ClassicalSharp.Model { using( Bitmap bmp = new Bitmap( "char.png" ) ) { window.DefaultPlayerSkinType = Utils.GetSkinType( bmp ); - DefaultTexId = graphics.CreateTexture( bmp ); + cache.HumanoidTexId = graphics.CreateTexture( bmp ); } } @@ -86,7 +86,7 @@ namespace ClassicalSharp.Model { ModelSet model; protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.PlayerTextureId <= 0 ? DefaultTexId : p.PlayerTextureId; + int texId = p.PlayerTextureId <= 0 ? cache.HumanoidTexId : p.PlayerTextureId; graphics.BindTexture( texId ); SkinType skinType = p.SkinType; @@ -104,10 +104,6 @@ namespace ClassicalSharp.Model { DrawRotate( 0, 1.4375f, 0, -p.PitchRadians, 0, 0, model.Hat ); } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - } - class ModelSet { public ModelPart Head, Torso, LeftLeg, RightLeg, LeftArm, RightArm, Hat; diff --git a/ClassicalSharp/Model/SheepModel.cs b/ClassicalSharp/Model/SheepModel.cs index ef26ff06d..bcecce959 100644 --- a/ClassicalSharp/Model/SheepModel.cs +++ b/ClassicalSharp/Model/SheepModel.cs @@ -27,8 +27,10 @@ namespace ClassicalSharp.Model { FurRightLegBack = MakeFurLeg( 0.03125f, 0.34375f, 0.28125f, 0.59375f ); } - DefaultTexId = graphics.CreateTexture( "sheep.png" ); - furTextureId = graphics.CreateTexture( "sheep_fur.png" ); + if( cache.SheepTexId <= 0 ) + cache.SheepTexId = graphics.CreateTexture( "sheep.png" ); + if( cache.SheepFurTexId <= 0 ) + cache.SheepFurTexId = graphics.CreateTexture( "sheep_fur.png" ); } ModelPart MakeHead() { @@ -69,7 +71,7 @@ namespace ClassicalSharp.Model { protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.SheepTexId : p.MobTextureId; graphics.BindTexture( texId ); DrawRotate( 0, 1.125f, -0.5f, -p.PitchRadians, 0, 0, Head ); @@ -80,7 +82,7 @@ namespace ClassicalSharp.Model { DrawRotate( 0, 0.75f, 0.4375f, p.leftLegXRot, 0, 0, RightLegBack ); graphics.AlphaTest = true; if( Fur ) { - graphics.BindTexture( furTextureId ); + graphics.BindTexture( cache.SheepFurTexId ); DrawPart( FurTorso ); DrawRotate( 0, 1.125f, -0.5f, -p.PitchRadians, 0, 0, FurHead ); DrawRotate( 0, 0.75f, -0.3125f, p.leftLegXRot, 0, 0, FurLeftLegFront ); @@ -90,13 +92,6 @@ namespace ClassicalSharp.Model { } } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - if( Fur ) { - graphics.DeleteTexture( ref furTextureId ); - } - } - ModelPart Head, Torso, LeftLegFront, RightLegFront, LeftLegBack, RightLegBack; ModelPart FurHead, FurTorso, FurLeftLegFront, FurRightLegFront, FurLeftLegBack, FurRightLegBack; } diff --git a/ClassicalSharp/Model/SkeletonModel.cs b/ClassicalSharp/Model/SkeletonModel.cs index 1d39daf84..d6a29463d 100644 --- a/ClassicalSharp/Model/SkeletonModel.cs +++ b/ClassicalSharp/Model/SkeletonModel.cs @@ -1,5 +1,4 @@ using System; -using ClassicalSharp.GraphicsAPI; using OpenTK; namespace ClassicalSharp.Model { @@ -15,7 +14,8 @@ namespace ClassicalSharp.Model { LeftArm = MakeLeftArm( 0.375f, 0.25f ); RightArm = MakeRightArm( 0.25f, 0.375f ); - DefaultTexId = graphics.CreateTexture( "skeleton.png" ); + if( cache.SkeletonTexId <= 0 ) + cache.SkeletonTexId = graphics.CreateTexture( "skeleton.png" ); } ModelPart MakeLeftArm( float x1, float x2 ) { @@ -57,7 +57,7 @@ namespace ClassicalSharp.Model { protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; graphics.AlphaTest = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.SkeletonTexId : p.MobTextureId; graphics.BindTexture( texId ); DrawRotate( 0, 1.5f, 0, -p.PitchRadians, 0, 0, Head ); @@ -65,11 +65,7 @@ namespace ClassicalSharp.Model { DrawRotate( 0, 0.75f, 0, p.leftLegXRot, 0, 0, LeftLeg ); DrawRotate( 0, 0.75f, 0, p.rightLegXRot, 0, 0, RightLeg ); DrawRotate( 0, 1.375f, 0, (float)Math.PI / 2, 0, p.leftArmZRot, LeftArm ); - DrawRotate( 0, 1.375f, 0, (float)Math.PI / 2, 0, p.rightArmZRot, RightArm ); - } - - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); + DrawRotate( 0, 1.375f, 0, (float)Math.PI / 2, 0, p.rightArmZRot, RightArm ); } ModelPart Head, Torso, LeftLeg, RightLeg, LeftArm, RightArm; diff --git a/ClassicalSharp/Model/SpiderModel.cs b/ClassicalSharp/Model/SpiderModel.cs index 498be2d00..020c73966 100644 --- a/ClassicalSharp/Model/SpiderModel.cs +++ b/ClassicalSharp/Model/SpiderModel.cs @@ -1,5 +1,4 @@ using System; -using ClassicalSharp.GraphicsAPI; using OpenTK; namespace ClassicalSharp.Model { @@ -14,7 +13,8 @@ namespace ClassicalSharp.Model { LeftLeg = MakeLeg( -1.1875f, -0.1875f ); RightLeg = MakeLeg( 0.1875f, 1.1875f ); - DefaultTexId = graphics.CreateTexture( "spider.png" ); + if( cache.SpiderTexId <= 0 ) + cache.SpiderTexId = graphics.CreateTexture( "spider.png" ); } ModelPart MakeHead() { @@ -49,7 +49,7 @@ namespace ClassicalSharp.Model { const float eighthPi = (float)( Math.PI / 8 ); protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.SpiderTexId : p.MobTextureId; graphics.BindTexture( texId ); graphics.AlphaTest = true; @@ -60,17 +60,13 @@ namespace ClassicalSharp.Model { DrawRotate( -0.1875f, 0.5f, 0, 0, quarterPi, eighthPi, LeftLeg ); DrawRotate( -0.1875f, 0.5f, 0, 0, eighthPi, eighthPi, LeftLeg ); DrawRotate( -0.1875f, 0.5f, 0, 0, -eighthPi, eighthPi, LeftLeg ); - DrawRotate( -0.1875f, 0.5f, 0, 0, -quarterPi, eighthPi, LeftLeg ); + DrawRotate( -0.1875f, 0.5f, 0, 0, -quarterPi, eighthPi, LeftLeg ); DrawRotate( 0.1875f, 0.5f, 0, 0, -quarterPi, -eighthPi, RightLeg ); DrawRotate( 0.1875f, 0.5f, 0, 0, -eighthPi, -eighthPi, RightLeg ); DrawRotate( 0.1875f, 0.5f, 0, 0, eighthPi, -eighthPi, RightLeg ); DrawRotate( 0.1875f, 0.5f, 0, 0, quarterPi, -eighthPi, RightLeg ); } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - } - ModelPart Head, Link, End, LeftLeg, RightLeg; } } \ No newline at end of file diff --git a/ClassicalSharp/Model/ZombieModel.cs b/ClassicalSharp/Model/ZombieModel.cs index c2cc57e4b..aa378c717 100644 --- a/ClassicalSharp/Model/ZombieModel.cs +++ b/ClassicalSharp/Model/ZombieModel.cs @@ -15,7 +15,8 @@ namespace ClassicalSharp.Model { LeftArm = MakeLeftArm( 0.5f, 0.25f ); RightArm = MakeRightArm( 0.25f, 0.5f ); - DefaultTexId = graphics.CreateTexture( "zombie.png" ); + if( cache.ZombieTexId <= 0 ) + cache.ZombieTexId = graphics.CreateTexture( "zombie.png" ); } ModelPart MakeLeftArm( float x1, float x2 ) { @@ -56,7 +57,7 @@ namespace ClassicalSharp.Model { protected override void DrawPlayerModel( Player p ) { graphics.Texturing = true; - int texId = p.MobTextureId <= 0 ? DefaultTexId : p.MobTextureId; + int texId = p.MobTextureId <= 0 ? cache.ZombieTexId : p.MobTextureId; graphics.BindTexture( texId ); DrawRotate( 0, 1.5f, 0, -p.PitchRadians, 0, 0, Head ); @@ -68,10 +69,6 @@ namespace ClassicalSharp.Model { graphics.AlphaTest = true; } - public override void Dispose() { - graphics.DeleteTexture( ref DefaultTexId ); - } - ModelPart Head, Torso, LeftLeg, RightLeg, LeftArm, RightArm; } } \ No newline at end of file diff --git a/ClassicalSharp/TexturePack/ZipExtractor.cs b/ClassicalSharp/TexturePack/ZipExtractor.cs index e08943cad..5fba7a611 100644 --- a/ClassicalSharp/TexturePack/ZipExtractor.cs +++ b/ClassicalSharp/TexturePack/ZipExtractor.cs @@ -3,6 +3,8 @@ using System.Drawing; using System.IO; using System.IO.Compression; using System.Text; +using ClassicalSharp.Model; +using ClassicalSharp.GraphicsAPI; namespace ClassicalSharp.TexturePack { @@ -46,7 +48,7 @@ namespace ClassicalSharp.TexturePack { Utils.LogWarning( "May not be able to properly extract a .zip enty with a version later than 2.0" ); byte[] data = DecompressEntry( reader, compressionMethod, compressedSize, uncompressedSize ); - if( data != null ) + if( data != null ) HandleZipEntry( fileName, data ); } @@ -69,14 +71,44 @@ namespace ClassicalSharp.TexturePack { } } + /*ChickenTexId, CreeperTexId, PigTexId, SheepTexId, + SkeletonTexId, SpiderTexId, ZombieTexId, SheepFurTexId, HumanoidTexId;*/ void HandleZipEntry( string filename, byte[] data ) { Console.WriteLine( filename ); MemoryStream stream = new MemoryStream( data ); + ModelCache cache = game.ModelCache; + IGraphicsApi api = game.Graphics; switch( filename ) { case "terrain.png": - game.ChangeTerrainAtlas( new Bitmap( stream ) ); - break; + game.ChangeTerrainAtlas( new Bitmap( stream ) ); break; + case "chicken.png": + UpdateTexture( ref cache.ChickenTexId, stream, false ); break; + case "creeper.png": + UpdateTexture( ref cache.CreeperTexId, stream, false ); break; + case "pig.png": + UpdateTexture( ref cache.PigTexId, stream, false ); break; + case "sheep.png": + UpdateTexture( ref cache.SheepTexId, stream, false ); break; + case "skeleton.png": + UpdateTexture( ref cache.SkeletonTexId, stream, false ); break; + case "spider.png": + UpdateTexture( ref cache.SpiderTexId, stream, false ); break; + case "zombie.png": + UpdateTexture( ref cache.ZombieTexId, stream, false ); break; + case "sheep_fur.png": + UpdateTexture( ref cache.SheepFurTexId, stream, false ); break; + case "char.png": + UpdateTexture( ref cache.HumanoidTexId, stream, true ); break; + } + } + + void UpdateTexture( ref int texId, Stream stream, bool setSkinType ) { + game.Graphics.DeleteTexture( ref texId ); + using( Bitmap bmp = new Bitmap( stream ) ) { + if( setSkinType ) + game.DefaultPlayerSkinType = Utils.GetSkinType( bmp ); + texId = game.Graphics.CreateTexture( bmp ); } } }