Show / Hide Table of Contents

Class GameHost

Represents the SadConsole game engine.

Inheritance
object
GameHost
Game
Implements
IDisposable
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: SadConsole
Assembly: SadConsole.dll
Syntax
public abstract class GameHost : IDisposable
Remarks

When a new host is created, the host should do the following:

  • Run LoadDefaultFonts.
  • Run SetRenderer for window, controls, layered, and default renderers.
  • Run LoadMappedColors.
  • Configure the Screen to a new console with ScreenCellsX and ScreenCellsY.
  • Prior to running the game, run SplashScreens.SplashScreenManager.CheckRun().

Constructors

GameHost()

Declaration
protected GameHost()

Fields

_gameStartedAt

The date and time the game was started.

Declaration
protected DateTime _gameStartedAt
Field Value
Type Description
DateTime

_rendererSteps

Holds all of the IRenderStep types.

Declaration
protected Dictionary<string, Type> _rendererSteps
Field Value
Type Description
Dictionary<string, Type>

_renderers

Holds all of the IRenderer types.

Declaration
protected Dictionary<string, Type> _renderers
Field Value
Type Description
Dictionary<string, Type>

Properties

DefaultFont

The default font for any type that does not provide a font.

Declaration
public IFont DefaultFont { get; set; }
Property Value
Type Description
IFont

DefaultFontSize

The default font size to use with the DefaultFont.

Declaration
public IFont.Sizes DefaultFontSize { get; set; }
Property Value
Type Description
IFont.Sizes

DrawCalls

Draw calls registered for the next drawing frame.

Declaration
public Queue<IDrawCall> DrawCalls { get; }
Property Value
Type Description
Queue<IDrawCall>

DrawFrameDelta

The elapsed time between now and the last draw call.

Declaration
public TimeSpan DrawFrameDelta { get; set; }
Property Value
Type Description
TimeSpan

EmbeddedFont

The font automatically loaded by SadConsole. Standard IBM style font.

Declaration
public SadFont EmbeddedFont { get; }
Property Value
Type Description
SadFont

EmbeddedFontExtended

The font automatically loaded by SadConsole. Standard IBM style font. Extended with extra SadConsole characters.

Declaration
public SadFont EmbeddedFontExtended { get; }
Property Value
Type Description
SadFont

FocusedScreenObjects

The stack of focused consoles used by the mouse and keyboard.

Declaration
public FocusedScreenObjectStack FocusedScreenObjects { get; set; }
Property Value
Type Description
FocusedScreenObjectStack

Fonts

Collection of fonts. Used mainly by the deserialization system.

Declaration
public Dictionary<string, IFont> Fonts { get; }
Property Value
Type Description
Dictionary<string, IFont>

FrameNumber

A frame number counter, incremented every game frame.

Declaration
public int FrameNumber { get; set; }
Property Value
Type Description
int

GameRunningTotalTime

The total time the game has been running.

Declaration
public TimeSpan GameRunningTotalTime { get; }
Property Value
Type Description
TimeSpan

Instance

Instance of the game host.

Declaration
public static GameHost Instance { get; protected set; }
Property Value
Type Description
GameHost

Keyboard

Global keyboard object used by SadConsole during the update frame.

Declaration
public Keyboard Keyboard { get; }
Property Value
Type Description
Keyboard

Mouse

Global mouse object used by SadConsole during the update frame.

Declaration
public Mouse Mouse { get; }
Property Value
Type Description
Mouse

Random

A global random number generator.

Declaration
public Random Random { get; set; }
Property Value
Type Description
Random

RootComponents

Update components that run before the Screen is processed.

Declaration
public List<RootComponent> RootComponents { get; set; }
Property Value
Type Description
List<RootComponent>

Screen

The active screen processed by the game.

Declaration
public IScreenObject? Screen { get; set; }
Property Value
Type Description
IScreenObject

ScreenCellsX

How many cells fit in the render area width used by SadConsole.

Declaration
public int ScreenCellsX { get; }
Property Value
Type Description
int

ScreenCellsY

How many cells fit in the render area width used by SadConsole.

Declaration
public int ScreenCellsY { get; }
Property Value
Type Description
int

SerializerPathHint

Contains the path to a file being serialized or deserialized.

Declaration
public static string SerializerPathHint { get; }
Property Value
Type Description
string

StartingConsole

The console created by the game and automatically assigned to Screen.

Declaration
public Console? StartingConsole { get; protected set; }
Property Value
Type Description
Console

UpdateFrameDelta

The elapsed time between now and the last update call.

Declaration
public TimeSpan UpdateFrameDelta { get; set; }
Property Value
Type Description
TimeSpan

_splashScreens

The splash screens to show on game startup.

Declaration
protected Queue<IScreenSurface>? _splashScreens { get; set; }
Property Value
Type Description
Queue<IScreenSurface>

Methods

CreateTexture(int, int)

Creates a texture.

Declaration
public abstract ITexture CreateTexture(int width, int height)
Parameters
Type Name Description
int width

The width of the texture in pixels.

int height

The height of the texture in pixels.

Returns
Type Description
ITexture

The texture from the game host.

DestroyDefaultStartingConsole()

Destroys the StartingConsole instance.

Declaration
public void DestroyDefaultStartingConsole()
Remarks

Prior to calling this method, you must set Screen to an object other than StartingConsole.

Dispose()

Disposes this object.

Declaration
public void Dispose()

Dispose(bool)

Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

FileDelete(string)

Deletes a file.

Declaration
public virtual bool FileDelete(string file)
Parameters
Type Name Description
string file

The file to delete.

Returns
Type Description
bool

true if the file was deleted; otherwise false.

FileExists(string)

Checks if a file exists.

Declaration
public virtual bool FileExists(string file)
Parameters
Type Name Description
string file

The file to check.

Returns
Type Description
bool

true if the file exists; otherwise false.

~GameHost()

Disposes this object.

Declaration
protected ~GameHost()

GetDeviceScreenSize(out int, out int)

Gets the size of the current device's screen in pixels.

Declaration
public abstract void GetDeviceScreenSize(out int width, out int height)
Parameters
Type Name Description
int width

The width of the screen.

int height

The height of the screen.

GetKeyboardState()

Gets the state of the keyboard from the implemented host.

Declaration
public abstract IKeyboardState GetKeyboardState()
Returns
Type Description
IKeyboardState

The state of the keyboard.

GetMouseState()

Gets the state of the mouse from the implemented host.

Declaration
public abstract IMouseState GetMouseState()
Returns
Type Description
IMouseState

The state of the mouse.

GetRenderer(string)

Creates and returns an IRenderer by name.

Declaration
public virtual IRenderer? GetRenderer(string name)
Parameters
Type Name Description
string name

The name of the renderer.

Returns
Type Description
IRenderer

A new renderer.

GetRendererStep(string)

Creates and returns a IRenderStep by name.

Declaration
public virtual IRenderStep GetRendererStep(string name)
Parameters
Type Name Description
string name

The name of the renderer.

Returns
Type Description
IRenderStep

A new renderer.

GetTexture(Stream)

Gets a texture from the implemented host.

Declaration
public abstract ITexture GetTexture(Stream textureStream)
Parameters
Type Name Description
Stream textureStream

A stream containing the texture.

Returns
Type Description
ITexture

The texture from the game host.

GetTexture(string)

Gets a texture from the implemented host.

Declaration
public abstract ITexture GetTexture(string resourcePath)
Parameters
Type Name Description
string resourcePath

The path to the texture to load.

Returns
Type Description
ITexture

The texture from the game host.

LoadDefaultFonts(string?)

Loads the embedded IBM.font files. Sets the DefaultFont property.

Declaration
protected void LoadDefaultFonts(string? defaultFont)
Parameters
Type Name Description
string defaultFont

An optional font to load and set as the default.

Remarks

If defaultFont is null, the EmbeddedFont or EmbeddedFontExtended font is set based on the value of UseDefaultExtendedFont.

LoadFont(string)

Loads a font from a file and adds it to the Fonts collection.

Declaration
public IFont LoadFont(string font)
Parameters
Type Name Description
string font

The font file to load.

Returns
Type Description
IFont

A master font that you can generate a usable font from.

LoadMappedColors()

Uses reflection to examine the Color type and add any predefined colors into ColorMappings.

Declaration
protected static void LoadMappedColors()

OnFrameRender()

Raises the FrameRender event.

Declaration
protected virtual void OnFrameRender()

OnFrameUpdate()

Raises the FrameUpdate event.

Declaration
protected virtual void OnFrameUpdate()

OnGameEnding()

Raises the Ending event.

Declaration
protected virtual void OnGameEnding()

OnGameStarted()

Raises the Started event.

Declaration
protected virtual void OnGameStarted()

OpenStream(string, FileMode, FileAccess)

Opens a file stream with the specified mode and access.

Declaration
public virtual Stream OpenStream(string file, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read)
Parameters
Type Name Description
string file

The file to open.

FileMode mode

The mode for opening. Defaults to Open.

FileAccess access

The type of access for the stream. Defaults to Read.

Returns
Type Description
Stream

The stream object.

ResizeWindow(int, int, Point, bool)

Resizes the window to the specified cell count along the X-axis and Y-axis.

Declaration
public void ResizeWindow(int cellsX, int cellsY, Point cellSize, bool resizeOutputSurface = false)
Parameters
Type Name Description
int cellsX

The number of cells to fit horizontally.

int cellsY

The number of cells to fit vertically.

Point cellSize

The size of the cells in pixels.

bool resizeOutputSurface

When true resizes the screen output surface along with the window. Defaults to false

ResizeWindow(int, int, bool)

Resizes the window to the specified dimensions.

Declaration
public abstract void ResizeWindow(int width, int height, bool resizeOutputSurface = false)
Parameters
Type Name Description
int width

The width of the window in pixels.

int height

The height of the window in pixels.

bool resizeOutputSurface

When true resizes the screen output surface along with the window. Defaults to false

RestoreGlobalState()

Restores the global state that was saved with SaveGlobalState().

Declaration
public void RestoreGlobalState()

Run()

Runs the game.

Declaration
public abstract void Run()

SaveGlobalState()

Saves the global state, mainly the FocusedScreenObjects and Screen objects.

Declaration
public void SaveGlobalState()

SetRenderer(string, Type)

Sets the default IRenderer for a type.

Declaration
public void SetRenderer(string name, Type rendererType)
Parameters
Type Name Description
string name

The name to register the renderer as.

Type rendererType

The renderer type.

SetRendererStep(string, Type)

Sets the default IRenderStep for a type.

Declaration
public void SetRendererStep(string name, Type rendererStepType)
Parameters
Type Name Description
string name

The name to register the render step as.

Type rendererStepType

The render step type.

SetSplashScreens(params IScreenSurface[])

The splash screens the game should sequentially show on startup.

Declaration
public void SetSplashScreens(params IScreenSurface[] surfaces)
Parameters
Type Name Description
IScreenSurface[] surfaces

The splash screens to show.

Events

Ending

A callback to run after the Run() method is called;

Declaration
public event EventHandler<GameHost>? Ending
Event Type
Type Description
EventHandler<GameHost>

FrameRender

Raised when the game draws a frame to the screen.

Declaration
public event EventHandler<GameHost>? FrameRender
Event Type
Type Description
EventHandler<GameHost>

FrameUpdate

Raised when the game updates prior to drawing a frame.

Declaration
public event EventHandler<GameHost>? FrameUpdate
Event Type
Type Description
EventHandler<GameHost>

Started

A callback to run before the Run() method is called;

Declaration
public event EventHandler<GameHost>? Started
Event Type
Type Description
EventHandler<GameHost>

Implements

IDisposable

Extension Methods

ExtensionsSystem.GetDebuggerDisplayValue(object)
In this article

Back to top

Back to top Generated by DocFX