From ec050aaefcded3c3738f0daca7af73d3d0869dd3 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 8 May 2016 23:27:58 +1000 Subject: [PATCH] Move code away from TexturePackExtractor into their logical classes. --- ClassicalSharp/Game/Game.Properties.cs | 2 +- ClassicalSharp/Game/Game.cs | 22 ++++++++- ClassicalSharp/Model/ModelCache.cs | 36 ++++++++++++-- ClassicalSharp/Rendering/WeatherRenderer.cs | 27 +++++++--- .../TexturePack/TexturePackExtractor.cs | 49 ++----------------- 5 files changed, 78 insertions(+), 58 deletions(-) diff --git a/ClassicalSharp/Game/Game.Properties.cs b/ClassicalSharp/Game/Game.Properties.cs index f8a699ee2..1a55598e6 100644 --- a/ClassicalSharp/Game/Game.Properties.cs +++ b/ClassicalSharp/Game/Game.Properties.cs @@ -173,7 +173,7 @@ namespace ClassicalSharp { public Vector3 CurrentCameraPos; public Animations Animations; - internal int CloudsTexId, RainTexId, SnowTexId, GuiTexId, GuiClassicTexId; + internal int CloudsTexId, GuiTexId, GuiClassicTexId; internal bool screenshotRequested; internal EntryList AcceptedUrls = new EntryList( "acceptedurls.txt" ); internal EntryList DeniedUrls = new EntryList( "deniedurls.txt" ); diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 5d1dfcd0d..af1ad91c5 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -537,8 +537,6 @@ namespace ClassicalSharp { Graphics.Dispose(); Drawer2D.DisposeInstance(); Graphics.DeleteTexture( ref CloudsTexId ); - Graphics.DeleteTexture( ref RainTexId ); - Graphics.DeleteTexture( ref SnowTexId ); Graphics.DeleteTexture( ref GuiTexId ); Graphics.DeleteTexture( ref GuiClassicTexId ); foreach( WarningScreen screen in WarningOverlays ) @@ -559,6 +557,26 @@ namespace ClassicalSharp { Inventory.CanPlace[block] && Inventory.CanDelete[block]; } + + /// Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary), + /// and updates the native texture for it. + public void UpdateTexture( ref int texId, byte[] data, bool setSkinType ) { + MemoryStream stream = new MemoryStream( data ); + Graphics.DeleteTexture( ref texId ); + + using( Bitmap bmp = Platform.ReadBmp( stream ) ) { + if( setSkinType ) + DefaultPlayerSkinType = Utils.GetSkinType( bmp ); + + if( !FastBitmap.CheckFormat( bmp.PixelFormat ) ) { + using( Bitmap bmp32 = Drawer2D.ConvertTo32Bpp( bmp ) ) + texId = Graphics.CreateTexture( bmp32 ); + } else { + texId = Graphics.CreateTexture( bmp ); + } + } + } + public Game( string username, string mppass, string skinServer, bool nullContext, int width, int height ) { window = new DesktopWindow( this, username, nullContext, width, height ); diff --git a/ClassicalSharp/Model/ModelCache.cs b/ClassicalSharp/Model/ModelCache.cs index 048879a05..b039f02df 100644 --- a/ClassicalSharp/Model/ModelCache.cs +++ b/ClassicalSharp/Model/ModelCache.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; +using ClassicalSharp.Events; using ClassicalSharp.GraphicsAPI; namespace ClassicalSharp.Model { @@ -12,7 +13,7 @@ namespace ClassicalSharp.Model { IGraphicsApi api; public ModelCache( Game window ) { this.game = window; - api = game.Graphics; + api = game.Graphics; } public CustomModel[] CustomModels = new CustomModel[256]; @@ -23,6 +24,7 @@ namespace ClassicalSharp.Model { model.CreateParts(); cache["humanoid"] = model; cache["human"] = cache["humanoid"]; + game.Events.TextureChanged += TextureChanged; } internal int vb; @@ -57,15 +59,16 @@ namespace ClassicalSharp.Model { else if( modelName == "zombie" ) return new ZombieModel( game ); else if( modelName == "block" ) return new BlockModel( game ); else if( modelName == "chibi" ) return new ChibiModel( game ); - else if( modelName == "giant" ) return new GiantModel( game ); + else if( modelName == "giant" ) return new GiantModel( game ); return null; } public void Dispose() { - foreach( var entry in cache ) { + foreach( var entry in cache ) entry.Value.Dispose(); - } api.DeleteDynamicVb( vb ); + game.Events.TextureChanged -= TextureChanged; + api.DeleteTexture( ref ChickenTexId ); api.DeleteTexture( ref CreeperTexId ); api.DeleteTexture( ref PigTexId ); @@ -74,7 +77,30 @@ namespace ClassicalSharp.Model { api.DeleteTexture( ref SpiderTexId ); api.DeleteTexture( ref ZombieTexId ); api.DeleteTexture( ref SheepFurTexId ); - api.DeleteTexture( ref HumanoidTexId ); + api.DeleteTexture( ref HumanoidTexId ); + } + + void TextureChanged( object sender, TextureEventArgs e ) { + switch( e.Name ) { + case "chicken.png": + game.UpdateTexture( ref ChickenTexId, e.Data, false ); break; + case "creeper.png": + game.UpdateTexture( ref CreeperTexId, e.Data, false ); break; + case "pig.png": + game.UpdateTexture( ref PigTexId, e.Data, false ); break; + case "sheep.png": + game.UpdateTexture( ref SheepTexId, e.Data, false ); break; + case "skeleton.png": + game.UpdateTexture( ref SkeletonTexId, e.Data, false ); break; + case "spider.png": + game.UpdateTexture( ref SpiderTexId, e.Data, false ); break; + case "zombie.png": + game.UpdateTexture( ref ZombieTexId, e.Data, false ); break; + case "sheep_fur.png": + game.UpdateTexture( ref SheepFurTexId, e.Data, false ); break; + case "char.png": + game.UpdateTexture( ref HumanoidTexId, e.Data, true ); break; + } } } } diff --git a/ClassicalSharp/Rendering/WeatherRenderer.cs b/ClassicalSharp/Rendering/WeatherRenderer.cs index 4961a6985..1c468e0bb 100644 --- a/ClassicalSharp/Rendering/WeatherRenderer.cs +++ b/ClassicalSharp/Rendering/WeatherRenderer.cs @@ -1,5 +1,7 @@ // ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT using System; +using System.IO; +using ClassicalSharp.Events; using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Map; using OpenTK; @@ -12,6 +14,7 @@ namespace ClassicalSharp.Renderers { World map; IGraphicsApi graphics; BlockInfo info; + public int RainTexId, SnowTexId; public void Init( Game game ) { this.game = game; @@ -19,7 +22,8 @@ namespace ClassicalSharp.Renderers { graphics = game.Graphics; info = game.BlockInfo; weatherVb = graphics.CreateDynamicVb( VertexFormat.P3fT2fC4b, vertices.Length ); - } + game.Events.TextureChanged += TextureChanged; + } int weatherVb; short[] heightmap; @@ -34,7 +38,7 @@ namespace ClassicalSharp.Renderers { if( weather == Weather.Sunny ) return; if( heightmap == null ) InitHeightmap(); - graphics.BindTexture( weather == Weather.Rainy ? game.RainTexId : game.SnowTexId ); + graphics.BindTexture( weather == Weather.Rainy ? RainTexId : SnowTexId ); Vector3 camPos = game.CurrentCameraPos; Vector3I pos = Vector3I.Floor( camPos ); bool moved = pos != lastPos; @@ -109,6 +113,21 @@ namespace ClassicalSharp.Renderers { oneY = length * width; } + void TextureChanged( object sender, TextureEventArgs e ) { + if( e.Name == "snow.png" ) { + game.UpdateTexture( ref SnowTexId, e.Data, false ); + } else if( e.Name == "rain.png" ) { + game.UpdateTexture( ref RainTexId, e.Data, false ); + } + } + + public void Dispose() { + game.Graphics.DeleteTexture( ref RainTexId ); + game.Graphics.DeleteTexture( ref SnowTexId ); + graphics.DeleteDynamicVb( weatherVb ); + game.Events.TextureChanged -= TextureChanged; + } + void InitHeightmap() { heightmap = new short[map.Width * map.Length]; for( int i = 0; i < heightmap.Length; i++ ) { @@ -116,10 +135,6 @@ namespace ClassicalSharp.Renderers { } } - public void Dispose() { - graphics.DeleteDynamicVb( weatherVb ); - } - float GetRainHeight( int x, int z ) { if( x < 0 || z < 0 || x >= width || z >= length ) return map.EdgeHeight; int index = (x * length) + z; diff --git a/ClassicalSharp/TexturePack/TexturePackExtractor.cs b/ClassicalSharp/TexturePack/TexturePackExtractor.cs index 96f22f15e..f2ede71d9 100644 --- a/ClassicalSharp/TexturePack/TexturePackExtractor.cs +++ b/ClassicalSharp/TexturePack/TexturePackExtractor.cs @@ -50,38 +50,16 @@ namespace ClassicalSharp.TexturePack { Bitmap atlas = Platform.ReadBmp( stream ); if( !game.ChangeTerrainAtlas( atlas ) ) atlas.Dispose(); 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; case "clouds.png": case "cloud.png": - UpdateTexture( ref game.CloudsTexId, stream, false ); break; - case "rain.png": - UpdateTexture( ref game.RainTexId, stream, false ); break; - case "snow.png": - UpdateTexture( ref game.SnowTexId, stream, false ); break; + game.UpdateTexture( ref game.CloudsTexId, data, false ); break; case "gui.png": - UpdateTexture( ref game.GuiTexId, stream, false ); break; + game.UpdateTexture( ref game.GuiTexId, data, false ); break; case "gui_classic.png": - UpdateTexture( ref game.GuiClassicTexId, stream, false ); break; + game.UpdateTexture( ref game.GuiClassicTexId, data, false ); break; case "particles.png": - UpdateTexture( ref game.ParticleManager.ParticlesTexId, - stream, false ); break; + game.UpdateTexture( ref game.ParticleManager.ParticlesTexId, + data, false ); break; case "default.png": SetFontBitmap( game, stream ); break; } @@ -95,22 +73,5 @@ namespace ClassicalSharp.TexturePack { game.Drawer2D.SetFontBitmap( bmp ); game.Events.RaiseChatFontChanged(); } - - /// Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary), - /// and updates the native texture for it. - public void UpdateTexture( ref int texId, Stream stream, bool setSkinType ) { - game.Graphics.DeleteTexture( ref texId ); - using( Bitmap bmp = Platform.ReadBmp( stream ) ) { - if( setSkinType ) - game.DefaultPlayerSkinType = Utils.GetSkinType( bmp ); - - if( !FastBitmap.CheckFormat( bmp.PixelFormat ) ) { - using( Bitmap bmp32 = game.Drawer2D.ConvertTo32Bpp( bmp ) ) - texId = game.Graphics.CreateTexture( bmp32 ); - } else { - texId = game.Graphics.CreateTexture( bmp ); - } - } - } } }