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.

183 lines
4.3 KiB

5 years ago
  1. #pragma once
  2. #include "CELLMath.hpp"
  3. namespace CELL
  4. {
  5. enum DRAWMODE
  6. {
  7. DM_POINTS = 0,
  8. DM_LINES = 1,
  9. DM_LINE_LOOP = 2,
  10. DM_LINE_STRIP = 3,
  11. };
  12. class Span
  13. {
  14. public:
  15. int _xStart;
  16. int _xEnd;
  17. int _y;
  18. public:
  19. Span(int xStart,int xEnd,int y)
  20. {
  21. if (xStart < xEnd)
  22. {
  23. _xStart = xStart;
  24. _xEnd = xEnd;
  25. _y = y;
  26. }
  27. else
  28. {
  29. _xStart = _xEnd;
  30. _xEnd = _xStart;
  31. _y = y;
  32. }
  33. }
  34. };
  35. class Ege
  36. {
  37. public:
  38. int _x1;
  39. int _y1;
  40. int _x2;
  41. int _y2;
  42. Ege(int x1,int y1,int x2,int y2)
  43. {
  44. if (y1 < y2)
  45. {
  46. _x1 = x1;
  47. _y1 = y1;
  48. _x2 = x2;
  49. _y2 = y2;
  50. }
  51. else
  52. {
  53. _x1 = x2;
  54. _y1 = y2;
  55. _x2 = x1;
  56. _y2 = y1;
  57. }
  58. }
  59. };
  60. class Raster
  61. {
  62. public:
  63. Rgba* _buffer;
  64. int _width;
  65. int _height;
  66. Rgba _color;
  67. public:
  68. Raster(int w,int h,void* buffer);
  69. ~Raster(void);
  70. void clear();
  71. void drawPoint(int x,int y, Rgba color,int ptSize);
  72. void drawArrays(DRAWMODE mode,const float2* points,int count);
  73. void drawFilleRect(int startX,int startY,int w,int h);
  74. void drawRect(const int2* points,const Rgba* colors);
  75. public:
  76. void drawTriangle(int2 p0,int2 p1,int2 p2)
  77. {
  78. Ege eges[3] =
  79. {
  80. Ege(p0.x,p0.y,p1.x,p1.y),
  81. Ege(p1.x,p1.y,p2.x,p2.y),
  82. Ege(p2.x,p2.y,p0.x,p0.y),
  83. };
  84. int iMax = 0;
  85. int length = eges[0]._y2 - eges[0]._y1;
  86. for (int i = 1 ;i < 3 ; ++ i)
  87. {
  88. int len = eges[i]._y2 - eges[i]._y1;
  89. if (len > length)
  90. {
  91. length = i;
  92. }
  93. }
  94. int iShort1 = (iMax + 1)%3;
  95. int iShort2 = (iMax + 2)%3;
  96. drawEge(eges[iMax],eges[iShort1]);
  97. drawEge(eges[iMax],eges[iShort2]);
  98. }
  99. void drawEge(const Ege& e1,const Ege& e2)
  100. {
  101. float yOffset1 = e1._y2 - e1._y1;
  102. if (yOffset1 == 0)
  103. {
  104. return;
  105. }
  106. float yOffset = e2._y2 - e2._y1;
  107. if (yOffset == 0)
  108. {
  109. return;
  110. }
  111. float xOffset = e2._x2 - e2._x1;
  112. float scale = 0;
  113. float step = 1.0f/yOffset;
  114. float xOffset1 = e1._x2 - e1._x1;
  115. float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
  116. float step1 = 1.0f/yOffset1;
  117. for (int y = e2._y1 ; y < e2._y2 ; ++ y)
  118. {
  119. int x1 = e1._x1 + (int)(scale1 * xOffset1);
  120. int x2 = e2._x1 + (int)(scale * xOffset);
  121. Span span(x1,x2,y);
  122. drawSpan(span);
  123. scale += step;
  124. scale1 += step1;
  125. }
  126. }
  127. void drawSpan(const Span& span)
  128. {
  129. for (int x = span._xStart ; x < span._xEnd; ++ x)
  130. {
  131. setPixel(x,span._y,_color);
  132. }
  133. }
  134. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  135. void drawPoints(float2 pt1,Rgba4Byte color)
  136. {
  137. }
  138. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  139. {
  140. _buffer[y * _width + x] = color;
  141. }
  142. inline void setPixel(unsigned x,unsigned y,Rgba color)
  143. {
  144. if (x >= _width || y >= _height)
  145. {
  146. return;
  147. }
  148. _buffer[y * _width + x] = color;
  149. }
  150. };
  151. }