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.

244 lines
6.7 KiB

5 years ago
  1. #pragma once
  2. #include "CELLMath.hpp"
  3. #include "Image.hpp"
  4. namespace CELL
  5. {
  6. enum DRAWMODE
  7. {
  8. DM_POINTS = 0,
  9. DM_LINES = 1,
  10. DM_LINE_LOOP = 2,
  11. DM_LINE_STRIP = 3,
  12. };
  13. class Span
  14. {
  15. public:
  16. int _xStart;
  17. int _xEnd;
  18. Rgba _colorStart;
  19. Rgba _colorEnd;
  20. int _y;
  21. public:
  22. Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
  23. {
  24. if (xStart < xEnd)
  25. {
  26. _xStart = xStart;
  27. _xEnd = xEnd;
  28. _colorStart = colorStart;
  29. _colorEnd = colorEnd;
  30. _y = y;
  31. }
  32. else
  33. {
  34. _xStart = _xEnd;
  35. _xEnd = _xStart;
  36. _colorStart = colorEnd;
  37. _colorEnd = colorStart;
  38. _y = y;
  39. }
  40. }
  41. };
  42. class Ege
  43. {
  44. public:
  45. int _x1;
  46. int _y1;
  47. Rgba _color1;
  48. int _x2;
  49. int _y2;
  50. Rgba _color2;
  51. Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
  52. {
  53. if (y1 < y2)
  54. {
  55. _x1 = x1;
  56. _y1 = y1;
  57. _color1 = color1;
  58. _x2 = x2;
  59. _y2 = y2;
  60. _color2 = color2;
  61. }
  62. else
  63. {
  64. _x1 = x2;
  65. _y1 = y2;
  66. _color1 = color2;
  67. _x2 = x1;
  68. _y2 = y1;
  69. _color2 = color1;
  70. }
  71. }
  72. };
  73. class Raster
  74. {
  75. public:
  76. uint* _buffer;
  77. int _width;
  78. int _height;
  79. Rgba _color;
  80. public:
  81. Raster(int w,int h,void* buffer);
  82. ~Raster(void);
  83. void clear();
  84. void drawPoint(int x,int y, Rgba color,int ptSize);
  85. void drawArrays(DRAWMODE mode,const float2* points,int count);
  86. void drawFilleRect(int startX,int startY,int w,int h);
  87. void drawRect(const int2* points,const Rgba* colors);
  88. void drawImage(int startX,int startY,const Image* image);
  89. void drawImageWidthColorKey(int startX,int startY,const Image* image,Rgba key);
  90. void drawImageAlphaTest(int startX,int startY,const Image* image,byte alpha);
  91. void drawImageAlphaBlend(int startX,int startY,const Image* image,float alpha);
  92. void drawImageAlpha( int startX,int startY,const Image* image,float alpha );
  93. void drawImage(int startX,int startY,const Image* image,int x1,int y1,int w,int h)
  94. {
  95. int left = tmax<int>(startX,0);
  96. int top = tmax<int>(startY,0);
  97. int right = tmin<int>(startX + w,_width);
  98. int bottom = tmin<int>(startY + h,_height);
  99. for (int x = left ; x < right ; ++ x)
  100. {
  101. for (int y = top ; y < bottom ; ++ y)
  102. {
  103. Rgba srcColor = image->pixelAt(x - left + x1,y - top + y1);
  104. setPixelEx(x,y,srcColor);
  105. }
  106. }
  107. }
  108. public:
  109. void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
  110. {
  111. Ege eges[3] =
  112. {
  113. Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
  114. Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
  115. Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
  116. };
  117. int iMax = 0;
  118. int length = eges[0]._y2 - eges[0]._y1;
  119. for (int i = 1 ;i < 3 ; ++ i)
  120. {
  121. int len = eges[i]._y2 - eges[i]._y1;
  122. if (len > length)
  123. {
  124. length = i;
  125. }
  126. }
  127. int iShort1 = (iMax + 1)%3;
  128. int iShort2 = (iMax + 2)%3;
  129. drawEge(eges[iMax],eges[iShort1]);
  130. drawEge(eges[iMax],eges[iShort2]);
  131. }
  132. void drawEge(const Ege& e1,const Ege& e2)
  133. {
  134. float yOffset1 = e1._y2 - e1._y1;
  135. if (yOffset1 == 0)
  136. {
  137. return;
  138. }
  139. float yOffset = e2._y2 - e2._y1;
  140. if (yOffset == 0)
  141. {
  142. return;
  143. }
  144. float xOffset = e2._x2 - e2._x1;
  145. float scale = 0;
  146. float step = 1.0f/yOffset;
  147. float xOffset1 = e1._x2 - e1._x1;
  148. float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
  149. float step1 = 1.0f/yOffset1;
  150. for (int y = e2._y1 ; y < e2._y2 ; ++ y)
  151. {
  152. int x1 = e1._x1 + (int)(scale1 * xOffset1);
  153. int x2 = e2._x1 + (int)(scale * xOffset);
  154. Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
  155. Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
  156. Span span(x1,x2,y,color1,color2);
  157. drawSpan(span);
  158. scale += step;
  159. scale1 += step1;
  160. }
  161. }
  162. void drawSpan(const Span& span)
  163. {
  164. float length = span._xEnd - span._xStart;
  165. float scale = 0;
  166. float step = 1.0f/length;
  167. for (int x = span._xStart ; x < span._xEnd; ++ x)
  168. {
  169. Rgba color = colorLerp(
  170. span._colorStart
  171. ,span._colorEnd
  172. ,scale
  173. );
  174. scale += step;
  175. setPixel(x,span._y,color);
  176. }
  177. }
  178. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  179. void drawPoints(float2 pt1,Rgba4Byte color)
  180. {
  181. }
  182. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  183. {
  184. _buffer[y * _width + x] = color._color;
  185. }
  186. inline Rgba getPixel(unsigned x,unsigned y)
  187. {
  188. return Rgba(_buffer[y * _width + x]);
  189. }
  190. inline void setPixel(unsigned x,unsigned y,Rgba color)
  191. {
  192. if (x >= _width || y >= _height)
  193. {
  194. return;
  195. }
  196. _buffer[y * _width + x] = color._color;
  197. }
  198. };
  199. }