Something went wrong. Try again.
.NET bindings for Terminus
Something went wrong. Try again.
Terminus.NET Renderer.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290namespace Terminus;
/// <summary>/// Renderer class./// </summary>public sealed class Renderer : IDisposable{ private nint renderer;
/// <summary> /// Callback called every time the debug menu is drawn. /// </summary> /// /// <remarks> /// Use this function to call <see cref="AddDebug" /> in order to add debug information to the debug menu. /// </remarks> public static Action<Renderer> DebugMenuCallback = (renderer) => {};
/// <param name="title"> /// Window title. /// </param> /// /// <param name="vsync"> /// Whether or not to enable vertical sync. /// </param> /// /// <param name="font_path"> /// Path to a .TTF font to use instead of the default. /// </param> /// /// <param name="font_size"> /// The size, in points, of the font. /// </param> public Renderer(string title, bool vsync = true, string? font_path = null, float font_size = 20f) { renderer = NativeMethods.Terminus_Init(title, vsync, font_path ?? "", font_size); NativeMethods.Terminus_SetDebugMenuCallback(renderer, Handler); }
/// <inheritdoc /> public void Dispose() { NativeMethods.Terminus_Destroy(renderer); }
/// <summary> /// Present a frame to the screen. /// </summary> /// /// <remarks> /// Call this method in a loop. /// </remarks> /// /// <remarks> /// Some methods call this method internally. /// </remarks> public void Present() { NativeMethods.Terminus_Present(renderer); }
/// <summary> /// Toggle debug mode. /// </summary> /// /// <remarks> /// Debug mode is separate from the debug menu. /// </remarks> public void SetDebug(bool debug) { NativeMethods.Terminus_SetDebug(renderer, debug); }
/// <summary> /// Draw a single cell. /// </summary> /// /// <param name="cell"> /// The cell to draw. /// </param> /// /// <param name="pos"> /// The cell's position. /// </param> public void SetCell(Cell cell, Bound pos) { NativeMethods.Terminus_SetCell(renderer, cell, pos); }
/// <summary> /// Draw a single cell. /// </summary> /// /// <param name="cell"> /// The cell to draw. /// </param> /// /// <param name="x"> /// The cell's X-position. /// </param> /// /// <param name="y"> /// The cell's Y-position. /// </param> public void SetCell(Cell cell, int x, int y) { SetCell(cell, new Bound(x, y)); }
/// <summary> /// Write a single character to the cursor position. Also advance the cursor position. /// </summary> public void Write(Cell cell) { NativeMethods.Terminus_WriteCell(renderer, cell); }
/// <summary> /// Clears the screen. /// </summary> public void Clear() { NativeMethods.Terminus_Clear(renderer); }
/// <summary> /// Sets the cursor position. /// </summary> public void SetCursor(Bound pos) { NativeMethods.Terminus_SetCursor(renderer, pos); }
/// <summary> /// Get the size of the canvas in cells (rows × columns). /// </summary> public Bound GetCanvasSize() { return NativeMethods.Terminus_GetCanvasSize(renderer); }
/// <summary> /// Fill a row with a cell. /// </summary> public void FillRow(Cell cell, int row) { NativeMethods.Terminus_FillRow(renderer, cell, row); }
/// <summary> /// Fill a column with a cell. /// </summary> public void FillCol(Cell cell, int col) { NativeMethods.Terminus_FillCol(renderer, cell, col); }
/// <summary> /// Fill the screen with a cell. /// </summary> public void FillScreen(Cell cell) { NativeMethods.Terminus_FillScreen(renderer, cell); }
/// <summary> /// Read a single key from the keyboard. /// </summary> /// /// <remarks> /// Calls Present() internally. /// </remarks> public KeyPress ReadKey() { return NativeMethods.Terminus_ReadKey(renderer); }
/// <summary> /// Writes a single character to the screen. /// </summary> public void Write(char x) { Write(new Cell(x, Color.White, Color.Black, Style.Normal)); }
/// <summary> /// Writes a string to the screen. /// </summary> public void Write(string text) { foreach (var x in text) Write(x); }
/// <summary> /// Moves the cursor to the next line. /// </summary> public void WriteLine() { }
/// <summary> /// Writes a string to the screen and moves the cursor to the next line. /// </summary> public void WriteLine(string text) { Write(text); WriteLine(); }
/// <summary> /// Toggles the debug menu. /// </summary> public void DrawDebugMenu(bool debug) { NativeMethods.Terminus_DrawDebugMenu(renderer, debug); }
/// <summary> /// Polls the state of the mouse. /// </summary> public MouseClick PollMouse() { return NativeMethods.Terminus_PollMouse(renderer); }
/// <summary> /// Waits for a mouse click and returns the state of the mouse. /// </summary> /// /// <remarks> /// Calls Present() internally. /// </remarks> public MouseClick ReadMouse() { return NativeMethods.Terminus_ReadMouse(renderer); }
private void Handler(nint renderer) { DebugMenuCallback(this); }
/// <summary> /// Adds a line of text to the debug menu. /// </summary> /// /// <remarks> /// You should only call this in <see cref="DebugMenuCallback" />. /// </remarks> public void AddDebug(string text) { NativeMethods.Terminus_AddDebugString(renderer, text); }
/// <summary> /// Return a string containing a dump of the contents of the screen. /// </summary> /// /// <remarks> /// Contains ANSI escape codes. /// </remarks> public string DumpScreen() { using var buf = new NativeString(NativeMethods.Terminus_DumpScreen(renderer)); return buf.ToString(); }
/// <summary> /// Get the name of the font family in use. /// </summary> public string GetFontName() { using var buf = new NativeString(NativeMethods.Terminus_GetFontName(renderer)); return buf.ToString(); }
/// <summary> /// Get the size, in points, of the font in use. /// </summary> /// /// <remarks> /// Default font size is 20pt as of Terminus v0.2. /// </remarks> public float GetFontSize() { return NativeMethods.Terminus_GetFontSize(renderer); }}