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
SkiaExtensions.cs
1using System.Drawing;
2using SkiaSharp;
3
4namespace Gondwana.SkiaSharp;
5
9public static class SkiaExtensions
10{
16 public static Rectangle ToRectangle(this SKRect rect)
17 => Rectangle.FromLTRB(
18 (int)rect.Left,
19 (int)rect.Top,
20 (int)rect.Right,
21 (int)rect.Bottom);
22
28 public static SKColor ToSKColor(this Color color)
29 => new(
30 color.R,
31 color.G,
32 color.B,
33 color.A);
34
40 public static Color ToColor(this SKColor skcolor)
41 => Color.FromArgb(
42 skcolor.Alpha,
43 skcolor.Red,
44 skcolor.Green,
45 skcolor.Blue);
46
52 public static SKRect ToSKRect(this Rectangle rect)
53 => new(
54 rect.X,
55 rect.Y,
56 rect.X + rect.Width,
57 rect.Y + rect.Height);
58
64 public static SKRect ToSKRect(this RectangleF rect)
65 => new(
66 rect.X,
67 rect.Y,
68 rect.X + rect.Width,
69 rect.Y + rect.Height);
70
76 public static SKRectI ToSKRectI(this Rectangle rect)
77 => new(
78 rect.Left,
79 rect.Top,
80 rect.Right,
81 rect.Bottom);
82
89 public static SKPoint[] ToSKPoints(this Point[] points, bool enclose = false)
90 {
91 if (!enclose)
92 return points.Select(p => new SKPoint(p.X, p.Y)).ToArray();
93
94 ;
95
96 var len = points.Length + 1;
97 var result = new SKPoint[len];
98
99 for (int i = 0; i < points.Length; i++)
100 result[i] = new SKPoint(points[i].X, points[i].Y);
101
102 if (enclose)
103 result[^1] = result[0]; // append first point to close polygon
104
105 return result;
106 }
107
114 public static SKBitmap CopyRegion(this SKBitmap source, Rectangle sourceRect)
115 {
116 if (source == null || source.IsEmpty)
117 throw new ArgumentException("Invalid source bitmap.", nameof(source));
118
119 if (sourceRect.Width <= 0 || sourceRect.Height <= 0)
120 throw new ArgumentException("Source rectangle must have positive size.", nameof(sourceRect));
121
122 var skSourceRect = sourceRect.ToSKRectI();
123
124 if (!source.Info.Rect.Contains(skSourceRect))
125 throw new ArgumentOutOfRangeException(nameof(sourceRect), "Source rectangle is outside the bitmap bounds.");
126
127 var copy = new SKBitmap(
128 new SKImageInfo(
129 sourceRect.Width,
130 sourceRect.Height,
131 source.Info.ColorType,
132 source.Info.AlphaType));
133
134 using var canvas = new SKCanvas(copy);
135
136 canvas.Clear(SKColors.Transparent);
137
138 canvas.DrawBitmap(
139 source,
140 skSourceRect,
141 new SKRect(
142 0,
143 0,
144 sourceRect.Width,
145 sourceRect.Height));
146
147 return copy;
148 }
149
156 public static SKImage CopyRegion(this SKImage source, Rectangle sourceRect)
157 {
158 if (source == null)
159 throw new ArgumentNullException(nameof(source));
160
161 if (sourceRect.Width <= 0 || sourceRect.Height <= 0)
162 throw new ArgumentException("Source rectangle must have positive size.", nameof(sourceRect));
163
164 var skSourceRect = new SKRect(
165 sourceRect.Left,
166 sourceRect.Top,
167 sourceRect.Right,
168 sourceRect.Bottom);
169
170 var skDestRect = new SKRect(
171 0,
172 0,
173 sourceRect.Width,
174 sourceRect.Height);
175
176 var imageInfo = new SKImageInfo(
177 sourceRect.Width,
178 sourceRect.Height,
179 source.ColorType,
180 source.AlphaType,
181 source.ColorSpace);
182
183 using var surface = SKSurface.Create(imageInfo)
184 ?? throw new InvalidOperationException("Could not create SKSurface.");
185
186 surface.Canvas.Clear(SKColors.Transparent);
187
188 surface.Canvas.DrawImage(
189 source,
190 skSourceRect,
191 skDestRect);
192
193 return surface.Snapshot();
194 }
195
203 public static void SaveBitmapToFile(
204 this SKBitmap bitmap,
205 string filePath,
206 SKEncodedImageFormat format = SKEncodedImageFormat.Png,
207 int quality = 100)
208 {
209 if (bitmap == null || bitmap.IsEmpty)
210 throw new ArgumentException("Invalid bitmap.");
211
212 using var image = SKImage.FromBitmap(bitmap);
213 using var data = image.Encode(format, quality);
214 using var stream = File.OpenWrite(filePath);
215 data.SaveTo(stream);
216 }
217
225 public static byte[] EncodeBitmapToBytes(
226 this SKBitmap bitmap,
227 SKEncodedImageFormat format = SKEncodedImageFormat.Png,
228 int quality = 100)
229 {
230 if (bitmap == null || bitmap.IsEmpty)
231 throw new ArgumentException("Invalid bitmap.");
232
233 using var image = SKImage.FromBitmap(bitmap);
234 using var data = image.Encode(format, quality);
235 return data.ToArray();
236 }
237
245 public static void ApplyAlphaMask(this SKBitmap bitmap, SKColor targetColor, byte tolerance = 5)
246 {
247 if (bitmap == null || bitmap.IsEmpty)
248 throw new ArgumentException("Invalid bitmap.");
249
250 int width = bitmap.Width;
251 int height = bitmap.Height;
252
253 for (int y = 0; y < height; y++)
254 {
255 for (int x = 0; x < width; x++)
256 {
257 var color = bitmap.GetPixel(x, y);
258
259 if (IsColorClose(color, targetColor, tolerance))
260 {
261 // Fully transparent should also have RGB = 0 for premul correctness.
262 bitmap.SetPixel(
263 x,
264 y,
265 new SKColor(
266 0,
267 0,
268 0,
269 0));
270 }
271 }
272 }
273
274 bitmap.NotifyPixelsChanged();
275 }
276
284 public static SKBitmap PremultiplyAlpha(this SKBitmap bitmap)
285 {
286 if (bitmap == null || bitmap.IsEmpty)
287 throw new ArgumentException("Invalid bitmap.");
288
289 if (bitmap.AlphaType == SKAlphaType.Premul)
290 return bitmap;
291
292 var info = new SKImageInfo(
293 bitmap.Width,
294 bitmap.Height,
295 bitmap.ColorType,
296 SKAlphaType.Premul);
297
298 var premulBitmap = new SKBitmap(info);
299
300 int width = bitmap.Width;
301 int height = bitmap.Height;
302
303 for (int y = 0; y < height; y++)
304 {
305 for (int x = 0; x < width; x++)
306 {
307 var c = bitmap.GetPixel(x, y);
308 byte a = c.Alpha;
309
310 byte r = (byte)((c.Red * a + 127) / 255);
311 byte g = (byte)((c.Green * a + 127) / 255);
312 byte b = (byte)((c.Blue * a + 127) / 255);
313
314 premulBitmap.SetPixel(
315 x,
316 y,
317 new SKColor(
318 r,
319 g,
320 b,
321 a));
322 }
323 }
324
325 premulBitmap.NotifyPixelsChanged();
326 return premulBitmap;
327 }
328
329 private static bool IsColorClose(SKColor a, SKColor b, byte tolerance)
330 {
331 return
332 Math.Abs(a.Red - b.Red) <= tolerance &&
333 Math.Abs(a.Green - b.Green) <= tolerance &&
334 Math.Abs(a.Blue - b.Blue) <= tolerance;
335 }
336}
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.