Graphics.cpp 3.7 KB

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