RenderBuffer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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) : Buffer(w, h) {
  7. FURI_LOG_D("App", "New renderBuffer");
  8. }
  9. void RenderBuffer::reset() {
  10. int size = width() * (height() / 8);
  11. for (int i = 0; i < size; i++) {
  12. data[i] = 0;
  13. }
  14. }
  15. void RenderBuffer::render(Canvas *const canvas) {
  16. // canvas_clear(canvas);
  17. for (uint8_t x = 0; x < width(); x++) {
  18. for (uint8_t y = 0; y < height(); y++) {
  19. if (test_pixel(x, y))
  20. canvas_draw_dot(canvas, x, y);
  21. }
  22. }
  23. // canvas_commit(canvas);
  24. }
  25. void RenderBuffer::draw_line(int x0, int y0, int x1, int y1, PixelColor draw_mode) {
  26. int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
  27. int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
  28. int err = (dx > dy ? dx : -dy) / 2;
  29. while (true) {
  30. if (test_coordinate(x0, y0)) {
  31. set_pixel(x0, y0, draw_mode);
  32. }
  33. if (x0 == x1 && y0 == y1) break;
  34. int e2 = err;
  35. if (e2 > -dx) {
  36. err -= dy;
  37. x0 += sx;
  38. }
  39. if (e2 < dy) {
  40. err += dx;
  41. y0 += sy;
  42. }
  43. }
  44. }
  45. void RenderBuffer::draw_circle(int x, int y, int r, PixelColor color) {
  46. int16_t a = r;
  47. int16_t b = 0;
  48. int16_t decision = 1 - a;
  49. while (b <= a) {
  50. set_pixel_with_check(a + x, b + y, color);
  51. set_pixel_with_check(b + x, a + y, color);
  52. set_pixel_with_check(-a + x, b + y, color);
  53. set_pixel_with_check(-b + x, a + y, color);
  54. set_pixel_with_check(-a + x, -b + y, color);
  55. set_pixel_with_check(-b + x, -a + y, color);
  56. set_pixel_with_check(a + x, -b + y, color);
  57. set_pixel_with_check(b + x, -a + y, color);
  58. b++;
  59. if (decision <= 0) {
  60. decision += 2 * b + 1;
  61. } else {
  62. a--;
  63. decision += 2 * (b - a) + 1;
  64. }
  65. }
  66. }
  67. void RenderBuffer::draw(Sprite *const sprite, Vector position, float rotation) {
  68. draw(sprite, position, sprite->width(), sprite->height(), rotation);
  69. }
  70. void RenderBuffer::draw(Sprite *const sprite, Vector position, uint8_t x_cap, uint8_t y_cap, float rotation) {
  71. switch (sprite->draw_mode) {
  72. default:
  73. case BlackOnly:
  74. draw_sprite(sprite, true, Black, position, x_cap, y_cap, rotation);
  75. break;
  76. case WhiteOnly:
  77. draw_sprite(sprite, false, White, position, x_cap, y_cap, rotation);
  78. break;
  79. case WhiteAsBlack:
  80. draw_sprite(sprite, false, Black, position, x_cap, y_cap, rotation);
  81. break;
  82. case BlackAsWhite:
  83. draw_sprite(sprite, true, White, position, x_cap, y_cap, rotation);
  84. break;
  85. case WhiteAsInverted:
  86. draw_sprite(sprite, false, Flip, position, x_cap, y_cap, rotation);
  87. break;
  88. case BlackAsInverted:
  89. draw_sprite(sprite, true, Flip, position, x_cap, y_cap, rotation);
  90. break;
  91. }
  92. }
  93. //TODO: proper scaling
  94. void
  95. RenderBuffer::draw_sprite(Sprite *const sprite, bool is_black, PixelColor draw_color, const Vector &position,
  96. uint8_t x_cap, uint8_t y_cap, float rotation) {
  97. Vector anchor = sprite->get_offset();
  98. float cosTheta = cos(rotation/* + M_PI_2*/);
  99. float sinTheta = sin(rotation/* + M_PI_2*/);
  100. float transformedX, transformedY, rotatedX, rotatedY;
  101. int max_w = fmin(sprite->width(), x_cap);
  102. int max_h = fmin(sprite->height(), y_cap);
  103. bool isOn;
  104. int16_t finalX, finalY;
  105. for (int y = 0; y < max_h; y++) {
  106. for (int x = 0; x < max_w; x++) {
  107. transformedX = (x - anchor.x);
  108. transformedY = (y - anchor.y);
  109. rotatedX = transformedX * cosTheta - transformedY * sinTheta;
  110. rotatedY = transformedX * sinTheta + transformedY * cosTheta;
  111. finalX = (int16_t) floor(rotatedX + position.x);
  112. finalY = (int16_t) floor(rotatedY + position.y);
  113. if (test_coordinate(finalX, finalY)) {
  114. isOn = sprite->test_pixel(x, y) == is_black;
  115. if (isOn)
  116. set_pixel(finalX, finalY, draw_color);
  117. }
  118. }
  119. }
  120. }
  121. RenderBuffer::~RenderBuffer() {
  122. FURI_LOG_D("App", "RenderBuffer end");
  123. }
  124. void RenderBuffer::draw_rbox(int16_t x0, int16_t y0, int16_t x1, int16_t y1, PixelColor draw_mode) {
  125. for (int16_t x = x0; x < x1; x++) {
  126. for (int16_t y = y0; y < y1; y++) {
  127. if (((x == x0 || x == x1 - 1) && (y == y0 || y == y1 - 1)) || !test_coordinate(x, y)) continue;
  128. set_pixel(x, y, draw_mode);
  129. }
  130. }
  131. }
  132. void RenderBuffer::draw_rbox_frame(int16_t x0, int16_t y0, int16_t x1, int16_t y1, PixelColor draw_mode) {
  133. draw_line(x0 + 1, y0, x1 - 1, y0, draw_mode);
  134. draw_line(x0 + 1, y1, x1 - 1, y1, draw_mode);
  135. draw_line(x0, y0 + 1, x0, y1 - 1, draw_mode);
  136. draw_line(x1, y0 + 1, x1, y1 - 1, draw_mode);
  137. }
  138. void RenderBuffer::draw_box(int16_t x0, int16_t y0, int16_t x1, int16_t y1, PixelColor draw_mode) {
  139. for (int16_t x = x0; x < x1; x++) {
  140. for (int16_t y = y0; y < y1; y++) {
  141. set_pixel(x, y, draw_mode);
  142. }
  143. }
  144. }