Graphics.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "Graphics.h"
  2. #include "render/Pixel.h"
  3. #include "utility/Rect.h"
  4. #include "Logger.h"
  5. #include "utility/misc.h"
  6. #include "utility/minmax.h"
  7. #include <3ds.h>
  8. #include <sf2d.h>
  9. using namespace WalrusRPG; /*::Graphics*/
  10. using WalrusRPG::Graphics::Pixel;
  11. using WalrusRPG::Utils::Rect;
  12. sf2d_rendertarget *target = nullptr;
  13. inline u32 pixel2u32(const Pixel &pix)
  14. {
  15. return RGBA8(pix.r << 3, pix.g << 2, pix.b << 3, 0xFF);
  16. }
  17. namespace
  18. {
  19. constexpr int OFFSET_X = 40;
  20. constexpr int OFFSET_Y = 0;
  21. void set_scissor()
  22. {
  23. sf2d_set_scissor_test(GPU_SCISSOR_NORMAL, 80, 0, 320, 240);
  24. }
  25. }
  26. void Graphics::init()
  27. {
  28. // Logger::log("Graphics init");
  29. sf2d_init();
  30. sf2d_set_clear_color(0);
  31. sf2d_set_3D(0);
  32. sf2d_set_vblank_wait(1);
  33. set_scissor();
  34. }
  35. void Graphics::deinit()
  36. {
  37. Logger::log("Graphics deinit");
  38. sf2d_free_target(target);
  39. sf2d_fini();
  40. }
  41. void Graphics::frame_begin()
  42. {
  43. sf2d_start_frame(GFX_TOP, GFX_LEFT);
  44. }
  45. void Graphics::frame_end()
  46. {
  47. sf2d_draw_rectangle(0, 0, OFFSET_X, 240, 0xFF000000);
  48. sf2d_draw_rectangle(320 + OFFSET_X, 0, OFFSET_X, 240, 0xFF000000);
  49. sf2d_end_frame();
  50. sf2d_swapbuffers();
  51. }
  52. void Graphics::put_sprite(const Texture &sheet, int x, int y, const Rect &window)
  53. {
  54. sf2d_draw_texture_part(sheet.data, x + OFFSET_X, y + OFFSET_Y, window.x, window.y,
  55. window.width, window.height);
  56. }
  57. void Graphics::put_sprite_tint(const Texture &sheet, int x, int y, const Rect &window,
  58. const Pixel &color)
  59. {
  60. sf2d_draw_texture_part_blend(sheet.data, x + OFFSET_X, y + OFFSET_Y, window.x,
  61. window.y, window.width, window.height, pixel2u32(color));
  62. }
  63. void Graphics::put_sprite_clipping(const Texture &sheet, int x, int y,
  64. const Rect &sprite_window, const Rect &clipping_window)
  65. {
  66. sf2d_set_scissor_test(GPU_SCISSOR_NORMAL, clipping_window.x + OFFSET_X,
  67. clipping_window.y + OFFSET_Y, clipping_window.width,
  68. clipping_window.height);
  69. sf2d_draw_texture_part(sheet.data, x, y, sprite_window.x, sprite_window.y,
  70. sprite_window.width, sprite_window.height);
  71. set_scissor();
  72. }
  73. void Graphics::fill(const Pixel &color)
  74. {
  75. sf2d_clear_target(target, pixel2u32(color));
  76. }
  77. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  78. {
  79. // Why both of them at +1? If only I knew...
  80. sf2d_draw_line(x, y, x+1, y+1, 1, pixel2u32(color));
  81. }
  82. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y,
  83. const Pixel &color)
  84. {
  85. // Because sf2dlib has issues with lines, let's port Nspire's functions.
  86. if (x > x2)
  87. {
  88. uint16_t temp = x;
  89. x = x2;
  90. x2 = temp;
  91. }
  92. for (; x <= x2; x++)
  93. {
  94. put_pixel(x + OFFSET_X, y + OFFSET_Y, color);
  95. }
  96. }
  97. void Graphics::put_vertical_line(uint16_t x, uint16_t y, uint16_t y2, const Pixel &color)
  98. {
  99. // Because sf2dlib has issues with lines, let's port Nspire's functions.
  100. if (y > y2)
  101. {
  102. uint16_t temp = y;
  103. y = y2;
  104. y2 = temp;
  105. }
  106. for (; y <= y2; y++)
  107. {
  108. put_pixel(x + OFFSET_X, y + OFFSET_Y, color);
  109. }
  110. }
  111. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2,
  112. const Pixel &color)
  113. {
  114. // Because sf2dlib has issues with lines, let's port Nspire's functions.
  115. if (x == x2)
  116. {
  117. put_vertical_line(x + OFFSET_X, y + OFFSET_Y, y2 + OFFSET_Y, color);
  118. return;
  119. }
  120. else if (y == y2)
  121. {
  122. put_horizontal_line(x + OFFSET_X, x2 + OFFSET_X, y + OFFSET_Y, color);
  123. return;
  124. }
  125. int dx = abs(x - x2), sx = x < x2 ? 1 : -1;
  126. int dy = abs(y - y2), sy = y < y2 ? 1 : -1;
  127. int err = (dx > dy ? dx : -dy) / 2, e2;
  128. while(true)
  129. {
  130. put_pixel(x + OFFSET_X, y + OFFSET_Y, color);
  131. if (x == x2 && y == y2)
  132. break;
  133. e2 = err;
  134. if (e2 > -dx)
  135. {
  136. err -= dy;
  137. x += sx;
  138. }
  139. if (e2 < dy)
  140. {
  141. err += dx;
  142. y += sy;
  143. }
  144. }
  145. }
  146. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  147. {
  148. sf2d_draw_rectangle(rect.x + OFFSET_X, rect.y + OFFSET_Y, rect.width, rect.height,
  149. pixel2u32(color));
  150. }