Files
TFT_eSPI/Extensions/Sprite.h
T

186 lines
8.7 KiB
C++
Raw Permalink Normal View History

2018-02-24 19:02:20 +00:00
/***************************************************************************************
// The following class creates Sprites in RAM, graphics can then be drawn in the Sprite
// and rendered quickly onto the TFT screen. The class inherits the graphics functions
// from the TFT_eSPI class. Some functions are overridden by this class so that the
// graphics are written to the Sprite rather than the TFT.
***************************************************************************************/
class TFT_eSprite : public TFT_eSPI {
public:
TFT_eSprite(TFT_eSPI *tft);
// Create a sprite of width x height pixels, return a pointer to the RAM area
// Sketch can cast returned value to (uint16_t*) for 16 bit depth if needed
2020-01-11 00:58:38 +00:00
// RAM required is:
// - 1 bit per pixel for 1 bit colour depth
// - 1 byte per pixel for 8 bit colour
// - 2 bytes per pixel for 16 bit color depth
2020-01-11 01:27:46 +00:00
~TFT_eSprite(void);
void* createSprite(int16_t width, int16_t height, uint8_t frames = 1);
2018-02-24 19:02:20 +00:00
// Delete the sprite to free up the RAM
void deleteSprite(void);
2020-01-11 00:58:38 +00:00
// Select the frame buffer for graphics write (for 2 colour ePaper and DMA toggle buffer)
// Returns a pointer to the Sprite frame buffer
void* frameBuffer(int8_t f);
2020-01-24 22:07:45 -08:00
// Set or get the colour depth to 4, 8 or 16 bits. Can be used to change depth an existing
2018-02-24 19:02:20 +00:00
// sprite, but clears it to black, returns a new pointer if sprite is re-created.
void* setColorDepth(int8_t b);
2019-01-06 01:14:10 +00:00
int8_t getColorDepth(void);
2018-02-24 19:02:20 +00:00
2020-01-25 12:49:29 -08:00
// Set the palette for a 4 bit depth sprite. Only the first 16 colours in the map are used.
2020-01-26 21:17:49 +00:00
void createPalette(uint16_t *palette, int colors = 16); // Palette in RAM
void createPalette(const uint16_t *palette, int colors = 16); // Palette in FLASH
2020-01-25 12:49:29 -08:00
2020-01-26 21:17:49 +00:00
// Set a single palette index to the given color
2020-01-25 12:49:29 -08:00
void setPaletteColor(uint8_t index, uint16_t color);
2020-01-26 21:17:49 +00:00
// Get the color at the given palette index
2020-01-25 12:49:29 -08:00
uint16_t getPaletteColor(uint8_t index);
2020-01-24 22:07:45 -08:00
2020-01-11 00:58:38 +00:00
// Set foreground and background colours for 1 bit per pixel Sprite
void setBitmapColor(uint16_t fg, uint16_t bg);
void drawPixel(int32_t x, int32_t y, uint32_t color);
2018-02-24 19:02:20 +00:00
2020-01-11 00:58:38 +00:00
void drawChar(int32_t x, int32_t y, uint16_t c, uint32_t color, uint32_t bg, uint8_t font),
2018-02-24 19:02:20 +00:00
2020-01-11 00:58:38 +00:00
// Fill Sprite with a colour
2018-02-24 19:02:20 +00:00
fillSprite(uint32_t color),
2019-01-06 01:14:10 +00:00
// Define a window to push 16 bit colour pixels into in a raster order
2020-01-11 00:58:38 +00:00
// Colours are converted to the set Sprite colour bit depth
2018-02-24 19:02:20 +00:00
setWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1),
2020-01-11 00:58:38 +00:00
// Push a color (aka singe pixel) to the screen
2018-02-24 19:02:20 +00:00
pushColor(uint32_t color),
2020-01-11 00:58:38 +00:00
// Push len colors (pixels) to the screen
2018-02-24 19:02:20 +00:00
pushColor(uint32_t color, uint16_t len),
// Push a pixel preformatted as a 8 or 16 bit colour (avoids conversion overhead)
writeColor(uint16_t color),
// Set the scroll zone, top left corner at x,y with defined width and height
// The colour (optional, black is default) is used to fill the gap after the scroll
setScrollRect(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t color = TFT_BLACK),
2018-02-24 19:02:20 +00:00
// Scroll the defined zone dx,dy pixels. Negative values left,up, positive right,down
// dy is optional (default is then no up/down scroll).
// The sprite coordinate frame does not move because pixels are moved
scroll(int16_t dx, int16_t dy = 0),
2020-01-11 00:58:38 +00:00
// Draw lines
2018-02-24 19:02:20 +00:00
drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color),
drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color),
drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color),
2020-01-11 00:58:38 +00:00
// Fill a rectangular area with a color (aka draw a filled rectangle)
fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
2018-02-24 19:02:20 +00:00
// Set the sprite text cursor position for print class (does not change the TFT screen cursor)
2020-01-11 00:58:38 +00:00
//setCursor(int16_t x, int16_t y); // Not needed, so uses TFT class function
2020-01-11 00:58:38 +00:00
// Set the coordinate rotation of the Sprite (for 1bpp Sprites only)
// Note: this uses coordinate rotation and is primarily for ePaper which does not support
// CGRAM rotation (like TFT drivers do) within the displays internal hardware
void setRotation(uint8_t rotation);
uint8_t getRotation(void);
2018-02-24 19:02:20 +00:00
2019-01-06 01:14:10 +00:00
// Push a rotated copy of Sprite to TFT with optional transparent colour
2020-01-11 00:58:38 +00:00
bool pushRotated(int16_t angle, int32_t transp = -1); // Using fixed point maths
2019-01-06 01:14:10 +00:00
// Push a rotated copy of Sprite to another different Sprite with optional transparent colour
2020-01-11 00:58:38 +00:00
bool pushRotated(TFT_eSprite *spr, int16_t angle, int32_t transp = -1); // Using fixed point maths
2020-03-20 00:30:12 +00:00
// Set and get the pivot point for this Sprite
2019-01-06 01:14:10 +00:00
void setPivot(int16_t x, int16_t y);
int16_t getPivotX(void),
getPivotY(void);
2020-03-20 00:30:12 +00:00
// Get the TFT bounding box for a rotated copy of this Sprite
bool getRotatedBounds(int16_t angle, int16_t *min_x, int16_t *min_y, int16_t *max_x, int16_t *max_y);
// Get the destination Sprite bounding box for a rotated copy of this Sprite
bool getRotatedBounds(TFT_eSprite *spr, int16_t angle, int16_t *min_x, int16_t *min_y,
int16_t *max_x, int16_t *max_y);
// Bounding box support function
void getRotatedBounds(int16_t angle, int16_t w, int16_t h, int16_t xp, int16_t yp,
int16_t *min_x, int16_t *min_y, int16_t *max_x, int16_t *max_y);
2019-01-06 01:14:10 +00:00
2018-02-24 19:02:20 +00:00
// Read the colour of a pixel at x,y and return value in 565 format
uint16_t readPixel(int32_t x0, int32_t y0);
2020-03-30 21:51:26 +01:00
// return the numerical value of the pixel at x,y (used when scrolling)
// 16bpp = colour, 8bpp = byte, 4bpp = colour index, 1bpp = 1 or 0
uint16_t readPixelValue(int32_t x, int32_t y);
2020-01-24 22:07:45 -08:00
// Write an image (colour bitmap) to the sprite. Not implemented for _bpp == 4.
void pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, uint16_t *data);
void pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, const uint16_t *data);
2018-02-24 19:02:20 +00:00
// Swap the byte order for pushImage() - corrects different image endianness
void setSwapBytes(bool swap);
bool getSwapBytes(void);
// Push the sprite to the TFT screen, this fn calls pushImage() in the TFT class.
// Optionally a "transparent" colour can be defined, pixels of that colour will not be rendered
void pushSprite(int32_t x, int32_t y);
void pushSprite(int32_t x, int32_t y, uint16_t transparent);
int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font),
drawChar(uint16_t uniCode, int32_t x, int32_t y);
2018-02-24 19:02:20 +00:00
// Return the width and height of the sprite
int16_t width(void),
height(void);
// Used by print class to print text to cursor position
size_t write(uint8_t);
// Functions associated with anti-aliased fonts
void drawGlyph(uint16_t code);
void printToSprite(String string);
void printToSprite(char *cbuffer, uint16_t len);
2018-02-24 19:02:20 +00:00
int16_t printToSprite(int16_t x, int16_t y, uint16_t index);
private:
TFT_eSPI *_tft;
2019-01-06 01:14:10 +00:00
// Reserve memory for the Sprite and return a pointer
void* callocSprite(int16_t width, int16_t height, uint8_t frames = 1);
2018-02-24 19:02:20 +00:00
protected:
2019-01-06 01:14:10 +00:00
uint8_t _bpp; // bits per pixel (1, 8 or 16)
uint16_t *_img; // pointer to 16 bit sprite
uint8_t *_img8; // pointer to 8 bit sprite
2020-01-24 22:07:45 -08:00
uint8_t *_img4; // pointer to 4 bit sprite (uses color map)
uint8_t *_img8_1; // pointer to frame 1
uint8_t *_img8_2; // pointer to frame 2
2018-02-24 19:02:20 +00:00
2020-01-24 22:07:45 -08:00
uint16_t *_colorMap; // color map: 16 entries, used with 4 bit color map.
2020-02-06 20:34:36 +00:00
int16_t _xpivot; // x pivot point coordinate
int16_t _ypivot; // y pivot point coordinate
2020-03-20 00:30:12 +00:00
int32_t _sinra;
int32_t _cosra;
2019-01-06 01:14:10 +00:00
bool _created; // A Sprite has been created and memory reserved
2018-02-24 19:02:20 +00:00
bool _gFont = false;
// int32_t _icursor_x, _icursor_y;
uint8_t _rotation = 0;
2018-02-24 19:02:20 +00:00
int32_t _xs, _ys, _xe, _ye, _xptr, _yptr; // for setWindow
int32_t _sx, _sy; // x,y for scroll zone
uint32_t _sw, _sh; // w,h for scroll zone
uint32_t _scolor; // gap fill colour for scroll zone
2020-02-06 20:34:36 +00:00
bool _iswapBytes; // Swap the byte order for Sprite pushImage()
2018-02-24 19:02:20 +00:00
int32_t _iwidth, _iheight; // Sprite memory image bit width and height (swapped during rotations)
int32_t _dwidth, _dheight; // Real display width and height (for <8bpp Sprites)
int32_t _bitwidth; // Sprite image bit width for drawPixel (for <8bpp Sprites, not swapped)
2018-02-24 19:02:20 +00:00
};