FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
DoubleBuffer

Benchmark of the drawing primitives with double buffering (flicker-free full screen animation) at 320x200, 64 colors.

Three tests run one after the other in an endless loop. Each test adds a new object every 5 frames until its maximum is reached, then moves to the next test. The yellow label at the top shows the number of objects, the test name and the measured frames per second:

Test Content
Balls Up to 300 filled circles bouncing around
Polygons Rotating filled polygons with random shape and color
Sprites Moving sprites

The demo runs on its own: there are no controls.

What it demonstrates

  • VGAController::setResolution(..., doubleBuffered = true) and Canvas::swapBuffers()
  • Full-screen redraw at every frame with Canvas::clear(), fillEllipse(), fillPath() and sprites
  • Measuring the drawing throughput (FPS) as the scene gets heavier

Set DOUBLEBUFFERING to 0 in DoubleBuffer.cpp to see the same scene without double buffering (the flickering becomes visible).

Hardware

  • VGA output (default pins)

Build

See Building the examples.

Source code

Main file of the example, main/DoubleBuffer.cpp. The complete project is in the examples/VGA/DoubleBuffer folder.

1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2026 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#include "esp_random.h"
28#include "fabgl.h"
29
30
31// random integer in the range [0, max)
32static inline int randomInt(int max) { return max > 0 ? (int) (esp_random() % (uint32_t) max) : 0; }
33// random integer in the range [min, max)
34static inline int randomInt(int min, int max) { return max > min ? min + (int) (esp_random() % (uint32_t) (max - min)) : min; }
35
36
37
38
39fabgl::VGAController DisplayController;
40fabgl::Canvas canvas(&DisplayController);
41
42
43
44#define DOUBLEBUFFERING 1
45
46
47struct Test {
48 virtual ~Test() { };
49 virtual void update() = 0;
50 virtual bool nextState() = 0;
51 virtual int testState() = 0;
52 virtual char const * name() = 0;
53};
54
55
56#include "ballstest.h"
57#include "polygonstest.h"
58#include "spritestest.h"
59
60
61
62void setup()
63{
64 DisplayController.begin();
65 DisplayController.setResolution(VGA_320x200_75Hz, -1, -1, DOUBLEBUFFERING);
66 //DisplayController.moveScreen(-8, 0);
67
68 // get a font for about 40x14 text screen
69 canvas.selectFont(&fabgl::FONT_8x8);
70
71 canvas.setGlyphOptions(GlyphOptions().FillBackground(true));
72}
73
74
75void loop()
76{
77 static int64_t stime = esp_timer_get_time();
78 static int FPS = 0;
79 static int FPSCounter = 0;
80 static int testIndex = 0;
81 static Test * test = new BallsTest;
82
83 if (test->nextState() == false) {
84 delete test;
85 ++testIndex;
86 switch (testIndex) {
87 case 1:
88 test = new PolygonsTest;
89 break;
90 case 2:
91 test = new SpritesTest;
92 break;
93 default:
94 testIndex = 0;
95 test = new BallsTest;
96 break;
97 }
98 }
99
100 if (esp_timer_get_time() - stime > 1000000) {
101 // calculate FPS
102 FPS = FPSCounter;
103 stime = esp_timer_get_time();
104 FPSCounter = 0;
105 }
106 ++FPSCounter;
107
108 test->update();
109
110 // display test state and FPS
111 canvas.setPenColor(Color::Blue);
112 canvas.setBrushColor(Color::Yellow);
113 canvas.drawTextFmt(80, 5, " %d %s at %d FPS ", test->testState(), test->name(), FPS);
114
115 if (DOUBLEBUFFERING)
116 canvas.swapBuffers();
117}
118
119
120extern "C" void app_main()
121{
122 setup();
123 while (true)
124 loop();
125}
Provides a set of drawing methods.
Definition canvas.h:73
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
VGA bitmapped display controller (64 colors).
Single include file of the FabGL library.
#define VGA_320x200_75Hz
Definition fabglconf.h:183
Specifies glyph painting options.