RenderBuffer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "RenderBuffer.h"
  2. #include "Sprite.h"
  3. #include "Helpers.h"
  4. #include "Vector.h"
  5. #include "cmath"
  6. RenderBuffer::RenderBuffer(uint8_t w, uint8_t h, bool doubleBuffered) : Buffer(w, h) {
  7. FURI_LOG_D("App", "New renderBuffer");
  8. if (doubleBuffered)
  9. frontBuffer = new Buffer(w, h);
  10. }
  11. /**
  12. * Renders the contents of the RenderBuffer onto the given Canvas.
  13. * This function iterates over each pixel in the RenderBuffer and checks
  14. * if it should be drawn onto the canvas. If a pixel satisfies the test_pixel
  15. * condition, it calls the canvas_draw_dot function to draw the pixel onto
  16. * the canvas.
  17. *
  18. * @param canvas A pointer to the Canvas object where the RenderBuffer will be rendered.
  19. */
  20. void RenderBuffer::render(Canvas *const canvas) {
  21. if (!doubleBuffer)
  22. canvas_draw_xbm(canvas, 0, 0, width(), height(), data);
  23. else
  24. canvas_draw_xbm(canvas, 0, 0, width(), height(), frontBuffer->data);
  25. }
  26. /**
  27. * @brief Draws a line from (x0, y0) to (x1, y1) on the render buffer.
  28. *
  29. * @param x0 The x-coordinate of the starting point of the line.
  30. * @param y0 The y-coordinate of the starting point of the line.
  31. * @param x1 The x-coordinate of the ending point of the line.
  32. * @param y1 The y-coordinate of the ending point of the line.
  33. * @param draw_mode The color to draw the line with.
  34. */
  35. void RenderBuffer::draw_line(int x0, int y0, int x1, int y1, PixelColor draw_mode) {
  36. int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
  37. int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
  38. int err = (dx > dy ? dx : -dy) / 2;
  39. while (true) {
  40. if (test_coordinate(x0, y0)) {
  41. set_pixel(x0, y0, draw_mode);
  42. }
  43. if (x0 == x1 && y0 == y1) break;
  44. int e2 = err;
  45. if (e2 > -dx) {
  46. err -= dy;
  47. x0 += sx;
  48. }
  49. if (e2 < dy) {
  50. err += dx;
  51. y0 += sy;
  52. }
  53. }
  54. }
  55. /**
  56. * @brief Draws a circle on the render buffer.
  57. *
  58. * This function draws a circle using Bresenham's circle drawing algorithm. The circle is centered at the given x and y coordinates,
  59. * with the given radius, and using the specified color. The circle is drawn by plotting symmetrical points in the eight octants
  60. * of the circle.
  61. *
  62. * @param x The x-coordinate of the center of the circle.
  63. * @param y The y-coordinate of the center of the circle.
  64. * @param r The radius of the circle.
  65. * @param color The color of the circle.
  66. */
  67. void RenderBuffer::draw_circle(int x, int y, int r, PixelColor color) {
  68. int16_t a = r;
  69. int16_t b = 0;
  70. int16_t decision = 1 - a;
  71. while (b <= a) {
  72. set_pixel_with_check(a + x, b + y, color);
  73. set_pixel_with_check(b + x, a + y, color);
  74. set_pixel_with_check(-a + x, b + y, color);
  75. set_pixel_with_check(-b + x, a + y, color);
  76. set_pixel_with_check(-a + x, -b + y, color);
  77. set_pixel_with_check(-b + x, -a + y, color);
  78. set_pixel_with_check(a + x, -b + y, color);
  79. set_pixel_with_check(b + x, -a + y, color);
  80. b++;
  81. if (decision <= 0) {
  82. decision += 2 * b + 1;
  83. } else {
  84. a--;
  85. decision += 2 * (b - a) + 1;
  86. }
  87. }
  88. }
  89. /**
  90. * @brief RenderBuffer class handles rendering operations on a buffer.
  91. */
  92. void RenderBuffer::draw(Sprite *const sprite, Vector position, float rotation) {
  93. draw(sprite, position, sprite->width(), sprite->height(), rotation);
  94. }
  95. /**
  96. * Draws a sprite on the render buffer.
  97. *
  98. * @param sprite A pointer to the sprite to be drawn.
  99. * @param position The position where the sprite should be drawn.
  100. * @param x_cap The horizontal limit for the drawing area.
  101. * @param y_cap The vertical limit for the drawing area.
  102. * @param rotation The rotation angle for the sprite.
  103. */
  104. void RenderBuffer::draw(Sprite *const sprite, Vector position, uint8_t x_cap, uint8_t y_cap, float rotation) {
  105. switch (sprite->draw_mode) {
  106. default:
  107. case BlackOnly:
  108. draw_sprite(sprite, true, Black, position, x_cap, y_cap, rotation);
  109. break;
  110. case WhiteOnly:
  111. draw_sprite(sprite, false, White, position, x_cap, y_cap, rotation);
  112. break;
  113. case WhiteAsBlack:
  114. draw_sprite(sprite, false, Black, position, x_cap, y_cap, rotation);
  115. break;
  116. case BlackAsWhite:
  117. draw_sprite(sprite, true, White, position, x_cap, y_cap, rotation);
  118. break;
  119. case WhiteAsInverted:
  120. draw_sprite(sprite, false, Flip, position, x_cap, y_cap, rotation);
  121. break;
  122. case BlackAsInverted:
  123. draw_sprite(sprite, true, Flip, position, x_cap, y_cap, rotation);
  124. break;
  125. }
  126. }
  127. /**
  128. * @brief Draws a sprite on the render buffer.
  129. *
  130. * This function takes a sprite and draws it on the render buffer at the specified position, with optional rotation,
  131. * and limited to a maximum width and height cap. The draw color and whether to consider certain pixels as black can
  132. * also be specified.
  133. *
  134. * @warning This function does not implement proper scaling.
  135. *
  136. * @param sprite The sprite to be drawn.
  137. * @param is_black Whether to consider certain pixels in the sprite as black.
  138. * @param draw_color The color to use for drawing.
  139. * @param position The position at which to draw the sprite.
  140. * @param x_cap The maximum width cap for drawing.
  141. * @param y_cap The maximum height cap for drawing.
  142. * @param rotation The rotation angle, in radians, for the sprite. Positive values rotate the sprite clockwise.
  143. */
  144. //TODO: proper scaling
  145. void RenderBuffer::draw_sprite(Sprite *const sprite, bool is_black, PixelColor draw_color, const Vector &position,
  146. uint8_t x_cap, uint8_t y_cap, float rotation) {
  147. Vector center = sprite->get_offset();
  148. Vector transform;
  149. int max_w = fmin(sprite->width(), x_cap);
  150. int max_h = fmin(sprite->height(), y_cap);
  151. bool isOn;
  152. int16_t finalX, finalY;
  153. for (int y = 0; y < max_h; y++) {
  154. for (int x = 0; x < max_w; x++) {
  155. transform = Vector(x, y) - center;
  156. transform.rotate(rotation);
  157. transform += position;
  158. finalX = (int16_t) round(transform.x);
  159. finalY = (int16_t) round(transform.y);
  160. if (test_coordinate(finalX, finalY)) {
  161. isOn = sprite->test_pixel(x, y) == is_black;
  162. if (isOn)
  163. set_pixel(finalX, finalY, draw_color);
  164. }
  165. }
  166. }
  167. }
  168. RenderBuffer::~RenderBuffer() {
  169. if (doubleBuffer)
  170. delete frontBuffer;
  171. delete data;
  172. FURI_LOG_I("App", "RenderBuffer end");
  173. }
  174. /**
  175. * @brief Draws a filled rectangle on the render buffer.
  176. *
  177. * This function draws a filled rectangle on the render buffer defined by the top-left corner (x0, y0) and the
  178. * bottom-right corner (x1, y1) coordinates. The draw_mode parameter specifies the color to use for filling the rectangle.
  179. *
  180. * The rectangle is drawn by iterating through each pixel within the given coordinates and setting its color to the draw_mode.
  181. * However, the corners with coordinates (x0, y0) and (x1, y1) are not filled, resulting in an unfilled rectangle shape.
  182. *
  183. * If a pixel falls on the corners of the rectangle or if it is outside the boundaries of the render buffer, it is skipped.
  184. *
  185. * @param x0 The x-coordinate of the top-left corner of the rectangle.
  186. * @param y0 The y-coordinate of the top-left corner of the rectangle.
  187. * @param x1 The x-coordinate of the bottom-right corner of the rectangle.
  188. * @param y1 The y-coordinate of the bottom-right corner of the rectangle.
  189. * @param draw_mode The color to use for filling the rectangle.
  190. */
  191. void RenderBuffer::draw_rbox(int16_t x0, int16_t y0, int16_t x1, int16_t y1, PixelColor draw_mode) {
  192. for (int16_t x = x0; x < x1; x++) {
  193. for (int16_t y = y0; y < y1; y++) {
  194. if (((x == x0 || x == x1 - 1) && (y == y0 || y == y1 - 1)) || !test_coordinate(x, y)) continue;
  195. set_pixel(x, y, draw_mode);
  196. }
  197. }
  198. }
  199. /**
  200. * @brief Draws a rectangular frame on the RenderBuffer using the given coordinates and draw mode.
  201. *
  202. * This function draws a rectangular frame on the RenderBuffer defined by the top-left corner (x0, y0)
  203. * and the bottom-right corner (x1, y1). The frame consists of four lines that form the outline of the rectangle.
  204. * The drawing mode is specified by the draw_mode parameter.
  205. *
  206. * @param x0 The x-coordinate of the top-left corner of the rectangle.
  207. * @param y0 The y-coordinate of the top-left corner of the rectangle.
  208. * @param x1 The x-coordinate of the bottom-right corner of the rectangle.
  209. * @param y1 The y-coordinate of the bottom-right corner of the rectangle.
  210. * @param draw_mode The drawing mode to be used for rendering the lines.
  211. *
  212. * @see PixelColor
  213. * @see RenderBuffer::draw_line
  214. *
  215. * @note This function assumes that the RenderBuffer instance has been properly initialized and allocated.
  216. * The given coordinates must be within the valid range of the RenderBuffer dimensions.
  217. * The draw_mode should be one of the specified PixelColor enumeration values.
  218. */
  219. void RenderBuffer::draw_rbox_frame(int16_t x0, int16_t y0, int16_t x1, int16_t y1, PixelColor draw_mode) {
  220. draw_line(x0 + 1, y0, x1 - 1, y0, draw_mode);
  221. draw_line(x0 + 1, y1, x1 - 1, y1, draw_mode);
  222. draw_line(x0, y0 + 1, x0, y1 - 1, draw_mode);
  223. draw_line(x1, y0 + 1, x1, y1 - 1, draw_mode);
  224. }
  225. /**
  226. * @brief Draws a filled box on the render buffer.
  227. *
  228. * This function draws a filled box with the specified coordinates and color on the render buffer.
  229. * The box is drawn by setting each pixel within the specified coordinates to the specified color.
  230. *
  231. * @param x0 The x-coordinate of the top-left corner of the box.
  232. * @param y0 The y-coordinate of the top-left corner of the box.
  233. * @param x1 The x-coordinate of the bottom-right corner of the box.
  234. * @param y1 The y-coordinate of the bottom-right corner of the box.
  235. * @param draw_mode The color to be used for drawing the box.
  236. *
  237. * @note The coordinates (x0, y0) are inclusive, while (x1, y1) are exclusive.
  238. */
  239. void RenderBuffer::draw_box(int16_t x0, int16_t y0, int16_t x1, int16_t y1, PixelColor draw_mode) {
  240. for (int16_t x = x0; x < x1; x++) {
  241. for (int16_t y = y0; y < y1; y++) {
  242. set_pixel(x, y, draw_mode);
  243. }
  244. }
  245. }
  246. void RenderBuffer::swap() {
  247. if (doubleBuffer)
  248. frontBuffer->swap(data);
  249. }