From 0402119ec7608d96782e143a6d15df30662a8084 Mon Sep 17 00:00:00 2001 From: Tim Sarbin Date: Tue, 11 Dec 2018 19:58:13 -0500 Subject: [PATCH] Added render priority for layers --- OpenDiablo2.Common/Models/MPQCOF.cs | 14 +++++++++----- OpenDiablo2.Core/ResourceManager.cs | 3 +-- OpenDiablo2.SDL2/SDL2CharacterRenderer.cs | 23 +++++++++-------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/OpenDiablo2.Common/Models/MPQCOF.cs b/OpenDiablo2.Common/Models/MPQCOF.cs index a92b4a37..825006a2 100644 --- a/OpenDiablo2.Common/Models/MPQCOF.cs +++ b/OpenDiablo2.Common/Models/MPQCOF.cs @@ -55,9 +55,10 @@ namespace OpenDiablo2.Common.Models public eMobMode MobMode { get; private set; } public List Animations { get; private set; } - public IEnumerable Layers { get; private set; } + public COFLayer[] Layers { get; private set; } + public Dictionary CompositLayers { get; private set; } public IEnumerable AnimationFrames { get; private set; } - public byte[] Priority { get; private set; } + public eCompositType[] Priority { get; private set; } public int NumberOfDirections { get; internal set; } public int FramesPerDirection { get; internal set; } public int NumberOfLayers { get; internal set; } @@ -80,6 +81,8 @@ namespace OpenDiablo2.Common.Models br.ReadBytes(25); // Skip 25 unknown bytes... var layers = new List(); + result.CompositLayers = new Dictionary(); + for (var layerIdx = 0; layerIdx < result.NumberOfLayers; layerIdx++) { var layer = new COFLayer @@ -91,14 +94,15 @@ namespace OpenDiablo2.Common.Models br.ReadByte(); // Unknown layer.IsTransparent = br.ReadByte() != 0; layer.DrawEffect = (eDrawEffect)br.ReadByte(); - layers.Add(layer); layer.WeaponClass = Encoding.ASCII.GetString(br.ReadBytes(4)).Trim('\0').ToWeaponClass(); layer.ShieldCode = ShieldCode; layer.WeaponCode = weaponCode; + layers.Add(layer); + result.CompositLayers[layer.CompositType] = layerIdx; } - result.Layers = layers; + result.Layers = layers.ToArray(); result.AnimationFrames = br.ReadBytes(result.FramesPerDirection).Select(x => (eAnimationFrame)x); - result.Priority = br.ReadBytes(result.FramesPerDirection * result.NumberOfLayers * result.NumberOfDirections); + result.Priority = br.ReadBytes(result.FramesPerDirection * result.NumberOfLayers * result.NumberOfDirections).Select(x => (eCompositType)x).ToArray(); var cofName = $"{hero.ToToken()}{mobMode.ToToken()}{weaponClass.ToToken()}".ToUpper(); result.Animations = animations[cofName]; diff --git a/OpenDiablo2.Core/ResourceManager.cs b/OpenDiablo2.Core/ResourceManager.cs index 5264211c..f370fb1a 100644 --- a/OpenDiablo2.Core/ResourceManager.cs +++ b/OpenDiablo2.Core/ResourceManager.cs @@ -89,8 +89,7 @@ namespace OpenDiablo2.Core binaryData = new byte[stream.Length]; stream.Read(binaryData, 0, (int)stream.Length); } - var result = new MPQDCC(binaryData, palette); - return result; + return new MPQDCC(binaryData, palette); }); } } diff --git a/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs b/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs index cabd7f9a..07d7a02a 100644 --- a/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs +++ b/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs @@ -192,23 +192,18 @@ namespace OpenDiablo2.SDL2_ SDL.SDL_LockTexture(texture, IntPtr.Zero, out IntPtr pixels, out int pitch); UInt32* data = (UInt32*)pixels; - var priorities = new int[animationData.NumberOfLayers]; - Array.Copy( - animationData.Priority, - (directionConversion[LocationDetails.MovementDirection] * animationData.FramesPerDirection * animationData.NumberOfLayers) - + (frameIndex * animationData.NumberOfLayers), - priorities, - 0, - animationData.NumberOfLayers - ); - - for (var i = 0; i < layerData.Length; i++) + var priorityBase = (directionConversion[LocationDetails.MovementDirection] * animationData.FramesPerDirection * animationData.NumberOfLayers) + + (frameIndex * animationData.NumberOfLayers); + for (var i = 0; i < animationData.NumberOfLayers; i++) { - //var layer = layerData[priorities[i]]; - var layer = layerData[i]; + var comp = animationData.Priority[priorityBase + i]; + if (!animationData.CompositLayers.ContainsKey(comp)) + continue; + + var layer = layerData[animationData.CompositLayers[comp]]; if (layer == null) - continue; + continue; // TODO: This is most likely not ok var direction = layer.Directions[directionConversion[LocationDetails.MovementDirection]]; var frame = direction.Frames[frameIndex];