Skip to content

Commit

Permalink
performance improvements (#25)
Browse files Browse the repository at this point in the history
* performance improvements

* update build info
  • Loading branch information
gmriggs authored May 25, 2021
1 parent 9559027 commit c8e077c
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 347 deletions.
15 changes: 4 additions & 11 deletions ACViewer/Extensions/TextBoxExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls;

using ACViewer.View;

namespace ACViewer
{
public static class TextBoxExtensions
{
public static void WriteLine(this TextBox textBox, string line)
{
if (textBox.Text.Length != 0)
textBox.AppendText("\n");

textBox.AppendText(line);
textBox.ScrollToEnd();
MainWindow.Instance.AddStatusText(line);
}
}
}
16 changes: 4 additions & 12 deletions ACViewer/FileTypes/ObjDesc.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
using ACE.DatLoader.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using ACE.DatLoader.Entity;

namespace ACViewer.FileTypes
{

/// <summary>
/// Used in the Clothing Table entries to change the look/color of a default model (e.g. because it has clothing equipped)
/// </summary>
public class ObjDesc
{
public uint PaletteID { get; private set; }
public List<SubPalette> SubPalettes { get; } = new List<SubPalette>();
public List<TextureMapChange> TextureChanges { get; } = new List<TextureMapChange>();
public List<AnimationPartChange> AnimPartChanges { get; } = new List<AnimationPartChange>();

public Dictionary<byte, List<TextureMapChange>> TextureChanges { get; } = new Dictionary<byte, List<TextureMapChange>>();
public Dictionary<byte, AnimationPartChange> AnimPartChanges { get; } = new Dictionary<byte, AnimationPartChange>();
}
}
3 changes: 0 additions & 3 deletions ACViewer/GameView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ protected override void Draw(GameTime time)
{
GraphicsDevice.Clear(new Color(0, 0, 0));

//if (Render != null)
//Render.Draw();

switch (ViewMode)
{
case ViewMode.Texture:
Expand Down
16 changes: 2 additions & 14 deletions ACViewer/Model/GfxObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void BuildVertexBuffer()
VertexBuffer.SetData(VertexArray.ToArray());
}

public void LoadTextures()
public void LoadTextures(List<ACE.DatLoader.Entity.TextureMapChange> textureChanges = null, Dictionary<int, uint> customPaletteColors = null, bool useCache = true)
{
Textures = new List<Texture2D>();
Surfaces = new List<Surface>();
Expand All @@ -75,19 +75,7 @@ public void LoadTextures()
var surface = DatManager.PortalDat.ReadFromDat<Surface>(surfaceID);
Surfaces.Add(surface);

Textures.Add(TextureCache.Get(surfaceID));
}
}
public void LoadTextures(List<ACE.DatLoader.Entity.TextureMapChange> textureChanges, Dictionary<int, uint> customPaletteColors)
{
Textures = new List<Texture2D>();
Surfaces = new List<Surface>();

foreach (var surfaceID in _gfxObj.Surfaces)
{
var surface = DatManager.PortalDat.ReadFromDat<Surface>(surfaceID);

Textures.Add(TextureCache.Get(surfaceID, textureChanges, customPaletteColors, false));
Textures.Add(TextureCache.Get(surfaceID, textureChanges, customPaletteColors, useCache));
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions ACViewer/Model/Setup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using ACE.DatLoader;
Expand Down Expand Up @@ -57,31 +58,32 @@ public Setup(uint setupID, FileTypes.ObjDesc objDesc, Dictionary<int, uint> cust
BuildBoundingBox();
return;
}

_setup = DatManager.PortalDat.ReadFromDat<SetupModel>(setupID);

Parts = new List<GfxObj>();
for (var i = 0; i < _setup.Parts.Count; i++)

for (byte i = 0; i < _setup.Parts.Count; i++)
{
uint gfxObjID;
GfxObj gfxObj;

var apChange = objDesc.AnimPartChanges.Where(s => s.PartIndex == i).FirstOrDefault();
if (apChange != null)
if (objDesc.AnimPartChanges.TryGetValue(i, out var apChange))
{
gfxObjID = apChange.PartID;
var gfxObjID = apChange.PartID;
gfxObj = new GfxObj(gfxObjID, false);
List<ACE.DatLoader.Entity.TextureMapChange> tmChanges = objDesc.TextureChanges.FindAll(s => s.PartIndex == i);

objDesc.TextureChanges.TryGetValue(i, out var tmChanges);

gfxObj.LoadTextures(tmChanges, customPaletteColors, false);

gfxObj.LoadTextures(tmChanges, customPaletteColors);
gfxObj.BuildPolygons();
}
else
{
gfxObjID = _setup.Parts[i];
var gfxObjID = _setup.Parts[i];
gfxObj = new GfxObj(gfxObjID, false);

gfxObj.LoadTextures(null, customPaletteColors);

gfxObj.BuildPolygons();
}

Expand Down
4 changes: 0 additions & 4 deletions ACViewer/Model/SetupInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public SetupInstance(uint setupID)
/// <summary>
/// For loading a SetupInstance with a Clothing Base
/// </summary>
/// <param name="setupID"></param>
/// <param name="clothingBase"></param>
/// <param name="palTemplate"></param>
/// <param name="shade"></param>
public SetupInstance(uint setupID, FileTypes.ObjDesc objDesc, Dictionary<int, uint> customPaletteColors)
{
Setup = new Setup(setupID, objDesc, customPaletteColors);
Expand Down
40 changes: 25 additions & 15 deletions ACViewer/ModelViewer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
Expand Down Expand Up @@ -27,39 +28,39 @@ public enum ModelType

public class ModelViewer
{
public static MainWindow MainWindow { get => MainWindow.Instance; }
public static MainWindow MainWindow => MainWindow.Instance;
public static GameView GameView => GameView.Instance;

public static ModelViewer Instance;
public static ModelViewer Instance { get; set; }

public SetupInstance Setup;
public R_EnvCell EnvCell;
public R_Environment Environment;

public static GraphicsDevice GraphicsDevice { get => GameView.Instance.GraphicsDevice; }
public Render.Render Render { get => GameView.Instance.Render; }
public static Effect Effect { get => ACViewer.Render.Render.Effect; }
public static Camera Camera { get => GameView.Camera; }
public static GraphicsDevice GraphicsDevice => GameView.GraphicsDevice;
public Render.Render Render => GameView.Render;
public static Effect Effect => ACViewer.Render.Render.Effect;
public static Camera Camera => GameView.Camera;

public ViewObject ViewObject { get; set; }

public bool GfxObjMode = false;

public WpfKeyboard Keyboard { get => GameView.Instance._keyboard; }
public WpfKeyboard Keyboard => GameView._keyboard;

public KeyboardState PrevKeyboardState;

public ModelType ModelType;

public static GameView GameView => GameView.Instance;

public static Player Player => GameView.Player;

public ModelViewer()
{
Instance = this;
}

public void LoadModel(uint id)
{
TextureCache.Init();

// can be either a gfxobj or setup id
// if gfxobj, create a simple setup
MainWindow.Status.WriteLine($"Loading {id:X8}");
Expand All @@ -75,6 +76,8 @@ public void LoadModel(uint id)

public void LoadModel(uint id, ClothingTable clothingBase, uint palTemplate, float shade)
{
TextureCache.Init();

// can be either a gfxobj or setup id
// if gfxobj, create a simple setup
GfxObjMode = false;
Expand All @@ -84,23 +87,29 @@ public void LoadModel(uint id, ClothingTable clothingBase, uint palTemplate, flo
FileTypes.ObjDesc objDesc = new FileTypes.ObjDesc();

var cbe = clothingBase.ClothingBaseEffects[id].CloObjectEffects;

foreach (var objEffect in cbe)
{
ACE.DatLoader.Entity.AnimationPartChange apChange = new ACE.DatLoader.Entity.AnimationPartChange();
apChange.PartID = objEffect.ModelId;
apChange.PartIndex = (byte)objEffect.Index;

objDesc.AnimPartChanges.Add(apChange);
objDesc.AnimPartChanges.Add(apChange.PartIndex, apChange);

foreach(var texEffect in objEffect.CloTextureEffects)
{
ACE.DatLoader.Entity.TextureMapChange tmChange = new ACE.DatLoader.Entity.TextureMapChange();
tmChange.PartIndex = apChange.PartIndex;
tmChange.OldTexture = texEffect.OldTexture;
tmChange.NewTexture = texEffect.NewTexture;

objDesc.TextureChanges.Add(tmChange);
if (!objDesc.TextureChanges.TryGetValue(tmChange.PartIndex, out var tmChanges))
{
tmChanges = new List<ACE.DatLoader.Entity.TextureMapChange>();
objDesc.TextureChanges.Add(tmChange.PartIndex, tmChanges);
}
tmChanges.Add(tmChange);
}

}

// To hold our Custom Palette (palette swaps)
Expand All @@ -124,13 +133,14 @@ public void LoadModel(uint id, ClothingTable clothingBase, uint palTemplate, flo
}
}


Setup = new SetupInstance(id, objDesc, customPaletteColors);

InitObject(id);

Render.Camera.InitModel(Setup.Setup.BoundingBox);

ModelType = ModelType.Setup;

MainWindow.Status.WriteLine($"Loading {id:X8} with ClothingBase {clothingBase.Id:X8}, PaletteTemplate {palTemplate}, and Shade {shade}");
}

Expand Down
2 changes: 1 addition & 1 deletion ACViewer/Render/R_Landblock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void BuildTextures()

var mipData = baseTexture.GetMipData(numColors);

var overlays = new Texture2D(GraphicsDevice, baseTexture.Width, baseTexture.Height, true, SurfaceFormat.Color, 5);
var overlays = new Texture2D(GraphicsDevice, baseTexture.Width, baseTexture.Height, TextureCache.UseMipMaps, SurfaceFormat.Color, 5);
//overlays.SetData(0, 0, null, data, 0, numColors);
for (var j = 0; j < numLevels; j++)
overlays.SetData(j, 0, null, mipData[j], 0, mipData[j].Length);
Expand Down
Loading

0 comments on commit c8e077c

Please sign in to comment.