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
DirectPath.cs
1using System.Drawing;
5using SkiaSharp;
6
8
10public sealed class DirectPath : DirectShapeBase<DirectPath>
11{
12 private SKPath? _sourcePath;
13 private SKPath? _transformedPath;
14 private SKRect? _transformedBounds;
15 private bool _resourcesDisposed;
16
18 public DirectPath(Color color, RenderSurfaceHostBase host, SceneLayer sceneLayer, Rectangle worldBounds, string pathData, string? nickname = null)
19 : this(color, host, DirectDrawingMode.SceneLayer, sceneLayer, null, worldBounds, ParsePath(pathData), nickname)
20 {
21 }
22
24 public DirectPath(Color color, RenderSurfaceHostBase host, View view, Rectangle screenBounds, string pathData, string? nickname = null)
25 : this(color, host, DirectDrawingMode.View, null, view, screenBounds, ParsePath(pathData), nickname)
26 {
27 }
28
29 private DirectPath(Color color, RenderSurfaceHostBase host, DirectDrawingMode mode,
30 SceneLayer? sceneLayer, View? view, Rectangle bounds, SKPath path, string? nickname)
31 : base(color, host, mode, sceneLayer, view, bounds, nickname)
32 {
33 lock (RenderStateSynchronization.SyncRoot)
34 {
35 if (_resourcesDisposed)
36 {
37 path.Dispose();
38 throw new ObjectDisposedException(nameof(DirectPath));
39 }
40
41 _sourcePath = path;
42 ForceRefresh();
43 }
44 }
45
47 public DirectPath SetPathData(string pathData)
48 {
49 lock (RenderStateSynchronization.SyncRoot)
50 {
51 ObjectDisposedException.ThrowIf(_resourcesDisposed, this);
52
53 SKPath replacement = ParsePath(pathData);
54 _sourcePath?.Dispose();
55 _sourcePath = replacement;
56 _transformedBounds = null;
57 ForceRefresh();
58 }
59
60 return this;
61 }
62
64 public DirectPath SetFillColor(Color? color) => SetShapeFill(color);
65
66 private static SKPath ParsePath(string pathData)
67 {
68 ArgumentException.ThrowIfNullOrWhiteSpace(pathData);
69 SKPath? path = SKPath.ParseSvgPathData(pathData);
70
71 if (path is null)
72 throw new ArgumentException("Expected nonempty SVG path data.", nameof(pathData));
73
74 if (path.IsEmpty)
75 {
76 path.Dispose();
77 throw new ArgumentException("Expected nonempty SVG path data.", nameof(pathData));
78 }
79
80 SKRect bounds = path.Bounds;
81
82 if (!float.IsFinite(bounds.Left) || !float.IsFinite(bounds.Top) ||
83 !float.IsFinite(bounds.Right) || !float.IsFinite(bounds.Bottom) ||
84 !float.IsFinite(bounds.Width) || !float.IsFinite(bounds.Height))
85 {
86 path.Dispose();
87 throw new ArgumentException("Path bounds must be finite.", nameof(pathData));
88 }
89
90 return path;
91 }
92
94 protected override void DrawGeometry(SKCanvas canvas, SKRect bounds, SKPaint paint)
95 {
96 lock (RenderStateSynchronization.SyncRoot)
97 {
98 SKPath? source = _sourcePath;
99
100 if (source is null)
101 return;
102
103 // The geometry bounds already include the stroke inset. Both fill and
104 // outline use the same cache entry; color-only changes do not affect it.
105 if (_transformedBounds != bounds || _transformedPath is null)
106 {
107 SKRect sourceBounds = source.Bounds;
108 float sx = sourceBounds.Width > 0 ? bounds.Width / sourceBounds.Width : 1f;
109 float sy = sourceBounds.Height > 0 ? bounds.Height / sourceBounds.Height : 1f;
110 float tx = (sourceBounds.Width > 0 ? bounds.Left : bounds.MidX) - sourceBounds.Left * sx;
111 float ty = (sourceBounds.Height > 0 ? bounds.Top : bounds.MidY) - sourceBounds.Top * sy;
112 var matrix = SKMatrix.CreateScaleTranslation(sx, sy, tx, ty);
113 _transformedPath ??= new SKPath();
114 source.Transform(in matrix, _transformedPath);
115 _transformedBounds = bounds;
116 }
117
118 canvas.DrawPath(_transformedPath, paint);
119 }
120 }
121
123 protected override void Dispose(bool disposing)
124 {
125 // Explicit disposal is already synchronized by DirectDrawingBase.Dispose().
126 if (disposing && !_resourcesDisposed)
127 {
128 _resourcesDisposed = true;
129 _sourcePath?.Dispose();
130 _sourcePath = null;
131 _transformedPath?.Dispose();
132 _transformedPath = null;
133 _transformedBounds = null;
134 }
135
136 base.Dispose(disposing);
137 }
138}
A retained vector path described using SVG path-data syntax, fitted to the drawing bounds.
Definition DirectPath.cs:11
override void Dispose(bool disposing)
Releases resources used by this DirectDrawingBase instance.When disposing is true,...
DirectPath SetFillColor(Color? color)
Sets the interior color, or null to disable filling.
DirectPath(Color color, RenderSurfaceHostBase host, View view, Rectangle screenBounds, string pathData, string? nickname=null)
Creates a view-space path within the supplied bounds.
Definition DirectPath.cs:24
override void DrawGeometry(SKCanvas canvas, SKRect bounds, SKPaint paint)
Definition DirectPath.cs:94
DirectPath SetPathData(string pathData)
Replaces the SVG path data. Invalid data leaves the existing path unchanged.
Definition DirectPath.cs:47
DirectPath(Color color, RenderSurfaceHostBase host, SceneLayer sceneLayer, Rectangle worldBounds, string pathData, string? nickname=null)
Creates a scene-layer path within the supplied bounds.
Definition DirectPath.cs:18
TShape SetShapeFill(Color? color)
Sets or clears the fill of a fillable shape.
DirectShapeBase(Color color, RenderSurfaceHostBase host, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle bounds, string? nickname)
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.