Class RenderTargetComponent
- Namespace
- BSGameLib.Game.Components
- Assembly
- BSGameLib.Game.dll
A component that draws itself into its own IRenderTarget, then presents that target as a sprite in its parent's render pass. Subclasses implement DrawSelf(IGameTime).
public abstract class RenderTargetComponent : DrawableComponent, IUpdateable, IEquatable<IUpdateable>, IDrawable, IEquatable<IDrawable>, IInitializable, IIdentifiable<Guid>, IEquatable<IInitializable>, IDisposable
- Inheritance
-
RenderTargetComponent
- Implements
- Inherited Members
Remarks
The target is recreated when the window is resized, and whenever its size no longer matches GetTargetSize(), so a subclass does not handle resizing.
Constructors
RenderTargetComponent(ILogger<RenderTargetComponent>, IContentManager, IRenderTargetAllocator, IGameWindow, IRenderContextFactory)
Creates the component. It creates its render target when Initialize() runs.
protected RenderTargetComponent(ILogger<RenderTargetComponent> logger, IContentManager contentManager, IRenderTargetAllocator renderTargetAllocator, IGameWindow gameWindow, IRenderContextFactory renderContextFactory)
Parameters
loggerILogger<RenderTargetComponent>contentManagerIContentManagerrenderTargetAllocatorIRenderTargetAllocatorgameWindowIGameWindowrenderContextFactoryIRenderContextFactory
RenderTargetComponent(ILogger<RenderTargetComponent>, IContentManager, IRenderTargetAllocator, IGameWindow, IRenderContextFactory, IRenderTarget)
Creates the component with renderTarget as its target, which Initialize() keeps rather than creating one.
protected RenderTargetComponent(ILogger<RenderTargetComponent> logger, IContentManager contentManager, IRenderTargetAllocator renderTargetAllocator, IGameWindow gameWindow, IRenderContextFactory renderContextFactory, IRenderTarget renderTarget)
Parameters
loggerILogger<RenderTargetComponent>contentManagerIContentManagerrenderTargetAllocatorIRenderTargetAllocatorgameWindowIGameWindowrenderContextFactoryIRenderContextFactoryrenderTargetIRenderTarget
Exceptions
- ArgumentNullException
renderTargetAllocatororgameWindowis null.
Properties
IsInitialized
Whether Initialize() has run.
public bool IsInitialized { get; }
Property Value
RenderTarget
The target this component draws into. Null until one is created. Setting it does not dispose the previous target.
public IRenderTarget RenderTarget { get; set; }
Property Value
Methods
BeginDraw()
Does nothing. The target is drawn in Draw(IGameTime).
public override sealed void BeginDraw()
CreateRenderTarget()
Creates a render target at GetTargetSize(), with each side at least one pixel.
protected virtual IRenderTarget CreateRenderTarget()
Returns
Dispose()
Unloads the component's content and disposes its render target.
public void Dispose()
Dispose(bool)
Stops following the window's resizes, unloads the component's content and disposes the render target. Called by Dispose().
protected virtual void Dispose(bool isDisposing)
Parameters
isDisposingboolNot checked; the same work is done either way.
Draw(IGameTime)
Draws DrawSelf(IGameTime) into the render target in its own render pass, then calls PostDraw() and presents the target with PresentToParent(Rectangle?, Color?). The target is first recreated if its size has changed.
public override sealed void Draw(IGameTime gameTime)
Parameters
gameTimeIGameTime
Exceptions
- InvalidOperationException
No render context factory was given to the constructor.
DrawSelf(IGameTime)
Draws the component's content into the render target. Called inside the target's render pass.
protected abstract void DrawSelf(IGameTime gameTime)
Parameters
gameTimeIGameTime
EndDraw()
Does nothing. The work is done in Draw(IGameTime).
public override sealed void EndDraw()
Equals(RenderTargetComponent)
Whether other has the same Id as this component.
protected bool Equals(RenderTargetComponent other)
Parameters
otherRenderTargetComponent
Returns
Equals(IInitializable)
Whether other has the same Id as this component. False when other is null.
public bool Equals(IInitializable other)
Parameters
otherIInitializable
Returns
Equals(object)
Whether obj is a render target component of the same type with the same Id.
public override bool Equals(object obj)
Parameters
objobject
Returns
GetHashCode()
Returns a hash code based on Id, consistent with Equals(object).
public override int GetHashCode()
Returns
GetTargetSize()
The size of the render target, in pixels. By default the window's Size, truncated to whole pixels. It can be zero; the target is then made one pixel on each side that is zero.
protected virtual (int Width, int Height) GetTargetSize()
Returns
Initialize()
Creates the render target at GetTargetSize() if there is none, and subscribes to the window's resize event so the target is recreated on resize. Does nothing after the first call.
public virtual void Initialize()
PostDraw()
Called after the render pass that contains DrawSelf(IGameTime) has ended, before the target is presented. Does nothing by default.
protected virtual void PostDraw()
PresentToParent(Rectangle?, Color?)
Draws the render target as a sprite in the parent's render context. Does nothing when there is no render target.
public virtual void PresentToParent(Rectangle? destination = null, Color? tint = null)
Parameters
destinationRectangle?Where the sprite is drawn. The whole target at (0, 0) at its own size when null.
tintColor?The colour the sprite is drawn in. White, which applies no tint, when null.
Exceptions
- InvalidOperationException
No render context factory was given to the constructor.
Operators
operator ==(RenderTargetComponent, RenderTargetComponent)
Whether left and right have the same Id. Two nulls are equal.
public static bool operator ==(RenderTargetComponent left, RenderTargetComponent right)
Parameters
leftRenderTargetComponentrightRenderTargetComponent
Returns
operator ==(RenderTargetComponent, IInitializable)
Whether left and right have the same Id. Two nulls are equal.
public static bool operator ==(RenderTargetComponent left, IInitializable right)
Parameters
leftRenderTargetComponentrightIInitializable
Returns
operator !=(RenderTargetComponent, RenderTargetComponent)
Whether left and right have different Id values.
public static bool operator !=(RenderTargetComponent left, RenderTargetComponent right)
Parameters
leftRenderTargetComponentrightRenderTargetComponent
Returns
operator !=(RenderTargetComponent, IInitializable)
Whether left and right have different Id values.
public static bool operator !=(RenderTargetComponent left, IInitializable right)
Parameters
leftRenderTargetComponentrightIInitializable