Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
RecordingBackbuffer.cs
1using System.Drawing;
10using SkiaSharp;
11
13
18internal sealed class RecordingBackbuffer : BackbufferBase
19{
20 private const int MaxAtlasBatchSize = 256;
21 private const int MaxSnappedVertexBatchSize = 4096;
22 private static readonly int[] PartialBatchSizes = [128, 64, 32, 16, 8, 4, 2, 1];
23
24 private static readonly int[] PartialSnappedVertexBatchSizes =
25 [2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1];
26
27 private sealed class AtlasBatchBuffer(int size)
28 {
29 internal SKRect[] Sprites { get; } = new SKRect[size];
30 internal SKRotationScaleMatrix[] Transforms { get; } = new SKRotationScaleMatrix[size];
31 }
32
33 private sealed class SnappedVertexBatchBuffer(int tileCount)
34 {
35 internal SKPoint[] Positions { get; } = new SKPoint[tileCount * 4];
36 internal SKPoint[] TextureCoordinates { get; } = new SKPoint[tileCount * 4];
37 internal ushort[] Indices { get; } = CreateQuadIndices(tileCount);
38 }
39
40 private readonly record struct FrameAtlasKey(
41 Tilesheet Tilesheet,
42 string RegionName,
43 int XTile,
44 int YTile);
45
46 private readonly record struct FrameAtlasInfo(
47 SKImage Atlas,
48 SKRect Source,
49 Spacing Overhang);
50
51 internal readonly record struct FixedGridRenderPlan(
52 SceneLayer Layer,
53 Rectangle WorldRect);
54
55 private readonly record struct LayerScreenMap(
56 float OffsetX,
57 float OffsetY,
58 float ScaleX,
59 float ScaleY)
60 {
61 internal static LayerScreenMap Create(View view, SceneLayer layer)
62 {
63 RectangleF unit = view.WorldRectToScreenRect(
64 layer,
65 new RectangleF(
66 0f,
67 0f,
68 1f,
69 1f));
70
71 return new(
72 unit.Left,
73 unit.Top,
74 unit.Width,
75 unit.Height);
76 }
77
78 internal RectangleF Map(Rectangle world) =>
79 new(
80 OffsetX + world.Left * ScaleX,
81 OffsetY + world.Top * ScaleY,
82 world.Width * ScaleX,
83 world.Height * ScaleY);
84
85 internal RectangleF MapPixelSnapped(Rectangle world)
86 {
87 float left = Snap(OffsetX + world.Left * ScaleX);
88 float top = Snap(OffsetY + world.Top * ScaleY);
89 float right = Snap(OffsetX + world.Right * ScaleX);
90 float bottom = Snap(OffsetY + world.Bottom * ScaleY);
91
92 return RectangleF.FromLTRB(
93 left,
94 top,
95 right,
96 bottom);
97 }
98
99 private static float Snap(float value) =>
100 MathF.Round(value, MidpointRounding.AwayFromZero);
101
102 internal SKMatrix CreateWorldToScreenMatrix() =>
103 SKMatrix.CreateScaleTranslation(
104 ScaleX,
105 ScaleY,
106 OffsetX,
107 OffsetY);
108 }
109
110 private readonly SKPictureRecorder _recorder = new();
111 private readonly SKRect[] _pendingSprites = new SKRect[MaxAtlasBatchSize];
112
113 private readonly SKRotationScaleMatrix[] _pendingTransforms =
114 new SKRotationScaleMatrix[MaxAtlasBatchSize];
115
116 private readonly SKPoint[] _pendingSnappedPositions =
117 new SKPoint[MaxSnappedVertexBatchSize * 4];
118
119 private readonly SKPoint[] _pendingSnappedTextureCoordinates =
120 new SKPoint[MaxSnappedVertexBatchSize * 4];
121
122 private readonly ushort[] _fullSnappedVertexIndices =
123 CreateQuadIndices(MaxSnappedVertexBatchSize);
124
125 private readonly Dictionary<FrameAtlasKey, FrameAtlasInfo> _frameAtlasCache = [];
126 private readonly Dictionary<SKImage, SKShader> _atlasShaderCache = [];
127
128 private readonly Dictionary<int, AtlasBatchBuffer> _partialBatchBuffers = new()
129 {
130 [1] = new(1),
131 [2] = new(2),
132 [4] = new(4),
133 [8] = new(8),
134 [16] = new(16),
135 [32] = new(32),
136 [64] = new(64),
137 [128] = new(128)
138 };
139
140 private readonly Dictionary<int, SnappedVertexBatchBuffer> _partialSnappedVertexBuffers =
141 PartialSnappedVertexBatchSizes.ToDictionary(
142 size => size,
143 size => new SnappedVertexBatchBuffer(size));
144
145 private readonly SKPaint _snappedVertexPaint = new()
146 {
147 IsAntialias = false,
148 BlendMode = SKBlendMode.SrcOver
149 };
150
151 private SKCanvas? _canvas;
152 private SKImage? _pendingAtlas;
153 private SKImage? _pendingSnappedVertexAtlas;
154 private int _pendingCount;
155 private int _pendingSnappedVertexCount;
156
157 internal int AtlasBatchCount { get; private set; }
158 internal int AtlasBatchedTileCount { get; private set; }
159
160 internal RecordingBackbuffer() : base(1, 1) { }
161
163 public override SKCanvas Canvas => _canvas ?? throw new InvalidOperationException("No recording is active.");
164
166 public override bool IsGlThreadRendered => true;
167
168 internal void Start(BackbufferBase source)
169 {
170 ResetPendingBatch();
171 ResetSnappedVertexBatch();
172 DisposeAtlasShaders();
173 _frameAtlasCache.Clear();
174 AtlasBatchCount = 0;
175 AtlasBatchedTileCount = 0;
176
177 UpdateSize(source.Width, source.Height);
178 ClearColor = source.ClearColor;
179 FogPaint.Dispose();
180 GridLinePaint.Dispose();
181 CollisionBoxPaint.Dispose();
182 FogPaint = source.FogPaint.Clone();
183 GridLinePaint = source.GridLinePaint.Clone();
184 CollisionBoxPaint = source.CollisionBoxPaint.Clone();
185
186 _canvas = _recorder.BeginRecording(
187 new SKRect(
188 0,
189 0,
190 Width,
191 Height));
192 }
193
194 internal SKPicture Complete()
195 {
196 FlushTileBatch();
197 FlushSnappedVertexBatch();
198 var picture = _recorder.EndRecording();
199 _canvas = null;
200 ResetPendingBatch();
201 ResetSnappedVertexBatch();
202 DisposeAtlasShaders();
203 return picture;
204 }
205
206 internal void Cancel()
207 {
208 ResetPendingBatch();
209 ResetSnappedVertexBatch();
210
211 if (_canvas is null)
212 {
213 DisposeAtlasShaders();
214 return;
215 }
216
217 var picture = _recorder.EndRecording();
218 _canvas = null;
219 picture.Dispose();
220 DisposeAtlasShaders();
221 }
222
223 internal bool TryPrepareFixedGridLayer(
224 SceneLayer layer,
225 Rectangle worldRect,
226 out FixedGridRenderPlan plan)
227 {
228 plan = default;
229
230 if (layer.WrapHorizontally ||
231 layer.WrapVertically ||
232 layer.TransformedTiles.Count != 0 ||
233 layer.ShowGridLines ||
234 layer.ShowCollisionBoxes ||
235 !layer.IsFixedGridSnapshotFastPathEligible ||
236 !RenderTileQuery.IsRenderOrdered(layer))
237 {
238 return false;
239 }
240
241 var queryRect = worldRect;
242 queryRect.Inflate(layer.TileWidth, layer.TileHeight);
243 queryRect.Inflate(1, 1);
244
245 if (SpriteManager.Instance.HasVisibleSpriteInWorldRect(queryRect, layer) ||
246 DirectDrawingManager.Instance.HasVisibleDrawingForLayer(layer, worldRect))
247 {
248 return false;
249 }
250
251 plan = new(layer, worldRect);
252 return true;
253 }
254
255 internal int DrawFixedGridLayer(
256 View view,
257 FixedGridRenderPlan plan,
258 Rectangle clipRect)
259 {
260 var layer = plan.Layer;
261 LayerScreenMap screen = LayerScreenMap.Create(view, layer);
262 bool snapOrthogonalEdges = layer.CoordinateSystem is OrthogonalCoordinates;
263 int tileCount = 0;
264
265 Canvas.Save();
266 Canvas.ClipRect(clipRect.ToSKRect());
267
268 if (!snapOrthogonalEdges)
269 {
270 var worldToScreen = screen.CreateWorldToScreenMatrix();
271 Canvas.Concat(ref worldToScreen);
272 }
273
274 try
275 {
276 RenderTileQuery.VisitCandidates(
277 layer.CoordinateSystem,
278 layer,
279 plan.WorldRect,
280 includeOverhang: true,
281 tile =>
282 {
283 if (!tile.Visible || tile.CurrentFrame.Tilesheet is null)
284 return true;
285
286 tileCount++;
287
288 if (snapOrthogonalEdges)
289 DrawOrthogonalFixedGridTileSnapped(layer, screen, tile);
290 else
291 DrawFixedGridTileInWorldSpace(layer, tile);
292
293 return true;
294 });
295
296 if (snapOrthogonalEdges)
297 FlushSnappedVertexBatch();
298 else
299 FlushTileBatch();
300
301 return tileCount;
302 }
303 finally
304 {
305 Canvas.Restore();
306 }
307 }
308
309 internal bool TryDrawFixedGridDrawables(
310 View view,
311 IReadOnlyList<IDrawable> drawables,
312 Rectangle clipRect)
313 {
314 if (drawables.Count == 0)
315 return true;
316
317 SceneLayer? layer = null;
318
319 // This path deliberately handles only the engine's fixed tile type and no
320 // post-tile debug/fog overlays. Anything richer retains the generic renderer.
321 for (int i = 0; i < drawables.Count; i++)
322 {
323 if (drawables[i].GetType() != typeof(SceneLayerTile) ||
324 drawables[i] is not SceneLayerTile tile ||
325 tile.EnableFog)
326 {
327 return false;
328 }
329
330 layer ??= tile.SceneLayer;
331
332 if (!ReferenceEquals(layer, tile.SceneLayer))
333 return false;
334 }
335
336 if (layer is null ||
337 layer.ShowGridLines ||
338 layer.ShowCollisionBoxes)
339 {
340 return false;
341 }
342
343 LayerScreenMap screen = LayerScreenMap.Create(view, layer);
344
345 Canvas.Save();
346 Canvas.ClipRect(clipRect.ToSKRect());
347
348 if (layer.CoordinateSystem is OrthogonalCoordinates)
349 {
350 try
351 {
352 for (int i = 0; i < drawables.Count; i++)
353 DrawOrthogonalFixedGridTileSnapped(
354 layer,
355 screen,
356 (SceneLayerTile)drawables[i]);
357
358 FlushSnappedVertexBatch();
359 return true;
360 }
361 finally
362 {
363 Canvas.Restore();
364 }
365 }
366
367 var worldToScreen = screen.CreateWorldToScreenMatrix();
368 bool worldSpaceActive = false;
369
370 try
371 {
372 for (int i = 0; i < drawables.Count; i++)
373 {
374 var tile = (SceneLayerTile)drawables[i];
375
376 if (tile.Transform == TileTransform.Identity &&
377 TryGetFrameAtlasInfo(tile.CurrentFrame, out _))
378 {
379 if (!worldSpaceActive)
380 {
381 Canvas.Save();
382 Canvas.Concat(ref worldToScreen);
383 worldSpaceActive = true;
384 }
385
386 DrawFixedGridTileInWorldSpace(layer, tile);
387 continue;
388 }
389
390 if (worldSpaceActive)
391 {
392 FlushTileBatch();
393 Canvas.Restore();
394 worldSpaceActive = false;
395 }
396
397 DrawFixedGridTile(
398 view,
399 layer,
400 screen,
401 tile);
402 }
403
404 if (worldSpaceActive)
405 {
406 FlushTileBatch();
407 Canvas.Restore();
408 worldSpaceActive = false;
409 }
410
411 return true;
412 }
413 finally
414 {
415 if (worldSpaceActive)
416 {
417 FlushTileBatch();
418 Canvas.Restore();
419 }
420
421 Canvas.Restore();
422 }
423 }
424
425 private void DrawOrthogonalFixedGridTileSnapped(
426 SceneLayer layer,
427 LayerScreenMap screen,
428 SceneLayerTile tile)
429 {
430 if (tile.Transform == TileTransform.Identity &&
431 TryGetFrameAtlasInfo(tile.CurrentFrame, out var frame))
432 {
433 Rectangle world = GetScaledFixedTileBounds(layer, tile, frame);
434
435 RectangleF destination = screen.MapPixelSnapped(world);
436
437 if (destination.Width <= 0f || destination.Height <= 0f)
438 return;
439
440 QueueSnappedVertexAtlas(
441 frame.Atlas,
442 frame.Source,
443 destination);
444
445 return;
446 }
447
448 FlushSnappedVertexBatch();
449
450 RectangleF transformedDestination =
451 screen.MapPixelSnapped(tile.DrawLocationWorld);
452
453 if (transformedDestination.Width > 0f &&
454 transformedDestination.Height > 0f)
455 {
456 tile.Draw(this, transformedDestination);
457 }
458 }
459
460 private void DrawFixedGridTileInWorldSpace(
461 SceneLayer layer,
462 SceneLayerTile tile)
463 {
464 if (!TryGetFrameAtlasInfo(tile.CurrentFrame, out var frame))
465 return;
466
467 Rectangle world = GetScaledFixedTileBounds(layer, tile, frame);
468
469 // Record atlas entries in integer world coordinates. A single canvas transform
470 // scales/translates the complete fixed-grid layer to screen space, so adjacent
471 // tile edges are transformed from the same world boundary instead of from
472 // independently rounded fractional destination rectangles.
473 if (TryQueueAtlas(frame.Atlas, frame.Source, world))
474 return;
475
476 FlushTileBatch();
477
478 Canvas.DrawImage(
479 frame.Atlas,
480 frame.Source,
481 world.ToSKRect(),
482 SKSamplingOptions.Default);
483 }
484
485 private void DrawFixedGridTile(
486 View view,
487 SceneLayer layer,
488 LayerScreenMap screen,
489 SceneLayerTile tile)
490 {
491 if (tile.Transform != TileTransform.Identity)
492 {
493 FlushTileBatch();
494 tile.Draw(this, tile.GetDrawLocationScreen(view));
495 return;
496 }
497
498 if (!TryGetFrameAtlasInfo(tile.CurrentFrame, out var frame))
499 {
500 FlushTileBatch();
501 tile.Draw(this, tile.GetDrawLocationScreen(view));
502 return;
503 }
504
505 Rectangle world = GetScaledFixedTileBounds(layer, tile, frame);
506
507 RectangleF destination = screen.Map(world);
508
509 if (!TryQueueAtlas(frame.Atlas, frame.Source, destination))
510 {
511 FlushTileBatch();
512 tile.Draw(this, destination);
513 }
514 }
515
516 private static Rectangle GetScaledFixedTileBounds(
517 SceneLayer layer,
518 SceneLayerTile tile,
519 FrameAtlasInfo frame)
520 {
521 Rectangle cell = RenderTileQuery.GetFixedTileCellBounds(layer, tile);
522 Spacing overhang = TileTransformGeometry.ScaleOverhang(
523 new Size((int)frame.Source.Width, (int)frame.Source.Height),
524 cell.Size,
525 frame.Overhang);
526
527 return TileBounds.ApplyOverhang(cell, overhang, true);
528 }
529
530 private void QueueSnappedVertexAtlas(
531 SKImage atlas,
532 SKRect source,
533 RectangleF destination)
534 {
535 if (_pendingSnappedVertexAtlas is not null &&
536 !ReferenceEquals(_pendingSnappedVertexAtlas, atlas))
537 {
538 FlushSnappedVertexBatch();
539 }
540
541 _pendingSnappedVertexAtlas = atlas;
542
543 int tileIndex = _pendingSnappedVertexCount++;
544 int vertexIndex = tileIndex * 4;
545
546 float left = destination.Left;
547 float top = destination.Top;
548 float right = destination.Right;
549 float bottom = destination.Bottom;
550
551 _pendingSnappedPositions[vertexIndex] = new(left, top);
552 _pendingSnappedPositions[vertexIndex + 1] = new(right, top);
553 _pendingSnappedPositions[vertexIndex + 2] = new(right, bottom);
554 _pendingSnappedPositions[vertexIndex + 3] = new(left, bottom);
555
556 _pendingSnappedTextureCoordinates[vertexIndex] =
557 new(source.Left, source.Top);
558
559 _pendingSnappedTextureCoordinates[vertexIndex + 1] =
560 new(source.Right, source.Top);
561
562 _pendingSnappedTextureCoordinates[vertexIndex + 2] =
563 new(source.Right, source.Bottom);
564
565 _pendingSnappedTextureCoordinates[vertexIndex + 3] =
566 new(source.Left, source.Bottom);
567
568 if (_pendingSnappedVertexCount == MaxSnappedVertexBatchSize)
569 FlushFullSnappedVertexBatch();
570 }
571
572 private void FlushSnappedVertexBatch()
573 {
574 if (_pendingSnappedVertexCount == 0)
575 {
576 _pendingSnappedVertexAtlas = null;
577 return;
578 }
579
580 if (_pendingSnappedVertexAtlas is null)
581 {
582 throw new InvalidOperationException(
583 "Snapped vertex batch has queued tiles without an atlas image.");
584 }
585
586 int offset = 0;
587 int remaining = _pendingSnappedVertexCount;
588
589 foreach (int size in PartialSnappedVertexBatchSizes)
590 {
591 while (remaining >= size)
592 {
593 var buffers = _partialSnappedVertexBuffers[size];
594
595 Array.Copy(
596 _pendingSnappedPositions,
597 offset * 4,
598 buffers.Positions,
599 0,
600 size * 4);
601
602 Array.Copy(
603 _pendingSnappedTextureCoordinates,
604 offset * 4,
605 buffers.TextureCoordinates,
606 0,
607 size * 4);
608
609 DrawSnappedVertexBatch(
610 _pendingSnappedVertexAtlas,
611 buffers.Positions,
612 buffers.TextureCoordinates,
613 buffers.Indices,
614 size);
615
616 offset += size;
617 remaining -= size;
618 }
619 }
620
621 ResetSnappedVertexBatch();
622 }
623
624 private void FlushFullSnappedVertexBatch()
625 {
626 if (_pendingSnappedVertexAtlas is null ||
627 _pendingSnappedVertexCount != MaxSnappedVertexBatchSize)
628 {
629 return;
630 }
631
632 DrawSnappedVertexBatch(
633 _pendingSnappedVertexAtlas,
634 _pendingSnappedPositions,
635 _pendingSnappedTextureCoordinates,
636 _fullSnappedVertexIndices,
637 MaxSnappedVertexBatchSize);
638
639 _pendingSnappedVertexCount = 0;
640 }
641
642 private void DrawSnappedVertexBatch(
643 SKImage atlas,
644 SKPoint[] positions,
645 SKPoint[] textureCoordinates,
646 ushort[] indices,
647 int tileCount)
648 {
649 using var vertices = SKVertices.CreateCopy(
650 SKVertexMode.Triangles,
651 positions,
652 textureCoordinates,
653 null,
654 indices);
655
656 if (!_atlasShaderCache.TryGetValue(atlas, out var shader))
657 {
658 shader = atlas.ToShader(
659 SKShaderTileMode.Clamp,
660 SKShaderTileMode.Clamp,
661 new SKSamplingOptions(
662 SKFilterMode.Nearest,
663 SKMipmapMode.None));
664
665 _atlasShaderCache.Add(atlas, shader);
666 }
667
668 _snappedVertexPaint.Shader = shader;
669
670 Canvas.DrawVertices(
671 vertices,
672 SKBlendMode.Modulate,
673 _snappedVertexPaint);
674
675 AtlasBatchCount++;
676 AtlasBatchedTileCount += tileCount;
677 }
678
679 private static ushort[] CreateQuadIndices(int tileCount)
680 {
681 var indices = new ushort[tileCount * 6];
682
683 for (int tile = 0; tile < tileCount; tile++)
684 {
685 int vertex = tile * 4;
686 int index = tile * 6;
687
688 indices[index] = (ushort)vertex;
689 indices[index + 1] = (ushort)(vertex + 1);
690 indices[index + 2] = (ushort)(vertex + 2);
691 indices[index + 3] = (ushort)vertex;
692 indices[index + 4] = (ushort)(vertex + 2);
693 indices[index + 5] = (ushort)(vertex + 3);
694 }
695
696 return indices;
697 }
698
699 private void ResetSnappedVertexBatch()
700 {
701 _pendingSnappedVertexAtlas = null;
702 _pendingSnappedVertexCount = 0;
703 }
704
705 private void DisposeAtlasShaders()
706 {
707 _snappedVertexPaint.Shader = null;
708
709 foreach (var shader in _atlasShaderCache.Values)
710 shader.Dispose();
711
712 _atlasShaderCache.Clear();
713 }
714
715 internal bool TryQueueTile(SceneLayerTile tile, RectangleF destination)
716 {
717 if (tile.Transform != TileTransform.Identity ||
718 !TryGetFrameAtlasInfo(tile.CurrentFrame, out var frame))
719 {
720 return false;
721 }
722
723 return TryQueueAtlas(frame.Atlas, frame.Source, destination);
724 }
725
726 private bool TryGetFrameAtlasInfo(Frame frame, out FrameAtlasInfo info)
727 {
728 if (frame.Tilesheet is null)
729 {
730 info = default;
731 return false;
732 }
733
734 var key = new FrameAtlasKey(
735 frame.Tilesheet,
736 frame.RegionName ?? string.Empty,
737 frame.XTile,
738 frame.YTile);
739
740 if (_frameAtlasCache.TryGetValue(key, out info))
741 return true;
742
743 var atlas = frame.AtlasImage;
744 Rectangle source = frame.AtlasSourceBounds;
745
746 if (atlas is null || source.IsEmpty)
747 {
748 info = default;
749 return false;
750 }
751
752 info = new(
753 atlas,
754 source.ToSKRect(),
755 frame.Overhang);
756
757 _frameAtlasCache.Add(key, info);
758 return true;
759 }
760
761 private bool TryQueueAtlas(
762 SKImage atlas,
763 SKRect source,
764 RectangleF destination)
765 {
766 if (source.Width <= 0 ||
767 source.Height <= 0 ||
768 destination.Width <= 0 ||
769 destination.Height <= 0)
770 {
771 return false;
772 }
773
774 float scaleX = destination.Width / source.Width;
775 float scaleY = destination.Height / source.Height;
776 float tolerance = MathF.Max(0.0001f, MathF.Abs(scaleX) * 0.0001f);
777
778 if (!float.IsFinite(scaleX) ||
779 !float.IsFinite(scaleY) ||
780 MathF.Abs(scaleX - scaleY) > tolerance)
781 {
782 return false;
783 }
784
785 if (_pendingAtlas is not null && !ReferenceEquals(_pendingAtlas, atlas))
786 FlushTileBatch();
787
788 _pendingAtlas = atlas;
789 int index = _pendingCount++;
790 _pendingSprites[index] = source;
791
792 _pendingTransforms[index] = new SKRotationScaleMatrix(
793 scaleX,
794 0f,
795 destination.Left,
796 destination.Top);
797
798 if (_pendingCount == MaxAtlasBatchSize)
799 FlushFullTileBatch();
800
801 return true;
802 }
803
804 internal void FlushTileBatch()
805 {
806 if (_pendingCount == 0)
807 {
808 _pendingAtlas = null;
809 return;
810 }
811
812 if (_pendingAtlas is null)
813 throw new InvalidOperationException("Atlas batch has queued tiles without an atlas image.");
814
815 int offset = 0;
816 int remaining = _pendingCount;
817
818 foreach (int size in PartialBatchSizes)
819 {
820 while (remaining >= size)
821 {
822 var buffers = _partialBatchBuffers[size];
823
824 Array.Copy(
825 _pendingSprites,
826 offset,
827 buffers.Sprites,
828 0,
829 size);
830
831 Array.Copy(
832 _pendingTransforms,
833 offset,
834 buffers.Transforms,
835 0,
836 size);
837
838 Canvas.DrawAtlas(
839 _pendingAtlas,
840 buffers.Sprites,
841 buffers.Transforms,
842 SKSamplingOptions.Default);
843
844 AtlasBatchCount++;
845 AtlasBatchedTileCount += size;
846 offset += size;
847 remaining -= size;
848 }
849 }
850
851 ResetPendingBatch();
852 }
853
854 private void FlushFullTileBatch()
855 {
856 if (_pendingAtlas is null || _pendingCount != MaxAtlasBatchSize)
857 return;
858
859 Canvas.DrawAtlas(
860 _pendingAtlas,
861 _pendingSprites,
862 _pendingTransforms,
863 SKSamplingOptions.Default);
864
865 AtlasBatchCount++;
866 AtlasBatchedTileCount += MaxAtlasBatchSize;
867 _pendingCount = 0;
868 }
869
870 private void ResetPendingBatch()
871 {
872 _pendingAtlas = null;
873 _pendingCount = 0;
874 }
875
877 protected internal override void BeginFrame() { }
878
880 protected internal override void EndFrame() { }
881
883 protected internal override SKImage Snapshot() =>
884 throw new NotSupportedException(
885 "A recording has commands, not pixels. Read pixels on the GL thread after replay.");
886
888 protected internal override void DrawTileFrame(Tile tile, RectangleF destRectScreen)
889 {
890 var image = tile.CurrentFrame.SkImage;
891
892 if (image is not null)
893 Canvas.DrawImage(image, destRectScreen.ToSKRect());
894 }
895
897 public override void Dispose()
898 {
899 Cancel();
900 _snappedVertexPaint.Dispose();
901 _recorder.Dispose();
902 base.Dispose();
903 }
904}
virtual Frame CurrentFrame
Gets or sets the current frame. The initial frame supplies the tile's default collision adjustment an...
Definition Tile.cs:185
virtual RectangleF GetDrawLocationScreen(View view)
Transforms the drawable bounds into screen pixels for the specified view.
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
RectangleF WorldRectToScreenRect(SceneLayer layer, RectangleF worldRect)
Converts a world-space pixel rectangle into a screen-space rectangle for this View,...
Definition View.cs:340
override void Draw(Gondwana.Rendering.Backbuffers.BackbufferBase backbuffer, RectangleF destRectScreen)
Draws the current content into the backbuffer at the supplied screen-pixel bounds.
TileTransform Transform
Gets or sets this placement's orthogonal orientation without changing its source frame.
override Rectangle DrawLocationWorld
Gets the world-space pixel rectangle where this tile should be drawn.
bool WrapVertically
Gets or sets a value indicating whether this layer repeats along its row axis at the grid boundaries.
bool WrapHorizontally
Gets or sets a value indicating whether this layer repeats along its column axis at the grid boundari...
bool ShowGridLines
Gets or sets a value indicating whether grid lines should be rendered over this layer for debugging.
int TileHeight
Gets or sets the height of each tile in this layer, measured in pixels.
int TileWidth
Gets or sets the width of each tile in this layer, measured in pixels.
bool ShowCollisionBoxes
Gets or sets a value indicating whether collision boxes should be rendered over this layer for debugg...
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.
TileTransform
The eight orthogonal orientations of artwork placed in a fixed scene cell.
record struct Spacing([property:JsonProperty("left")] int Left, [property:JsonProperty("top")] int Top, [property:JsonProperty("right")] int Right, [property:JsonProperty("bottom")] int Bottom)
Represents dimensions in pixels when spacing Tilesheets.
Definition Spacing.cs:12
@ Cancel
The dialog was cancelled.
@ Sprites
Represents the sprites state, including sprite instances, positions, properties, and sprite-related g...
readonly int YTile
The vertical tile coordinate (row index) within the tilesheet.
Definition Frame.cs:31
readonly Tilesheet Tilesheet
The tilesheet that contains the source bitmap for this frame.
Definition Frame.cs:16
readonly string RegionName
The tilesheet region that contains the source bitmap for this frame.
Definition Frame.cs:21
readonly int XTile
The horizontal tile coordinate (column index) within the tilesheet.
Definition Frame.cs:26
readonly? SKImage SkImage
Gets the SkiaSharp image for this frame.
Definition Frame.cs:74
readonly Spacing Overhang
Gets the overhang dimensions for this frame.
Definition Frame.cs:90