Skip to content

Commit

Permalink
Merge pull request #39 from scottchiefbaker/master
Browse files Browse the repository at this point in the history
Add an example that uses sprites to update the screen
  • Loading branch information
takkaO committed May 8, 2024
2 parents ba1ddb8 + 576dd9e commit 5100f1f
Show file tree
Hide file tree
Showing 2 changed files with 784 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/TFT_eSPI/Fast_Font_Updates/Fast_Font_Updates.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//////////////////////////////////////////////////////////////////////
// This example renders millis() to a sprite in the middle of the
// screen, and then draws over it with a new sprite. This should
// have the effect of smooth screen updates without any flickering
// or font tearing.
//
// Using this method I can get 100+ FPS on an ESP32 with a size 64
// font.
//////////////////////////////////////////////////////////////////////

#include "OpenFontRender.h"
OpenFontRender ofr;

#include "NotoSans_Bold.h"
#define TTF_FONT NotoSans_Bold

#include <SPI.h>
#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();
TFT_eSprite sprite = TFT_eSprite(&tft);

void setup(void) {
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);

ofr.setDrawer(sprite);
ofr.loadFont(TTF_FONT, sizeof(TTF_FONT));
}

void loop() {
// Build the sprite and fill it with black
void *out = sprite.createSprite(140, 50);
if (out == NULL) Serial.println("Could not allocate memory for sprite!");
sprite.fillSprite(TFT_BLACK);

// Set all the font settings and write millis() to the sprite
ofr.setFontSize(64);
ofr.setCursor(sprite.width() / 2, sprite.height() / 2); // Middle of box
ofr.setAlignment(Align::MiddleCenter);
ofr.setFontColor(TFT_WHITE, TFT_BLACK);
ofr.printf("%d", millis());

// Calculate the offsets needed to center the sprite box
int hcenter = (tft.width() / 2) - (sprite.width() / 2);
int vcenter = (tft.height() / 2) - (sprite.height() / 2);

// Push the sprite to the screen
sprite.pushSprite(hcenter,vcenter);
}
Loading

0 comments on commit 5100f1f

Please sign in to comment.