canvas.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*!
  2. Simple debug painting canvas with 0/1 pixel representation.
  3. Usage:
  4. Canvas c = new Canvas(100,100);
  5. for(int i = 0; i < 100;i++)
  6. {
  7. float val = i/100;
  8. float y = Easing.EaseInOutExpo(val);
  9. c.DrawPixel(i,y*100);
  10. }
  11. c.SaveToFile("output");
  12. */
  13. typedef bool PIXEL;
  14. class Canvas
  15. {
  16. int m_SizeX; int m_SizeY;
  17. ref array<ref array<PIXEL>> m_Pixels = new array<ref array<PIXEL>>;
  18. void Canvas(int size_x, int size_y)
  19. {
  20. m_SizeX = size_x;
  21. m_SizeY = size_y;
  22. for(int i = 0; i < size_y; i++)
  23. {
  24. array<PIXEL> x_line = new array<PIXEL>;
  25. for(int z = 0; z < size_x;z++)
  26. {
  27. x_line.Insert(false);
  28. }
  29. m_Pixels.Insert(x_line);
  30. }
  31. }
  32. void DrawPixel(int x, int y)
  33. {
  34. if((x > m_SizeX - 1) || (y > m_SizeY - 1))
  35. return;
  36. //Print("x:" +x+",y:"+y);
  37. m_Pixels.Get(y).InsertAt(true, x);
  38. }
  39. void PrintOut()
  40. {
  41. string line = "";
  42. int y_lines = m_SizeY - 1;
  43. for(int i = y_lines; i >= 0; i--)
  44. {
  45. line = "";
  46. for(int z = 0; z < m_SizeX;z++)
  47. {
  48. if(m_Pixels.Get(i).Get(z))
  49. {
  50. line += "X";
  51. }
  52. else
  53. {
  54. line += " ";
  55. }
  56. }
  57. Print(line);
  58. }
  59. }
  60. void SaveToFile(string filename)
  61. {
  62. FileHandle file = OpenFile("$profile:"+filename, FileMode.WRITE);
  63. string line = "";
  64. int y_lines = m_SizeY - 1;
  65. for(int i = y_lines; i >= 0; i--)
  66. {
  67. line = "";
  68. for(int z = 0; z < m_SizeX;z++)
  69. {
  70. if(m_Pixels.Get(i).Get(z))
  71. {
  72. line += "X";
  73. }
  74. else
  75. {
  76. line += " ";
  77. }
  78. }
  79. FPrintln(file, line);
  80. }
  81. }
  82. }