Skip to content

Commit

Permalink
Merge pull request #123 from ethanmoffat/fix_perf_pregame
Browse files Browse the repository at this point in the history
Improve performance in pre-game menus
  • Loading branch information
ethanmoffat authored Mar 25, 2022
2 parents 8cbdc19 + 8a7a7f3 commit 5fa705f
Show file tree
Hide file tree
Showing 36 changed files with 380 additions and 268 deletions.
25 changes: 18 additions & 7 deletions EOLib.Graphics/NativeGraphicsManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using AutomaticTypeMapper;
using Microsoft.Xna.Framework.Graphics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace EOLib.Graphics
Expand Down Expand Up @@ -45,12 +47,21 @@ public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transp
}
}

using (var mem = new System.IO.MemoryStream())
using (var i = BitmapFromResource(file, resourceVal, transparent))
{
using (var i = BitmapFromResource(file, resourceVal, transparent))
((Image)i).Save(mem, new PngEncoder());

ret = Texture2D.FromStream(_graphicsDeviceProvider.GraphicsDevice, mem);
if (!i.DangerousTryGetSinglePixelMemory(out var mem))
{
using (var ms = new MemoryStream())
{
i.SaveAsPng(ms);
ret = Texture2D.FromStream(_graphicsDeviceProvider.GraphicsDevice, ms);
}
}
else
{
ret = new Texture2D(_graphicsDeviceProvider.GraphicsDevice, i.Width, i.Height);
ret.SetData(mem.ToArray());
}
}

lock (__cachelock__)
Expand All @@ -61,9 +72,9 @@ public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transp
return ret;
}

private IImage BitmapFromResource(GFXTypes file, int resourceVal, bool transparent)
private Image<Rgba32> BitmapFromResource(GFXTypes file, int resourceVal, bool transparent)
{
var ret = (Image)_gfxLoader.LoadGFX(file, resourceVal);
var ret = (Image<Rgba32>)_gfxLoader.LoadGFX(file, resourceVal);

if (transparent)
{
Expand Down
6 changes: 6 additions & 0 deletions EOLib/Domain/Character/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class Character : ICharacter

public bool NoWall { get; private set; }

public Character()
{
RenderProperties = new CharacterRenderProperties();
Stats = new CharacterStats();
}

public ICharacter WithID(int id)
{
var character = MakeCopy(this);
Expand Down
94 changes: 94 additions & 0 deletions EndlessClient/Content/ContentProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using AutomaticTypeMapper;
using EOLib;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace EndlessClient.Content
{
public interface IContentProvider
{
IReadOnlyDictionary<string, Texture2D> Textures { get; }

IReadOnlyDictionary<string, SpriteFont> Fonts { get; }

void SetContentManager(ContentManager content);

void Load();
}

[AutoMappedType(IsSingleton = true)]
public class ContentProvider : IContentProvider
{
private readonly Dictionary<string, Texture2D> _textures;
private readonly Dictionary<string, SpriteFont> _fonts;

private ContentManager _content;

public const string Cursor = "cursor";

public const string TBBack = "tbBack";
public const string TBLeft = "tbLeft";
public const string TBRight = "tbRight";

public const string ChatTL = @"ChatBubble\TL";
public const string ChatTM = @"ChatBubble\TM";
public const string ChatTR = @"ChatBubble\TR";
public const string ChatML = @"ChatBubble\ML";
public const string ChatMM = @"ChatBubble\MM";
public const string ChatMR = @"ChatBubble\MR";
public const string ChatRL = @"ChatBubble\RL";
public const string ChatRM = @"ChatBubble\RM";
public const string ChatRR = @"ChatBubble\RR";
public const string ChatNUB = @"ChatBubble\NUB";

public IReadOnlyDictionary<string, Texture2D> Textures => _textures;

public IReadOnlyDictionary<string, SpriteFont> Fonts => _fonts;

public ContentProvider()
{
_textures = new Dictionary<string, Texture2D>();
_fonts = new Dictionary<string, SpriteFont>();
}

public void SetContentManager(ContentManager content)
{
_content = content;
}

public void Load()
{
RefreshTextures();
RefreshFonts();
}

private void RefreshTextures()
{
if (_content == null)
return;

_textures[Cursor] = _content.Load<Texture2D>(Cursor);

_textures[TBBack] = _content.Load<Texture2D>(TBBack);
_textures[TBLeft] = _content.Load<Texture2D>(TBLeft);
_textures[TBRight] = _content.Load<Texture2D>(TBRight);

_textures[ChatTL] = _content.Load<Texture2D>(ChatTL);
_textures[ChatTM] = _content.Load<Texture2D>(ChatTM);
_textures[ChatTR] = _content.Load<Texture2D>(ChatTR);
_textures[ChatML] = _content.Load<Texture2D>(ChatML);
_textures[ChatMM] = _content.Load<Texture2D>(ChatMM);
_textures[ChatMR] = _content.Load<Texture2D>(ChatMR);
_textures[ChatRL] = _content.Load<Texture2D>(ChatRL);
_textures[ChatRM] = _content.Load<Texture2D>(ChatRM);
_textures[ChatRR] = _content.Load<Texture2D>(ChatRR);
_textures[ChatNUB] = _content.Load<Texture2D>(ChatNUB);
}

private void RefreshFonts()
{
_fonts[Constants.FontSize08] = _content.Load<SpriteFont>(Constants.FontSize08);
}
}
}
22 changes: 0 additions & 22 deletions EndlessClient/Content/IContentManagerRepository.cs

This file was deleted.

6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/BackButtonControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand All @@ -21,9 +21,9 @@ protected BackButtonControlSet(IMainButtonController mainButtonController)
_mainButtonController = mainButtonController;
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

_backButtonTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 24, true);
}
Expand Down
12 changes: 6 additions & 6 deletions EndlessClient/ControlSets/BaseControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand Down Expand Up @@ -43,7 +43,7 @@ protected BaseControlSet()
}

public virtual void InitializeResources(INativeGraphicsManager gfxManager,
ContentManager xnaContentManager)
IContentProvider contentProvider)
{
if (_resourcesInitialized)
throw new InvalidOperationException("Error initializing resources: resources have already been initialized");
Expand All @@ -52,10 +52,10 @@ public virtual void InitializeResources(INativeGraphicsManager gfxManager,
_secondaryButtonTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 14, true);
_smallButtonSheet = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 15, true);

_textBoxBackground = xnaContentManager.Load<Texture2D>("tbBack");
_textBoxLeft = xnaContentManager.Load<Texture2D>("tbLeft");
_textBoxRight = xnaContentManager.Load<Texture2D>("tbRight");
_textBoxCursor = xnaContentManager.Load<Texture2D>("cursor");
_textBoxBackground = contentProvider.Textures[ContentProvider.TBBack];
_textBoxLeft = contentProvider.Textures[ContentProvider.TBLeft];
_textBoxRight = contentProvider.Textures[ContentProvider.TBRight];
_textBoxCursor = contentProvider.Textures[ContentProvider.Cursor];

_backgroundImages = new Texture2D[7];
for (int i = 0; i < _backgroundImages.Length; ++i)
Expand Down
15 changes: 10 additions & 5 deletions EndlessClient/ControlSets/ControlSetFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using EndlessClient.Input;
using EndlessClient.UIControls;
using EOLib.Config;
using EOLib.Domain.Login;
using EOLib.Graphics;

namespace EndlessClient.ControlSets
Expand All @@ -18,10 +19,11 @@ public class ControlSetFactory : IControlSetFactory
private readonly INativeGraphicsManager _nativeGraphicsManager;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly IHudControlsFactory _hudControlsFactory;
private readonly IContentManagerProvider _contentManagerProvider;
private readonly IContentProvider _contentProvider;
private readonly IKeyboardDispatcherProvider _keyboardDispatcherProvider;
private readonly IConfigurationProvider _configProvider;
private readonly ICharacterInfoPanelFactory _characterInfoPanelFactory;
private readonly ICharacterSelectorProvider _characterSelectorProvider;
private IMainButtonController _mainButtonController;
private IAccountController _accountController;
private ILoginController _loginController;
Expand All @@ -30,18 +32,20 @@ public class ControlSetFactory : IControlSetFactory
public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager,
IEOMessageBoxFactory messageBoxFactory,
IHudControlsFactory hudControlsFactory,
IContentManagerProvider contentManagerProvider,
IContentProvider contentProvider,
IKeyboardDispatcherProvider keyboardDispatcherProvider,
IConfigurationProvider configProvider,
ICharacterInfoPanelFactory characterInfoPanelFactory)
ICharacterInfoPanelFactory characterInfoPanelFactory,
ICharacterSelectorProvider characterSelectorProvider)
{
_nativeGraphicsManager = nativeGraphicsManager;
_messageBoxFactory = messageBoxFactory;
_hudControlsFactory = hudControlsFactory;
_contentManagerProvider = contentManagerProvider;
_contentProvider = contentProvider;
_keyboardDispatcherProvider = keyboardDispatcherProvider;
_configProvider = configProvider;
_characterInfoPanelFactory = characterInfoPanelFactory;
_characterSelectorProvider = characterSelectorProvider;
}

public IControlSet CreateControlsForState(GameStates newState, IControlSet currentControlSet)
Expand All @@ -51,7 +55,7 @@ public IControlSet CreateControlsForState(GameStates newState, IControlSet curre
throw new InvalidOperationException("Missing controllers - the Unity container was initialized incorrectly");

var controlSet = GetSetBasedOnState(newState);
controlSet.InitializeResources(_nativeGraphicsManager, _contentManagerProvider.Content);
controlSet.InitializeResources(_nativeGraphicsManager, _contentProvider);
controlSet.InitializeControls(currentControlSet);
return controlSet;
}
Expand Down Expand Up @@ -89,6 +93,7 @@ private IControlSet GetSetBasedOnState(GameStates newState)
_keyboardDispatcherProvider.Dispatcher,
_mainButtonController,
_characterInfoPanelFactory,
_characterSelectorProvider,
_characterManagementController,
_accountController);
case GameStates.PlayingTheGame:
Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/CreateAccountControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.Input;
using EOLib;
using EOLib.Domain.Account;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand Down Expand Up @@ -41,9 +41,9 @@ public CreateAccountControlSet(KeyboardDispatcher dispatcher,
_accountController = accountController;
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

_labelsTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 12, true);
}
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/ControlSets/EmptyControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using XNAControls;

namespace EndlessClient.ControlSets
Expand All @@ -19,7 +19,7 @@ public class EmptyControlSet : IControlSet

public IReadOnlyList<IXNAControl> XNAControlComponents => AllComponents.OfType<IXNAControl>().ToList();

public void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
}

Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/IControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using XNAControls;

namespace EndlessClient.ControlSets
Expand All @@ -29,8 +29,8 @@ public interface IControlSet : IDisposable
/// Initialize the required resources for the control set from the resource dependencies. Should be called before InitializeControls()
/// </summary>
/// <param name="gfxManager">An initialized native graphics manager</param>
/// <param name="xnaContentManager">The ContentManager for the game</param>
void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager);
/// <param name="contentProvider">The ContentProvider for the game</param>
void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider);

/// <summary>
/// Create the controls for this IControlSet based on an existing set of controls
Expand Down
5 changes: 3 additions & 2 deletions EndlessClient/ControlSets/InitialControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EOLib;
Expand Down Expand Up @@ -38,9 +39,9 @@ public InitialControlSet(IConfigurationProvider configProvider,
_randomGen = new Random();
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

for (int i = 0; i < _personSet1.Length; ++i)
_personSet1[i] = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 41 + i, true);
Expand Down
Loading

0 comments on commit 5fa705f

Please sign in to comment.