Graphics.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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::fill(const Pixel &color)
  36. {
  37. CXfb::buffer_fill(color);
  38. }
  39. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  40. {
  41. CXfb::draw_pixel(x, y, color.value);
  42. }
  43. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y,
  44. const Pixel &color)
  45. {
  46. if (x > x2)
  47. {
  48. uint16_t temp = x;
  49. x = x2;
  50. x2 = temp;
  51. }
  52. for (; x < x2; x++)
  53. {
  54. CXfb::draw_pixel(x, y, color);
  55. }
  56. }
  57. void Graphics::put_vertical_line(uint16_t x, uint16_t y, uint16_t y2, const Pixel &color)
  58. {
  59. if (y > y2)
  60. {
  61. uint16_t temp = y;
  62. y = y2;
  63. y2 = temp;
  64. }
  65. for (; y < y2; y++)
  66. {
  67. CXfb::draw_pixel(x, y, color);
  68. }
  69. }
  70. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2,
  71. const Pixel &color)
  72. {
  73. if (x == x2)
  74. {
  75. put_vertical_line(x, y, y2, color);
  76. return;
  77. }
  78. else if (y == y2)
  79. {
  80. put_horizontal_line(x, x2, y, color);
  81. return;
  82. }
  83. int dx = abs(x - x2), sx = x < x2 ? 1 : -1;
  84. int dy = abs(y - y2), sy = y < y2 ? 1 : -1;
  85. int err = (dx > dy ? dx : -dy) / 2, e2;
  86. for (;;)
  87. {
  88. put_pixel(x, y, color);
  89. if (x == x2 && y == y2)
  90. break;
  91. e2 = err;
  92. if (e2 > -dx)
  93. {
  94. err -= dy;
  95. x += sx;
  96. }
  97. if (e2 < dy)
  98. {
  99. err += dx;
  100. y += sy;
  101. }
  102. }
  103. }
  104. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  105. {
  106. uint16_t xmax = min(320, rect.x + rect.width);
  107. uint16_t ymax = min(240, rect.y + rect.height);
  108. for (uint16_t x = rect.x; x < xmax; x++)
  109. {
  110. for (uint16_t y = rect.y; y < ymax; y++)
  111. {
  112. CXfb::draw_pixel(x, y, color.value);
  113. }
  114. }
  115. }