19public abstract class BackbufferBase : IDisposable
24 private BackbufferBase()
61 public abstract SKCanvas
Canvas {
get; }
84 protected internal abstract SKImage Snapshot();
104 protected internal abstract void BeginFrame();
131 protected internal abstract void DrawTileFrame(
Tile tile, RectangleF destRectScreen);
152 protected internal abstract void EndFrame();
172 Color = SKColors.White,
182 Color = SKColors.Green,
190 public int Width => Volatile.Read(ref _width);
195 public int Height => Volatile.Read(ref _height);
218 protected internal virtual void RequestResize(
int width,
int height)
241 protected internal event Action<int, int>? SizeChanged;
261 Volatile.Write(ref _width, width);
262 Volatile.Write(ref _height, height);
264 SizeChanged?.Invoke(width, height);
271 protected internal Rectangle DirtyRectangle {
get;
private set; }
273 private SKColor _clearColor = SKColors.Black;
281 BlendMode = SKBlendMode.Src,
282 Style = SKPaintStyle.Fill,
283 FilterQuality = SKFilterQuality.None,
295 _fillPaint.Color = value;
299 internal void ClearRect(Rectangle rectPx)
315 AddToBackbufferDirtyRectangle(rectPx);
320 internal void DrawDrawables(
View view, IEnumerable<IDrawable> drawables, Rectangle clipRect)
322 if (
this is RecordingBackbuffer recording &&
323 drawables is IReadOnlyList<IDrawable> ordered &&
324 recording.TryDrawFixedGridDrawables(view, ordered, clipRect))
330 Canvas.ClipRect(clipRect.ToSKRect());
332 var tiles =
new List<(Tile Tile, WrappedDrawable? Instance)>();
334 foreach (var entry
in drawables)
336 var instance = entry as WrappedDrawable;
337 var drawable = instance?.Owner ?? entry;
338 using var scope = instance?.Enter(view);
340 if (!drawable.Visible)
343 var destRectScreen = drawable.GetDrawLocationScreen(view);
345 if (drawable is
Sprite sprite)
347 destRectScreen = sprite.ApplyJiggleToDestRect(destRectScreen);
350 bool batched =
this is RecordingBackbuffer recorder &&
354 recorder.TryQueueTile(layerTile, destRectScreen);
358 (
this as RecordingBackbuffer)?.FlushTileBatch();
359 drawable.Draw(
this, destRectScreen);
366 RectangleF visualBoundsScreen = drawable is Sprite drawnSprite
367 ? drawnSprite.GetVisualBoundsScreen(destRectScreen)
370 AddToBackbufferDirtyRectangle(visualBoundsScreen.ToPixelAlignedRect());
373 if (drawable is Tile tile)
374 tiles.Add((tile, instance));
377 (
this as RecordingBackbuffer)?.FlushTileBatch();
379 PostDrawTiles(view, tiles);
384 private void PostDrawTiles(View view, List<(Tile Tile, WrappedDrawable? Instance)> tiles)
386 foreach (var entry
in tiles)
388 var tile = entry.Tile;
389 using var scope = entry.Instance?.Enter(view);
390 var layer = tile.SceneLayer;
391 bool drawFog = tile.EnableFog;
392 bool drawGrid = layer.ShowGridLines && tile.Visible && tile.IsPositionFixed;
393 bool drawCollision = layer.ShowCollisionBoxes && tile.Visible && tile.CollisionsEnabled;
395 if (!drawFog && !drawGrid && !drawCollision)
404 && layer.CoordinateSystemType == Gondwana.Drawing.Coordinates.CoordinateSystemTypes.Orthogonal)
406 var worldRect = layer.CoordinateSystem.GetPixelRangeForTile(tile, includeOverhang:
false);
412 var colRectScreen = tile.GetCollisionAreaScreen(view).ToSKRect();
420 if (drawFog || drawGrid)
422 var worldPts = tile.OutlinePointsWorld;
423 var ptsScreen =
new SKPoint[worldPts.Length];
425 for (
int i = 0; i < worldPts.Length; i++)
434 ptsScreen[i] =
new SKPoint(sp.X, sp.Y);
439 using var path =
new SKPath();
440 path.AddPoly(ptsScreen, close:
true);
446 var enclosed =
new SKPoint[ptsScreen.Length + 1];
447 Array.Copy(ptsScreen, enclosed, ptsScreen.Length);
448 enclosed[^1] = ptsScreen[0];
455 var colRectScreen = tile.GetCollisionAreaScreen(view).ToSKRect();
468 protected internal void AddToBackbufferDirtyRectangle(Rectangle area)
473 area.Inflate(area.Width, area.Height);
475 DirtyRectangle = DirtyRectangle.IsEmpty
491 protected internal void ClearDirtyRectangle()
506 public virtual byte[]
ToByteArray(SKEncodedImageFormat format = SKEncodedImageFormat.Png,
int quality = 100)
508 using var image = Snapshot();
509 using var data = image.Encode(format, quality);
510 return data.ToArray();
546 GC.SuppressFinalize(
this);