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
DirectVideo.cs
1using System.Drawing;
7using SkiaSharp;
8
10
17{
18 private readonly IVideoPlayer _player;
19 private readonly VideoFramePresenter _presenter = new();
20 private bool _videoDisposed;
21 private StretchMode _stretch = StretchMode.Fill;
22
23 // ---- knobs ----
24
39 {
40 get => _stretch;
41 set
42 {
43 if (_stretch != value)
44 {
45 _stretch = value;
46 ForceRefresh();
47 }
48 }
49 }
50
51 private double _playbackRate = 1.0;
52
72 public double PlaybackRate
73 {
74 get => _playbackRate;
75 set
76 {
77 _player.SetRate(value);
78 _playbackRate = value;
79 }
80 }
81
91 public bool Loop
92 {
93 get => _player.Loop;
94 set => _player.Loop = value;
95 }
96
105 IVideoPlayer player,
106 Uri source,
107 RenderSurfaceHostBase renderSurfaceHost,
108 SceneLayer sceneLayer,
109 Rectangle worldBounds,
110 string? name = null)
111 : this(
112 player,
113 VideoSource.FromUri(source),
114 renderSurfaceHost,
115 sceneLayer,
116 worldBounds,
117 name)
118 {
119 }
120
129 IVideoPlayer player,
130 Uri source,
131 RenderSurfaceHostBase renderSurfaceHost,
132 View view,
133 Rectangle screenBounds,
134 string? name = null)
135 : this(
136 player,
137 VideoSource.FromUri(source),
138 renderSurfaceHost,
139 view,
140 screenBounds,
141 name)
142 {
143 }
144
153 Func<IVideoPlayer> playerFactory,
154 Uri source,
155 RenderSurfaceHostBase renderSurfaceHost,
156 SceneLayer sceneLayer,
157 Rectangle worldBounds,
158 string? name = null)
159 : this(
160 playerFactory,
161 VideoSource.FromUri(source),
162 renderSurfaceHost,
163 sceneLayer,
164 worldBounds,
165 name)
166 {
167 }
168
177 Func<IVideoPlayer> playerFactory,
178 Uri source,
179 RenderSurfaceHostBase renderSurfaceHost,
180 View view,
181 Rectangle screenBounds,
182 string? name = null)
183 : this(
184 playerFactory,
185 VideoSource.FromUri(source),
186 renderSurfaceHost,
187 view,
188 screenBounds,
189 name)
190 {
191 }
192
201 IVideoPlayer player,
202 VideoSource source,
203 RenderSurfaceHostBase renderSurfaceHost,
204 SceneLayer sceneLayer,
205 Rectangle worldBounds,
206 string? name = null)
207 : this(
208 () => player,
209 source,
210 renderSurfaceHost,
212 sceneLayer,
213 null,
214 null,
215 worldBounds,
216 name)
217 {
218 }
219
228 IVideoPlayer player,
229 VideoSource source,
230 RenderSurfaceHostBase renderSurfaceHost,
231 View view,
232 Rectangle screenBounds,
233 string? name = null)
234 : this(
235 () => player,
236 source,
237 renderSurfaceHost,
239 null,
240 view,
241 screenBounds,
242 null,
243 name)
244 {
245 }
246
255 Func<IVideoPlayer> playerFactory,
256 VideoSource source,
257 RenderSurfaceHostBase renderSurfaceHost,
258 SceneLayer sceneLayer,
259 Rectangle worldBounds,
260 string? name = null)
261 : this(
262 playerFactory,
263 source,
264 renderSurfaceHost,
266 sceneLayer,
267 null,
268 null,
269 worldBounds,
270 name)
271 {
272 }
273
282 Func<IVideoPlayer> playerFactory,
283 VideoSource source,
284 RenderSurfaceHostBase renderSurfaceHost,
285 View view,
286 Rectangle screenBounds,
287 string? name = null)
288 : this(
289 playerFactory,
290 source,
291 renderSurfaceHost,
293 null,
294 view,
295 screenBounds,
296 null,
297 name)
298 {
299 }
300
301 private DirectVideo(
302 Func<IVideoPlayer> playerFactory,
303 VideoSource source,
304 RenderSurfaceHostBase renderSurfaceHost,
306 SceneLayer? sceneLayer,
307 View? view,
308 Rectangle? screenBounds,
309 Rectangle? worldBounds,
310 string? name)
311 : base(
312 renderSurfaceHost,
313 mode,
314 sceneLayer,
315 view,
316 screenBounds,
317 worldBounds,
318 name)
319 {
320 try
321 {
322 ArgumentNullException.ThrowIfNull(playerFactory);
323 ArgumentNullException.ThrowIfNull(source);
324
325 _player = playerFactory() ?? throw new ArgumentException(
326 "The factory returned no video player.",
327 nameof(playerFactory));
328
329 HookPlayer();
330 source.Open(_player);
331 _player.Play();
332 }
333 catch
334 {
335 _presenter.Dispose();
336
337 if (_player is not null)
338 {
339 _player.FrameReady -= OnFrameReady;
340 _player.StateChanged -= OnPlayerStateChanged;
341 _player.Dispose();
342 }
343
344 base.Dispose(true); // Construction registered this drawing; undo on failure.
345 throw;
346 }
347 }
348
351 public void Open(VideoSource source)
352 {
353 ObjectDisposedException.ThrowIf(_videoDisposed, this);
354 ArgumentNullException.ThrowIfNull(source);
355 source.Open(_player);
356 _presenter.Update(); // Clear the prior source immediately on this engine thread.
357 ForceRefresh();
358 _player.Play();
359 }
360
363 public void Open(Uri source) => Open(VideoSource.FromUri(source));
364
366 public VideoMetadata Metadata => _player.Metadata;
367
369 public (int width, int height) NaturalSize => _player.NaturalSize;
370
372 public bool CanControlAudio => _player.CanControlAudio;
373
375 public float Volume
376 {
377 get => _player.Volume;
378 set => _player.Volume = value;
379 }
380
382 public bool IsMuted
383 {
384 get => _player.IsMuted;
385 set => _player.IsMuted = value;
386 }
387
391 private void HookPlayer()
392 {
393 _player.FrameReady += OnFrameReady;
394 _player.StateChanged += OnPlayerStateChanged;
395 }
396
402 protected override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
403 {
404 // DirectDrawingBase.Draw applies opacity and fades once.
405 _presenter.Draw(backbuffer.Canvas, destRectScreen, Stretch);
406 }
407
408 internal static SKRect ComputeDestRect(
409 RectangleF bounds,
410 int srcW,
411 int srcH,
412 StretchMode mode) => VideoFramePresenter.ComputeDestRect(
413 bounds,
414 srcW,
415 srcH,
416 mode);
417
418 // The event pointer is valid only during the callback; copy before returning.
419 private void OnFrameReady(object? sender, VideoFrameReadyEventArgs e) => _presenter.Publish(e);
420
421 private void OnPlayerStateChanged(object? sender, VideoStateChangedEventArgs e)
422 {
423 if (e.State is "MediaOpening" or "MediaOpened")
424 _presenter.Reset();
425 }
426
429 public override void Update(long tick)
430 {
431 if (_videoDisposed)
432 return;
433
434 if (_presenter.Update())
435 ForceRefresh();
436
437 base.Update(tick);
438 }
439 // --- control surface (thin passthrough to player) ---
440
449 public void Play() => _player.Play();
450
458 public void Pause() => _player.Pause();
459
467 public void Stop() => _player.Stop();
468
494 public void Seek(TimeSpan position) => _player.Seek(position);
495
496 // --- cleanup ---
497
502 protected override void Dispose(bool disposing)
503 {
504 if (!disposing)
505 {
506 base.Dispose(false);
507 return;
508 }
509
510 if (_videoDisposed)
511 return;
512
513 _videoDisposed = true;
514 _presenter.Dispose();
515 _player.FrameReady -= OnFrameReady;
516 _player.StateChanged -= OnPlayerStateChanged;
517
518 try
519 {
520 _player.Dispose();
521 }
522 finally
523 {
524 base.Dispose(true);
525 }
526 }
527}
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...
double PlaybackRate
Gets or sets the playback speed multiplier.
DirectVideo(Func< IVideoPlayer > playerFactory, Uri source, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? name=null)
Creates a screen-space video using an owned player from the factory.
bool IsMuted
Mutes audio without changing the retained volume.
void Open(Uri source)
Replaces the URI source and starts playback on the engine/UI thread.
DirectVideo(Func< IVideoPlayer > playerFactory, VideoSource source, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? name=null)
Creates a world-space URI/stream/GAF video using an owned player from the factory.
DirectVideo(IVideoPlayer player, VideoSource source, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? name=null)
Creates a screen-space URI/stream/GAF video and starts playback. Owns the player.
int width
Decoded source dimensions, or (0,0) until known.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the DirectVideo and optionally releases the managed resource...
DirectVideo(IVideoPlayer player, Uri source, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? name=null)
Creates a screen-space video with required presentation bounds. Owns the player.
bool CanControlAudio
Whether the backend supports volume and mute controls.
DirectVideo(IVideoPlayer player, Uri source, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? name=null)
Creates a world-space video with required presentation bounds. Owns the player.
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Renders the current video frame to the backbuffer at the specified destination rectangle.
DirectVideo(Func< IVideoPlayer > playerFactory, VideoSource source, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? name=null)
Creates a screen-space URI/stream/GAF video using an owned player from the factory.
DirectVideo(IVideoPlayer player, VideoSource source, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? name=null)
Creates a world-space URI/stream/GAF video and starts playback. Owns the player.
void Stop()
Stops video playback and resets the playback position to the beginning.
void Pause()
Pauses video playback at the current position.
void Seek(TimeSpan position)
Seeks to a specific position in the video timeline.
void Play()
Starts or resumes video playback.
DirectVideo(Func< IVideoPlayer > playerFactory, Uri source, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? name=null)
Creates a world-space video using an owned player from the factory.
void Open(VideoSource source)
Replaces the source and starts playback. Call on the engine/UI thread.
override void Update(long tick)
Consumes the latest decoded frame on the engine thread, then advances fades.
bool Loop
Gets or sets a value indicating whether the video should automatically restart from the beginning whe...
StretchMode Stretch
Gets or sets the stretch mode that determines how the video frame is scaled to fit within the destina...
VideoMetadata Metadata
Metadata snapshot for the active source; Pending values are not authoritative.
float Volume
Audible output gain from zero to one.
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 ...
Reusable latest-frame presentation for engine drawings and desktop previews.
void Reset()
Clears queued frames when the source changes; Update clears the displayed frame.
void Dispose()
Closes the callback mailbox and releases presentation-owned pixels.
Provides data for video frame ready events, containing frame buffer information and timing data.
A URI, borrowed/owned stream, or GAF asset to open with a video backend.
Definition VideoSource.cs:7
static VideoSource FromUri(Uri source)
Creates a reusable URI source.
Provides data for video state changed events.
string State
Gets the current state of the video player.
Defines the contract for a video player that can play, pause, stop, and seek video content.
void Play()
Starts or resumes playback of the video.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.
StretchMode
Defines how video content should be stretched or scaled to fit within its display bounds.
Definition StretchMode.cs:7
record VideoMetadata(VideoMetadataStatus Status, bool HasAudio, TimeSpan Duration)
An immutable metadata snapshot. Values are authoritative only when status is Ready.