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
WinFormGpuRenderSurfaceControl.cs
3using SkiaSharp.Views.Desktop;
4
6
10public partial class WinFormGpuRenderSurfaceControl : UserControl
11{
12 private readonly SKGLControl _glControl;
13 private WinFormGpuRenderSurfaceAdapter? _adapter;
14
19
23 public RenderSurfaceHost<GpuBackbuffer> Host { get; private set; } = null!;
24
29 {
30 // Do not Dock=Fill: WinForms will otherwise physically resize SKGLControl to 0×0
31 // during minimize/layout. OpenTK forwards that size to its native GLFW child. Keep the
32 // drawable at least 1×1 and let this wrapper report the real presentation size instead.
33 _glControl = new SKGLControl();
34 Controls.Add(_glControl);
35 UpdateGlControlBounds();
36
37 // Forward mouse events from the inner GL control to this outer control so that a
38 // WinFormsMouseAdapter attached to this control sees them. The inner SKGLControl
39 // fills the entire client area and therefore receives all mouse input; without this
40 // forwarding the outer control's mouse events never fire.
41 _glControl.MouseDown += (_, e) => OnMouseDown(e);
42 _glControl.MouseUp += (_, e) => OnMouseUp(e);
43 _glControl.MouseMove += (_, e) => OnMouseMove(e);
44 _glControl.MouseClick += (_, e) => OnMouseClick(e);
45 _glControl.MouseWheel += (_, e) => OnMouseWheel(e);
46 _glControl.MouseEnter += (_, e) => OnMouseEnter(e);
47 _glControl.MouseLeave += (_, e) => OnMouseLeave(e);
48
49 this.Load += (_, _) => InitializeBackbuffer();
50
51 // Keep the native GL drawable valid even when this wrapper is temporarily 0×0. The
52 // adapter separately observes this wrapper's real size and suspends presentation at zero.
53 SizeChanged += (_, __) => UpdateGlControlBounds();
54
55 // Fire once after this control is realized
56 HandleCreated += (_, __) => _adapter?.RefreshDestinationSize();
57 if (IsHandleCreated)
58 BeginInvoke((Action)(() => _adapter?.RefreshDestinationSize()));
59 }
60
65 protected override void OnParentChanged(EventArgs e)
66 {
67 base.OnParentChanged(e);
68 if (IsHandleCreated)
69 BeginInvoke((Action)(() => _adapter?.RefreshDestinationSize()));
70 }
71
72 private void UpdateGlControlBounds()
73 {
74 var rect = DisplayRectangle;
75 _glControl.SetBounds(
76 rect.X,
77 rect.Y,
78 Math.Max(1, rect.Width),
79 Math.Max(1, rect.Height));
80 }
81
82 private void InitializeBackbuffer()
83 {
84 _adapter = new WinFormGpuRenderSurfaceAdapter(_glControl, this);
85 Host = new RenderSurfaceHost<GpuBackbuffer>(_adapter);
86
87 // Register the host so the adapter drives all rendering on the GL thread.
88 _adapter.SetHost(Host);
89 }
90
95 protected override void Dispose(bool disposing)
96 {
97 if (disposing && (components != null))
98 {
99 components.Dispose();
100 Host?.Dispose();
101 _adapter?.Dispose();
102 }
103
104 base.Dispose(disposing);
105 }
106}
Provides a GPU-accelerated render surface adapter for Windows Forms using OpenGL and SKGLControl.
void SetHost(RenderSurfaceHostBase host)
Registers the RenderSurfaceHostBase whose scene this adapter should render and wires the GL paint loo...
RenderSurfaceHost< GpuBackbuffer > Host
Gets the render surface host used for displaying game content.
override void Dispose(bool disposing)
Clean up any resources being used.
WinFormGpuRenderSurfaceAdapter Adapter
Gets the GPU render surface adapter used by this control.
override void OnParentChanged(EventArgs e)
Refreshes the render surface size when the control's parent changes.
WinFormGpuRenderSurfaceControl()
Initializes a new instance of the WinFormGpuRenderSurfaceControl class.