Graphics.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. constexpr int OFFSET_X = 40;
  19. constexpr int OFFSET_Y = 0;
  20. void set_scissor()
  21. {
  22. sf2d_set_scissor_test(GPU_SCISSOR_NORMAL, 80, 0, 320, 240);
  23. }
  24. }
  25. void Graphics::init()
  26. {
  27. // Logger::log("Graphics init");
  28. sf2d_init();
  29. sf2d_set_clear_color(0);
  30. sf2d_set_3D(0);
  31. sf2d_set_vblank_wait(1);
  32. set_scissor();
  33. }
  34. void Graphics::deinit()
  35. {
  36. Logger::log("Graphics deinit");
  37. sf2d_free_target(target);
  38. sf2d_fini();
  39. }
  40. void Graphics::frame_begin()
  41. {
  42. sf2d_start_frame(GFX_TOP, GFX_LEFT);
  43. }
  44. void Graphics::frame_end()
  45. {
  46. sf2d_draw_rectangle(0, 0, OFFSET_X, 240, 0xFF000000);
  47. sf2d_draw_rectangle(320+OFFSET_X, 0, OFFSET_X, 240, 0xFF000000);
  48. sf2d_end_frame();
  49. sf2d_swapbuffers();
  50. }
  51. void Graphics::put_sprite(const Texture &sheet, int x, int y, const Rect &window)
  52. {
  53. sf2d_draw_texture_part(sheet.data, x+OFFSET_X, y+OFFSET_Y, window.x, window.y, window.width, window.height);
  54. }
  55. void Graphics::put_sprite_tint(const Texture &sheet, int x, int y, const Rect &window,
  56. const Pixel &color)
  57. {
  58. sf2d_draw_texture_part_blend(sheet.data, x+OFFSET_X, y+OFFSET_Y, window.x, window.y, window.width, window.height, pixel2u32(color));
  59. }
  60. void Graphics::put_sprite_clipping(const Texture &sheet, int x, int y,
  61. const Rect &sprite_window, const Rect &clipping_window)
  62. {
  63. sf2d_set_scissor_test(GPU_SCISSOR_NORMAL, clipping_window.x+OFFSET_X, clipping_window.y+OFFSET_Y, clipping_window.width, clipping_window.height);
  64. sf2d_draw_texture_part(sheet.data, x, y, sprite_window.x, sprite_window.y, sprite_window.width, sprite_window.height);
  65. set_scissor();
  66. }
  67. void Graphics::fill(const Pixel &color)
  68. {
  69. sf2d_clear_target(target, pixel2u32(color));
  70. }
  71. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  72. {
  73. sf2d_set_pixel(&(target->texture), x+OFFSET_X, y+OFFSET_Y, pixel2u32(color));
  74. }
  75. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y,
  76. const Pixel &color)
  77. {
  78. // Because sf2dlib has issues with lines, let's port Nspire's functions.
  79. if (x > x2)
  80. {
  81. uint16_t temp = x;
  82. x = x2;
  83. x2 = temp;
  84. }
  85. for (; x <= x2; x++)
  86. {
  87. put_pixel(x+OFFSET_X, y+OFFSET_Y, color);
  88. }
  89. }
  90. void Graphics::put_vertical_line(uint16_t x, uint16_t y, uint16_t y2, const Pixel &color)
  91. {
  92. // Because sf2dlib has issues with lines, let's port Nspire's functions.
  93. if (y > y2)
  94. {
  95. uint16_t temp = y;
  96. y = y2;
  97. y2 = temp;
  98. }
  99. for (; y <= y2; y++)
  100. {
  101. put_pixel(x+OFFSET_X, y+OFFSET_Y, color);
  102. }}
  103. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2,
  104. const Pixel &color)
  105. {
  106. // Because sf2dlib has issues with lines, let's port Nspire's functions.
  107. if (x == x2)
  108. {
  109. put_vertical_line(x+OFFSET_X, y+OFFSET_Y, y2+OFFSET_Y, color);
  110. return;
  111. }
  112. else if (y == y2)
  113. {
  114. put_horizontal_line(x+OFFSET_X, x2+OFFSET_X, y+OFFSET_Y, color);
  115. return;
  116. }
  117. int dx = abs(x - x2), sx = x < x2 ? 1 : -1;
  118. int dy = abs(y - y2), sy = y < y2 ? 1 : -1;
  119. int err = (dx > dy ? dx : -dy) / 2, e2;
  120. for (;;)
  121. {
  122. put_pixel(x+OFFSET_X, y+OFFSET_Y, color);
  123. if (x == x2 && y == y2)
  124. break;
  125. e2 = err;
  126. if (e2 > -dx)
  127. {
  128. err -= dy;
  129. x += sx;
  130. }
  131. if (e2 < dy)
  132. {
  133. err += dx;
  134. y += sy;
  135. }
  136. }}
  137. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  138. {
  139. sf2d_draw_rectangle(rect.x+OFFSET_X, rect.y+OFFSET_Y, rect.width, rect.height, pixel2u32(color));
  140. }