Multiple consoles, types and relations

SadConsole can render multiple different consoles in the screen, and each console can have a different font.

Different types of screen objects

  • ScreenObject (A container that provides parent/child, components, position, mouse and keyboard handling)
  • ScreenSurface (A ScreenObject that renders an ICellSurface)
  • Console (A ScreenSurface with a Cursor)
  • ControlsConsole (A Console with a ControlHost component to host UI elements)

Screen object relations

Each screen can have one parent, and multiple children.
Each child receives render updates in the order they were added.
This means that if child two overlaps child one, you will see child two on top of child one.

Note: A side effect of this can also lead to one console stealing focus on things such as mouse input and keyboard input if child one does any handling, and you click on child two which is overlayed. You will lose focus to child one. Hence your input will no longer pickup from child one. Which can be quite confusing at times.

Creating multiple screen objects

Create a new screen object is pretty simple, we just use the basic constructor and we can put some random garbage on it to actually see the screen surface.
Then all we have to do is position them accordingly, and add them as a child to the parent
that will host these consoles. Here is an example.

var surface1 = new ScreenSurface(20, 10);
surface1.FillWithRandomGarbage(255);

var surface2 = new ScreenSurface(20, 10);
surface2.Position = new Point(24, 0);
surface2.FillWithRandomGarbage(255);

var surface3 = new ScreenSurface(20, 10);
surface3.Position = new Point(0, 12);
surface3.FillWithRandomGarbage(255);

var surface4 = new ScreenSurface(20, 10);
surface4.Position = new Point(24, 12);
surface4.FillWithRandomGarbage(255);

Game.Instance.StartingConsole.Children.Add(surface1);
Game.Instance.StartingConsole.Children.Add(surface2);
Game.Instance.StartingConsole.Children.Add(surface3);
Game.Instance.StartingConsole.Children.Add(surface4);

Each child surface is rendered in the same order as it was added.

What is the StartingConsole ?

When SadConsole initializes the gameloop it creates a basic Console for you already which
is initialized to the Game.Instance.StartingConsole which in turn is set to the Game.Instance.Screen property which is the console that is currently being rendered to the screen.

It can be replaced with your own Console during game configuration initialization, you don’t have to keep it around. Eg:

new Builder().SetStartingScreen((a) => new Console(10, 10))

Or if you have your own class for example:

public class CustomConsole : SadConsole.Console
{
	public CustomConsole()
    {
        // Container
    }
}

Then you can also use the following generic overload, which requires an IScreenObject with a parameterless constructor:

new Builder().SetStartingScreen<CustomConsole>()

It can also be done at runtime, but you should then also remove the starting console, as it just uses up memory.

Game.Instance.Screen = new Console(10, 10);
Game.Instance.DestroyDefaultStartingConsole();

Using a screen container to store consoles

It could be useful to store all your consoles in one neat container, as such we can use a ScreenObject type class, a bare minimum with no surface or mouse/keyboard input handling as the container will never have to deal with these things, and they only soak up extra unneeded memory and processing power.

public class ScreenContainer : ScreenObject
{
	public ScreenContainer()
    {

    }
}

We can then expand this by adding some consoles.

public class ScreenContainer : ScreenObject
{
	public readonly SadConsole.Console World;
	public readonly SadConsole.Console PlayerStats;

	public ScreenContainer()
	{
		World = new SadConsole.Console(100, 100);
		PlayerStats = new SadConsole.Console(50, 25);

		Children.Add(World);
		Children.Add(PlayerStats);
	}
}

The ScreenContainer can then be used as our main console in the game, that displays and handles displaying of all our child consoles.

Thats it for multiple consoles, their types and relations.
The ControlsConsole will be picked up more in depth in a later tutorial.

Leave a comment

Design a site like this with WordPress.com
Get started