Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct Cell {
- public readonly int X;
- public readonly int Y;
- public readonly char Symbol;
- public readonly ConsoleColor ColorBG;
- public readonly ConsoleColor ColorFG;
- public Cell(
- Int32 x,
- Int32 y,
- Char c = ' ',
- ConsoleColor bg = ConsoleColor.Black,
- ConsoleColor fg = ConsoleColor.White) {
- X = x;
- Y = y;
- Symbol = c;
- ColorBG = bg;
- ColorFG = fg;
- }
- public override Boolean Equals([NotNullWhen(true)] Object? obj) {
- if(obj is not Cell other)
- return false;
- return
- X == other.X
- && Y == other.Y
- && Symbol == other.Symbol
- && ColorBG == other.ColorBG
- && ColorFG == other.ColorFG;
- }
- }
- public class FrameBuffer {
- private Cell[,] _buffer;
- private uint _width;
- private uint _height;
- public FrameBuffer(uint width, uint height) {
- _width = width;
- _height = height;
- _buffer = new Cell[_height, _width];
- }
- public uint Width => _width;
- public uint Height => _height;
- public Cell this[int x, int y] => GetCell(x, y);
- public ref Cell GetCell(int x, int y) => ref _buffer[y, x];
- public void SetCell(Cell c) => _buffer[c.Y, c.X] = c;
- public void ClearAll() => Array.Clear(_buffer);
- public override String ToString() {
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i < _height; i++) {
- for(int j = 0; j < _width; j++) {
- sb.Append(GetCell(j, i).Symbol);
- }
- sb.AppendLine();
- }
- return sb.ToString();
- }
- public void CopyTo(FrameBuffer other) {
- Array.Copy(_buffer, other._buffer, _buffer.Length);
- }
- }
- public class Renderer {
- FrameBuffer _current;
- FrameBuffer _next;
- FrameBuffer _previous;
- public Renderer(uint width, uint heigth) {
- _current = new(width, heigth);
- _next = new(width, heigth);
- _previous = new(width, heigth);
- Console.CursorVisible = false;
- Console.OutputEncoding = System.Text.Encoding.UTF8;
- }
- public void BeginDraw() => _next.ClearAll();
- public void EndDraw() {
- (_next, _current) = (_current, _next);
- Render();
- _current.CopyTo(_previous);
- }
- public FrameBuffer DrawBuffer => _next;
- private void Render() {
- for(int i = 0; i < _current.Width; i++) {
- for(int j = 0; j < _current.Height; j++) {
- ref Cell current = ref _current.GetCell(i, j);
- ref Cell previous = ref _previous.GetCell(i, j);
- if(!current.Equals(previous)) {
- Console.SetCursorPosition(previous.X, previous.Y);
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(' ');
- Console.SetCursorPosition(current.X, current.Y);
- Console.BackgroundColor = current.ColorBG;
- Console.ForegroundColor = current.ColorFG;
- Console.Write(current.Symbol);
- }
- }
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment