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
ImageInstanceLayer.cs
1using System.Drawing;
8using SkiaSharp;
9
11
108{
109 private readonly Random _rng = new();
110
111 private readonly SKPaint _paint = new()
112 {
113 IsAntialias = true,
114 BlendMode = SKBlendMode.SrcOver
115 };
116
117 private long _instanceLastTick;
118
122 public List<ImageInstance> Instances { get; } = new();
123
161 public Func<Rectangle, Random, IEnumerable<ImageInstance>>? Initializer { get; set; }
162
172 public Func<ImageInstance, Rectangle, bool>? ShouldRecycle { get; set; }
173
202 public Func<ImageInstance, Rectangle, Random, ImageInstance>? RecycleInstance { get; set; }
203
226 public Action<ImageInstance, float>? UpdateInstance { get; set; }
227
236 RenderSurfaceHostBase renderSurfaceHost,
237 View view,
238 Rectangle screenBounds,
239 string? nickname = null)
240 : base(
241 renderSurfaceHost,
243 sceneLayer: null,
244 view: view,
245 screenBounds: screenBounds,
246 worldBounds: null,
247 name: nickname)
248 {
249 }
250
259 RenderSurfaceHostBase renderSurfaceHost,
260 SceneLayer sceneLayer,
261 Rectangle worldBounds,
262 string? nickname = null)
263 : base(
264 renderSurfaceHost,
266 sceneLayer: sceneLayer,
267 view: null,
268 screenBounds: null,
269 worldBounds: worldBounds,
270 name: nickname)
271 {
272 }
273
286 RenderSurfaceHostBase renderSurfaceHost,
287 View view,
288 Rectangle screenBounds,
289 Func<Rectangle, Random, IEnumerable<ImageInstance>>? initializer,
290 Func<ImageInstance, Rectangle, bool>? shouldRecycle = null,
291 Func<ImageInstance, Rectangle, Random, ImageInstance>? recycleInstance = null,
292 Action<ImageInstance, float>? updateInstance = null,
293 string? nickname = null)
294 : this(
295 renderSurfaceHost,
296 view,
297 screenBounds,
298 nickname)
299 {
300 Initializer = initializer;
301 ShouldRecycle = shouldRecycle;
302 RecycleInstance = recycleInstance;
303 UpdateInstance = updateInstance;
304
306 }
307
321 RenderSurfaceHostBase renderSurfaceHost,
322 SceneLayer sceneLayer,
323 Rectangle worldBounds,
324 Func<Rectangle, Random, IEnumerable<ImageInstance>>? initializer,
325 Func<ImageInstance, Rectangle, bool>? shouldRecycle = null,
326 Func<ImageInstance, Rectangle, Random, ImageInstance>? recycleInstance = null,
327 Action<ImageInstance, float>? updateInstance = null,
328 string? nickname = null)
329 : this(
330 renderSurfaceHost,
331 sceneLayer,
332 worldBounds,
333 nickname)
334 {
335 Initializer = initializer;
336 ShouldRecycle = shouldRecycle;
337 RecycleInstance = recycleInstance;
338 UpdateInstance = updateInstance;
339
341 }
342
347 {
348 Instances.Clear();
349
350 var bounds = Mode == DirectDrawingMode.SceneLayer
352 : ScreenBounds;
353
354 if (Initializer is not null)
355 Instances.AddRange(Initializer(bounds, _rng));
356
357 ForceRefresh();
358 }
359
365 public override void Update(long tick)
366 {
367 if (tick <= _lastTick)
368 return;
369
370 float dt = 0f;
371
372 if (_instanceLastTick > 0)
373 dt = HighResTimer.GetDuration(_instanceLastTick, tick);
374
375 _instanceLastTick = tick;
376
377 if (dt <= 0f)
378 {
379 base.Update(tick);
380 return;
381 }
382
383 var bounds = Mode == DirectDrawingMode.SceneLayer
385 : ScreenBounds;
386
387 for (int i = 0; i < Instances.Count; i++)
388 {
389 var instance = Instances[i];
390 var oldRefreshBounds = GetRefreshBounds(instance);
391
392 instance.Bounds = new RectangleF(
393 instance.Bounds.X + (instance.VelocityX * dt),
394 instance.Bounds.Y + (instance.VelocityY * dt),
395 instance.Bounds.Width,
396 instance.Bounds.Height);
397
398 instance.Rotation += instance.AngularVelocity * dt;
399
400 UpdateInstance?.Invoke(instance, dt);
401
402 if (ShouldRecycle is not null &&
403 RecycleInstance is not null &&
404 ShouldRecycle(instance, bounds))
405 {
406 var recycled = RecycleInstance(instance, bounds, _rng);
407 Instances[i] = recycled;
408
409 AddRefreshRect(oldRefreshBounds);
410 AddRefreshRect(GetRefreshBounds(recycled));
411 }
412 else
413 {
414 AddRefreshRect(oldRefreshBounds);
415 AddRefreshRect(GetRefreshBounds(instance));
416 }
417 }
418
419 base.Update(tick);
420 }
421
427 protected override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
428 {
429 var canvas = backbuffer.Canvas;
430
431 Rectangle srcBounds = Mode == DirectDrawingMode.SceneLayer
433 : ScreenBounds;
434
435 float sx = srcBounds.Width > 0
436 ? destRectScreen.Width / srcBounds.Width
437 : 1f;
438
439 float sy = srcBounds.Height > 0
440 ? destRectScreen.Height / srcBounds.Height
441 : 1f;
442
443 canvas.Save();
444 canvas.ClipRect(destRectScreen.ToSKRect());
445
446 foreach (var instance in Instances)
447 {
448 _paint.Color = instance.Tint;
449
450 var dst = new SKRect(
451 destRectScreen.Left + ((instance.Bounds.Left - srcBounds.Left) * sx),
452 destRectScreen.Top + ((instance.Bounds.Top - srcBounds.Top) * sy),
453 destRectScreen.Left + ((instance.Bounds.Right - srcBounds.Left) * sx),
454 destRectScreen.Top + ((instance.Bounds.Bottom - srcBounds.Top) * sy));
455
456 if (instance.Rotation != 0f)
457 {
458 float cx = (dst.Left + dst.Right) * 0.5f;
459 float cy = (dst.Top + dst.Bottom) * 0.5f;
460
461 canvas.Save();
462 canvas.RotateDegrees(instance.Rotation, cx, cy);
463 canvas.DrawBitmap(instance.Bitmap, dst, _paint);
464 canvas.Restore();
465 }
466 else
467 {
468 canvas.DrawBitmap(instance.Bitmap, dst, _paint);
469 }
470 }
471
472 canvas.Restore();
473 }
474
479 protected override void Dispose(bool disposing)
480 {
481 if (disposing)
482 _paint.Dispose();
483
484 base.Dispose(disposing);
485 }
486
491 private void AddRefreshRect(RectangleF rect)
492 {
493 const float pad = 4f;
494
495 var expanded = Rectangle.Ceiling(RectangleF.Inflate(rect, pad, pad));
496
497 if (Mode == DirectDrawingMode.SceneLayer)
498 {
499 SceneLayer!.RefreshQueue.AddWorldRect(expanded);
500 return;
501 }
502
503 foreach (var sceneLayer in RenderSurfaceHost.Scene.SceneLayers)
504 sceneLayer.RefreshQueue.AddViewScreenRect(View!, sceneLayer, expanded);
505 }
506
512 private static RectangleF GetRefreshBounds(ImageInstance instance)
513 {
514 if (instance.Rotation == 0f && instance.AngularVelocity == 0f)
515 return instance.Bounds;
516
517 float cx = instance.Bounds.Left + (instance.Bounds.Width * 0.5f);
518 float cy = instance.Bounds.Top + (instance.Bounds.Height * 0.5f);
519
520 float halfWidth = instance.Bounds.Width * 0.5f;
521 float halfHeight = instance.Bounds.Height * 0.5f;
522 float halfDiagonal = MathF.Sqrt((halfWidth * halfWidth) + (halfHeight * halfHeight));
523
524 return new RectangleF(
525 cx - halfDiagonal,
526 cy - halfDiagonal,
527 halfDiagonal * 2f,
528 halfDiagonal * 2f);
529 }
530}
View? View
Gets the view to which this direct drawing is attached, or null if the drawing is in scene-layer mode...
long _lastTick
The last engine tick value used for delta-time calculations in Update.
RenderSurfaceHostBase RenderSurfaceHost
Gets the render surface host that manages this direct drawing's rendering pipeline.
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
Rectangle WorldBounds
Gets or sets the world-space bounds of this direct drawing in pixels.
DirectDrawingMode Mode
Gets the drawing mode that determines how this direct drawing is positioned and transformed.
Rectangle ScreenBounds
Gets or sets the screen-space bounds of this direct drawing in pixels.
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...
override void Dispose(bool disposing)
Disposes managed resources.
List< ImageInstance > Instances
Gets the collection of image instances managed by this layer.
override void Update(long tick)
Updates all instances, applying built-in motion, optional custom updates, and optional recycling beha...
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Renders all image instances to the backbuffer.
Action< ImageInstance, float >? UpdateInstance
Gets or sets an optional callback invoked after the built-in motion update for each instance.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, Func< Rectangle, Random, IEnumerable< ImageInstance > >? initializer, Func< ImageInstance, Rectangle, bool >? shouldRecycle=null, Func< ImageInstance, Rectangle, Random, ImageInstance >? recycleInstance=null, Action< ImageInstance, float >? updateInstance=null, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class and assigns optional hooks.
void InitializeInstances()
Clears the current instance collection and rebuilds it using Initializer, if one is assigned.
Func< ImageInstance, Rectangle, bool >? ShouldRecycle
Gets or sets an optional callback used to determine whether an instance should be recycled.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle screenBounds, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, Func< Rectangle, Random, IEnumerable< ImageInstance > >? initializer, Func< ImageInstance, Rectangle, bool >? shouldRecycle=null, Func< ImageInstance, Rectangle, Random, ImageInstance >? recycleInstance=null, Action< ImageInstance, float >? updateInstance=null, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class for scene-layer rendering and assigns opti...
Func< ImageInstance, Rectangle, Random, ImageInstance >? RecycleInstance
Gets or sets an optional callback used to create a replacement instance when recycling occurs.
ImageInstanceLayer(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle worldBounds, string? nickname=null)
Initializes a new instance of the ImageInstanceLayer class for scene-layer rendering.
Func< Rectangle, Random, IEnumerable< ImageInstance > >? Initializer
Gets or sets an optional callback used to populate the initial set of instances.
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 ...
Scene Scene
Gets the source Scenes.Scene used for rendering operations.
ReadOnlyCollection< SceneLayer > SceneLayers
Gets a read-only collection of all layers in this scene, in their current order.
Definition Scene.cs:265
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.