Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Removing Image dependancy on TextureData.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Oct 7, 2020
1 parent 87f45a9 commit 8d64e70
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Disks/PixelVisionOS/System/Tools/WorkspaceTool/saves.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"GameChip":
{
"savedData":{
"sessionID": "202010060946551271",
"sessionID": "202010071320023653",
"scrollPos": "0",
"selection": "6",
"lastPath": "/Workspace/SpriteStressDemo/"
"selection": "0",
"lastPath": "/Workspace/"
}
}
}
4 changes: 2 additions & 2 deletions SDK/Editor/Exporters/BackgroundScriptRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ public void SetImageAsData(Image image, string maskColor = "#FF00FF")

try
{
var palette = image.colors.Select(ColorUtils.HexToColor).ToArray();
var palette = image.Colors.Select(ColorUtils.HexToColor).ToArray();

var imageExporter = new PNGWriter();

var exporter = new PixelDataExporter(fileName, image.Pixels, image.width, image.height, palette, imageExporter,
var exporter = new PixelDataExporter(fileName, image.GetPixels(), image.Width, image.Height, palette, imageExporter,
maskColor);

exporter.CalculateSteps();
Expand Down
8 changes: 4 additions & 4 deletions SDK/Editor/Services/LuaServicePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,16 @@ public Image ReadImage(WorkspacePath src, string maskHex = "#ff00ff", string[] c

public void SaveImage(WorkspacePath dest, Image image)
{
var width = image.width;
var height = image.height;
var hexColors = image.colors;
var width = image.Width;
var height = image.Height;
var hexColors = image.Colors;

// convert colors
var totalColors = hexColors.Length;
var colors = new Color[totalColors];
for (var i = 0; i < totalColors; i++) colors[i] = ColorUtils.HexToColor(hexColors[i]);

var pixelData = image.Pixels;
var pixelData = image.GetPixels();

var exporter =
new PixelDataExporter(dest.EntityName, pixelData, width, height, colors, _pngWriter, "#FF00FF");
Expand Down
61 changes: 61 additions & 0 deletions SDK/Engine/Data/TextureData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// Shawn Rakowski - @shwany
//

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using PixelVision8.Engine.Utils;

namespace PixelVision8.Engine
Expand All @@ -34,6 +36,65 @@ namespace PixelVision8.Engine
/// </summary>
public class TextureData : AbstractData
{

public string[] colors;
public int Columns => width / _spriteSize.X;
public int Rows => height / _spriteSize.Y;
public int TotalSprites => Columns * Rows;

protected Point _spriteSize;
protected List<int> _colorIDs = new List<int>();
protected int _colorID;
protected Point _pos;
protected int[] _pixelData;

/// <summary>
/// Get a single sprite from the Image.
/// </summary>
/// <param name="id"></param>
/// <param name="cps">Total number of colors supported by the sprite.</param>
/// <returns></returns>
public int[] GetSpriteData(int id, int? cps = null)
{
_pos = MathUtil.CalculatePosition(id, Columns);

_pixelData = GetPixels(_pos.X * 8, _pos.Y * 8, _spriteSize.X, _spriteSize.Y);

// If there is a CPS cap, we need to go through all the pixels and make sure they are in range.
if (cps.HasValue)
{

_colorIDs.Clear();

for (int i = 0; i < TotalPixels; i++)
{
_colorID = _pixelData[i];

if (_colorID > -1 && _colorIDs.IndexOf(_colorID) == -1)
{
if (_colorIDs.Count < cps.Value)
{
_colorIDs.Add(_colorID);
}
else
{
_pixelData[i] = -1;
}

}


}

}

// Return the new sprite image
return _pixelData;
}




protected PixelData pixelData = new PixelData(256, 256);

protected int _height
Expand Down
68 changes: 41 additions & 27 deletions SDK/Runner/Data/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,47 @@
/// Wrapper for texture data that includes Hex color data to rebuild colors when
/// exporting and cutting up sprites.
/// </summary>
public class Image : TextureData
public struct Image
{
public string[] colors;
public int Columns => width / _spriteSize.X;
public int Rows => height / _spriteSize.Y;

private PixelData pixelData;
public int Width => pixelData.Width;

public int Height => pixelData.Height;
public int Columns => Width / _spriteSize.X;
public int Rows => Height / _spriteSize.Y;

public string[] Colors;

public int TotalSprites => Columns * Rows;

protected readonly Point _spriteSize;
private List<int> _colorIDs = new List<int>();
private Point _spriteSize;
private List<int> _colorIDs;
private int _colorID;
private Point _pos;
private int[] _pixelData;
private int[] _tmpPixelData;

public Image(int width, int height, string[] colors, int[] pixelData = null, Point? spriteSize = null) : base(width, height)
public Image(int width, int height, string[] colors, int[] pixels = null, Point? spriteSize = null)// : base(width, height)
{
this.colors = colors;
pixelData = new PixelData(256, 256);

this.Colors = colors;
_spriteSize = spriteSize ?? new Point(8, 8);

if (pixelData != null)
_colorIDs = new List<int>();
_colorID = 0;
_pos = Point.Zero;
_tmpPixelData = null;

Resize(width, height);

if (pixels != null)
{
SetPixels(pixelData);
PixelDataUtil.SetPixels(pixels, pixelData);
}

}

/// <summary>
/// Get a single sprite from the Image.
/// </summary>
Expand All @@ -62,17 +78,17 @@ public int[] GetSpriteData(int id, int? cps = null)
{
_pos = MathUtil.CalculatePosition(id, Columns);

_pixelData = GetPixels(_pos.X * 8, _pos.Y * 8, _spriteSize.X, _spriteSize.Y);

// _pixelData = GetPixels(_pos.X * 8, _pos.Y * 8, _spriteSize.X, _spriteSize.Y);
_tmpPixelData = PixelDataUtil.GetPixels(pixelData, _pos.X * 8, _pos.Y * 8, _spriteSize.X, _spriteSize.Y);
// If there is a CPS cap, we need to go through all the pixels and make sure they are in range.
if (cps.HasValue)
{

_colorIDs.Clear();

for (int i = 0; i < TotalPixels; i++)
for (int i = 0; i < _tmpPixelData.Length; i++)
{
_colorID = _pixelData[i];
_colorID = _tmpPixelData[i];

if (_colorID > -1 && _colorIDs.IndexOf(_colorID) == -1)
{
Expand All @@ -82,23 +98,21 @@ public int[] GetSpriteData(int id, int? cps = null)
}
else
{
_pixelData[i] = -1;
_tmpPixelData[i] = -1;
}

}


}

}

// Return the new sprite image
return _pixelData;
return _tmpPixelData;
}

public void SetPixels(int[] newPixelData) => PixelDataUtil.SetPixels(newPixelData, pixelData);

public void RemapColors(Color colors)
{
// TODO need to create a way to remap the colors in the current image to the one passed in
}
public int[] GetPixels() => PixelDataUtil.GetPixels(pixelData);

public void Resize(int newWidth, int newHeight) => PixelDataUtil.Resize(ref pixelData, newWidth, newHeight);


}

0 comments on commit 8d64e70

Please sign in to comment.