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.

149 lines
3.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 drawEge(const Ege& e1,const Ege& e2)
  77. {
  78. float xOffset = e2._x2 - e2._x1;
  79. float yOffset = e2._y2 - e2._y1;
  80. float scale = 0;
  81. float step = 1.0f/yOffset;
  82. float xOffset1 = e1._x2 - e1._x1;
  83. float yOffset1 = e1._y2 - e1._y1;
  84. float scale1 = 0;
  85. float step1 = 1.0f/yOffset1;
  86. for (int y = e2._y1 ; y <= e2._y2 ; ++ y)
  87. {
  88. int x1 = e1._x1 + scale1 * xOffset1;
  89. int x2 = e2._x1 + scale * xOffset;
  90. Span span(x1,x2,y);
  91. drawSpan(span);
  92. scale += step;
  93. scale1 += step1;
  94. }
  95. }
  96. void drawSpan(const Span& span)
  97. {
  98. for (int x = span._xStart ; x <= span._xEnd; ++ x)
  99. {
  100. setPixel(x,span._y,_color);
  101. }
  102. }
  103. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  104. void drawPoints(float2 pt1,Rgba4Byte color)
  105. {
  106. }
  107. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  108. {
  109. _buffer[y * _width + x] = color;
  110. }
  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. }