Graphics.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <stdio.h>
  2. #include "Graphics.h"
  3. #include "Logger.h"
  4. #include "CXfb.h"
  5. #include "utility/misc.h"
  6. #include "utility/minmax.h"
  7. using namespace WalrusRPG; /*::Graphics*/
  8. using namespace Nspire;
  9. using WalrusRPG::Utils::Rect;
  10. void Graphics::init()
  11. {
  12. Logger::log("Graphics init");
  13. CXfb::buffer_allocate();
  14. }
  15. void Graphics::deinit()
  16. {
  17. Logger::log("Graphics deinit");
  18. CXfb::buffer_free();
  19. }
  20. void Graphics::frame_begin()
  21. {
  22. }
  23. void Graphics::frame_end()
  24. {
  25. CXfb::buffer_swap_render();
  26. }
  27. void Graphics::put_sprite(const Texture &sheet, int x, int y, const Rect &window)
  28. {
  29. CXfb::draw_sprite_sheet(sheet.data, x, y, window);
  30. }
  31. void Graphics::put_sprite_tint(const Texture &sheet, int x, int y, const Rect &window,
  32. const Pixel &color)
  33. {
  34. CXfb::draw_sprite_sheet_tint(sheet.data, x, y, window, color.value);
  35. }
  36. void Graphics::put_sprite_clipping(const Texture &sheet, int x, int y,
  37. const Rect &sprite_window, const Rect &clipping_window)
  38. {
  39. const signed &ss_x = sprite_window.x, ss_y = sprite_window.y;
  40. const signed &ss_w = sprite_window.width, &ss_h = sprite_window.height;
  41. const signed &cx = clipping_window.x, &cy = clipping_window.y;
  42. const signed &cw = clipping_window.width, &ch = clipping_window.height;
  43. const signed lx = x - cx, ly = y - cy;
  44. if (lx < -ss_w || lx > cw)
  45. return;
  46. if (ly < -ss_h || ly > ch)
  47. return;
  48. signed fx = x, fy = y;
  49. unsigned fssx = ss_x, fssy = ss_y;
  50. signed fssw = ss_w, fssh = ss_h;
  51. if (lx < 0)
  52. {
  53. fssw = ss_w + lx;
  54. fssx = -lx;
  55. fx = cx;
  56. }
  57. if (lx > cw - ss_w)
  58. {
  59. fssw -= lx - (cw - ss_w);
  60. }
  61. if (ly > ch - ss_h)
  62. {
  63. fssh -= ly - (ch - ss_h);
  64. }
  65. if (ly < 0)
  66. {
  67. fssh = ss_h + ly;
  68. fssy = -ly;
  69. fy = cy;
  70. }
  71. CXfb::draw_sprite_sheet(sheet.data, fx, fy, {fssx, fssy, fssw, fssh});
  72. }
  73. void Graphics::fill(const Pixel &color)
  74. {
  75. CXfb::buffer_fill(color);
  76. }
  77. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  78. {
  79. CXfb::draw_pixel(x, y, color.value);
  80. }
  81. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y,
  82. const Pixel &color)
  83. {
  84. if (x > x2)
  85. {
  86. uint16_t temp = x;
  87. x = x2;
  88. x2 = temp;
  89. }
  90. for (; x <= x2; x++)
  91. {
  92. CXfb::draw_pixel(x, y, color);
  93. }
  94. }
  95. void Graphics::put_vertical_line(uint16_t x, uint16_t y, uint16_t y2, const Pixel &color)
  96. {
  97. if (y > y2)
  98. {
  99. uint16_t temp = y;
  100. y = y2;
  101. y2 = temp;
  102. }
  103. for (; y <= y2; y++)
  104. {
  105. CXfb::draw_pixel(x, y, color);
  106. }
  107. }
  108. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2,
  109. const Pixel &color)
  110. {
  111. if (x == x2)
  112. {
  113. put_vertical_line(x, y, y2, color);
  114. return;
  115. }
  116. else if (y == y2)
  117. {
  118. put_horizontal_line(x, x2, y, color);
  119. return;
  120. }
  121. int dx = abs(x - x2), sx = x < x2 ? 1 : -1;
  122. int dy = abs(y - y2), sy = y < y2 ? 1 : -1;
  123. int err = (dx > dy ? dx : -dy) / 2, e2;
  124. for (;;)
  125. {
  126. put_pixel(x, y, color);
  127. if (x == x2 && y == y2)
  128. break;
  129. e2 = err;
  130. if (e2 > -dx)
  131. {
  132. err -= dy;
  133. x += sx;
  134. }
  135. if (e2 < dy)
  136. {
  137. err += dx;
  138. y += sy;
  139. }
  140. }
  141. }
  142. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  143. {
  144. uint16_t xmax = min(320, rect.x + rect.width);
  145. uint16_t ymax = min(240, rect.y + rect.height);
  146. for (uint16_t x = rect.x; x < xmax; x++)
  147. {
  148. for (uint16_t y = rect.y; y < ymax; y++)
  149. {
  150. CXfb::draw_pixel(x, y, color.value);
  151. }
  152. }
  153. }