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
BackbufferBase.cs
1using System.Drawing;
7using SkiaSharp;
8
10
19public abstract class BackbufferBase : IDisposable
20{
21 private int _width;
22 private int _height;
23
24 private BackbufferBase()
25 {
26 }
27
37 protected BackbufferBase(int width, int height)
38 : this()
39 {
40 _width = width;
41 _height = height;
42 }
43
61 public abstract SKCanvas Canvas { get; }
62
84 protected internal abstract SKImage Snapshot();
85
104 protected internal abstract void BeginFrame();
105
131 protected internal abstract void DrawTileFrame(Tile tile, RectangleF destRectScreen);
132
152 protected internal abstract void EndFrame();
153
157 public SKPaint FogPaint { get; set; } = new()
158 {
159 Color = new SKColor(
160 0,
161 0,
162 0,
163 128),
164 IsAntialias = true
165 };
166
170 public SKPaint GridLinePaint { get; set; } = new()
171 {
172 Color = SKColors.White,
173 IsStroke = true,
174 StrokeWidth = 1
175 };
176
180 public SKPaint CollisionBoxPaint { get; set; } = new()
181 {
182 Color = SKColors.Green,
183 IsStroke = true,
184 StrokeWidth = 1
185 };
186
190 public int Width => Volatile.Read(ref _width);
191
195 public int Height => Volatile.Read(ref _height);
196
218 protected internal virtual void RequestResize(int width, int height)
219 {
220 /* no-op by default */
221 }
222
241 protected internal event Action<int, int>? SizeChanged;
242
259 protected void UpdateSize(int width, int height)
260 {
261 Volatile.Write(ref _width, width);
262 Volatile.Write(ref _height, height);
263
264 SizeChanged?.Invoke(width, height);
265 }
266
271 protected internal Rectangle DirtyRectangle { get; private set; }
272
273 private SKColor _clearColor = SKColors.Black;
274
278 protected readonly SKPaint _fillPaint = new()
279 {
280 IsAntialias = false,
281 BlendMode = SKBlendMode.Src,
282 Style = SKPaintStyle.Fill,
283 FilterQuality = SKFilterQuality.None,
284 };
285
289 public SKColor ClearColor
290 {
291 get => _clearColor;
292 set
293 {
294 _clearColor = value;
295 _fillPaint.Color = value;
296 }
297 }
298
299 internal void ClearRect(Rectangle rectPx)
300 {
301 if (rectPx.IsEmpty)
302 return;
303
304 _fillPaint.Color = ClearColor;
305
306 // Screen-pixel space
307 Canvas.Save();
308 Canvas.ResetMatrix();
309
310 // Rect is expected to be in the current canvas coordinate space.
311 Canvas.DrawRect(rectPx.ToSKRect(), _fillPaint);
312
313 // GL surfaces always present the full backbuffer and do not maintain dirty regions.
315 AddToBackbufferDirtyRectangle(rectPx);
316
317 Canvas.Restore();
318 }
319
320 internal void DrawDrawables(View view, IEnumerable<IDrawable> drawables, Rectangle clipRect)
321 {
322 if (this is RecordingBackbuffer recording &&
323 drawables is IReadOnlyList<IDrawable> ordered &&
324 recording.TryDrawFixedGridDrawables(view, ordered, clipRect))
325 {
326 return;
327 }
328
329 Canvas.Save();
330 Canvas.ClipRect(clipRect.ToSKRect());
331
332 var tiles = new List<(Tile Tile, WrappedDrawable? Instance)>();
333
334 foreach (var entry in drawables)
335 {
336 var instance = entry as WrappedDrawable;
337 var drawable = instance?.Owner ?? entry;
338 using var scope = instance?.Enter(view);
339
340 if (!drawable.Visible)
341 continue;
342
343 var destRectScreen = drawable.GetDrawLocationScreen(view);
344
345 if (drawable is Sprite sprite)
346 {
347 destRectScreen = sprite.ApplyJiggleToDestRect(destRectScreen);
348 }
349
350 bool batched = this is RecordingBackbuffer recorder &&
351 instance is null &&
352 drawable.GetType() == typeof(SceneLayerTile) &&
353 drawable is SceneLayerTile layerTile &&
354 recorder.TryQueueTile(layerTile, destRectScreen);
355
356 if (!batched)
357 {
358 (this as RecordingBackbuffer)?.FlushTileBatch();
359 drawable.Draw(this, destRectScreen);
360 }
361
362 // GPU/GL surfaces always present the complete backbuffer. Avoid calculating visual
363 // bounds just to pass them to dirty-region tracking that the GPU path never consumes.
365 {
366 RectangleF visualBoundsScreen = drawable is Sprite drawnSprite
367 ? drawnSprite.GetVisualBoundsScreen(destRectScreen)
368 : destRectScreen;
369
370 AddToBackbufferDirtyRectangle(visualBoundsScreen.ToPixelAlignedRect());
371 }
372
373 if (drawable is Tile tile)
374 tiles.Add((tile, instance));
375 }
376
377 (this as RecordingBackbuffer)?.FlushTileBatch();
378
379 PostDrawTiles(view, tiles);
380
381 Canvas.Restore();
382 }
383
384 private void PostDrawTiles(View view, List<(Tile Tile, WrappedDrawable? Instance)> tiles)
385 {
386 foreach (var entry in tiles)
387 {
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;
394
395 if (!drawFog && !drawGrid && !drawCollision)
396 continue;
397
398 // Orthogonal grid outlines are rectangles. Avoid building two transient point arrays
399 // and performing four point-wise world-to-screen transforms for every fixed tile.
400 // Rectangle conversion uses the same view transform while preserving the grid cell
401 // bounds (includeOverhang: false) used by the legacy polygon path.
402 if (drawGrid
403 && !drawFog
404 && layer.CoordinateSystemType == Gondwana.Drawing.Coordinates.CoordinateSystemTypes.Orthogonal)
405 {
406 var worldRect = layer.CoordinateSystem.GetPixelRangeForTile(tile, includeOverhang: false);
407 var screenRect = view.WorldRectToScreenRect(layer, worldRect);
408 Canvas.DrawRect(screenRect.ToSKRect(), GridLinePaint);
409
410 if (drawCollision)
411 {
412 var colRectScreen = tile.GetCollisionAreaScreen(view).ToSKRect();
413 Canvas.DrawRect(colRectScreen, CollisionBoxPaint);
414 }
415
416 continue;
417 }
418
419 // Polygon geometry is only needed for fog or non-orthogonal grid outlines.
420 if (drawFog || drawGrid)
421 {
422 var worldPts = tile.OutlinePointsWorld;
423 var ptsScreen = new SKPoint[worldPts.Length];
424
425 for (int i = 0; i < worldPts.Length; i++)
426 {
427 var p = worldPts[i];
428
429 var sp = view.WorldPxToScreenPx(
430 layer,
431 new PointF(p.X, p.Y)
432 );
433
434 ptsScreen[i] = new SKPoint(sp.X, sp.Y);
435 }
436
437 if (drawFog)
438 {
439 using var path = new SKPath();
440 path.AddPoly(ptsScreen, close: true);
441 Canvas.DrawPath(path, FogPaint);
442 }
443
444 if (drawGrid)
445 {
446 var enclosed = new SKPoint[ptsScreen.Length + 1];
447 Array.Copy(ptsScreen, enclosed, ptsScreen.Length);
448 enclosed[^1] = ptsScreen[0];
449 Canvas.DrawPoints(SKPointMode.Polygon, enclosed, GridLinePaint);
450 }
451 }
452
453 if (drawCollision)
454 {
455 var colRectScreen = tile.GetCollisionAreaScreen(view).ToSKRect();
456 Canvas.DrawRect(colRectScreen, CollisionBoxPaint);
457 }
458 }
459 }
460
468 protected internal void AddToBackbufferDirtyRectangle(Rectangle area)
469 {
470 if (IsGlThreadRendered || area.IsEmpty)
471 return;
472
473 area.Inflate(area.Width, area.Height);
474
475 DirtyRectangle = DirtyRectangle.IsEmpty
476 ? area
477 : Rectangle.Union(DirtyRectangle, area);
478 }
479
491 protected internal void ClearDirtyRectangle()
492 {
493 DirtyRectangle = Rectangle.Empty;
494 }
495
506 public virtual byte[] ToByteArray(SKEncodedImageFormat format = SKEncodedImageFormat.Png, int quality = 100)
507 {
508 using var image = Snapshot();
509 using var data = image.Encode(format, quality);
510 return data.ToArray();
511 }
512
523 public virtual bool IsGlThreadRendered => false;
524
539 public virtual void Dispose()
540 {
541 _fillPaint.Dispose();
542 FogPaint.Dispose();
543 GridLinePaint.Dispose();
544 CollisionBoxPaint.Dispose();
545
546 GC.SuppressFinalize(this);
547 }
548}
Represents a movable visual element on a scene layer.
Definition Sprite.cs:21
Represents an abstract base class for drawable tiles in the Gondwana engine. Provides core functional...
Definition Tile.cs:19
BackbufferBase(int width, int height)
Initializes a new instance of the BackbufferBase class with the specified dimensions.
virtual bool IsGlThreadRendered
Gets a value indicating whether this backbuffer is rendered on the GL thread rather than the engine's...
int Width
Gets the current Backbuffer width in a thread-safe manner.
SKPaint CollisionBoxPaint
Gets or sets the paint settings used to render collision boxes.
void UpdateSize(int width, int height)
Updates the stored backbuffer dimensions and raises the SizeChanged event.
SKPaint FogPaint
Gets or sets the paint object used to render fog effects.
SKPaint GridLinePaint
Gets or sets the paint settings used to render grid lines.
virtual byte[] ToByteArray(SKEncodedImageFormat format=SKEncodedImageFormat.Png, int quality=100)
Converts the current image to a byte array in the specified format and quality.
int Height
Gets the current Backbuffer height in a thread-safe manner.
SKColor ClearColor
Gets or sets the color used to clear the drawing surface.
virtual void Dispose()
Releases all resources used by the BackbufferBase instance.
readonly SKPaint _fillPaint
Paint used to fill cleared regions of the backbuffer with the current ClearColor.
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
PointF WorldPxToScreenPx(SceneLayer layer, PointF worldPx)
Converts a world-space pixel position into screen-space coordinates for this view,...
Definition View.cs:289
RectangleF WorldRectToScreenRect(SceneLayer layer, RectangleF worldRect)
Converts a world-space pixel rectangle into a screen-space rectangle for this View,...
Definition View.cs:340
Represents the values stored at a single location on a SceneLayer.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.