Refactoring

move files, abstract some logic for later reuse
This commit is contained in:
Kurt
2020-05-11 22:14:27 -07:00
parent 46d4f4e8c1
commit 60faecdc69
18 changed files with 321 additions and 270 deletions

View File

@@ -3,12 +3,12 @@
namespace NHSE.Sprites
{
public static class FieldItemSpriteDrawer
public static class ItemLayerSprite
{
public static Bitmap GetBitmapItemLayer(FieldItemLayer layer)
public static Bitmap GetBitmapItemLayer(ItemLayer layer)
{
var items = layer.Tiles;
var height = layer.MapHeight;
var height = layer.MaxHeight;
var width = items.Length / height;
var bmpData = new int[width * height];
@@ -31,7 +31,7 @@ private static void LoadBitmapLayer(Item[] items, int[] bmpData, int width, int
}
}
private static void LoadPixelsFromLayer(FieldItemLayer layer, int x0, int y0, int width, int[] bmpData)
private static void LoadPixelsFromLayer(ItemLayer layer, int x0, int y0, int width, int[] bmpData)
{
var stride = layer.GridWidth;
@@ -49,7 +49,7 @@ private static void LoadPixelsFromLayer(FieldItemLayer layer, int x0, int y0, in
}
// non-allocation image generator
public static Bitmap GetBitmapItemLayerAcre(FieldItemLayer layer, int x0, int y0, int scale, int[] acre1, int[] acreScale, Bitmap dest, int transparency = -1)
public static Bitmap GetBitmapItemLayerViewGrid(ItemLayer layer, int x0, int y0, int scale, int[] acre1, int[] acreScale, Bitmap dest, int transparency = -1, int gridlineColor = 0)
{
var w = layer.GridWidth;
var h = layer.GridHeight;
@@ -65,7 +65,6 @@ public static Bitmap GetBitmapItemLayerAcre(FieldItemLayer layer, int x0, int y0
DrawDirectionals(acreScale, layer, w, x0, y0, scale);
// Slap on a grid
const int gridlineColor = 0; // let the underlying image grid show instead
DrawGrid(acreScale, w, h, scale, gridlineColor);
// Return final data
@@ -73,7 +72,7 @@ public static Bitmap GetBitmapItemLayerAcre(FieldItemLayer layer, int x0, int y0
return dest;
}
private static void DrawDirectionals(int[] data, FieldItemLayer layer, int w, int x0, int y0, int scale)
private static void DrawDirectionals(int[] data, ItemLayer layer, int w, int x0, int y0, int scale)
{
for (int x = x0; x < x0 + layer.GridWidth; x++)
{
@@ -171,9 +170,9 @@ public static void DrawGrid(int[] data, int w, int h, int scale, int gridlineCol
}
}
public static Bitmap GetBitmapItemLayer(FieldItemLayer layer, int x, int y, int[] data, Bitmap dest, int transparency = -1)
public static Bitmap GetBitmapItemLayer(ItemLayer layer, int x, int y, int[] data, Bitmap dest, int transparency = -1)
{
LoadBitmapLayer(layer.Tiles, data, layer.MapWidth, layer.MapHeight);
LoadBitmapLayer(layer.Tiles, data, layer.MaxWidth, layer.MaxHeight);
if (transparency >> 24 != 0xFF)
ImageUtil.ClampAllTransparencyTo(data, transparency);
ImageUtil.SetBitmapData(dest, data);

View File

@@ -0,0 +1,74 @@
using System;
using System.Drawing;
using NHSE.Core;
namespace NHSE.Sprites
{
public sealed class MapViewer : MapView, IDisposable
{
// Cached acre view objects to remove allocation/GC
private readonly int[] PixelsItemAcre1;
private readonly int[] PixelsItemAcreX;
private readonly Bitmap ScaleAcre;
private readonly int[] PixelsItemMap;
private readonly Bitmap MapReticle;
private readonly int[] PixelsBackgroundAcre1;
private readonly int[] PixelsBackgroundAcreX;
private readonly Bitmap BackgroundAcre;
private readonly int[] PixelsBackgroundMap1;
private readonly int[] PixelsBackgroundMapX;
private readonly Bitmap BackgroundMap;
public MapViewer(MapManager m) : base(m)
{
var l1 = m.Items.Layer1;
PixelsItemAcre1 = new int[l1.GridWidth * l1.GridHeight];
PixelsItemAcreX = new int[PixelsItemAcre1.Length * AcreScale * AcreScale];
ScaleAcre = new Bitmap(l1.GridWidth * AcreScale, l1.GridHeight * AcreScale);
PixelsItemMap = new int[l1.MapWidth * l1.MapHeight * MapScale * MapScale];
MapReticle = new Bitmap(l1.MapWidth * MapScale, l1.MapHeight * MapScale);
PixelsBackgroundAcre1 = new int[16 * 16];
PixelsBackgroundAcreX = new int[PixelsItemAcreX.Length];
BackgroundAcre = new Bitmap(ScaleAcre.Width, ScaleAcre.Height);
PixelsBackgroundMap1 = new int[PixelsItemMap.Length / 4];
PixelsBackgroundMapX = new int[PixelsItemMap.Length];
BackgroundMap = new Bitmap(MapReticle.Width, MapReticle.Height);
}
public void Dispose()
{
ScaleAcre.Dispose();
MapReticle.Dispose();
BackgroundAcre.Dispose();
BackgroundMap.Dispose();
}
public Bitmap GetLayerAcre(int t) => GetLayerAcre(X, Y, t);
public Bitmap GetMapWithReticle(int t) => GetMapWithReticle(X, Y, t, Map.CurrentLayer);
public Bitmap GetBackgroundTerrain(int index = -1)
{
return TerrainSprite.GetMapWithBuildings(Map, null, PixelsBackgroundMap1, PixelsBackgroundMapX, BackgroundMap, 2, index);
}
private Bitmap GetLayerAcre(int topX, int topY, int t)
{
var layer = Map.CurrentLayer;
return ItemLayerSprite.GetBitmapItemLayerViewGrid(layer, topX, topY, AcreScale, PixelsItemAcre1, PixelsItemAcreX, ScaleAcre, t);
}
public Bitmap GetBackgroundAcre(Font f, byte tbuild, byte tterrain, int index = -1)
{
return TerrainSprite.GetAcre(this, f, PixelsBackgroundAcre1, PixelsBackgroundAcreX, BackgroundAcre, index, tbuild, tterrain);
}
private Bitmap GetMapWithReticle(int topX, int topY, int t, FieldItemLayer layer)
{
return ItemLayerSprite.GetBitmapItemLayer(layer, topX, topY, PixelsItemMap, MapReticle, t);
}
}
}

View File

@@ -17,7 +17,7 @@ public static class TerrainSprite
private const int PlazaWidth = 6 * 2;
private const int PlazaHeight = 5 * 2;
public static void CreateMap(TerrainManager mgr, int[] pixels)
public static void CreateMap(TerrainLayer mgr, int[] pixels)
{
int i = 0;
for (int y = 0; y < mgr.MapHeight; y++)
@@ -29,7 +29,7 @@ public static void CreateMap(TerrainManager mgr, int[] pixels)
}
}
public static Bitmap CreateMap(TerrainManager mgr, int scale, int x, int y, int[] scale1, int[] scaleX, Bitmap map)
public static Bitmap CreateMap(TerrainLayer mgr, int scale, int x, int y, int[] scale1, int[] scaleX, Bitmap map)
{
CreateMap(mgr, scale1);
ImageUtil.ScalePixelImage(scale1, scaleX, map.Width, map.Height, scale);
@@ -37,7 +37,7 @@ public static Bitmap CreateMap(TerrainManager mgr, int scale, int x, int y, int[
return DrawReticle(map, mgr, x, y, scale);
}
public static Bitmap CreateMap(TerrainManager mgr, int[] scale1, int[] scaleX, Bitmap map, int scale, int acreIndex = -1)
public static Bitmap CreateMap(TerrainLayer mgr, int[] scale1, int[] scaleX, Bitmap map, int scale, int acreIndex = -1)
{
CreateMap(mgr, scale1);
ImageUtil.ScalePixelImage(scale1, scaleX, map.Width, map.Height, scale);
@@ -74,7 +74,7 @@ public static Bitmap GetMapWithBuildings(MapTerrainStructure m, Font? f, int[] s
return map;
}
private static void DrawPlaza(this Graphics gfx, TerrainManager g, ushort px, ushort py, int scale)
private static void DrawPlaza(this Graphics gfx, TerrainLayer g, ushort px, ushort py, int scale)
{
g.GetBuildingCoordinate(px, py, scale, out var x, out var y);
@@ -84,7 +84,7 @@ private static void DrawPlaza(this Graphics gfx, TerrainManager g, ushort px, us
gfx.FillRectangle(Plaza, x, y, width, height);
}
private static void DrawBuildings(this Graphics gfx, TerrainManager g, IReadOnlyList<Building> buildings, Font? f, int scale, int index = -1)
private static void DrawBuildings(this Graphics gfx, TerrainLayer g, IReadOnlyList<Building> buildings, Font? f, int scale, int index = -1)
{
for (int i = 0; i < buildings.Count; i++)
{
@@ -109,13 +109,13 @@ private static void DrawBuilding(Graphics gfx, Font? f, int scale, Brush pen, in
}
}
private static void SetAcreTerrainPixels(int x, int y, TerrainManager t, int[] data, int[] scaleX, int scale)
private static void SetAcreTerrainPixels(int x, int y, TerrainLayer t, int[] data, int[] scaleX, int scale)
{
GetAcre1(x, y, t, data);
ImageUtil.ScalePixelImage(data, scaleX, 16 * scale, 16 * scale, scale);
}
private static void GetAcre1(int topX, int topY, TerrainManager t, int[] data)
private static void GetAcre1(int topX, int topY, TerrainLayer t, int[] data)
{
int index = 0;
for (int y = 0; y < 16; y++)
@@ -157,8 +157,8 @@ public static Bitmap GetAcre(MapView m, Font f, int[] scale1, int[] scaleX, Bitm
}
ImageUtil.GetBitmapData(acre, scaleX);
FieldItemSpriteDrawer.DrawGrid(scaleX, acre.Width, acre.Height, m.AcreScale, grid1);
FieldItemSpriteDrawer.DrawGrid(scaleX, acre.Width, acre.Height, m.TerrainScale, grid2);
ItemLayerSprite.DrawGrid(scaleX, acre.Width, acre.Height, m.AcreScale, grid1);
ItemLayerSprite.DrawGrid(scaleX, acre.Width, acre.Height, m.TerrainScale, grid2);
ImageUtil.SetBitmapData(acre, scaleX);
foreach (var b in buildings)
@@ -176,7 +176,7 @@ public static Bitmap GetAcre(MapView m, Font f, int[] scale1, int[] scaleX, Bitm
return acre;
}
private static void DrawTerrainTileNames(int topX, int topY, Graphics gfx, TerrainManager t, Font f, int scale, byte transparency)
private static void DrawTerrainTileNames(int topX, int topY, Graphics gfx, TerrainLayer t, Font f, int scale, byte transparency)
{
var pen= transparency != byte.MaxValue ? new SolidBrush(Color.FromArgb(transparency, Color.Black)) : Tile;
@@ -196,7 +196,7 @@ private static void DrawTerrainTileNames(int topX, int topY, Graphics gfx, Terra
}
}
private static void DrawAcrePlaza(this Graphics gfx, TerrainManager g, int topX, int topY, ushort px, ushort py, int scale, byte transparency)
private static void DrawAcrePlaza(this Graphics gfx, TerrainLayer g, int topX, int topY, ushort px, ushort py, int scale, byte transparency)
{
g.GetBuildingRelativeCoordinates(topX, topY, scale, px, py, out var x, out var y);