Gondwana Game Engine Development (master) - 2.6.1
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
PresentationTransform.cs
1using System.Drawing;
2using SkiaSharp;
3
4namespace Gondwana.Rendering;
5
9public readonly record struct PresentationTransform(float Scale, SKRect DestinationRect)
10{
17 public static PresentationTransform Fit(int bufferWidth, int bufferHeight, int adapterWidth, int adapterHeight)
18 {
19 if (bufferWidth <= 0 || bufferHeight <= 0 || adapterWidth <= 0 || adapterHeight <= 0)
20 return default;
21
22 float scale = Math.Min((float)adapterWidth / bufferWidth, (float)adapterHeight / bufferHeight);
23 float width = bufferWidth * scale;
24 float height = bufferHeight * scale;
25 return new(scale, SKRect.Create((adapterWidth - width) / 2f, (adapterHeight - height) / 2f, width, height));
26 }
27
32 public bool TryAdapterPxToScreenPx(PointF adapterPx, out PointF screenPx)
33 {
34 screenPx = Scale > 0
35 ? new((adapterPx.X - DestinationRect.Left) / Scale, (adapterPx.Y - DestinationRect.Top) / Scale)
36 : new(-1, -1);
37 return Scale > 0 && DestinationRect.Contains(adapterPx.X, adapterPx.Y);
38 }
39
43 public Rectangle ScreenRectToAdapterRect(Rectangle screenRect)
44 => Scale <= 0 ? Rectangle.Empty : Rectangle.FromLTRB(
45 (int)Math.Floor(DestinationRect.Left + screenRect.Left * Scale),
46 (int)Math.Floor(DestinationRect.Top + screenRect.Top * Scale),
47 (int)Math.Ceiling(DestinationRect.Left + screenRect.Right * Scale),
48 (int)Math.Ceiling(DestinationRect.Top + screenRect.Bottom * Scale));
49
50 internal static int ScaleDimension(int dimension, float renderScale)
51 {
52 double scaled = Math.Round(dimension * (double)renderScale, MidpointRounding.AwayFromZero);
53 if (scaled > int.MaxValue)
54 throw new ArgumentOutOfRangeException(nameof(renderScale), "Scaled dimension exceeds the supported integer range.");
55 return Math.Max(1, (int)scaled);
56 }
57}
58
readonly record struct PresentationTransform(float Scale, SKRect DestinationRect)
Aspect-preserving mapping between logical Backbuffer ScreenPx and adapter pixels.
RenderScalingFilter
Filtering used only when presenting the finished Backbuffer.
@ Linear
Bilinear presentation filtering.
@ NearestNeighbor
Unfiltered nearest-pixel presentation for pixel art.