szymski

Untitled

Jan 4th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SimpleConsoleGame.Source
  8. {
  9. internal class ConsoleBuffer
  10. {
  11. public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.White;
  12. public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
  13.  
  14. public int Width { get; }
  15. public int Height { get; }
  16.  
  17. char[,] buffer;
  18. ConsoleColor[,] colorBuffer;
  19. ConsoleColor[,] bgColorBuffer;
  20. bool[,] changed;
  21.  
  22. public ConsoleBuffer(int width, int height)
  23. {
  24. Width = width;
  25. Height = height;
  26.  
  27. buffer = new char[Width, Height];
  28. colorBuffer = new ConsoleColor[width, height];
  29. bgColorBuffer = new ConsoleColor[width, height];
  30. changed = new bool[Width, Height];
  31. }
  32.  
  33. public void DrawChar(int x, int y, char c)
  34. {
  35. if (buffer[x, y] != c || colorBuffer[x, y] != ForegroundColor || bgColorBuffer[x, y] != BackgroundColor)
  36. {
  37. buffer[x, y] = c;
  38. colorBuffer[x, y] = ForegroundColor;
  39. bgColorBuffer[x, y] = BackgroundColor;
  40. changed[x, y] = true;
  41. }
  42. }
  43.  
  44. public void DrawString(int x, int y, string str)
  45. {
  46. for (int i = 0; i < str.Length; i++)
  47. DrawChar(x + i, y, str[i]);
  48. }
  49.  
  50. public void Blit(int posX, int posY)
  51. {
  52. Console.SetCursorPosition(posX, posY);
  53.  
  54. for (int y = 0; y < Height; y++)
  55. {
  56. int lastX = 0;
  57. for (int x = 0; x < Width; x++)
  58. {
  59. if (changed[x, y])
  60. {
  61. if (lastX != x - 1)
  62. Console.SetCursorPosition(posX + x, Console.CursorTop);
  63. Console.ForegroundColor = colorBuffer[x, y];
  64. Console.BackgroundColor = bgColorBuffer[x, y];
  65. Console.Write(buffer[x, y]);
  66. changed[x, y] = false;
  67. lastX = x;
  68. }
  69. }
  70. Console.WriteLine();
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment