Sprite loading progress

This commit is contained in:
Matt Thorson 2020-05-27 20:29:45 -07:00
parent a2f5f157fc
commit 6bfc6c7721
4 changed files with 38 additions and 18 deletions

View File

@ -247,10 +247,10 @@ namespace Strawberry
{ {
let path = scope String(); let path = scope String();
file.GetFilePath(path); file.GetFilePath(path);
let sprite = new [Friend]Sprite(path); let sprite = new [Friend]Sprite(path);
path.Remove(0, ContentRoot.Length + 8); path.Remove(0, ContentRoot.Length + 9);
path.RemoveFromEnd(4);
Sprites.Add(new String(path), sprite); Sprites.Add(new String(path), sprite);
} }
} }

View File

@ -33,12 +33,13 @@ namespace Strawberry
public readonly String Path; public readonly String Path;
public int Width { get; private set; }
public int Height { get; private set; }
private Frame[] frames; private Frame[] frames;
private List<Layer> layers; private List<Layer> layers;
private List<Tag> tags; private List<Tag> tags;
private List<Slice> slices; private List<Slice> slices;
private int width;
private int height;
private Modes mode; private Modes mode;
private this(String path) private this(String path)
@ -53,6 +54,11 @@ namespace Strawberry
Unload(); Unload();
} }
public Frame this[int index]
{
get => frames[index];
}
private void Unload() private void Unload()
{ {
for (let f in frames) for (let f in frames)
@ -125,8 +131,8 @@ namespace Strawberry
// Frame Count / Width / Height / Color Mode // Frame Count / Width / Height / Color Mode
frames = new Frame[WORD()]; frames = new Frame[WORD()];
width = WORD(); Width = WORD();
height = WORD(); Height = WORD();
mode = (Modes)(WORD() / 8); mode = (Modes)(WORD() / 8);
// Other Info, Ignored // Other Info, Ignored
@ -148,13 +154,13 @@ namespace Strawberry
// Body // Body
{ {
var temp = scope:: uint8[width * height * (int)mode]; var temp = scope:: uint8[Width * Height * (int)mode];
let palette = scope:: Color[256]; let palette = scope:: Color[256];
HasUserData last = null; HasUserData last = null;
for (int i = 0; i < frames.Count; i++) for (int i = 0; i < frames.Count; i++)
{ {
let frame = new Frame(width, height); let frame = new Frame(Width, Height);
frames[i] = frame; frames[i] = frame;
int64 frameStart, frameEnd; int64 frameStart, frameEnd;
@ -257,7 +263,7 @@ namespace Strawberry
else if (celType == 1) else if (celType == 1)
{ {
var linkFrame = frames[WORD()]; var linkFrame = frames[WORD()];
var linkCel = linkFrame.Cels[frame.Cels.Count]; var linkCel = linkFrame.[Friend]cels[frame.[Friend]cels.Count];
width = linkCel.Width; width = linkCel.Width;
height = linkCel.Height; height = linkCel.Height;
@ -279,7 +285,7 @@ namespace Strawberry
if (cel.Layer.Visible) if (cel.Layer.Visible)
CelToFrame(frame, cel); CelToFrame(frame, cel);
frame.Cels.Add(cel); frame.[Friend]cels.Add(cel);
} }
// PALETTE CHUNK // PALETTE CHUNK
else if (chunkType == Chunks.Palette) else if (chunkType == Chunks.Palette)
@ -418,12 +424,12 @@ namespace Strawberry
if (cel.Layer.BlendMode < BlendModes.Count) if (cel.Layer.BlendMode < BlendModes.Count)
blend = BlendModes[cel.Layer.BlendMode]; blend = BlendModes[cel.Layer.BlendMode];
for (int sx = Math.Max(0, -cel.X), int right = Math.Min(cel.Width, width - cel.X); sx < right; sx++) for (int sx = Math.Max(0, -cel.X), int right = Math.Min(cel.Width, Width - cel.X); sx < right; sx++)
{ {
int dx = cel.X + sx; int dx = cel.X + sx;
int dy = cel.Y * width; int dy = cel.Y * Width;
for (int sy = Math.Max(0, -cel.Y), int bottom = Math.Min(cel.Height, height - cel.Y); sy < bottom; sy++, dy += width) for (int sy = Math.Max(0, -cel.Y), int bottom = Math.Min(cel.Height, Height - cel.Y); sy < bottom; sy++, dy += Width)
{ {
if (dx + dy >= 0 && dx + dy < pxLen) if (dx + dy >= 0 && dx + dy < pxLen)
blend(frame.Pixels, (dx + dy) * 4, cel.Pixels[sx + sy * cel.Width], opacity); blend(frame.Pixels, (dx + dy) * 4, cel.Pixels[sx + sy * cel.Width], opacity);
@ -433,14 +439,15 @@ namespace Strawberry
// Data // Data
private class Frame public class Frame
{ {
public SDL.Texture* Texture; public SDL.Texture* Texture;
public float Duration; public float Duration;
public List<Cel> Cels;
public uint8* Pixels; public uint8* Pixels;
public int32 PixelsLength; public int32 PixelsLength;
private List<Cel> cels;
public this(int w, int h) public this(int w, int h)
{ {
Texture = SDL.CreateTexture(Game.Renderer, (uint32)SDL.PIXELFORMAT_ARGB8888, (int32)SDL.TextureAccess.Static, (int32)w, (int32)h); Texture = SDL.CreateTexture(Game.Renderer, (uint32)SDL.PIXELFORMAT_ARGB8888, (int32)SDL.TextureAccess.Static, (int32)w, (int32)h);
@ -449,14 +456,14 @@ namespace Strawberry
SDL.LockTexture(Texture, null, out ptr, out PixelsLength); SDL.LockTexture(Texture, null, out ptr, out PixelsLength);
Pixels = (uint8*)ptr; Pixels = (uint8*)ptr;
Cels = new List<Cel>(); cels = new List<Cel>();
} }
public ~this() public ~this()
{ {
for (let c in Cels) for (let c in cels)
delete c; delete c;
delete Cels; delete cels;
SDL.DestroyTexture(Texture); SDL.DestroyTexture(Texture);
} }

View File

@ -36,5 +36,13 @@ namespace Strawberry
SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A);
SDL.RenderDrawLine(Game.Renderer, (int32)fromn.X, (int32)fromn.Y, (int32)ton.X, (int32)ton.Y); SDL.RenderDrawLine(Game.Renderer, (int32)fromn.X, (int32)fromn.Y, (int32)ton.X, (int32)ton.Y);
} }
static public void Sprite(Sprite sprite, int frame, Point position)
{
SDL.Rect src = Strawberry.Rect(0, 0, sprite.Width, sprite.Height);
SDL.Rect dst = Strawberry.Rect(position.X, position.Y, sprite.Width, sprite.Height);
SDL.RenderCopy(Game.Renderer, sprite[frame].Texture, &src, &dst);
}
} }
} }

View File

@ -166,5 +166,10 @@ namespace Strawberry
{ {
return Rect(a.X * b.X, a.Y * b.Y, a.Width * b.X, a.Height * b.Y); return Rect(a.X * b.X, a.Y * b.Y, a.Width * b.X, a.Height * b.Y);
} }
static public implicit operator SDL2.SDL.Rect(Rect r)
{
return SDL2.SDL.Rect((int32)r.X, (int32)r.Y, (int32)r.Width, (int32)r.Height);
}
} }
} }