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
DirectImage.cs
1using System.Drawing;
7using SkiaSharp;
8
10
59public sealed class DirectImage : DirectDrawingMovableBase
60{
69 public enum ScaleMode
70 {
76
83
90
97
104 }
105
106 private SKImage? _image; // primary storage
107 private SKBitmap? _bitmap; // optional if provided instead of SKImage
108 private SKRect? _src; // source sprite region (in image pixels)
109 private ScaleMode _scale = ScaleMode.Stretch;
110
111 private readonly SKPaint _paint = new()
112 {
113 IsAntialias = true,
114 FilterQuality = SKFilterQuality.Medium,
115 BlendMode = SKBlendMode.SrcOver,
116 Color = SKColors.White // used for tint/alpha via color filter
117 };
118
119 // visual modifiers
120 private SKColor? _tint; // multiplied over image
121 private byte _opacity = 255; // 0..255
122
123 // rotation
124 private float _rotationDeg = 0f;
125
126 // normalized anchor (0..1) where 0,0 = top-left of dest rect
127 private float _anchorX = 0.5f, _anchorY = 0.5f;
128
129 private DirectImage(
130 SKBitmap bitmap,
131 RenderSurfaceHostBase renderSurfaceHost,
133 SceneLayer? sceneLayer,
134 View? view,
135 Rectangle? screenBounds,
136 Rectangle? worldBounds,
137 string? name = null)
138 : base(
139 renderSurfaceHost,
140 mode,
141 sceneLayer,
142 view,
143 screenBounds,
144 worldBounds,
145 name)
146 {
147 SetBitmap(bitmap);
148 }
149
150 private DirectImage(
151 SKImage image,
152 RenderSurfaceHostBase renderSurfaceHost,
154 SceneLayer? sceneLayer,
155 View? view,
156 Rectangle? screenBounds,
157 Rectangle? worldBounds,
158 string? nickname = null)
159 : base(
160 renderSurfaceHost,
161 mode,
162 sceneLayer,
163 view,
164 screenBounds,
165 worldBounds,
166 nickname)
167 {
168 SetImage(image);
169 }
170
192 SKBitmap bitmap,
193 RenderSurfaceHostBase renderSurfaceHost,
194 SceneLayer sceneLayer,
195 Rectangle worldBounds,
196 string? nickname = null) : this(
197 bitmap,
198 renderSurfaceHost,
200 sceneLayer,
201 null,
202 null,
203 worldBounds,
204 nickname)
205 {
206 }
207
228 SKBitmap bitmap,
229 RenderSurfaceHostBase renderSurfaceHost,
230 View view,
231 Rectangle screenBounds,
232 string? nickname = null) : this(
233 bitmap,
234 renderSurfaceHost,
236 null,
237 view,
238 screenBounds,
239 null,
240 nickname)
241 {
242 }
243
266 SKImage image,
267 RenderSurfaceHostBase renderSurfaceHost,
268 SceneLayer sceneLayer,
269 Rectangle worldBounds,
270 string? nickname = null) : this(
271 image,
272 renderSurfaceHost,
274 sceneLayer,
275 null,
276 null,
277 worldBounds,
278 nickname)
279 {
280 }
281
303 SKImage image,
304 RenderSurfaceHostBase renderSurfaceHost,
305 View view,
306 Rectangle screenBounds,
307 string? nickname = null) : this(
308 image,
309 renderSurfaceHost,
311 null,
312 view,
313 screenBounds,
314 null,
315 nickname)
316 {
317 }
318
336 public DirectImage SetBitmap(SKBitmap bitmap)
337 {
338 _bitmap = bitmap ?? throw new ArgumentNullException(nameof(bitmap));
339 _image = null;
340 ForceRefresh();
341 return this;
342 }
343
361 public DirectImage SetImage(SKImage image)
362 {
363 _image = image ?? throw new ArgumentNullException(nameof(image));
364 _bitmap = null;
365 ForceRefresh();
366 return this;
367 }
368
390 public DirectImage SetSourceRect(SKRect? srcPixels)
391 {
392 _src = srcPixels;
393 ForceRefresh();
394 return this;
395 }
396
420 public DirectImage SetScaleMode(ScaleMode mode)
421 {
422 _scale = mode;
423 ForceRefresh();
424 return this;
425 }
426
446 public DirectImage SetTint(SKColor color)
447 {
448 _tint = color;
449 ForceRefresh();
450 return this;
451 }
452
466 public DirectImage ClearTint()
467 {
468 _tint = null;
469 ForceRefresh();
470 return this;
471 }
472
491 public DirectImage SetOpacity(byte alpha)
492 {
493 _opacity = alpha;
494 ForceRefresh();
495 return this;
496 }
497
518 public DirectImage SetBlendMode(SKBlendMode mode)
519 {
520 _paint.BlendMode = mode;
521 ForceRefresh();
522 return this;
523 }
524
551 public DirectImage SetFilterQuality(SKFilterQuality q)
552 {
553 _paint.FilterQuality = q;
554 ForceRefresh();
555 return this;
556 }
557
576 public DirectImage SetRotation(float degrees)
577 {
578 _rotationDeg = degrees;
579 ForceRefresh();
580 return this;
581 }
582
608 public DirectImage SetAnchor(float ax, float ay)
609 {
610 _anchorX = Math.Clamp(ax, 0f, 1f);
611 _anchorY = Math.Clamp(ay, 0f, 1f);
612 ForceRefresh();
613 return this;
614 }
615
639 protected override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
640 {
641 var canvas = backbuffer.Canvas;
642
643 // get SKImage to draw
644 var img = _image ?? (_bitmap != null ? SKImage.FromBitmap(_bitmap) : null);
645
646 if (img is null)
647 return;
648
649 if (backbuffer is RecordingBackbuffer && img.IsTextureBacked)
650 throw new NotSupportedException(
651 "Desktop frame recording requires a CPU image, not a context-bound texture.");
652
653 // source rect (pixels in image space)
654 SKRect src = _src ?? new SKRect(
655 0,
656 0,
657 img.Width,
658 img.Height);
659
660 // destination rect (screen space), computed by scale mode
661 SKRect dst = ComputeDestRect(destRectScreen.ToPixelAlignedRect(), src, _scale);
662
663 // apply tint/opacity via color filter
664 // combine tint with opacity (multiply in linear-ish sRGB)
665 var tint = _tint ?? SKColors.White;
666
667 var withAlpha = new SKColor(
668 tint.Red,
669 tint.Green,
670 tint.Blue,
671 _opacity);
672
673 using var cf = SKColorFilter.CreateBlendMode(withAlpha, SKBlendMode.Modulate);
674 _paint.ColorFilter = cf;
675
676 // rotation about anchor inside dst
677 if (_rotationDeg != 0f)
678 {
679 float ax = dst.Left + _anchorX * dst.Width;
680 float ay = dst.Top + _anchorY * dst.Height;
681 canvas.Save();
682 canvas.RotateDegrees(_rotationDeg, ax, ay);
683 }
684
685 // draw
686 canvas.DrawImage(
687 img,
688 src,
689 dst,
690 _paint);
691
692 if (_rotationDeg != 0f)
693 canvas.Restore();
694
695 // if we created a transient SKImage from a bitmap, dispose it now
696 if (_image is null && img is not null)
697 img.Dispose();
698 }
699
700 private static SKRect ComputeDestRect(Rectangle bounds, SKRect src, ScaleMode mode)
701 {
702 var dst = bounds.ToSKRect();
703
704 if (mode == ScaleMode.Stretch)
705 return dst;
706
707 if (mode == ScaleMode.Center) // center, no scale
708 {
709 float w = MathF.Min(dst.Width, src.Width);
710 float h = MathF.Min(dst.Height, src.Height);
711 float dx = dst.MidX - w * 0.5f, dy = dst.MidY - h * 0.5f;
712
713 return new SKRect(
714 dx,
715 dy,
716 dx + w,
717 dy + h);
718 }
719
720 float srcW = src.Width, srcH = src.Height;
721 float dstW = dst.Width, dstH = dst.Height;
722
723 float scaleX = dstW / srcW;
724 float scaleY = dstH / srcH;
725
726 float scale = mode switch
727 {
728 ScaleMode.Fit => MathF.Min(scaleX, scaleY),
729 ScaleMode.Fill => MathF.Max(scaleX, scaleY),
730 ScaleMode.PixelPerfect => MathF.Max(1f, MathF.Floor(MathF.Min(scaleX, scaleY))),
731 _ => 1f
732 };
733
734 float w2 = srcW * scale, h2 = srcH * scale;
735 float x = dst.MidX - w2 * 0.5f;
736 float y = dst.MidY - h2 * 0.5f;
737
738 return new SKRect(
739 x,
740 y,
741 x + w2,
742 y + h2);
743 }
744}
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...
DirectImage(SKBitmap bitmap, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? nickname=null)
Initializes a new instance of the DirectImage class for scene-layer (world-space) rendering using a b...
DirectImage SetAnchor(float ax, float ay)
Sets the rotation and scaling anchor point within the destination rectangle using normalized coordina...
DirectImage SetScaleMode(ScaleMode mode)
Sets how the image is scaled to fit within the destination rectangle.
DirectImage SetFilterQuality(SKFilterQuality q)
Sets the filter quality used when scaling the image.
DirectImage ClearTint()
Removes any color tint, restoring the image's original colors.
DirectImage SetBlendMode(SKBlendMode mode)
Sets the Skia blend mode used when compositing this image onto the backbuffer.
DirectImage SetTint(SKColor color)
Applies a color tint (multiplicative blend) to the image.
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Renders the image to the backbuffer with all configured transformations and effects applied.
DirectImage SetImage(SKImage image)
Replaces the backing image with a new one.
DirectImage SetBitmap(SKBitmap bitmap)
Replaces the backing bitmap with a new one.
DirectImage SetOpacity(byte alpha)
Sets the overall opacity (alpha transparency) of the image.
DirectImage(SKImage image, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? nickname=null)
Initializes a new instance of the DirectImage class for view (screen-space) rendering using a pre-enc...
DirectImage(SKImage image, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? nickname=null)
Initializes a new instance of the DirectImage class for scene-layer (world-space) rendering using a p...
DirectImage SetRotation(float degrees)
Sets the rotation angle in degrees around the anchor point.
DirectImage SetSourceRect(SKRect? srcPixels)
Sets the source rectangle within the image to display, enabling sprite sheet support.
ScaleMode
Defines how an image is scaled to fit within the destination rectangle.
@ Fit
Scales the image to fit entirely within the destination rectangle while preserving aspect ratio....
@ PixelPerfect
Scales the image by the largest integer factor that fits within the destination rectangle while prese...
@ Center
Displays the image at its native resolution without scaling, centered in the destination rectangle....
@ Fill
Scales the image to completely cover the destination rectangle while preserving aspect ratio....
@ Stretch
Stretches the image to fill the entire destination rectangle, ignoring aspect ratio....
DirectImage(SKBitmap bitmap, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? nickname=null)
Initializes a new instance of the DirectImage class for view (screen-space) rendering using a bitmap.
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 ...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.