acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Sprite.h (9556B)

      1 /***************************************************************************************
      2 // The following class creates Sprites in RAM, graphics can then be drawn in the Sprite
      3 // and rendered quickly onto the TFT screen. The class inherits the graphics functions
      4 // from the TFT_eSPI class. Some functions are overridden by this class so that the
      5 // graphics are written to the Sprite rather than the TFT.
      6 ***************************************************************************************/
      7 
      8 class TFT_eSprite : public TFT_eSPI {
      9 
     10  public:
     11 
     12   explicit TFT_eSprite(TFT_eSPI *tft);
     13   ~TFT_eSprite(void);
     14 
     15            // Create a sprite of width x height pixels, return a pointer to the RAM area
     16            // Sketch can cast returned value to (uint16_t*) for 16 bit depth if needed
     17            // RAM required is:
     18            //  - 1 bit per pixel for 1 bit colour depth
     19            //  - 1 nibble per pixel for 4 bit colour (with palette table)
     20            //  - 1 byte per pixel for 8 bit colour (332 RGB format)
     21            //  - 2 bytes per pixel for 16 bit color depth (565 RGB format)
     22   void*    createSprite(int16_t width, int16_t height, uint8_t frames = 1);
     23 
     24            // Returns a pointer to the sprite or nullptr if not created, user must cast to pointer type
     25   void*    getPointer(void);
     26 
     27            // Returns true if sprite has been created
     28   bool     created(void);
     29 
     30            // Delete the sprite to free up the RAM
     31   void     deleteSprite(void);
     32 
     33            // Select the frame buffer for graphics write (for 2 colour ePaper and DMA toggle buffer)
     34            // Returns a pointer to the Sprite frame buffer
     35   void*    frameBuffer(int8_t f);
     36   
     37            // Set or get the colour depth to 1, 4, 8 or 16 bits. Can be used to change depth an existing
     38            // sprite, but clears it to black, returns a new pointer if sprite is re-created.
     39   void*    setColorDepth(int8_t b);
     40   int8_t   getColorDepth(void);
     41 
     42            // Set the palette for a 4 bit depth sprite.  Only the first 16 colours in the map are used.
     43   void     createPalette(uint16_t *palette = nullptr, uint8_t colors = 16);       // Palette in RAM
     44   void     createPalette(const uint16_t *palette = nullptr, uint8_t colors = 16); // Palette in FLASH
     45 
     46            // Set a single palette index to the given color
     47   void     setPaletteColor(uint8_t index, uint16_t color);
     48 
     49            // Get the color at the given palette index
     50   uint16_t getPaletteColor(uint8_t index);
     51 
     52            // Set foreground and background colours for 1 bit per pixel Sprite
     53   void     setBitmapColor(uint16_t fg, uint16_t bg);
     54 
     55            // Draw a single pixel at x,y
     56   void     drawPixel(int32_t x, int32_t y, uint32_t color);
     57 
     58            // Draw a single character in the GLCD or GFXFF font
     59   void     drawChar(int32_t x, int32_t y, uint16_t c, uint32_t color, uint32_t bg, uint8_t size),
     60 
     61            // Fill Sprite with a colour
     62            fillSprite(uint32_t color),
     63 
     64            // Define a window to push 16 bit colour pixels into in a raster order
     65            // Colours are converted to the set Sprite colour bit depth
     66            setWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1),
     67            // Push a color (aka singe pixel) to the sprite's set window area
     68            pushColor(uint16_t color),
     69            // Push len colors (pixels) to the sprite's set window area
     70            pushColor(uint16_t color, uint32_t len),
     71            // Push a pixel pre-formatted as a 1, 4, 8 or 16 bit colour (avoids conversion overhead)
     72            writeColor(uint16_t color),
     73 
     74            // Set the scroll zone, top left corner at x,y with defined width and height
     75            // The colour (optional, black is default) is used to fill the gap after the scroll
     76            setScrollRect(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t color = TFT_BLACK),
     77            // Scroll the defined zone dx,dy pixels. Negative values left,up, positive right,down
     78            // dy is optional (default is 0, so no up/down scroll).
     79            // The sprite coordinate frame does not move because pixels are moved
     80            scroll(int16_t dx, int16_t dy = 0),
     81 
     82            // Draw lines
     83            drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color),
     84            drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color),
     85            drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color),
     86 
     87            // Fill a rectangular area with a color (aka draw a filled rectangle)
     88            fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
     89 
     90            // Set the coordinate rotation of the Sprite (for 1bpp Sprites only)
     91            // Note: this uses coordinate rotation and is primarily for ePaper which does not support
     92            // CGRAM rotation (like TFT drivers do) within the displays internal hardware
     93   void     setRotation(uint8_t rotation);
     94   uint8_t  getRotation(void);
     95 
     96            // Push a rotated copy of Sprite to TFT with optional transparent colour
     97   bool     pushRotated(int16_t angle, uint32_t transp = 0x00FFFFFF);
     98            // Push a rotated copy of Sprite to another different Sprite with optional transparent colour
     99   bool     pushRotated(TFT_eSprite *spr, int16_t angle, uint32_t transp = 0x00FFFFFF);
    100 
    101            // Get the TFT bounding box for a rotated copy of this Sprite
    102   bool     getRotatedBounds(int16_t angle, int16_t *min_x, int16_t *min_y, int16_t *max_x, int16_t *max_y);
    103            // Get the destination Sprite bounding box for a rotated copy of this Sprite
    104   bool     getRotatedBounds(TFT_eSprite *spr, int16_t angle, int16_t *min_x, int16_t *min_y,
    105                                                              int16_t *max_x, int16_t *max_y);
    106            // Bounding box support function
    107   void     getRotatedBounds(int16_t angle, int16_t w, int16_t h, int16_t xp, int16_t yp,
    108                             int16_t *min_x, int16_t *min_y, int16_t *max_x, int16_t *max_y);
    109 
    110            // Read the colour of a pixel at x,y and return value in 565 format 
    111   uint16_t readPixel(int32_t x0, int32_t y0);
    112 
    113            // return the numerical value of the pixel at x,y (used when scrolling)
    114            // 16bpp = colour, 8bpp = byte, 4bpp = colour index, 1bpp = 1 or 0
    115   uint16_t readPixelValue(int32_t x, int32_t y);
    116 
    117            // Write an image (colour bitmap) to the sprite.
    118   void     pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, uint16_t *data, uint8_t sbpp = 0);
    119   void     pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, const uint16_t *data);
    120 
    121            // Push the sprite to the TFT screen, this fn calls pushImage() in the TFT class.
    122            // Optionally a "transparent" colour can be defined, pixels of that colour will not be rendered
    123   void     pushSprite(int32_t x, int32_t y);
    124   void     pushSprite(int32_t x, int32_t y, uint16_t transparent);
    125 
    126            // Push a windowed area of the sprite to the TFT at tx, ty
    127   bool     pushSprite(int32_t tx, int32_t ty, int32_t sx, int32_t sy, int32_t sw, int32_t sh);
    128 
    129            // Push the sprite to another sprite at x,y. This fn calls pushImage() in the destination sprite (dspr) class.
    130   bool     pushToSprite(TFT_eSprite *dspr, int32_t x, int32_t y);
    131   bool     pushToSprite(TFT_eSprite *dspr, int32_t x, int32_t y, uint16_t transparent);
    132 
    133            // Draw a single character in the selected font
    134   int16_t  drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font),
    135            drawChar(uint16_t uniCode, int32_t x, int32_t y);
    136 
    137            // Return the width and height of the sprite
    138   int16_t  width(void),
    139            height(void);
    140 
    141            // Functions associated with anti-aliased fonts
    142            // Draw a single unicode character using the loaded font
    143   void     drawGlyph(uint16_t code);
    144            // Print string to sprite using loaded font at cursor position
    145   void     printToSprite(String string);
    146            // Print char array to sprite using loaded font at cursor position
    147   void     printToSprite(char *cbuffer, uint16_t len);
    148            // Print indexed glyph to sprite using loaded font at x,y
    149   int16_t  printToSprite(int16_t x, int16_t y, uint16_t index);
    150 
    151  private:
    152 
    153   TFT_eSPI *_tft;
    154 
    155            // Reserve memory for the Sprite and return a pointer
    156   void*    callocSprite(int16_t width, int16_t height, uint8_t frames = 1);
    157 
    158            // Override the non-inlined TFT_eSPI functions
    159   void     begin_nin_write(void) { ; }
    160   void     end_nin_write(void) { ; }
    161 
    162  protected:
    163 
    164   uint8_t  _bpp;     // bits per pixel (1, 4, 8 or 16)
    165   uint16_t *_img;    // pointer to 16 bit sprite
    166   uint8_t  *_img8;   // pointer to  1 and 8 bit sprite frame 1 or frame 2
    167   uint8_t  *_img4;   // pointer to  4 bit sprite (uses color map)
    168   uint8_t  *_img8_1; // pointer to frame 1
    169   uint8_t  *_img8_2; // pointer to frame 2
    170 
    171   uint16_t *_colorMap; // color map pointer: 16 entries, used with 4 bit color map.
    172 
    173   int32_t  _sinra;   // Sine of rotation angle in fixed point
    174   int32_t  _cosra;   // Cosine of rotation angle in fixed point
    175 
    176   bool     _created; // A Sprite has been created and memory reserved
    177   bool     _gFont = false; 
    178 
    179   int32_t  _xs, _ys, _xe, _ye, _xptr, _yptr; // for setWindow
    180   int32_t  _sx, _sy; // x,y for scroll zone
    181   uint32_t _sw, _sh; // w,h for scroll zone
    182   uint32_t _scolor;  // gap fill colour for scroll zone
    183 
    184   int32_t  _iwidth, _iheight; // Sprite memory image bit width and height (swapped during rotations)
    185   int32_t  _dwidth, _dheight; // Real sprite width and height (for <8bpp Sprites)
    186   int32_t  _bitwidth;         // Sprite image bit width for drawPixel (for <8bpp Sprites, not swapped)
    187 
    188 };