Graphics.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "Graphics.h"
  2. #include "CXfb.h"
  3. #include "stdio.h"
  4. #include "utility/misc.h"
  5. using namespace Nspire;
  6. using namespace WalrusRPG;
  7. using WalrusRPG::Graphics::Texture;
  8. using WalrusRPG::Graphics::Pixel;
  9. using WalrusRPG::Utils::Rect;
  10. void Graphics::init()
  11. {
  12. CXfb::buffer_allocate();
  13. }
  14. void Graphics::deinit()
  15. {
  16. CXfb::buffer_free();
  17. }
  18. void Graphics::frame_begin()
  19. {
  20. }
  21. void Graphics::frame_end()
  22. {
  23. CXfb::buffer_swap_render();
  24. }
  25. void Graphics::put_sprite(const Texture &sheet, int x, int y, const Rect &window)
  26. {
  27. CXfb::draw_sprite_sheet(sheet.data, x, y, window);
  28. }
  29. void Graphics::put_sprite_tint(const Texture &sheet, int x, int y, const Rect &window,
  30. const Pixel &color)
  31. {
  32. CXfb::draw_sprite_sheet_tint(sheet.data, x, y, window, color.value);
  33. }
  34. void Graphics::fill(const Pixel &color)
  35. {
  36. CXfb::buffer_fill(color);
  37. }
  38. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  39. {
  40. CXfb::draw_pixel(x, y, color.value);
  41. }
  42. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y,
  43. const Pixel &color)
  44. {
  45. if (x > x2)
  46. {
  47. uint16_t temp = x;
  48. x = x2;
  49. x2 = temp;
  50. }
  51. for (; x < x2; x++)
  52. {
  53. CXfb::draw_pixel(x, y, color);
  54. }
  55. }
  56. void Graphics::put_vertical_line(uint16_t x, uint16_t y, uint16_t y2, const Pixel &color)
  57. {
  58. if (y > y2)
  59. {
  60. uint16_t temp = y;
  61. y = y2;
  62. y2 = temp;
  63. }
  64. for (; y < y2; y++)
  65. {
  66. CXfb::draw_pixel(x, y, color);
  67. }
  68. }
  69. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2,
  70. const Pixel &color)
  71. {
  72. if (x == x2)
  73. {
  74. put_vertical_line(x, y, y2, color);
  75. return;
  76. }
  77. else if (y == y2)
  78. {
  79. put_horizontal_line(x, x2, y, color);
  80. return;
  81. }
  82. int dx = abs(x - x2), sx = x < x2 ? 1 : -1;
  83. int dy = abs(y - y2), sy = y < y2 ? 1 : -1;
  84. int err = (dx > dy ? dx : -dy) / 2, e2;
  85. for (;;)
  86. {
  87. put_pixel(x, y, color);
  88. if (x == x2 && y == y2)
  89. break;
  90. e2 = err;
  91. if (e2 > -dx)
  92. {
  93. err -= dy;
  94. x += sx;
  95. }
  96. if (e2 < dy)
  97. {
  98. err += dx;
  99. y += sy;
  100. }
  101. }
  102. }
  103. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  104. {
  105. uint16_t xmax = min(320, rect.x + rect.width);
  106. uint16_t ymax = min(240, rect.y + rect.height);
  107. for (uint16_t x = rect.x; x < xmax; x++)
  108. {
  109. for (uint16_t y = rect.y; y < ymax; y++)
  110. {
  111. CXfb::draw_pixel(x, y, color.value);
  112. }
  113. }
  114. }