Table of Contents

Class Scene2D

Namespace
LibGFX.Core
Assembly
LibGFX.dll

Represents a 2D scene

public class Scene2D : BaseScene, IIdentifier, ISerialization
Inheritance
Scene2D
Implements
Inherited Members

Constructors

Scene2D()

Creates a new 2D scene

public Scene2D()

Scene2D(params string[])

Creates a new 2D scene with the given layers

public Scene2D(params string[] layers)

Parameters

layers string[]

Properties

Layers

Gets or sets the collection of layers contained in the model.

public List<Layer> Layers { get; set; }

Property Value

List<Layer>

Remarks

Layers are typically processed in the order they appear in the collection. Modifying this list will affect the structure and behavior of the model.

LightManager

The light manager of the scene

public override ILightManager LightManager { get; }

Property Value

ILightManager

RenderTarget

Gets the render target associated with this instance.

public override IRenderTarget RenderTarget { get; }

Property Value

IRenderTarget

SceneLight

Sets the main light manager for the scene

public DirectionalLight2D SceneLight { get; set; }

Property Value

DirectionalLight2D

Methods

AddEnqueEntry(IEnqueEntry)

Adds an enque entry to the collection if it is of type EnqueScene2DEntry.

public override void AddEnqueEntry(IEnqueEntry entry)

Parameters

entry IEnqueEntry

The enque entry to add. Must be an instance of EnqueScene2DEntry.

Remarks

Only entries of type EnqueScene2DEntry are supported by this method. Attempting to add an entry of a different type will result in an exception.

Exceptions

ArgumentException

Thrown if entry is not of type EnqueScene2DEntry.

AddGameElement(GameElement)

Adds a game element to the first layer of the collection, creating a default layer if none exist.

public override void AddGameElement(GameElement element)

Parameters

element GameElement

The game element to add to the collection. If null, the method performs no action.

Remarks

If no layers are present, a new layer named "Default" is created before adding the element. The element is always added to the first layer in the collection.

AddGameElement(string, GameElement)

Attempts to add a game element to the specified layer.

public bool AddGameElement(string layerName, GameElement element)

Parameters

layerName string

The name of the layer to which the game element will be added. Cannot be null or empty.

element GameElement

The game element to add to the layer. Cannot be null.

Returns

bool

true if the element was successfully added to the specified layer; otherwise, false.

AddLight<T>(T)

Adds a light to the 2D scene. Supports adding either a directional light or a point light.

public override void AddLight<T>(T light) where T : Light

Parameters

light T

The light instance to add to the scene. Must be of type DirectionalLight2D or PointLight2D.

Type Parameters

T

The type of the light to add. Must be either DirectionalLight2D or PointLight2D.

Remarks

If a DirectionalLight2D is provided, it replaces the current directional light in the scene. If a PointLight2D is provided, it is added to the collection of point lights managed by the scene.

Exceptions

ArgumentException

Thrown if light is not of type DirectionalLight2D or PointLight2D.

ClearElements()

Removes all elements from the collection.

public override void ClearElements()

CreateDefaultScene()

Creates a new 2D scene with default settings.

public static Scene2D CreateDefaultScene()

Returns

Scene2D

A Scene2D instance initialized with default parameters.

Deserialize(JObject, SerializationContext, Func<JObject, bool>)

Deserializes the scene from a JSON object

public override void Deserialize(JObject obj, SerializationContext serializationContext, Func<JObject, bool> callback = null)

Parameters

obj JObject
serializationContext SerializationContext
callback Func<JObject, bool>

DisposeScene(IRenderDevice)

Disposes the scene

public override void DisposeScene(IRenderDevice renderer)

Parameters

renderer IRenderDevice

EnqueElements()

Adds all queued 2D scene elements to their designated layers and triggers any associated actions before insertion.

public override void EnqueElements()

Remarks

This method processes each entry in the queue, invoking any specified actions prior to adding the element to the scene. After all elements have been enqueued, the queue is cleared. Elements with a null reference are skipped and not added to the scene.

FindElement<T>(string)

Searches all layers for a game element with the specified name and returns the first match found.

public override T? FindElement<T>(string name) where T : GameElement

Parameters

name string

The name of the game element to locate. The search is case-sensitive and matches the element's name exactly.

Returns

T

The first GameElement with the specified name, or null if no matching element is found.

Type Parameters

T

Remarks

The search is performed in the order of the layers as they appear in the collection. If multiple elements share the same name across different layers, only the first match is returned.

FindElementsWithBehavior<T>()

Finds all game elements that implement the specified behavior type.

public override ICollection<GameElement> FindElementsWithBehavior<T>() where T : IGameBehavior

Returns

ICollection<GameElement>

A collection of game elements that implement the specified behavior type. The collection is empty if no elements with the behavior are found.

Type Parameters

T

The type of behavior to search for. Must be implemented by the game elements to be included in the results.

FindElementsWithTag(string)

Finds all game elements that have the specified tag across all layers.

public override ICollection<GameElement> FindElementsWithTag(string tag)

Parameters

tag string

The tag to search for. Only elements with this tag will be included in the results. Cannot be null.

Returns

ICollection<GameElement>

A collection of game elements that have the specified tag. The collection is empty if no elements with the tag are found.

Remarks

This method searches all layers in parallel to improve performance when working with a large number of layers. The returned collection is not thread-safe.

FindLayer(string)

Searches for a layer with the specified name and returns the first matching layer, if found.

public Layer? FindLayer(string name)

Parameters

name string

The name of the layer to locate. The comparison is case-sensitive.

Returns

Layer

The first Layer whose Name property matches the specified name, or null if no such layer exists.

ForEachElement(Action<GameElement>)

Invokes the specified action for each game element contained in all layers.

public override void ForEachElement(Action<GameElement> action)

Parameters

action Action<GameElement>

The action to perform on each GameElement. Cannot be null.

Remarks

The action is invoked once for every element in every layer, in the order they are stored. If the collection of layers or elements is modified during iteration, the behavior is undefined.

FreeScene(IRenderDevice)

Releases all resources associated with the scene and its layers using the specified render device.

public override void FreeScene(IRenderDevice renderer)

Parameters

renderer IRenderDevice

The render device to use when releasing resources for each layer. Cannot be null.

Remarks

After calling this method, the scene's layers are cleared and should not be used further. This method should be called when the scene is no longer needed to ensure proper resource cleanup.

GetAllElements()

Returns a collection containing all game elements from all layers in the order they appear.

public override IEnumerable<GameElement> GetAllElements()

Returns

IEnumerable<GameElement>

An enumerable collection of all GameElement instances contained in every layer. The collection will be empty if there are no elements in any layer.

GetElementByID(string)

Retrieves the first game element with the specified unique identifier from all layers.

public override GameElement? GetElementByID(string uuid)

Parameters

uuid string

The unique identifier of the game element to locate. Cannot be null.

Returns

GameElement

The first GameElement with the specified identifier, or null if no matching element is found.

Remarks

Searches each layer in order and returns the first matching element. If multiple elements share the same identifier across layers, only the first encountered is returned.

GetElements<T>()

Retrieves all game elements of the specified type from all layers.

public override ICollection<GameElement> GetElements<T>() where T : GameElement

Returns

ICollection<GameElement>

A collection containing all elements of type T found in all layers. The collection is empty if no matching elements are found.

Type Parameters

T

The type of game elements to retrieve.

Remarks

This method searches each layer in parallel and aggregates the results. The returned collection is not thread-safe.

GetLight<T>()

Retrieves the directional light for the 2D scene if the specified type is supported.

public override T GetLight<T>() where T : Light

Returns

T

The directional light instance cast to the specified type T.

Type Parameters

T

The type of light to retrieve. Must be DirectionalLight2D.

Remarks

Only DirectionalLight2D is supported for 2D scenes. Attempting to retrieve any other light type will result in an exception.

Exceptions

ArgumentException

Thrown if T is not DirectionalLight2D.

Init(Viewport, IRenderDevice)

Initializes the scene

public override void Init(Viewport viewport, IRenderDevice renderer)

Parameters

viewport Viewport
renderer IRenderDevice

InitializeElements(Viewport, IRenderDevice)

Initializes all elements in the collection using the specified viewport and render device.

public override void InitializeElements(Viewport viewport, IRenderDevice renderer)

Parameters

viewport Viewport

The viewport that defines the rendering context for the elements.

renderer IRenderDevice

The render device used to initialize the elements for drawing operations.

RemoveElement(GameElement)

Removes the specified game element from all layers in the collection.

public override void RemoveElement(GameElement element)

Parameters

element GameElement

The game element to remove from each layer. Cannot be null.

Remarks

This method attempts to remove the element from every layer in parallel. If the element does not exist in a layer, that layer is unaffected. This operation is thread-safe.

RemoveLight<T>(T)

Removes the specified light from the scene. Supports removal of directional and point lights.

public override void RemoveLight<T>(T light) where T : Light

Parameters

light T

The light instance to remove from the scene. Must be a DirectionalLight2D or PointLight2D object.

Type Parameters

T

The type of the light to remove. Must be either DirectionalLight2D or PointLight2D.

Remarks

If a DirectionalLight2D is specified, it will be unset from the scene. If a PointLight2D is specified, it will be removed from the collection of point lights. Other light types are not supported and will result in an exception.

Exceptions

ArgumentException

Thrown if light is not a DirectionalLight2D or PointLight2D.

Render(Viewport, IRenderDevice, Camera)

Renders the scene

public override void Render(Viewport viewport, IRenderDevice renderer, Camera camera)

Parameters

viewport Viewport
renderer IRenderDevice
camera Camera

RenderShadowMaps(Viewport, IRenderDevice, Camera)

Renders the shadow maps for the scene

public override void RenderShadowMaps(Viewport viewport, IRenderDevice renderer, Camera camera)

Parameters

viewport Viewport
renderer IRenderDevice
camera Camera

Serialize(JsonWriter, SerializationContext, Action<JsonWriter>)

Serializes the scene to a JSON object

public override void Serialize(JsonWriter writer, SerializationContext serializationContext, Action<JsonWriter> callback = null)

Parameters

writer JsonWriter
serializationContext SerializationContext
callback Action<JsonWriter>

Update(float)

Updates the scene

public override void Update(float dt)

Parameters

dt float

UpdatePhysics(float)

Updates the physics of the scene

public override void UpdatePhysics(float dt)

Parameters

dt float

Events

AfterLightCulling

Called after light culling has been performed

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> AfterLightCulling

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

AfterRenderTargetCreation

Called after the render target for the scene has been created

public override event Action<BaseScene, Viewport, IRenderDevice> AfterRenderTargetCreation

Event Type

Action<BaseScene, Viewport, IRenderDevice>

OnDispose

Called when the scene gets disposed

public override event Action<BaseScene, IRenderDevice> OnDispose

Event Type

Action<BaseScene, IRenderDevice>

OnDisposeEnd

Called after the scene has been disposed

public override event Action<BaseScene, IRenderDevice> OnDisposeEnd

Event Type

Action<BaseScene, IRenderDevice>

OnDisposeStart

Called before the scene is disposed

public override event Action<BaseScene, IRenderDevice> OnDisposeStart

Event Type

Action<BaseScene, IRenderDevice>

OnInitEnd

Called when the scene has been initialized

public override event Action<BaseScene, Viewport, IRenderDevice> OnInitEnd

Event Type

Action<BaseScene, Viewport, IRenderDevice>

OnInitStart

Called when the scene is initializing

public override event Action<BaseScene, Viewport, IRenderDevice> OnInitStart

Event Type

Action<BaseScene, Viewport, IRenderDevice>

OnPhysicsUpdateEnd

Called when the physics update ends

public override event Action<BaseScene, float> OnPhysicsUpdateEnd

Event Type

Action<BaseScene, float>

OnPhysicsUpdateStart

Called when the physics update starts

public override event Action<BaseScene, float> OnPhysicsUpdateStart

Event Type

Action<BaseScene, float>

OnRenderEnd

Called at the end of the render process

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> OnRenderEnd

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

OnRenderPassBegin

Called before the game elements get rendered

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> OnRenderPassBegin

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

OnRenderPassEnd

Called when all game elements have been rendered before the render target blitting

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> OnRenderPassEnd

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

OnRenderStart

Called immediately on the start of the render process

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> OnRenderStart

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

OnShadowPassEnd

Called when the shadow pass ends

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> OnShadowPassEnd

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

OnShadowPassStart

Called when the shadow pass starts

public override event Action<BaseScene, Viewport, IRenderDevice, Camera> OnShadowPassStart

Event Type

Action<BaseScene, Viewport, IRenderDevice, Camera>

OnUpdateEnd

Called when the scene has been updated

public override event Action<BaseScene, float> OnUpdateEnd

Event Type

Action<BaseScene, float>

OnUpdateStart

Called when the scene is updating

public override event Action<BaseScene, float> OnUpdateStart

Event Type

Action<BaseScene, float>