Guest User

Untitled

a guest
Mar 22nd, 2025
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. public struct Cell {
  2.     public readonly int X;
  3.     public readonly int Y;
  4.     public readonly char Symbol;
  5.     public readonly ConsoleColor ColorBG;
  6.     public readonly ConsoleColor ColorFG;
  7.  
  8.     public Cell(
  9.         Int32 x,
  10.         Int32 y,
  11.         Char c = ' ',
  12.         ConsoleColor bg = ConsoleColor.Black,
  13.         ConsoleColor fg = ConsoleColor.White) {
  14.         X = x;
  15.         Y = y;
  16.         Symbol = c;
  17.         ColorBG = bg;
  18.         ColorFG = fg;
  19.     }
  20.  
  21.     public override Boolean Equals([NotNullWhen(true)] Object? obj) {
  22.         if(obj is not Cell other)
  23.             return false;
  24.  
  25.         return
  26.             X == other.X
  27.             && Y == other.Y
  28.             && Symbol == other.Symbol
  29.             && ColorBG == other.ColorBG
  30.             && ColorFG == other.ColorFG;
  31.     }
  32. }
  33.  
  34. public class FrameBuffer {
  35.     private Cell[,] _buffer;
  36.     private uint _width;
  37.     private uint _height;
  38.  
  39.     public FrameBuffer(uint width, uint height) {
  40.         _width = width;
  41.         _height = height;
  42.         _buffer = new Cell[_height, _width];
  43.     }
  44.  
  45.     public uint Width => _width;
  46.     public uint Height => _height;
  47.  
  48.     public Cell this[int x, int y] => GetCell(x, y);
  49.     public ref Cell GetCell(int x, int y) => ref _buffer[y, x];
  50.     public void SetCell(Cell c) => _buffer[c.Y, c.X] = c;
  51.     public void ClearAll() => Array.Clear(_buffer);
  52.  
  53.     public override String ToString() {
  54.         StringBuilder sb = new StringBuilder();
  55.         for(int i = 0; i < _height; i++) {
  56.             for(int j = 0; j < _width; j++) {
  57.                 sb.Append(GetCell(j, i).Symbol);
  58.             }
  59.             sb.AppendLine();
  60.         }
  61.         return sb.ToString();
  62.     }
  63.  
  64.     public void CopyTo(FrameBuffer other) {
  65.         Array.Copy(_buffer, other._buffer, _buffer.Length);
  66.     }
  67. }
  68.  
  69. public class Renderer {
  70.     FrameBuffer _current;
  71.     FrameBuffer _next;
  72.     FrameBuffer _previous;
  73.  
  74.     public Renderer(uint width, uint heigth) {
  75.         _current = new(width, heigth);
  76.         _next = new(width, heigth);
  77.         _previous = new(width, heigth);
  78.         Console.CursorVisible = false;
  79.         Console.OutputEncoding = System.Text.Encoding.UTF8;
  80.     }
  81.  
  82.     public void BeginDraw() => _next.ClearAll();
  83.  
  84.     public void EndDraw() {
  85.         (_next, _current) = (_current, _next);
  86.         Render();
  87.         _current.CopyTo(_previous);
  88.     }
  89.  
  90.     public FrameBuffer DrawBuffer => _next;
  91.  
  92.     private void Render() {
  93.         for(int i = 0; i < _current.Width; i++) {
  94.             for(int j = 0; j < _current.Height; j++) {
  95.  
  96.                 ref Cell current = ref _current.GetCell(i, j);
  97.                 ref Cell previous = ref _previous.GetCell(i, j);
  98.  
  99.                 if(!current.Equals(previous)) {
  100.                     Console.SetCursorPosition(previous.X, previous.Y);
  101.                     Console.BackgroundColor = ConsoleColor.Black;
  102.                     Console.ForegroundColor = ConsoleColor.Black;
  103.                     Console.Write(' ');
  104.                     Console.SetCursorPosition(current.X, current.Y);
  105.                     Console.BackgroundColor = current.ColorBG;
  106.                     Console.ForegroundColor = current.ColorFG;
  107.                     Console.Write(current.Symbol);
  108.                 }
  109.             }
  110.             Console.ResetColor();
  111.         }
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment