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

Commit

Permalink
Removed TextureData from SpriteChip.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Oct 5, 2020
1 parent 50914a2 commit 892fe64
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Disks/PixelVisionOS/System/Tools/WorkspaceTool/saves.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"lastPath": "/Workspace/",
"selection": "0",
"scrollPos": "0",
"sessionID": "202010051028346993"
"sessionID": "202010051100230142"
}
}
}
7 changes: 4 additions & 3 deletions SDK/Editor/Editors/GameEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,8 @@ public int[] ReadToolSpriteData(int id, int scaleX = 1, int scaleY = 1, bool fli

var pos = gameChip.CalculatePosition(id, spriteChip.textureWidth / spriteChip.width);

spriteChip.texture.CopyPixels(ref pixelData, pos.X * 8, pos.Y * 8, blockSizeX, blockSizeY);
PixelDataUtil.CopyPixels(ref pixelData, spriteChip.PixelData, pos.X * 8, pos.Y * 8, blockSizeX, blockSizeY);
// spriteChip.texture.CopyPixels(ref pixelData, pos.X * 8, pos.Y * 8, blockSizeX, blockSizeY);

// var pixelData = Sprite(id);

Expand All @@ -2020,8 +2021,8 @@ public void WriteSpriteData(int id, int[] pixelData, int scaleX = 1, int scaleY
// var pos = gameChip.CalculatePosition(id, spriteChip.textureWidth / spriteChip.width);

// Console.WriteLine("Write sprite " + pos.X +" "+pos.Y);
spriteChip.texture.SetPixels(pos.X * 8, pos.Y * 8, blockSizeX, blockSizeY, pixelData);

// spriteChip.texture.SetPixels(pos.X * 8, pos.Y * 8, blockSizeX, blockSizeY, pixelData);
PixelDataUtil.SetPixels(spriteChip.PixelData, pos.X * 8, pos.Y * 8, blockSizeX, blockSizeY, pixelData);

// TODO need to invalidate the cached sprite data

Expand Down
4 changes: 3 additions & 1 deletion SDK/Editor/Exporters/SpriteExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public virtual void ConfigurePixelData()
var height = spriteChip.textureHeight;
var pixelData = new int[width * height];

spriteChip.texture.CopyPixels(ref pixelData, 0, 0, width, height);
PixelDataUtil.CopyPixels(ref pixelData, spriteChip.PixelData, 0, 0 , width, height);

// spriteChip.texture.CopyPixels(ref pixelData, 0, 0, width, height);

exporter = new PixelDataExporter(fullFileName, pixelData, width, height, colors, imageExporter,
engine.ColorChip.maskColor);
Expand Down
4 changes: 2 additions & 2 deletions SDK/Editor/Exporters/TilemapJsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private void SaveMapDataV1()
{
{
"sprites.png",
new SpriteVector(spriteChip.texture.width, spriteChip.texture.height, spriteChip.TotalSprites)
new SpriteVector(spriteChip.textureWidth, spriteChip.textureHeight, spriteChip.TotalSprites)
}
};

Expand All @@ -306,7 +306,7 @@ private void SaveMapDataV1()

// columns
sb.Append("\"columns\":");
sb.Append(spriteChip.texture.width / spriteSize.X);
sb.Append(spriteChip.textureWidth / spriteSize.X);
sb.Append(",");
JsonUtil.GetLineBreak(sb, 2);

Expand Down
46 changes: 32 additions & 14 deletions SDK/Engine/Chips/Graphics/SpriteChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public class SpriteChip : AbstractChip
/// <summary>
/// Internal <see cref="TextureData" /> where sprites are stored
/// </summary>
protected TextureData _texture = new TextureData(128, 128);
// protected TextureData _texture = new TextureData(128, 128);

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

/// <summary>
/// Internal <see cref="cache" /> for faster lookup
/// </summary>
Expand Down Expand Up @@ -99,24 +101,35 @@ public class SpriteChip : AbstractChip
/// TextureData. When requested, a clone of the <see cref="_texture" />
/// field is returned. This is expensive and only used for tools.
/// </summary>
public TextureData texture
// public TextureData texture
// {
// get => _texture;
// set => SpriteChipUtil.CloneTextureData(value, _texture);
// //TODO do we need this?
// }

public PixelData PixelData
{
get => _texture;
set => SpriteChipUtil.CloneTextureData(value, _texture);
//TODO do we need this?
get => pixelData;
// set
// {
// PixelDataUtil.Resize(ref pixelData, value.Width, value.Height);
// PixelDataUtil.SetPixels(value.Pixels, pixelData);
//
// }
}

/// <summary>
/// Return's the <see cref="Sprite" /> Ram's internal
/// <see cref="texture" /> <see cref="width" />
/// </summary>
public int textureWidth => _texture.width;
public int textureWidth => pixelData.Width;

/// <summary>
/// Return's the <see cref="Sprite" /> Ram's internal
/// <see cref="texture" /> <see cref="width" />
/// </summary>
public int textureHeight => _texture.height;
public int textureHeight => pixelData.Height;

/// <summary>
/// The virtual number of pages of sprite memory the SpriteChip
Expand Down Expand Up @@ -242,8 +255,9 @@ public void Resize(int w, int h)

if (textureWidth != w || textureHeight != h)
{
texture.Resize(w, h);
Clear();
PixelDataUtil.Resize(ref pixelData, w, h);
// texture.Resize(w, h);
// Clear();
}

//TODO this needs to be double checked at different size sprites
Expand Down Expand Up @@ -286,7 +300,9 @@ public void Clear()
{
cache = new string[TotalSprites];
// pixelDataCache = new int[totalSprites][];
_texture.Clear();

PixelDataUtil.Clear(pixelData);
// _texture.Clear();
}
// protected readonly int totalSpritePixels = 64;

Expand Down Expand Up @@ -315,7 +331,7 @@ public void ReadSpriteAt(int index, ref int[] pixelData)
}
else
{
width1 = _texture.width;
width1 = this.pixelData.Width;
// height1 = _texture.height;

w = width1 / width;
Expand All @@ -326,7 +342,8 @@ public void ReadSpriteAt(int index, ref int[] pixelData)
// Flip y for Unity
// tmpY = height1 - tmpY - height;

_texture.CopyPixels(ref pixelData, tmpX, tmpY, width, height);
PixelDataUtil.CopyPixels(ref pixelData, this.pixelData, tmpX, tmpY, width, height);
// _texture.CopyPixels(ref pixelData, tmpX, tmpY, width, height);
}
}

Expand Down Expand Up @@ -360,15 +377,16 @@ public void UpdateSpriteAt(int index, int[] pixels)
// Make sure we stay in bounds
index = MathHelper.Clamp(index, 0, totalSprites1 - 1);

var w1 = _texture.width / width;
var w1 = pixelData.Width / width;

x = index % w1 * width;
y = index / w1 * height;

// if (true)
// y = _texture.height - y - height;

_texture.SetPixels(x, y, width, height, pixels);
PixelDataUtil.SetPixels(this.pixelData, x, y, width, height, pixels);
// _texture.SetPixels(x, y, width, height, pixels);

CacheSprite(index, pixels);
}
Expand Down

0 comments on commit 892fe64

Please sign in to comment.