How to draw on a Console
Really, you don’t draw to a console. The console object only combines a text surface with a renderer and lets you use the keyboard/mouse. The console provides all the methods you need to work with a text surface and edit it.
SadConsole.Surfaces.BasicSurface is broken up into multiple cells, all arranged in a grid. Each cell can change its foreground and background color, and the character it is currently showing.
The foreground or the background of a cell can be colored. When the background is colored, it fills the entire cell with that color. When the foreground is colored, it changes the color of the character being displayed in that cell.
You can set or get the foreground color of a cell by using its x, y coordinates and the
console.SetForeground(10, 10, Microsoft.Xna.Framework.Color.Yellow);
You can set or get the background color of a cell by using its x, y coordinates and the
console.SetBackground(10, 10, Microsoft.Xna.Framework.Color.White);
The character displayed on a cell is changed by calling the
SetGlyph method. It works much the same as working with the color does. You can get or set the character in a cell. The overloads for the
SetGlyph method also allow you to provide foreground and background colors if you want.
// Set the character at 41,22 to index 1 on the font sheet, a smiley face.
char character = 'B';
console.SetGlyph(41, 22, 13, Color.Purple);
console.SetGlyph(41, 22, 13, Color.Purple, Color.Green);
Setting the glyph cell-by-cell is not very efficient when you have a string you want to print. The cell surface provides a way to print an entire string.
console.Print(2, 0, "This will be printed at 2,0");
Additionally, you can read the characters back from a console into a string. This uses an x,y location for the start of the string, and a length of string. For example, the previous code example printed a string at 2,0 that was 27 characters long, to pull that string back out into a variable, you would use:
string text = console.GetString(2, 0, 27);
\r\n,will also be printed. The text will not trigger a new line. The
Console type has a
Cursor property that helps with this. By using
Console.Cursor.Print, SadConsole will try and “pretty print” your string. It will respect new line characters, and it will try and break up words so that they don’t appear split across lines.
console.Cursor.Position = new Point(2, 0);