Move code away from TexturePackExtractor into their logical classes.

This commit is contained in:
UnknownShadow200 2016-05-08 23:27:58 +10:00
parent 723585661a
commit ec050aaefc
5 changed files with 78 additions and 58 deletions

View File

@ -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" );

View File

@ -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];
}
/// <summary> Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary),
/// and updates the native texture for it. </summary>
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 );

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using ClassicalSharp.Events;
using ClassicalSharp.GraphicsAPI;
namespace ClassicalSharp.Model {
@ -23,6 +24,7 @@ namespace ClassicalSharp.Model {
model.CreateParts();
cache["humanoid"] = model;
cache["human"] = cache["humanoid"];
game.Events.TextureChanged += TextureChanged;
}
internal int vb;
@ -62,10 +64,11 @@ namespace ClassicalSharp.Model {
}
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 );
@ -76,5 +79,28 @@ namespace ClassicalSharp.Model {
api.DeleteTexture( ref SheepFurTexId );
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;
}
}
}
}

View File

@ -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,6 +22,7 @@ namespace ClassicalSharp.Renderers {
graphics = game.Graphics;
info = game.BlockInfo;
weatherVb = graphics.CreateDynamicVb( VertexFormat.P3fT2fC4b, vertices.Length );
game.Events.TextureChanged += TextureChanged;
}
int weatherVb;
@ -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;

View File

@ -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();
}
/// <summary> Reads a bitmap from the stream (converting it to 32 bits per pixel if necessary),
/// and updates the native texture for it. </summary>
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 );
}
}
}
}
}