|
2 | 2 | #include "Shapes/Circle.hpp" |
3 | 3 | #include "Shapes/Collection.hpp" |
4 | 4 | #include "Shapes/Rectangle.hpp" |
| 5 | +#include "Utils.hpp" |
| 6 | +#include "esp_timer.h" |
| 7 | +#include "freertos/projdefs.h" |
| 8 | +#include <cstdint> |
5 | 9 | #include <cstdio> |
6 | 10 | #include <stdio.h> |
7 | 11 | #include <vector> |
8 | 12 |
|
9 | | -// Function to generate a predictable test scene |
10 | | -void generateTestScene(std::vector<Collection *> &collections) { |
11 | | - // Simple test scene with known positions and colors |
12 | | - Collection *testCollection = |
13 | | - new Collection(ShapeParams{0, 0, Color(0, 0, 0, 1.0f), 0}); |
| 13 | +void runSolarSystem() { |
| 14 | + const int width = 64; |
| 15 | + const int height = 64; |
| 16 | + Renderer renderer(width, height, Color(0, 0, 0, 1.0f)); |
14 | 17 |
|
15 | | - // Add a red circle at known position |
16 | | - Circle *circle1 = |
17 | | - new Circle(CircleParams{10, 10, Color(155, 0, 0, 1.0f), 10, true}); |
18 | | - testCollection->addShape(circle1); |
| 18 | + std::vector<Collection *> collections; |
19 | 19 |
|
20 | | - // Add a blue rectangle |
21 | | - Rectangle *rect1 = new Rectangle( |
22 | | - RectangleParams{20, 20, Color(0, 0, 255, 1.0f), 8, 6, true}); |
23 | | - testCollection->addShape(rect1); |
| 20 | + Collection *sunCollection = |
| 21 | + new Collection(ShapeParams{32, 32, Color(0, 0, 0, 1.0f), 0}); |
| 22 | + sunCollection->setPivot(32.0f, 32.0f); |
| 23 | + collections.push_back(sunCollection); |
24 | 24 |
|
25 | | - // Add a green circle |
26 | | - Circle *circle2 = |
27 | | - new Circle(CircleParams{35, 15, Color(0, 255, 0, 1.0f), 5, false}); |
28 | | - testCollection->addShape(circle2); |
| 25 | + Circle *sun = |
| 26 | + new Circle(CircleParams{32, 32, Color(255, 204, 0, 1.0f), 8, true}); |
| 27 | + sunCollection->addShape(sun); |
29 | 28 |
|
30 | | - collections.push_back(testCollection); |
31 | | -} |
| 29 | + Collection *earthCollection = |
| 30 | + new Collection(ShapeParams{32, 32, Color(0, 0, 0, 1.0f), 0}); |
| 31 | + earthCollection->setPivot(32.0f, 32.0f); |
32 | 32 |
|
33 | | -// Function to output pixels in a consistent format |
34 | | -void outputPixelsForComparison(const Pixels &pixels, const char *testName) { |
35 | | - printf("=== %s ===\n", testName); |
36 | | - printf("Total pixels: %zu\n", pixels.size()); |
37 | | - |
38 | | - // Sort pixels for consistent output (x, then y) |
39 | | - Pixels sorted = pixels; |
40 | | - // Simple bubble sort (good enough for testing) |
41 | | - for (size_t i = 0; i < sorted.size(); ++i) { |
42 | | - for (size_t j = i + 1; j < sorted.size(); ++j) { |
43 | | - if (sorted[j].x < sorted[i].x || |
44 | | - (sorted[j].x == sorted[i].x && sorted[j].y < sorted[i].y)) { |
45 | | - std::swap(sorted[i], sorted[j]); |
46 | | - } |
47 | | - } |
48 | | - } |
| 33 | + Circle *earth = new Circle( |
| 34 | + CircleParams{32 + 20, 32, Color(0, 100, 255, 1.0f), 4, true}); |
| 35 | + earthCollection->addShape(earth); |
49 | 36 |
|
50 | | - // Output first 50 pixels (or all if less) |
51 | | - size_t maxOutput = (sorted.size() > 50) ? 50 : sorted.size(); |
52 | | - printf("First %zu pixels:\n", maxOutput); |
| 37 | + Circle *alien = new Circle( |
| 38 | + CircleParams{32 - 20, 32, Color(0, 255, 100, 1.0f), 4, true}); |
| 39 | + earthCollection->addShape(alien); |
53 | 40 |
|
54 | | - for (size_t i = 0; i < maxOutput; i++) { |
55 | | - const Pixel &p = sorted[i]; |
56 | | - printf("Pixel[%zu]: (%d,%d) RGBA(%d,%d,%d,%.3f)\n", i, p.x, p.y, |
57 | | - p.color.r, p.color.g, p.color.b, p.color.a); |
58 | | - } |
| 41 | + Collection *moonCollection = |
| 42 | + new Collection(ShapeParams{32 + 20, 32, Color(0, 0, 0, 1.0f), 0}); |
| 43 | + Circle *moon = new Circle( |
| 44 | + CircleParams{32 + 20 + 8, 32, Color(200, 200, 200, 1.0f), 2, true}); |
| 45 | + moonCollection->addShape(moon); |
59 | 46 |
|
60 | | - if (sorted.size() > maxOutput) { |
61 | | - printf("... and %zu more pixels\n", sorted.size() - maxOutput); |
62 | | - } |
63 | | - printf("Checksum: %zu pixels\n\n", sorted.size()); |
64 | | -} |
| 47 | + earthCollection->addShape(moonCollection); |
| 48 | + sunCollection->addShape(earthCollection); |
65 | 49 |
|
66 | | -uint32_t calculatePixelChecksum(const Pixels &pixels) { |
67 | | - uint32_t sum = 0; |
68 | | - for (const auto &pixel : pixels) { |
69 | | - sum += pixel.x; |
70 | | - sum += pixel.y; |
71 | | - sum += pixel.color.r; |
72 | | - sum += pixel.color.g; |
73 | | - sum += pixel.color.b; |
74 | | - sum += static_cast<uint32_t>(pixel.color.a * 1000.0f); |
75 | | - } |
76 | | - return sum; |
77 | | -} |
78 | | -void runComparisonTest() { |
79 | | - printf("Starting C++/TypeScript Comparison Test\n"); |
80 | | - printf("=======================================\n"); |
81 | | - |
82 | | - const int width = 64; |
83 | | - const int height = 64; |
84 | | - Renderer renderer(width, height, Color(255, 255, 255, 1.0f)); |
| 50 | + Circle *earthOrbit = |
| 51 | + new Circle(CircleParams{32, 32, Color(100, 100, 100, 0.3f), 20, false}); |
| 52 | + Circle *moonOrbit = new Circle( |
| 53 | + CircleParams{32 + 20, 32, Color(100, 100, 100, 0.3f), 8, false}); |
| 54 | + sunCollection->addShape(earthOrbit); |
| 55 | + earthCollection->addShape(moonOrbit); |
85 | 56 |
|
86 | | - // Test 1: Simple static scene |
87 | | - printf("TEST 1: Simple Static Scene\n"); |
88 | | - std::vector<Collection *> scene1; |
89 | | - generateTestScene(scene1); |
| 57 | + DrawOptions options = {width, height, true}; // Anti-aliased |
90 | 58 |
|
91 | | - DrawOptions options = {width, height, false}; // Aliased for consistency |
92 | | - Pixels pixels1 = renderer.render(scene1, options); |
93 | | - outputPixelsForComparison(pixels1, "Static Scene Aliased"); |
94 | | - printf("Calculated checksum: 0x%08lX\n", calculatePixelChecksum(pixels1)); |
| 59 | + HUB75Display display(width, height); |
| 60 | + if (!display.isInitialized()) { |
| 61 | + printf("Display not initialized. Skipping display output.\n"); |
| 62 | + return; |
| 63 | + } |
95 | 64 |
|
96 | | - // Test 2: Same scene with anti-aliasing |
97 | | - printf("TEST 2: Anti-Aliased Version\n"); |
98 | | - options.antialias = true; |
99 | | - Pixels pixels2 = renderer.render(scene1, options); |
100 | | - outputPixelsForComparison(pixels2, "Static Scene Anti-Aliased"); |
101 | | - printf("Calculated checksum: 0x%08lX\n", calculatePixelChecksum(pixels2)); |
| 65 | + display.clear(); |
| 66 | + int counter = 0; |
| 67 | + uint64_t sumTime = 0; |
| 68 | + while (true) { |
| 69 | + earthCollection->rotate(1.5f); |
| 70 | + moonCollection->rotate(3.0f); |
| 71 | + uint64_t startTime = esp_timer_get_time(); |
| 72 | + Pixels pixels = renderer.render(collections, options); |
| 73 | + uint64_t endTime = esp_timer_get_time(); |
| 74 | + sumTime += (endTime - startTime); |
| 75 | + counter++; |
| 76 | + if (counter % 30 == 0) { |
| 77 | + float avgTimeMs = (sumTime / counter) / 1000.0f; |
| 78 | + printf("Average render time over %d frames: %.2f ms\n", counter, |
| 79 | + avgTimeMs); |
| 80 | + counter = 0; |
| 81 | + sumTime = 0; |
| 82 | + } |
| 83 | + display.setBuffer(pixels); |
| 84 | + vTaskDelay(pdMS_TO_TICKS(20)); |
| 85 | + } |
102 | 86 |
|
103 | 87 | // Cleanup |
104 | | - for (auto collection : scene1) { |
| 88 | + for (auto collection : collections) { |
105 | 89 | delete collection; |
106 | 90 | } |
107 | | - |
108 | | - printf("Comparison test completed.\n"); |
109 | | - printf("Copy this output and compare with TypeScript version.\n"); |
110 | 91 | } |
111 | 92 |
|
112 | 93 | extern "C" void app_main(void) { |
113 | 94 | printf("C++/TypeScript Output Comparison\n"); |
114 | 95 | printf("================================\n"); |
115 | 96 |
|
116 | | - runComparisonTest(); |
| 97 | + runSolarSystem(); |
117 | 98 | } |
0 commit comments