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

Commit

Permalink
Removing TextureData from the Canvas, Tilemap parser, and export serv…
Browse files Browse the repository at this point in the history
…ice.
  • Loading branch information
jessefreeman committed Oct 5, 2020
1 parent 892fe64 commit ccf6ede
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 146 deletions.
6 changes: 3 additions & 3 deletions Disks/PixelVisionOS/System/Tools/WorkspaceTool/saves.json
Expand Up @@ -3,10 +3,10 @@
"GameChip":
{
"savedData":{
"lastPath": "/Workspace/",
"selection": "0",
"lastPath": "/Workspace/SpriteStressDemo/",
"selection": "6",
"scrollPos": "0",
"sessionID": "202010051100230142"
"sessionID": "202010051148313627"
}
}
}
42 changes: 22 additions & 20 deletions SDK/Editor/Services/GameDataExportService.cs
Expand Up @@ -71,26 +71,28 @@ public void ExportGame(string path, IEngine engine, SaveFlags saveFlags, bool us
// Step 7 (optional). Look for fonts to load
if ((saveFlags & SaveFlags.Fonts) == SaveFlags.Fonts)
{
var fontChip = targetEngine.FontChip;
var spriteChip = targetEngine.SpriteChip;
var tmpTextureData = new TextureData(96, 64);

var fonts = fontChip.fonts;

foreach (var font in fonts)
{
var name = font.Key;
var sprites = font.Value;

// Clear the texture
tmpTextureData.Clear();

// Loop through all the characters and copy their texture data over
var total = sprites.Length;
for (var i = 0; i < total; i++)
{
}
}
// TODO this is not finished
// var fontChip = targetEngine.FontChip;
// var spriteChip = targetEngine.SpriteChip;
// var tmpTextureData = new PixelData(96, 64);
//
// var fonts = fontChip.fonts;
//
// foreach (var font in fonts)
// {
// var name = font.Key;
// var sprites = font.Value;
//
// // Clear the texture
// PixelDataUtil.Clear(tmpTextureData);
// // tmpTextureData.Clear();
//
// // Loop through all the characters and copy their texture data over
// var total = sprites.Length;
// for (var i = 0; i < total; i++)
// {
// }
// }
}

if ((saveFlags & SaveFlags.Tilemap) == SaveFlags.Tilemap)
Expand Down
10 changes: 1 addition & 9 deletions SDK/Engine/Chips/Game/GameChip.cs
@@ -1,4 +1,4 @@
//
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
Expand Down Expand Up @@ -1709,14 +1709,6 @@ public void UpdateTiles(int[] ids, int? colorOffset = null, int? flag = null)
/// tile map as well as scrolling of the tile map if it is larger then
/// the screen's resolution.
/// </summary>
/// <param name="textureData">
/// A reference to a <see cref="TextureData" /> class to populate with
/// tile map pixel data.
/// </param>
/// <param name="clearColor">
/// The transparent color to use when a tile is set to -1. The default
/// value is -1 for transparent.
/// </param>
/// <ignore />
protected void RebuildCache(Canvas targetTextureData)
{
Expand Down
2 changes: 1 addition & 1 deletion SDK/Engine/Chips/Graphics/TilemapChip.cs
@@ -1,4 +1,4 @@
//
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
Expand Down
114 changes: 66 additions & 48 deletions SDK/Engine/Data/Canvas.cs
Expand Up @@ -35,8 +35,8 @@ public struct CanvasDrawRequest
public int Y1;
public bool Fill;
public bool DrawCentered;
public Brush Stroke;
public Brush Pattern;
public PixelData Stroke;
public PixelData Pattern;
public int[] Ids;
public bool FlipH;
public bool FlipV;
Expand All @@ -47,20 +47,25 @@ public struct CanvasDrawRequest
public TextureData TargetTexture;
}

public class Brush : TextureData
{
public Brush(int width = 1, int height = 1) : base(width, height)
{
}
}
// public class Brush
// {
//
// private PixelData pixelData = new PixelData(1, 1);
// public Brush(int width = 1, int height = 1)
// {
// PixelDataUtil.Resize(ref pixelData, width, height);
// }
//
// public
// }


public class Canvas : TextureData, IDraw
{
private readonly GameChip gameChip;
private readonly Brush pattern;
private PixelData pattern;
private readonly Point spriteSize;
private readonly Brush stroke;
private PixelData stroke;
private readonly CanvasDrawRequest[] requestPool = new CanvasDrawRequest[1024];
private readonly TextureData tmpLayer = new TextureData();
private int currentRequest = -1;
Expand Down Expand Up @@ -92,11 +97,11 @@ public Canvas(int width, int height, GameChip gameChip = null) : base(width, hei
currentTexture = this;

this.gameChip = gameChip;
pattern = new Brush();
pattern.SetPixel(0, 0, 0);
pattern = new PixelData(1, 1) {Pixels = {[0] = 0}};
// pattern.SetPixel(0, 0, 0);

stroke = new Brush();
stroke.SetPixel(0, 0, 0);
stroke = new PixelData(1, 1) {Pixels = {[0] = 0}};
// stroke.SetPixel(0, 0, 0);
spriteSize = gameChip.SpriteSize();

// Create a pool of draw requests
Expand Down Expand Up @@ -191,13 +196,15 @@ public void SetStroke(int color, int size = 1)

newRequest.Action = "SetStroke";

if (newRequest.Stroke == null)
// if (newRequest.Stroke == null)
// {
// newRequest.Stroke = new PixelData(size, size);
// }
// else
if (newRequest.Stroke.Width != size || newRequest.Stroke.Height != size)
{
newRequest.Stroke = new Brush(size, size);
}
else if (newRequest.Stroke.width != size || newRequest.Stroke.height != size)
{
newRequest.Stroke.Resize(size, size);
PixelDataUtil.Resize(ref newRequest.Stroke, size, size);
// newRequest.Stroke.Resize(size, size);
}

var newPixels = new int[size * size];
Expand All @@ -206,7 +213,8 @@ public void SetStroke(int color, int size = 1)
newPixels[i] = color;
}

newRequest.Stroke.SetPixels(newPixels);
PixelDataUtil.SetPixels(newPixels, newRequest.Stroke);
// newRequest.Stroke.SetPixels(newPixels);

// Save the changes to the request
requestPool[currentRequest] = newRequest;
Expand Down Expand Up @@ -240,10 +248,12 @@ public void SetStroke(int color, int size = 1)

private void SetStrokeAction(CanvasDrawRequest request)
{
if (stroke.width != request.Stroke.width || pattern.height != request.Stroke.height)
stroke.Resize(request.Stroke.width, request.Stroke.height);
if (stroke.Width != request.Stroke.Width || pattern.Height != request.Stroke.Height)
PixelDataUtil.Resize(ref stroke, request.Stroke.Width, request.Stroke.Height);
// stroke.Resize(request.Stroke.width, request.Stroke.height);

stroke.SetPixels(request.Stroke.Pixels);
PixelDataUtil.SetPixels(request.Stroke.Pixels, stroke);
// stroke.SetPixels(request.Stroke.Pixels);
}

public void SetPattern(int[] newPixels, int newWidth, int newHeight)
Expand All @@ -257,36 +267,44 @@ public void SetPattern(int[] newPixels, int newWidth, int newHeight)

newRequest.Action = "SetPattern";

if (newRequest.Pattern == null)
{
newRequest.Pattern = new Brush(newWidth, newHeight);
}
else if (newRequest.Pattern.width != newWidth || newRequest.Pattern.height != newHeight)
// if (newRequest.Pattern == null)
// {
// newRequest.Pattern = new Brush(newWidth, newHeight);
// }
// else
if (newRequest.Pattern.Width != newWidth || newRequest.Pattern.Height != newHeight)
{
newRequest.Pattern.Resize(newWidth, newHeight);
PixelDataUtil.Resize(ref newRequest.Pattern, newWidth, newHeight);

// newRequest.Pattern.Resize(newWidth, newHeight);
}

newRequest.Pattern.SetPixels(newPixels);
PixelDataUtil.SetPixels(newPixels, newRequest.Pattern);

// newRequest.Pattern.SetPixels(newPixels);

// Save the changes to the request
requestPool[currentRequest] = newRequest;
}

public void SetPatternAction(CanvasDrawRequest request)
{
if (pattern.width != request.Pattern.width || pattern.height != request.Pattern.height)
pattern.Resize(request.Pattern.width, request.Pattern.height);
if (pattern.Width != request.Pattern.Width || pattern.Height != request.Pattern.Height)
PixelDataUtil.Resize(ref pattern, request.Pattern.Width, request.Pattern.Height);

// pattern.Resize(request.Pattern.width, request.Pattern.height);
PixelDataUtil.SetPixels(request.Pattern.Pixels, pattern);

pattern.SetPixels(request.Pattern.GetPixels());
// pattern.SetPixels(request.Pattern.GetPixels());
}

private void SetStrokePixel(int x, int y)
{
canDraw = wrap || x >= 0 && x <= width - stroke.width && y >= 0 && y <= height - stroke.height;
canDraw = wrap || x >= 0 && x <= width - stroke.Width && y >= 0 && y <= height - stroke.Height;

// TODO this should never be null
if (canDraw)
currentTexture.SetPixels(x, y, stroke.width, stroke.height, stroke.Pixels);
currentTexture.SetPixels(x, y, stroke.Width, stroke.Height, stroke.Pixels);

}

Expand Down Expand Up @@ -415,16 +433,16 @@ public void DrawSquare(int x0, int y0, int x1, int y1, bool fill = false, bool d
_tl.Y = _y0;

// Calculate the top right
_tr.X = _x0 + _w - (stroke.width * 2);
_tr.X = _x0 + _w - (stroke.Width * 2);
_tr.Y = _y0;

// Calculate the bottom right
_br.X = _x0 + _w - (stroke.width * 2);
_br.Y = _y0 + _h - (stroke.height * 2);
_br.X = _x0 + _w - (stroke.Width * 2);
_br.Y = _y0 + _h - (stroke.Height * 2);

// Calculate the bottom left
_bl.X = _x0;
_bl.Y = _y0 + _h - (stroke.height * 2);
_bl.Y = _y0 + _h - (stroke.Height * 2);

// Determine if the box should be drawn from the center
if (drawCentered)
Expand Down Expand Up @@ -460,7 +478,7 @@ public void DrawSquare(int x0, int y0, int x1, int y1, bool fill = false, bool d
if (fill)
{
// Make sure there are enough pixels to fill
if (Math.Abs(_w) > stroke.width && Math.Abs(_h) > stroke.height)
if (Math.Abs(_w) > stroke.Width && Math.Abs(_h) > stroke.Height)
{
// Trigger a flood fill
FloodFill(_center.X, _center.Y);
Expand Down Expand Up @@ -563,9 +581,9 @@ public void DrawEllipseAction(CanvasDrawRequest request)
}

// Adjust for border
_y0 += stroke.height;
_x1 -= stroke.width;
_y1 -= stroke.height;
_y0 += stroke.Height;
_x1 -= stroke.Width;
_y1 -= stroke.Height;

/* rectangular parameter enclosing the ellipse */
_a = Math.Abs(_x1 - _x0);
Expand Down Expand Up @@ -629,7 +647,7 @@ public void DrawEllipseAction(CanvasDrawRequest request)
_x1 = request.X0;
_y1 = request.Y0;

if (Math.Abs(_w) > stroke.width && Math.Abs(_h) > stroke.height)
if (Math.Abs(_w) > stroke.Width && Math.Abs(_h) > stroke.Height)
{
request.X0 = _w / 2;
request.Y0 = _h / 2;
Expand Down Expand Up @@ -838,11 +856,11 @@ public void FloodFillAction(CanvasDrawRequest request)
var spanRight = false;
while (y1 < height && currentTexture.GetPixel(temp.X, y1) == targetColor)
{
currentTexture.SetPixel(temp.X, y1, pattern.GetPixel(temp.X, y1));
currentTexture.SetPixel(temp.X, y1, PixelDataUtil.GetPixel(pattern, temp.X, y1));

if (!spanLeft && temp.X > 0 && currentTexture.GetPixel(temp.X - 1, y1) == targetColor)
{
if (currentTexture.GetPixel(temp.X - 1, y1) != pattern.GetPixel(temp.X, y1))
if (currentTexture.GetPixel(temp.X - 1, y1) != PixelDataUtil.GetPixel(pattern, temp.X, y1))
pixels.Push(new Point(temp.X - 1, y1));

spanLeft = true;
Expand All @@ -854,7 +872,7 @@ public void FloodFillAction(CanvasDrawRequest request)

if (!spanRight && temp.X < width - 1 && currentTexture.GetPixel(temp.X + 1, y1) == targetColor)
{
if (currentTexture.GetPixel(temp.X + 1, y1) != pattern.GetPixel(temp.X, y1))
if (currentTexture.GetPixel(temp.X + 1, y1) != PixelDataUtil.GetPixel(pattern, temp.X, y1))
pixels.Push(new Point(temp.X + 1, y1));

spanRight = true;
Expand Down

0 comments on commit ccf6ede

Please sign in to comment.