acidportal

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

Rotated_Sprite_1.ino (6128B)

      1 // This example plots a rotated Sprite to the screen using the pushRotated()
      2 // function. It is written for a 240 x 320 TFT screen.
      3 
      4 // Two rotation pivot points must be set, one for the Sprite and one for the TFT
      5 // using setPivot(). These pivot points do not need to be within the visible screen
      6 // or Sprite boundary.
      7 
      8 // When the Sprite is rotated and pushed to the TFT with pushRotated(angle) it will be
      9 // drawn so that the two pivot points coincide. This makes rotation about a point on the
     10 // screen very simple. The rotation is clockwise with increasing angle. The angle is in
     11 // degrees, an angle of 0 means no Sprite rotation.
     12 
     13 // The pushRotated() function works with 1, 4, 8 and 16 bit per pixel (bpp) Sprites.
     14 
     15 // The original Sprite is unchanged so can be plotted again at a different angle.
     16 
     17 // Optionally a transparent colour can be defined, pixels of this colour will
     18 // not be plotted to the TFT.
     19 
     20 // For 1 bpp Sprites the foreground and background colours are defined with the
     21 // function spr.setBitmapColor(foregroundColor, backgroundColor).
     22 
     23 // For 4 bpp Sprites the colour map index is used instead of the 16 bit colour
     24 // e.g. spr.setTextColor(5); // Green text in default colour map
     25 // See "Transparent_Sprite_Demo_4bit" example for default colour map details
     26 
     27 // Created by Bodmer 6/1/19 as an example to the TFT_eSPI library:
     28 // https://github.com/Bodmer/TFT_eSPI
     29 
     30 #include <TFT_eSPI.h>
     31 
     32 TFT_eSPI tft = TFT_eSPI();           // TFT object
     33 
     34 TFT_eSprite spr = TFT_eSprite(&tft); // Sprite object
     35 
     36 // =======================================================================================
     37 // Setup
     38 // =======================================================================================
     39 
     40 void setup()   {
     41   Serial.begin(250000); // Debug only
     42 
     43   tft.begin();  // initialize
     44   tft.setRotation(0);
     45 }
     46 
     47 // =======================================================================================
     48 // Loop
     49 // =======================================================================================
     50 
     51 void loop() {
     52   
     53   int xw = tft.width()/2;   // xw, yh is middle of screen
     54   int yh = tft.height()/2;
     55 
     56 
     57   showMessage("90 degree angles");
     58   tft.setPivot(xw, yh);     // Set pivot to middle of TFT screen
     59   drawX(xw, yh);            // Show where screen pivot is
     60 
     61   // Create the Sprite
     62   spr.setColorDepth(8);      // Create an 8bpp Sprite of 60x30 pixels
     63   spr.createSprite(64, 30);  // 8bpp requires 64 * 30 = 1920 bytes
     64   spr.setPivot(32, 55);      // Set pivot relative to top left corner of Sprite
     65   spr.fillSprite(TFT_BLACK); // Fill the Sprite with black
     66 
     67   spr.setTextColor(TFT_GREEN);        // Green text
     68   spr.setTextDatum(MC_DATUM);         // Middle centre datum
     69   spr.drawString("Hello", 32, 15, 4); // Plot text, font 4, in Sprite at 30, 15
     70 
     71   spr.pushRotated(0);
     72   spr.pushRotated(90);
     73   spr.pushRotated(180);
     74   spr.pushRotated(270);
     75 
     76   delay(2000);
     77 
     78 
     79   showMessage("45 degree angles");
     80   drawX(xw, yh);            // Show where screen pivot is
     81 
     82   spr.pushRotated(45);
     83   spr.pushRotated(135);
     84   spr.pushRotated(225);
     85   spr.pushRotated(315);
     86 
     87   delay(2000);  // Pause so we see it
     88 
     89 
     90   showMessage("Moved Sprite pivot point");
     91   drawX(xw, yh);          // Show where screen pivot is
     92 
     93   spr.setPivot(-20, 15);  // Change just the Sprite pivot point
     94 
     95   spr.pushRotated(45);
     96   spr.pushRotated(135);
     97   spr.pushRotated(225);
     98   spr.pushRotated(315);
     99 
    100   delay(2000);  // Pause so we see it
    101 
    102 
    103   showMessage("Moved TFT pivot point");
    104   tft.setPivot(100, 100); // Change just the TFT pivot point
    105   drawX(100, 100);        // Show where pivot is
    106 
    107   spr.pushRotated(45);
    108   spr.pushRotated(135);
    109   spr.pushRotated(225);
    110   spr.pushRotated(315);
    111 
    112   delay(2000);  // Pause so we see it
    113 
    114 
    115   showMessage("Transparent rotations");
    116   tft.fillCircle(xw, yh, 70, TFT_DARKGREY); // Draw a filled circle
    117 
    118   tft.setPivot(xw, yh);         // Set pivot to middle of screen
    119   drawX(xw, yh);                // Show where pivot is
    120   
    121   spr.deleteSprite();
    122 
    123   spr.setColorDepth(8);         // Create a 8bpp Sprite
    124   spr.createSprite(40, 30);     // Create a new Sprite 40x30
    125   spr.setPivot(20, 70);         // Set Sprite pivot at 20,80
    126 
    127   spr.setTextColor(TFT_RED);    // Red text in Sprite
    128   spr.setTextDatum(MC_DATUM);   // Middle centre datum
    129 
    130   int num = 1;
    131 
    132   for (int16_t angle = 30; angle <= 360; angle += 30)
    133   {
    134     spr.fillSprite(TFT_BLACK);         // Clear the Sprite
    135     spr.drawNumber(num, 20, 15, 4);    // Plot number, in Sprite at 20,15 and with font 4
    136     spr.pushRotated(angle, TFT_BLACK); // Plot rotated Sprite, black being transparent
    137     num++;
    138   }
    139 
    140   spr.setTextColor(TFT_WHITE);    // White text in Sprite
    141   spr.setPivot(-75, 15);          // Set Sprite pivot at -75,15
    142 
    143   for (int16_t angle = -90; angle < 270; angle += 30)
    144   {
    145     spr.fillSprite(TFT_BLACK);           // Clear the Sprite
    146     spr.drawNumber(angle+90, 20, 15, 4); // Plot number, in Sprite at 20,15 and with font 4
    147     spr.pushRotated(angle, TFT_BLACK);   // Plot rotated Sprite, black being transparent
    148     num++;
    149   }
    150   
    151   delay(8000);  // Pause so we see it
    152 
    153   spr.deleteSprite();
    154 
    155 }
    156 
    157 // =======================================================================================
    158 // Draw an X centered on x,y
    159 // =======================================================================================
    160 
    161 void drawX(int x, int y)
    162 {
    163   tft.drawLine(x-5, y-5, x+5, y+5, TFT_WHITE);
    164   tft.drawLine(x-5, y+5, x+5, y-5, TFT_WHITE);
    165 }
    166 
    167 // =======================================================================================
    168 // Show a message at the top of the screen
    169 // =======================================================================================
    170 
    171 void showMessage(String msg)
    172 {
    173   // Clear the screen areas
    174   tft.fillRect(0, 0, tft.width(), 20, TFT_BLACK);
    175   tft.fillRect(0, 20, tft.width(), tft.height()-20, TFT_BLUE);
    176 
    177   uint8_t td = tft.getTextDatum(); // Get current datum
    178 
    179   tft.setTextDatum(TC_DATUM);      // Set new datum
    180 
    181   tft.drawString(msg, tft.width()/2, 2, 2); // Message in font 2
    182 
    183   tft.setTextDatum(td); // Restore old datum
    184 }
    185 
    186 // =======================================================================================