Buffer.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "Buffer.h"
  2. #include "Sprite.h"
  3. Buffer::Buffer(uint8_t w, uint8_t h) : _width(w), _height(h) {
  4. data = (uint8_t *) malloc(sizeof(uint8_t) * w * ceil(h / 8.0));
  5. }
  6. Buffer::Buffer(const SpriteData *i) {
  7. FURI_LOG_D("BUFFER","Buffer init");
  8. _width = i->width;
  9. _height = i->height;//ceil(i->height / 8.0);
  10. data = (i->data);
  11. remove_buffer = false;
  12. }
  13. Buffer::~Buffer() {
  14. FURI_LOG_D("BUFFER", "Buffer removed");
  15. if (data && remove_buffer)
  16. delete data;
  17. }
  18. bool Buffer::test_pixel(uint8_t x, uint8_t y) {
  19. return data[(y >> 3) * _width + x] & (1 << (y & 7));
  20. }
  21. void Buffer::copy_into(uint8_t *other) {
  22. int size = (int) (_width * ceil(_height / 8.0));
  23. for (int i = 0; i < size; i++) {
  24. other[i] = data[i];
  25. }
  26. }
  27. void Buffer::copy_from(uint8_t *other) {
  28. int size = (int) (_width * ceil(_height / 8.0));
  29. for (int i = 0; i < size; i++) {
  30. data[i] = other[i];
  31. }
  32. }
  33. void Buffer::clear() {
  34. int size = (int) (_width * ceil(_height / 8.0));
  35. for (int i = 0; i < size; i++) {
  36. data[i] = 0;
  37. }
  38. }
  39. bool Buffer::test_coordinate(int x, int y) const {
  40. return x >= 0 && y >= 0 && x < _width && y < _height;
  41. }
  42. void Buffer::set_pixel_with_check(int16_t x, int16_t y, PixelColor draw_mode) {
  43. if (test_pixel(x, y))
  44. set_pixel(x, y, draw_mode);
  45. }
  46. void Buffer::set_pixel(int16_t x, int16_t y, PixelColor draw_mode) {
  47. uint8_t bit = 1 << (y & 7);
  48. uint8_t *p = data + (y >> 3) * width() + x;
  49. switch (draw_mode) {
  50. case Black:
  51. *p |= bit;
  52. break;
  53. case White:
  54. *p &= ~bit;
  55. break;
  56. case Flip:
  57. *p ^= bit;
  58. break;
  59. }
  60. }
  61. void Buffer::swap(uint8_t *&buffer) {
  62. uint8_t *back = data;
  63. data = buffer;
  64. buffer = back;
  65. }