How does drawing work?
When SadConsole draws a dirty surface, it does not draw it directly to the screen. Each Surface has a
LastRenderResult property that represents the last drawn state of the surface. All of these surfaces are gathered together and drawn on a final texture,
Global.RenderOutput. This texture is then drawn to the screen.
As stated above, each surface creates a texture that represents the final look of the console-surface. A
SadConsole.Renderers.ISurfaceRenderer object is responsible for this. When the renderer processes a surface, it first checks if the
IsDirty property is set to
true. If so, it updates the cached texture with the latest state of the surface.
This is efficient because there is less to render to the screen each frame. A 100x100 console has about 20,000 sprite-draws each frame. If a surface doesn’t change very often, this is a lot of wasted time for your GPU. By caching each draw to a texture, a single draw call is required each frame instead of all 20,000 individual draws.
Now that we know how surfaces are predrawn, let’s break down the code used to draw SadConsole. SadConsole is implemented as a MonoGame component,
SadConsole.Game.SadConsoleGameComponent, here is the
public override void Draw(GameTime gameTime)
The above code resets all of the previous frame’s draw calls. The current screen has
Draw called on it which should build a new list of draw calls. A callback is then invoked.
// Render to the global output texture
The above code tells the graphics device to draw to the final rendered texture. Each draw call is then then run. Afterwards, the graphics device is reset to draw to the screen.
GraphicsDevice.Viewport = oldViewPort;
Finally, if the
true, the final render texture is then drawn to the screen. Why does this
DoFinalDraw gate exist? This allows you to integrate SadConsole into an existing game. For example, if you had a 3D FPS where the player can walk up to and use a computer in the game, you can use SadConsole to process and draw. Then that final texture created by SadConsole could be mapped to the 3D computer object.