From 245eabe4c216410301edaa06714039bb11b3fafc Mon Sep 17 00:00:00 2001 From: Tim Sarbin Date: Thu, 22 Nov 2018 22:53:05 -0500 Subject: [PATCH] Added resource manager. Working on weird memory and performance things. --- OpenDiablo2.Common/Enums/eButtonType.cs | 8 +++ .../Interfaces/IResourceManager.cs | 16 ++++++ OpenDiablo2.Common/Models/ButtonLayout.cs | 23 ++++++++ OpenDiablo2.Common/Models/ImageSet.cs | 14 +++-- OpenDiablo2.Common/Models/MPQ.cs | 18 ++++--- OpenDiablo2.Common/OpenDiablo2.Common.csproj | 3 ++ OpenDiablo2.Common/ResourcePaths.cs | 1 + OpenDiablo2.Core/AutofacModule.cs | 3 +- OpenDiablo2.Core/GameEngine.cs | 11 ++-- OpenDiablo2.Core/OpenDiablo2.Core.csproj | 3 +- OpenDiablo2.Core/ResourceManager.cs | 53 +++++++++++++++++++ .../UI/{WideButton.cs => Button.cs} | 47 +++++++++++----- OpenDiablo2.SDL2/SDL2Label.cs | 2 + OpenDiablo2.SDL2/SDL2RenderWindow.cs | 9 ++-- OpenDiablo2.SDL2/SDL2Sprite.cs | 1 - OpenDiablo2.Scenes/AutofacModule.cs | 10 ++-- OpenDiablo2.Scenes/MainMenu.cs | 25 ++++++--- OpenDiablo2.Scenes/SelectHeroClass.cs | 23 ++++++-- OpenDiablo2.sln | 6 +++ OpenDiablo2/OpenDiablo2.csproj | 5 +- OpenDiablo2/Program.cs | 38 +++++++------ 21 files changed, 252 insertions(+), 67 deletions(-) create mode 100644 OpenDiablo2.Common/Enums/eButtonType.cs create mode 100644 OpenDiablo2.Common/Interfaces/IResourceManager.cs create mode 100644 OpenDiablo2.Common/Models/ButtonLayout.cs create mode 100644 OpenDiablo2.Core/ResourceManager.cs rename OpenDiablo2.Core/UI/{WideButton.cs => Button.cs} (75%) diff --git a/OpenDiablo2.Common/Enums/eButtonType.cs b/OpenDiablo2.Common/Enums/eButtonType.cs new file mode 100644 index 00000000..dd222d62 --- /dev/null +++ b/OpenDiablo2.Common/Enums/eButtonType.cs @@ -0,0 +1,8 @@ +namespace OpenDiablo2.Common.Enums +{ + public enum eButtonType + { + Wide, + Cancel + } +} diff --git a/OpenDiablo2.Common/Interfaces/IResourceManager.cs b/OpenDiablo2.Common/Interfaces/IResourceManager.cs new file mode 100644 index 00000000..0cc15dcb --- /dev/null +++ b/OpenDiablo2.Common/Interfaces/IResourceManager.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OpenDiablo2.Common.Models; + +namespace OpenDiablo2.Common.Interfaces +{ + public interface IResourceManager + { + ImageSet GetImageSet(string resourcePath); + MPQFont GetMPQFont(string resourcePath); + Palette GetPalette(string paletteName); + } +} diff --git a/OpenDiablo2.Common/Models/ButtonLayout.cs b/OpenDiablo2.Common/Models/ButtonLayout.cs new file mode 100644 index 00000000..0b997a3a --- /dev/null +++ b/OpenDiablo2.Common/Models/ButtonLayout.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OpenDiablo2.Common.Enums; + +namespace OpenDiablo2.Common.Models +{ + public class ButtonLayout + { + public int XSegments { get; internal set; } + public string ResourceName { get; internal set; } + public string PaletteName { get; internal set; } + + public static Dictionary Values = new Dictionary + { + {eButtonType.Wide, new ButtonLayout { XSegments = 2, ResourceName = ResourcePaths.WideButtonBlank, PaletteName = Palettes.Units } }, + {eButtonType.Cancel, new ButtonLayout {XSegments = 1,ResourceName = ResourcePaths.CancelButton,PaletteName = Palettes.Units } } + }; + } + +} diff --git a/OpenDiablo2.Common/Models/ImageSet.cs b/OpenDiablo2.Common/Models/ImageSet.cs index b937ee1b..f925f634 100644 --- a/OpenDiablo2.Common/Models/ImageSet.cs +++ b/OpenDiablo2.Common/Models/ImageSet.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace OpenDiablo2.Common.Models { - public class ImageFrame + public class ImageFrame : IDisposable { public UInt32 Flip; public UInt32 Width; @@ -21,6 +21,11 @@ namespace OpenDiablo2.Common.Models public UInt32 Length; public Int16[,] ImageData; + public void Dispose() + { + ImageData = new Int16[0, 0]; + } + public Color GetColor(int x, int y, Palette palette) { var index = ImageData[x, y]; @@ -33,7 +38,7 @@ namespace OpenDiablo2.Common.Models } } - public sealed class ImageSet + public sealed class ImageSet : IDisposable { static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); @@ -112,7 +117,6 @@ namespace OpenDiablo2.Common.Models continue; } - for (int p = 0; p < b; p++) { result.Frames[i].ImageData[x++, y] = br.ReadByte(); @@ -122,5 +126,9 @@ namespace OpenDiablo2.Common.Models } return result; } + + public void Dispose() + { + } } } diff --git a/OpenDiablo2.Common/Models/MPQ.cs b/OpenDiablo2.Common/Models/MPQ.cs index 44f9a852..e923bc9d 100644 --- a/OpenDiablo2.Common/Models/MPQ.cs +++ b/OpenDiablo2.Common/Models/MPQ.cs @@ -85,16 +85,18 @@ namespace OpenDiablo2.Common.Models private List GetFilePaths() { - var stream = OpenFile("(listfile)"); - if (stream == null) + using (var stream = OpenFile("(listfile)")) { - return new List(); + if (stream == null) + { + return new List(); + } + + var sr = new StreamReader(stream); + var text = sr.ReadToEnd(); + + return text.Split('\n').Where(x => !String.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToList(); } - - var sr = new StreamReader(stream); - var text = sr.ReadToEnd(); - - return text.Split('\n').Where(x => !String.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToList(); } static MPQ() diff --git a/OpenDiablo2.Common/OpenDiablo2.Common.csproj b/OpenDiablo2.Common/OpenDiablo2.Common.csproj index c403c499..688f729b 100644 --- a/OpenDiablo2.Common/OpenDiablo2.Common.csproj +++ b/OpenDiablo2.Common/OpenDiablo2.Common.csproj @@ -70,6 +70,7 @@ + @@ -79,12 +80,14 @@ + + diff --git a/OpenDiablo2.Common/ResourcePaths.cs b/OpenDiablo2.Common/ResourcePaths.cs index 237c9c17..e0ddd703 100644 --- a/OpenDiablo2.Common/ResourcePaths.cs +++ b/OpenDiablo2.Common/ResourcePaths.cs @@ -40,6 +40,7 @@ namespace OpenDiablo2.Common // --- UI --- public static string WideButtonBlank = "data\\global\\ui\\FrontEnd\\WideButtonBlank.dc6"; + public static string CancelButton = "data\\global\\ui\\FrontEnd\\CancelButtonBlank.dc6"; } } diff --git a/OpenDiablo2.Core/AutofacModule.cs b/OpenDiablo2.Core/AutofacModule.cs index fd2b2666..a536022a 100644 --- a/OpenDiablo2.Core/AutofacModule.cs +++ b/OpenDiablo2.Core/AutofacModule.cs @@ -19,8 +19,9 @@ namespace OpenDiablo2.Core builder.RegisterType().AsImplementedInterfaces().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); - builder.RegisterType().AsSelf().InstancePerDependency(); + builder.RegisterType