acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

Transparent_Sprite_Demo.ino (4568B)

      1 /*
      2   Sketch to show creation of a sprite with a transparent
      3   background, then plot it on the TFT.
      4 
      5   Example for library:
      6   https://github.com/Bodmer/TFT_eSPI
      7 
      8   A Sprite is notionally an invisible graphics screen that is
      9   kept in the processors RAM. Graphics can be drawn into the
     10   Sprite just as it can be drawn directly to the screen. Once
     11   the Sprite is completed it can be plotted onto the screen in
     12   any position. If there is sufficient RAM then the Sprite can
     13   be the same size as the screen and used as a frame buffer.
     14 
     15   A 16 bit Sprite occupies (2 * width * height) bytes in RAM.
     16 
     17   On a ESP8266 Sprite sizes up to 126 x 160 can be accommodated,
     18   this size requires 40kBytes of RAM for a 16 bit colour depth.
     19   
     20   When 8 bit colour depth sprites are created they occupy
     21   (width * height) bytes in RAM, so larger sprites can be
     22   created, or the RAM required is halved.
     23 */
     24 
     25 #include <TFT_eSPI.h>                 // Include the graphics library (this includes the sprite functions)
     26 
     27 TFT_eSPI    tft = TFT_eSPI();         // Create object "tft"
     28 
     29 TFT_eSprite img = TFT_eSprite(&tft);  // Create Sprite object "img" with pointer to "tft" object
     30                                       // the pointer is used by pushSprite() to push it onto the TFT
     31 
     32 void setup(void) {
     33   Serial.begin(250000);
     34 
     35   tft.init();
     36 
     37   tft.setRotation(0);
     38 }
     39 
     40 void loop() {
     41 
     42   tft.fillScreen(TFT_NAVY);
     43 
     44   // Draw 10 sprites containing a "transparent" colour
     45   for (int i = 0; i < 10; i++)
     46   {
     47     int x = random(240-70);
     48     int y = random(320-80);
     49     int c = random(0x10000); // Random colour
     50     drawStar(x, y, c);
     51   }
     52 
     53   delay(2000);
     54 
     55   uint32_t dt = millis();
     56 
     57   // Now go bananas and draw 500 more
     58   for (int i = 0; i < 500; i++)
     59   {
     60     int x = random(240-70);
     61     int y = random(320-80);
     62     int c = random(0x10000); // Random colour
     63     drawStar(x, y, c);
     64     yield(); // Stop watchdog reset
     65   }
     66 
     67   // Show time in milliseconds to draw and then push 1 sprite to TFT screen
     68   numberBox( 10, 10, (millis()-dt)/500.0 );
     69 
     70   delay(2000);
     71 
     72 }
     73 
     74 // #########################################################################
     75 // Create sprite, plot graphics in it, plot to screen, then delete sprite
     76 // #########################################################################
     77 void drawStar(int x, int y, int star_color)
     78 {
     79   // Create an 8 bit sprite 70x 80 pixels (uses 5600 bytes of RAM)
     80   img.setColorDepth(8);
     81   img.createSprite(70, 80);
     82 
     83   // Fill Sprite with a "transparent" colour
     84   // TFT_TRANSPARENT is already defined for convenience
     85   // We could also fill with any colour as "transparent" and later specify that
     86   // same colour when we push the Sprite onto the screen.
     87   img.fillSprite(TFT_TRANSPARENT);
     88 
     89   // Draw 2 triangles to create a filled in star
     90   img.fillTriangle(35, 0, 0,59, 69,59, star_color);
     91   img.fillTriangle(35,79, 0,20, 69,20, star_color);
     92 
     93   // Punch a star shaped hole in the middle with a smaller transparent star
     94   img.fillTriangle(35, 7, 6,56, 63,56, TFT_TRANSPARENT);
     95   img.fillTriangle(35,73, 6,24, 63,24, TFT_TRANSPARENT);
     96 
     97   // Push sprite to TFT screen CGRAM at coordinate x,y (top left corner)
     98   // Specify what colour is to be treated as transparent.
     99   img.pushSprite(x, y, TFT_TRANSPARENT);
    100 
    101   // Delete it to free memory
    102   img.deleteSprite();
    103  
    104 }
    105 
    106 // #########################################################################
    107 // Draw a number in a rounded rectangle with some transparent pixels
    108 // #########################################################################
    109 void numberBox(int x, int y, float num )
    110 {
    111 
    112   // Size of sprite
    113   #define IWIDTH  80
    114   #define IHEIGHT 35
    115 
    116   // Create a 8 bit sprite 80 pixels wide, 35 high (2800 bytes of RAM needed)
    117   img.setColorDepth(8);
    118   img.createSprite(IWIDTH, IHEIGHT);
    119 
    120   // Fill it with black (this will be the transparent colour this time)
    121   img.fillSprite(TFT_BLACK);
    122 
    123   // Draw a background for the numbers
    124   img.fillRoundRect(  0, 0,  80, 35, 15, TFT_RED);
    125   img.drawRoundRect(  0, 0,  80, 35, 15, TFT_WHITE);
    126 
    127   // Set the font parameters
    128   img.setTextSize(1);           // Font size scaling is x1
    129   img.setTextColor(TFT_WHITE);  // White text, no background colour
    130 
    131   // Set text coordinate datum to middle right
    132   img.setTextDatum(MR_DATUM);
    133 
    134   // Draw the number to 3 decimal places at 70,20 in font 4
    135   img.drawFloat(num, 3, 70, 20, 4);
    136 
    137   // Push sprite to TFT screen CGRAM at coordinate x,y (top left corner)
    138   // All black pixels will not be drawn hence will show as "transparent"
    139   img.pushSprite(x, y, TFT_BLACK);
    140 
    141   // Delete sprite to free up the RAM
    142   img.deleteSprite();
    143 }
    144