You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.7 KiB

5 years ago
  1. #pragma once
  2. #include "CELLMath.hpp"
  3. namespace CELL
  4. {
  5. class Raster
  6. {
  7. public:
  8. Rgba* _buffer;
  9. int _width;
  10. int _height;
  11. public:
  12. Raster(int w,int h,void* buffer);
  13. ~Raster(void);
  14. void clear();
  15. void drawPoint(int x,int y, Rgba color,int ptSize);
  16. void drawLine(float2 pt1,float2 pt2,Rgba color)
  17. {
  18. float xOffset = pt1.x - pt2.x;
  19. float yOffset = pt1.y - pt2.y;
  20. if (xOffset == 0 && yOffset == 0)
  21. {
  22. setPixel(pt1.x,pt1.y,color);
  23. }
  24. if (xOffset == 0)
  25. {
  26. float yMin;
  27. float yMax;
  28. if (pt1.y < pt2.y)
  29. {
  30. yMin = pt1.y;
  31. yMax = pt2.y;
  32. }
  33. else
  34. {
  35. yMin = pt2.y;
  36. yMax = pt1.y;
  37. }
  38. float slope = yOffset / xOffset;
  39. for (float y = yMin; y <= yMax ; y += 1.0f)
  40. {
  41. setPixel(pt2.x,y,color);
  42. }
  43. }
  44. else if(yOffset == 0)
  45. {
  46. float xMin;
  47. float xMax;
  48. if (pt1.x < pt2.x)
  49. {
  50. xMin = pt1.x;
  51. xMax = pt2.x;
  52. }
  53. else
  54. {
  55. xMin = pt2.x;
  56. xMax = pt1.x;
  57. }
  58. float slope = yOffset / xOffset;
  59. for (float x = xMin; x <= xMax ; x += 1.0f)
  60. {
  61. setPixel(x,pt2.y,color);
  62. }
  63. }
  64. else
  65. {
  66. if (fabs(xOffset) > fabs(yOffset))
  67. {
  68. float slope = yOffset / xOffset;
  69. float xMin;
  70. float xMax;
  71. if (pt1.x < pt2.x)
  72. {
  73. xMin = pt1.x;
  74. xMax = pt2.x;
  75. }
  76. else
  77. {
  78. xMin = pt2.x;
  79. xMax = pt1.x;
  80. }
  81. for (float x = xMin; x <= xMax ; x += 1.0f)
  82. {
  83. float y = pt1.y + (x - pt1.x) * slope;
  84. setPixel(x,y,color);
  85. }
  86. }
  87. else
  88. {
  89. float slope = xOffset / yOffset;
  90. float yMin;
  91. float yMax;
  92. if (pt1.y < pt2.y)
  93. {
  94. yMin = pt1.y;
  95. yMax = pt2.y;
  96. }
  97. else
  98. {
  99. yMin = pt2.y;
  100. yMax = pt1.y;
  101. }
  102. for (float y = pt1.y; y <= pt2.y ; y += 1.0f)
  103. {
  104. float x = pt1.x + (y - pt1.y) * slope;
  105. setPixel(pt2.x,y,color);
  106. }
  107. }
  108. }
  109. }
  110. public:
  111. inline void setPixel(unsigned x,unsigned y,Rgba color)
  112. {
  113. if (x >= _width || y >= _height)
  114. {
  115. return;
  116. }
  117. _buffer[y * _width + x] = color;
  118. }
  119. };
  120. }