Graphics.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, const Rect &sprite_window, const Rect &clipping_window)
  36. {
  37. const signed &ss_x = sprite_window.x, ss_y = sprite_window.y;
  38. const signed &ss_w = sprite_window.width, &ss_h = sprite_window.height;
  39. const signed &cx = clipping_window.x, &cy = clipping_window.y;
  40. const signed &cw = clipping_window.width, &ch = clipping_window.height;
  41. const signed lx = x - cx, ly = y - cy;
  42. if(lx < -ss_w || lx > cw) return;
  43. if(ly < -ss_h || ly > ch) return;
  44. signed fx = x, fy = y;
  45. signed fssx = ss_x, fssy = ss_y, fssw = ss_w, fssh = ss_h;
  46. if(lx < 0) {
  47. fssw = ss_w+lx;
  48. fssx = -lx;
  49. fx = cx;
  50. }
  51. if(lx > cw - ss_w) {
  52. fssw -= lx-(cw - ss_w);
  53. }
  54. if(ly > ch - ss_h) {
  55. fssh -= ly-(ch - ss_h);
  56. }
  57. if(ly < 0) {
  58. fssh = ss_h+ly;
  59. fssy = -ly;
  60. fy = cy;
  61. }
  62. CXfb::draw_sprite_sheet(sheet.data, fx, fy, {fssx, fssy, fssw, fssh});
  63. }
  64. void Graphics::fill(const Pixel &color)
  65. {
  66. CXfb::buffer_fill(color);
  67. }
  68. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  69. {
  70. CXfb::draw_pixel(x, y, color.value);
  71. }
  72. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y,
  73. const Pixel &color)
  74. {
  75. if (x > x2)
  76. {
  77. uint16_t temp = x;
  78. x = x2;
  79. x2 = temp;
  80. }
  81. for (; x <= x2; x++)
  82. {
  83. CXfb::draw_pixel(x, y, color);
  84. }
  85. }
  86. void Graphics::put_vertical_line(uint16_t x, uint16_t y, uint16_t y2, const Pixel &color)
  87. {
  88. if (y > y2)
  89. {
  90. uint16_t temp = y;
  91. y = y2;
  92. y2 = temp;
  93. }
  94. for (; y <= y2; y++)
  95. {
  96. CXfb::draw_pixel(x, y, color);
  97. }
  98. }
  99. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2,
  100. const Pixel &color)
  101. {
  102. if (x == x2)
  103. {
  104. put_vertical_line(x, y, y2, color);
  105. return;
  106. }
  107. else if (y == y2)
  108. {
  109. put_horizontal_line(x, x2, y, color);
  110. return;
  111. }
  112. int dx = abs(x - x2), sx = x < x2 ? 1 : -1;
  113. int dy = abs(y - y2), sy = y < y2 ? 1 : -1;
  114. int err = (dx > dy ? dx : -dy) / 2, e2;
  115. for (;;)
  116. {
  117. put_pixel(x, y, color);
  118. if (x == x2 && y == y2)
  119. break;
  120. e2 = err;
  121. if (e2 > -dx)
  122. {
  123. err -= dy;
  124. x += sx;
  125. }
  126. if (e2 < dy)
  127. {
  128. err += dx;
  129. y += sy;
  130. }
  131. }
  132. }
  133. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  134. {
  135. uint16_t xmax = min(320, rect.x + rect.width);
  136. uint16_t ymax = min(240, rect.y + rect.height);
  137. for (uint16_t x = rect.x; x < xmax; x++)
  138. {
  139. for (uint16_t y = rect.y; y < ymax; y++)
  140. {
  141. CXfb::draw_pixel(x, y, color.value);
  142. }
  143. }
  144. }