Graphics.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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,
  26. 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,
  31. const Rect &window,
  32. const Pixel &color)
  33. {
  34. CXfb::draw_sprite_sheet_tint(sheet.data, x, y, window, color.value);
  35. }
  36. void Graphics::fill(const Pixel &color)
  37. {
  38. CXfb::buffer_fill(color);
  39. }
  40. void Graphics::put_pixel(uint16_t x, uint16_t y, const Pixel &color)
  41. {
  42. CXfb::draw_pixel(x, y, color.value);
  43. }
  44. void Graphics::put_horizontal_line(uint16_t x, uint16_t x2, uint16_t y, 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. 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. CXfb::draw_pixel(x, y, color);
  66. }
  67. }
  68. void Graphics::put_line(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2, const Pixel& color)
  69. {
  70. if(x == x2)
  71. {
  72. put_vertical_line(x, y, y2, color);
  73. return;
  74. }
  75. else if(y == y2)
  76. {
  77. put_horizontal_line(x, x2, y, color);
  78. return;
  79. }
  80. int dx = abs(x-x2), sx = x<x2 ? 1 : -1;
  81. int dy = abs(y-y2), sy = y<y2 ? 1 : -1;
  82. int err = (dx>dy ? dx : -dy)/2, e2;
  83. for(;;){
  84. put_pixel(x,y, color);
  85. if (x==x2 && y==y2) break;
  86. e2 = err;
  87. if (e2 >-dx) { err -= dy; x += sx; }
  88. if (e2 < dy) { err += dx; y += sy; }
  89. }
  90. }
  91. void Graphics::put_rectangle(const Rect &rect, const Pixel &color)
  92. {
  93. uint16_t xmax = min(320, rect.x + rect.width);
  94. uint16_t ymax = min(240, rect.y + rect.height);
  95. for(uint16_t x = rect.x; x < xmax; x++)
  96. {
  97. for(uint16_t y = rect.y; y < ymax; y++)
  98. {
  99. CXfb::draw_pixel(x, y, color.value);
  100. }
  101. }
  102. }