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
DirectShapeBase.cs
1using System.Drawing;
7using SkiaSharp;
8
10
15public abstract class DirectShapeBase<TShape> : DirectDrawingMovableBase
16 where TShape : DirectShapeBase<TShape>
17{
18 private sealed record ShapeStyle(Color Color, Color? FillColor, float StrokeWidth);
19
20 private ShapeStyle _style = new(Color.Black, null, 1f);
21
22 protected DirectShapeBase(
23 Color color,
26 SceneLayer? sceneLayer,
27 View? view,
28 Rectangle bounds,
29 string? nickname)
30 : base(host, mode, sceneLayer, view,
31 mode == DirectDrawingMode.View ? bounds : null,
32 mode == DirectDrawingMode.SceneLayer ? bounds : null,
33 nickname)
34 {
35 _style = new ShapeStyle(color, null, 1f);
36 ForceRefresh();
37 }
38
40 public Color StrokeColor => _style.Color;
41
43 public Color? FillColor => _style.FillColor;
44
46 public float StrokeWidth => _style.StrokeWidth;
47
49 public TShape SetColor(Color color)
50 {
51 _style = _style with { Color = color };
52 ForceRefresh();
53 return (TShape)this;
54 }
55
57 public TShape SetStrokeWidth(float width)
58 {
59 if (!float.IsFinite(width) || width < 0)
60 throw new ArgumentOutOfRangeException(nameof(width));
61
62 _style = _style with { StrokeWidth = width };
63 ForceRefresh();
64 return (TShape)this;
65 }
66
68 protected TShape SetShapeFill(Color? color)
69 {
70 _style = _style with { FillColor = color };
71 ForceRefresh();
72 return (TShape)this;
73 }
74
76 protected sealed override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
77 {
78 if (destRectScreen.Width <= 0 || destRectScreen.Height <= 0)
79 return;
80
81 ShapeStyle style = _style;
82
83 if (style is null)
84 return;
85
86 // Keep all painted pixels inside the registered dirty-region bounds.
87 float strokeWidth = Math.Min(style.StrokeWidth, Math.Min(destRectScreen.Width, destRectScreen.Height));
88 float inset = strokeWidth / 2f;
89 var geometryBounds = new SKRect(
90 destRectScreen.Left + inset,
91 destRectScreen.Top + inset,
92 destRectScreen.Right - inset,
93 destRectScreen.Bottom - inset);
94
95 var canvas = backbuffer.Canvas;
96 canvas.Save();
97 try
98 {
99 canvas.ClipRect(new SKRect(destRectScreen.Left, destRectScreen.Top, destRectScreen.Right, destRectScreen.Bottom));
100
101 if (style.FillColor is Color fill)
102 {
103 using var paint = new SKPaint { Color = fill.ToSKColor(), IsAntialias = true, Style = SKPaintStyle.Fill };
104 DrawGeometry(canvas, geometryBounds, paint);
105 }
106
107 if (strokeWidth > 0)
108 {
109 using var paint = new SKPaint
110 {
111 Color = style.Color.ToSKColor(),
112 IsAntialias = true,
113 Style = SKPaintStyle.Stroke,
114 StrokeWidth = strokeWidth,
115 StrokeJoin = SKStrokeJoin.Round,
116 StrokeCap = SKStrokeCap.Round
117 };
118 DrawGeometry(canvas, geometryBounds, paint);
119 }
120 }
121 finally
122 {
123 canvas.Restore();
124 }
125 }
126
128 protected abstract void DrawGeometry(SKCanvas canvas, SKRect bounds, SKPaint paint);
129
131 protected static PointF[] CopyPoints(IEnumerable<PointF> points, int minimumCount)
132 {
133 ArgumentNullException.ThrowIfNull(points);
134 PointF[] copy = points.ToArray();
135
136 if (copy.Length < minimumCount || copy.Any(p =>
137 !float.IsFinite(p.X) || !float.IsFinite(p.Y) ||
138 p.X < 0 || p.X > 1 || p.Y < 0 || p.Y > 1))
139 throw new ArgumentException("Expected enough finite points in the normalized range [0, 1].", nameof(points));
140
141 return copy;
142 }
143
145 protected static SKPoint MapPoint(PointF point, SKRect bounds) =>
146 new(bounds.Left + point.X * bounds.Width, bounds.Top + point.Y * bounds.Height);
147
149 protected static void DrawPointSequence(SKCanvas canvas, SKRect bounds, SKPaint paint, PointF[] points, bool closed)
150 {
151 if (points.Length == 0)
152 return;
153
154 using var path = new SKPath();
155 path.AddPoly(points.Select(p => MapPoint(p, bounds)).ToArray(), closed);
156 canvas.DrawPath(path, paint);
157 }
158}
View? View
Gets the view to which this direct drawing is attached, or null if the drawing is in scene-layer mode...
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
DirectDrawingMovableBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle? screenBounds, Rectangle? worldBounds, string? name=null)
Initializes a new instance of the DirectDrawingMovableBase class with integrated movement capabilitie...
float StrokeWidth
Gets the outline width in screen pixels.
static SKPoint MapPoint(PointF point, SKRect bounds)
Maps a normalized local point into the drawing bounds.
static PointF[] CopyPoints(IEnumerable< PointF > points, int minimumCount)
Copies and validates a sequence of normalized local points.
TShape SetShapeFill(Color? color)
Sets or clears the fill of a fillable shape.
static void DrawPointSequence(SKCanvas canvas, SKRect bounds, SKPaint paint, PointF[] points, bool closed)
Draws a point sequence as an open or closed path.
Color StrokeColor
Gets the outline color.
DirectShapeBase(Color color, RenderSurfaceHostBase host, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle bounds, string? nickname)
TShape SetColor(Color color)
Sets the outline color.
TShape SetStrokeWidth(float width)
Sets the outline width in screen pixels; zero disables the outline.
Color? FillColor
Gets the optional fill color.
void DrawGeometry(SKCanvas canvas, SKRect bounds, SKPaint paint)
Renders geometry within the supplied stroke-inset bounds.
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
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.