Make Animations a game component, make TexturePackExtractor less hardcoded.

This commit is contained in:
UnknownShadow200 2016-05-08 23:05:56 +10:00
parent d837b31f84
commit 723585661a
12 changed files with 66 additions and 46 deletions

View file

@ -64,7 +64,7 @@ namespace ClassicalSharp.Gui {
Anchor.Centre, titleFont, SaveClassic ),
ButtonWidget.Create( game, -150, 120, 201, 40, "Save schematic", Anchor.Centre,
Anchor.Centre, titleFont, SaveSchematic ),
ChatTextWidget.Create( game, 120, 110, "&eCan be imported into MCedit", Anchor.Centre,
ChatTextWidget.Create( game, 110, 120, "&eCan be imported into MCedit", Anchor.Centre,
Anchor.Centre, regularFont ),
null,
MakeBack( false, titleFont,

View file

@ -89,8 +89,8 @@ namespace ClassicalSharp.Gui {
base.Init();
if( !extList ) {
game.EntityEvents.EntityAdded += PlayerSpawned;
game.EntityEvents.EntityRemoved += PlayerDespawned;
game.EntityEvents.Added += PlayerSpawned;
game.EntityEvents.Removed += PlayerDespawned;
} else {
game.EntityEvents.CpeListInfoAdded += PlayerListInfoAdded;
game.EntityEvents.CpeListInfoRemoved += PlayerDespawned;
@ -102,8 +102,8 @@ namespace ClassicalSharp.Gui {
base.Dispose();
overview.Dispose();
if( !extList ) {
game.EntityEvents.EntityAdded -= PlayerSpawned;
game.EntityEvents.EntityRemoved -= PlayerDespawned;
game.EntityEvents.Added -= PlayerSpawned;
game.EntityEvents.Removed -= PlayerDespawned;
} else {
game.EntityEvents.CpeListInfoAdded -= PlayerListInfoAdded;
game.EntityEvents.CpeListInfoChanged -= PlayerListInfoChanged;

View file

@ -27,14 +27,14 @@ namespace ClassicalSharp.Gui {
public override void Init() {
base.Init();
game.EntityEvents.EntityAdded += PlayerSpawned;
game.EntityEvents.EntityRemoved += PlayerDespawned;
game.EntityEvents.Added += PlayerSpawned;
game.EntityEvents.Removed += PlayerDespawned;
}
public override void Dispose() {
base.Dispose();
game.EntityEvents.EntityAdded -= PlayerSpawned;
game.EntityEvents.EntityRemoved -= PlayerDespawned;
game.EntityEvents.Added -= PlayerSpawned;
game.EntityEvents.Removed -= PlayerDespawned;
}
void PlayerSpawned( object sender, IdEventArgs e ) {

View file

@ -4,7 +4,7 @@
<ProjectGuid>{BEB1C785-5CAD-48FF-A886-876BF0A318D4}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<RootNamespace>ClassicalSharp</RootNamespace>
<AssemblyName>ClassicalSharp</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>

View file

@ -98,7 +98,7 @@ namespace ClassicalSharp.Entities {
}
void TextureChanged( object sender, TextureEventArgs e ) {
if( e.Texture != "char.png" ) return;
if( e.Name != "char.png" ) return;
for( int i = 0; i < Players.Length; i++ ) {
if( Players[i] == null || Players[i].TextureId != -1 ) continue;
Players[i].SkinType = game.DefaultPlayerSkinType;

View file

@ -10,12 +10,12 @@ namespace ClassicalSharp.Events {
IdEventArgs idArgs = new IdEventArgs();
/// <summary> Raised when an entity is spawned in the current world. </summary>
public event EventHandler<IdEventArgs> EntityAdded;
internal void RaiseEntityAdded( byte id ) { idArgs.Id = id; Raise( EntityAdded, idArgs ); }
public event EventHandler<IdEventArgs> Added;
internal void RaiseAdded( byte id ) { idArgs.Id = id; Raise( Added, idArgs ); }
/// <summary> Raised when an entity is despawned from the current world. </summary>
public event EventHandler<IdEventArgs> EntityRemoved;
internal void RaiseEntityRemoved( byte id ) { idArgs.Id = id; Raise( EntityRemoved, idArgs ); }
public event EventHandler<IdEventArgs> Removed;
internal void RaiseRemoved( byte id ) { idArgs.Id = id; Raise( Removed, idArgs ); }
/// <summary> Raised when a new CPE player list entry is created. </summary>
public event EventHandler<IdEventArgs> CpeListInfoAdded;

View file

@ -11,8 +11,8 @@ namespace ClassicalSharp.Events {
/// <summary> Raised when a texture is changed. (such as "terrain", "rain") </summary>
public event EventHandler<TextureEventArgs> TextureChanged;
internal void RaiseTextureChanged( string texture ) {
texArgs.Texture = texture; Raise( TextureChanged, texArgs ); }
internal void RaiseTextureChanged( string name, byte[] data ) {
texArgs.Name = name; texArgs.Data = data; Raise( TextureChanged, texArgs ); }
/// <summary> Raised when the user changed their view/fog distance. </summary>
public event EventHandler ViewDistanceChanged;
@ -78,8 +78,10 @@ namespace ClassicalSharp.Events {
public sealed class TextureEventArgs : EventArgs {
/// <summary> Location of the texture within a texture pack. (e.g. "snow", "default", "char") </summary>
/// <remarks> See TexturePackExtractor for a list of supported textures. </remarks>
public string Texture;
/// <summary> Location of the file within a texture pack, without a directory. (e.g. "snow.png") </summary>
public string Name;
/// <summary> Raw data of the file. </summary>
public byte[] Data;
}
}

View file

@ -84,7 +84,7 @@ namespace ClassicalSharp {
TerrainAtlas1D = new TerrainAtlas1D( Graphics );
TerrainAtlas = new TerrainAtlas2D( Graphics, Drawer2D );
Animations = new Animations( this );
Animations = AddComponent( new Animations() );
Inventory = AddComponent( new Inventory() );
BlockInfo.SetDefaultBlockPermissions( Inventory.CanPlace, Inventory.CanDelete );
@ -536,7 +536,6 @@ namespace ClassicalSharp {
Graphics.DeleteIb( defaultIb );
Graphics.Dispose();
Drawer2D.DisposeInstance();
Animations.Dispose();
Graphics.DeleteTexture( ref CloudsTexId );
Graphics.DeleteTexture( ref RainTexId );
Graphics.DeleteTexture( ref SnowTexId );

View file

@ -128,7 +128,7 @@ namespace ClassicalSharp {
if( item.Data != null ) {
Bitmap bmp = (Bitmap)item.Data;
game.World.TextureUrl = item.Url;
game.Animations.Dispose();
game.Animations.Clear();
if( !FastBitmap.CheckFormat( bmp.PixelFormat ) ) {
Utils.LogDebug( "Converting terrain atlas to 32bpp image" );
@ -142,7 +142,7 @@ namespace ClassicalSharp {
if( bmp == null ) {// Should never happen, but handle anyways.
ExtractDefault();
} else if( item.Url != game.World.TextureUrl ) {
game.Animations.Dispose();
game.Animations.Clear();
if( !game.ChangeTerrainAtlas( bmp ) ) { bmp.Dispose(); return; }
}
@ -155,7 +155,7 @@ namespace ClassicalSharp {
if( game.AsyncDownloader.TryGetItem( "texturePack", out item ) ) {
if( item.Data != null ) {
game.World.TextureUrl = item.Url;
game.Animations.Dispose();
game.Animations.Clear();
TexturePackExtractor extractor = new TexturePackExtractor();
extractor.Extract( (byte[])item.Data, game );
@ -166,7 +166,7 @@ namespace ClassicalSharp {
if( data == null ) { // Should never happen, but handle anyways.
ExtractDefault();
} else if( item.Url != game.World.TextureUrl ) {
game.Animations.Dispose();
game.Animations.Clear();
TexturePackExtractor extractor = new TexturePackExtractor();
extractor.Extract( data, game );
}

View file

@ -275,11 +275,11 @@ namespace ClassicalSharp.Net {
if( entityId != 0xFF ) {
Player oldPlayer = game.Players[entityId];
if( oldPlayer != null ) {
game.EntityEvents.RaiseEntityRemoved( entityId );
game.EntityEvents.RaiseRemoved( entityId );
oldPlayer.Despawn();
}
game.Players[entityId] = new NetPlayer( displayName, skinName, game, entityId );
game.EntityEvents.RaiseEntityAdded( entityId );
game.EntityEvents.RaiseAdded( entityId );
} else {
// Server is only allowed to change our own name colours.
if( Utils.StripColours( displayName ) != game.Username )
@ -307,7 +307,7 @@ namespace ClassicalSharp.Net {
if( player == null ) return;
if( entityId != 0xFF ) {
game.EntityEvents.RaiseEntityRemoved( entityId );
game.EntityEvents.RaiseRemoved( entityId );
player.Despawn();
game.Players[entityId] = null;
}

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using ClassicalSharp.Events;
using ClassicalSharp.GraphicsAPI;
#if ANDROID
using Android.Graphics;
@ -11,7 +12,7 @@ using Android.Graphics;
namespace ClassicalSharp.TexturePack {
/// <summary> Contains and describes the various animations applied to the terrain atlas. </summary>
public class Animations {
public class Animations : IGameComponent {
Game game;
IGraphicsApi api;
@ -19,14 +20,34 @@ namespace ClassicalSharp.TexturePack {
FastBitmap fastBmp;
List<AnimationData> animations = new List<AnimationData>();
public Animations( Game game ) {
public void Init( Game game ) {
this.game = game;
api = game.Graphics;
game.Events.TextureChanged += TextureChanged;
}
public void Ready( Game game ) { }
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
void TextureChanged( object sender, TextureEventArgs e ) {
if( e.Name == "animations.png" || e.Name == "animation.png" ) {
MemoryStream stream = new MemoryStream( e.Data );
SetAtlas( Platform.ReadBmp( stream ) );
} else if( e.Name == "animations.txt" || e.Name == "animation.txt" ) {
MemoryStream stream = new MemoryStream( e.Data );
StreamReader reader = new StreamReader( stream );
ReadAnimationsDescription( reader );
}
}
/// <summary> Sets the atlas bitmap that animation frames are contained within. </summary>
public void SetAtlas( Bitmap bmp ) {
Dispose();
if( !FastBitmap.CheckFormat( bmp.PixelFormat ) )
game.Drawer2D.ConvertTo32Bpp( ref bmp );
Clear();
this.bmp = bmp;
fastBmp = new FastBitmap( bmp, true, true );
}
@ -124,12 +145,17 @@ namespace ClassicalSharp.TexturePack {
/// <summary> Disposes the atlas bitmap that contains animation frames, and clears
/// the list of defined animations. </summary>
public void Dispose() {
public void Clear() {
animations.Clear();
if( bmp == null ) return;
fastBmp.Dispose(); fastBmp = null;
bmp.Dispose(); bmp = null;
}
public void Dispose() {
Clear();
game.Events.TextureChanged -= TextureChanged;
}
}
}

View file

@ -26,7 +26,7 @@ namespace ClassicalSharp.TexturePack {
void Extract( Stream stream, Game game ) {
this.game = game;
game.Animations.Dispose();
game.Animations.Clear();
ZipReader reader = new ZipReader();
reader.ShouldProcessZipEntry = (f) => true;
@ -79,22 +79,13 @@ namespace ClassicalSharp.TexturePack {
UpdateTexture( ref game.GuiTexId, stream, false ); break;
case "gui_classic.png":
UpdateTexture( ref game.GuiClassicTexId, stream, false ); break;
case "animations.png":
case "animation.png":
game.Animations.SetAtlas( Platform.ReadBmp( stream ) ); break;
case "animations.txt":
case "animation.txt":
StreamReader reader = new StreamReader( stream );
game.Animations.ReadAnimationsDescription( reader );
break;
case "particles.png":
UpdateTexture( ref game.ParticleManager.ParticlesTexId,
stream, false ); break;
case "default.png":
SetFontBitmap( game, stream ); break;
}
game.Events.RaiseTextureChanged( name );
}
game.Events.RaiseTextureChanged( name, data );
}
void SetFontBitmap( Game game, Stream stream ) {
@ -105,7 +96,9 @@ namespace ClassicalSharp.TexturePack {
game.Events.RaiseChatFontChanged();
}
void UpdateTexture( ref int texId, Stream stream, bool setSkinType ) {
/// <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 )