Skip to content

Commit 93e3176

Browse files
changed the way pixels are added to the framebuffer into a single vector with given size and custom PSRAM allocator
1 parent 03f8682 commit 93e3176

21 files changed

Lines changed: 177 additions & 149 deletions

include/Renderer.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ class Renderer {
1111
int width;
1212
int height;
1313
Color backgroundColor;
14+
Display displayGrid;
1415

1516
public:
1617
Renderer(int width, int height,
1718
const Color &backgroundColor = Color(0, 0, 0, 1.0f));
1819

19-
void render(Pixels &pixels,
20-
const std::vector<std::shared_ptr<Collection>> &collections,
20+
void render(const std::vector<std::shared_ptr<Collection>> &collections,
2121
const DrawOptions &options);
2222

23-
void drawText(Pixels &pixels, const std::string &text, int x, int y,
24-
const Font &font, const Color &color, bool wrap = false);
25-
26-
static Pixels blendPixels(const Pixels &pixels);
27-
static Pixel blendPixel(const Pixel &background, const Pixel &foreground);
23+
void drawText(const std::string &text, int x, int y, const Font &font,
24+
const Color &color, bool wrap = false);
2825

2926
int getWidth() const { return width; }
3027
int getHeight() const { return height; }
31-
const Color &getBackgroundColor() const { return backgroundColor; }
28+
const Display &getDisplayGrid() const { return displayGrid; }
29+
void clear();
3230
};

include/Shapes/Circle.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ class Circle : public Shape {
1818
public:
1919
Circle(const CircleParams &params);
2020
Collider *defaultCollider() override;
21-
void drawAntiAliased(Pixels &pixels) override;
22-
void drawAliased(Pixels &pixels) override;
21+
void drawAntiAliased(Display &displayGrid) override;
22+
void drawAliased(Display &displayGrid) override;
2323
int getRadius() const;
2424

2525
private:
2626
std::vector<std::pair<int, int>> getPointsToDraw(int cx, int cy, int x,
2727
int y);
2828
// Helpers
29-
void drawHorizontalLine(Pixels &pixels, int x1, int x2, int y);
30-
void drawOctantPointsAA(Pixels &pixels, int cx, int cy, int x, int y,
29+
void drawHorizontalLine(Display &displayGrid, int x1, int x2, int y);
30+
void drawOctantPointsAA(Display &displayGrid, int cx, int cy, int x, int y,
3131
float alpha);
3232

33-
void drawCirclePoints(Pixels &points, int cx, int cy, int x, int y);
34-
void drawAntiAliasedPoint(Pixels &points, int cx, int cy, int x, int y,
35-
float intensity);
36-
void fillCircle(Pixels &points, int cx, int cy, int r);
33+
void drawCirclePoints(Display &displayGrid, int cx, int cy, int x, int y);
34+
void drawAntiAliasedPoint(Display &displayGrid, int cx, int cy, int x,
35+
int y, float intensity);
36+
void fillCircle(Display &displayGrid, int cx, int cy, int r);
3737
};

include/Shapes/Collection.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class Collection : public Shape {
1313
void addShape(std::shared_ptr<Shape> shape);
1414
void removeShape(std::shared_ptr<Shape> shape);
1515

16-
void drawAliased(Pixels &pixels) override;
17-
void drawAntiAliased(Pixels &pixels) override;
16+
void drawAliased(Display &displayGrid) override;
17+
void drawAntiAliased(Display &displayGrid) override;
1818

1919
void clear();
2020

include/Shapes/LineSegment.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ class LineSegment : public Shape {
1818
public:
1919
LineSegment(const LineSegmentParams &params);
2020
Collider *defaultCollider() override;
21-
void drawAntiAliased(Pixels &pixels) override;
22-
void drawAliased(Pixels &pixels) override;
21+
void drawAntiAliased(Display &displayGrid) override;
22+
void drawAliased(Display &displayGrid) override;
2323
};

include/Shapes/Point.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ class Point : public Shape {
55
public:
66
Point(const ShapeParams &params);
77
Collider *defaultCollider() override;
8-
void drawAntiAliased(Pixels &pixels) override;
9-
void drawAliased(Pixels &pixels) override;
8+
void drawAntiAliased(Display &displayGrid) override;
9+
void drawAliased(Display &displayGrid) override;
1010
};

include/Shapes/Polygon.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class Polygon : public Shape {
2121
public:
2222
Polygon(const PolygonParams &params);
2323
Collider *defaultCollider() override;
24-
void drawAntiAliased(Pixels &pixels) override;
25-
void drawAliased(Pixels &pixels) override;
24+
void drawAntiAliased(Display &displayGrid) override;
25+
void drawAliased(Display &displayGrid) override;
2626

2727
private:
2828
std::vector<std::pair<int, int>> getTransformedVertices();
2929
std::vector<LineSegment *> getSegments();
30-
void getInsidePoints(Pixels &points,
30+
void getInsidePoints(Display &displayGrid,
3131
const std::vector<std::pair<int, int>> &vertices);
3232
};

include/Shapes/Rectangle.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ class Rectangle : public Shape {
2121
public:
2222
Rectangle(const RectangleParams &params);
2323
Collider *defaultCollider() override;
24-
void drawAntiAliased(Pixels &pixels) override;
25-
void drawAliased(Pixels &pixels) override;
24+
void drawAntiAliased(Display &displayGrid) override;
25+
void drawAliased(Display &displayGrid) override;
2626
int getWidth() const { return width; }
2727
int getHeight() const { return height; }
2828

2929
private:
3030
std::vector<std::pair<int, int>> getVertices();
31-
void getInsidePoints(Pixels &points,
31+
void getInsidePoints(Display &displayGrid,
3232
const std::vector<std::pair<int, int>> &vertices);
3333
};

include/Shapes/RegularPolygon.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class RegularPolygon : public Shape {
3636
RegularPolygon(const RegularPolygonSideParams &params);
3737
RegularPolygon(const RegularPolygonRadiusParams &params);
3838
Collider *defaultCollider() override;
39-
void drawAntiAliased(Pixels &pixels) override;
40-
void drawAliased(Pixels &pixels) override;
39+
void drawAntiAliased(Display &displayGrid) override;
40+
void drawAliased(Display &displayGrid) override;
4141
int getSides() const;
4242
int getRadius();
4343

@@ -48,6 +48,6 @@ class RegularPolygon : public Shape {
4848
int calculateRadiusFromSideLength(int sideLength);
4949
std::vector<std::pair<int, int>> getVertices();
5050
std::vector<LineSegment *> getSegments();
51-
void getInsidePoints(Pixels &points,
51+
void getInsidePoints(Display &displayGrid,
5252
const std::vector<std::pair<int, int>> &vertices);
5353
};

include/Shapes/Shape.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include "../Collider.hpp"
33
#include "../Texture.hpp"
44
#include "Utils.hpp"
5-
#include <vector>
65
#include <memory>
6+
#include <vector>
77

88
struct DrawOptions {
99
int screen_width;
@@ -75,10 +75,12 @@ class Shape {
7575

7676
// Protected helper methods
7777
std::pair<int, int> getTransformedPosition(int inputX, int inputY);
78-
void bresenhamLine(Pixels &points, int x0, int y0, int x1, int y1);
79-
void wuLine(Pixels &points, int x0, int y0, int x1, int y1);
80-
void addPixel(Pixels &points, int x, int y, float alpha);
81-
void getInsidePoints(Pixels &points,
78+
79+
void setPixelSafe(Display &displayGrid, int x, int y, const Color &c);
80+
void bresenhamLine(Display &points, int x0, int y0, int x1, int y1);
81+
void wuLine(Display &points, int x0, int y0, int x1, int y1);
82+
void addPixel(Display &points, int x, int y, float alpha);
83+
void getInsidePoints(Display &points,
8284
const std::vector<std::pair<int, int>> &vertices);
8385
void getUVAt(int x, int y, float &outU, float &outV);
8486

@@ -91,12 +93,12 @@ class Shape {
9193
std::pair<int, int> transformPoint(int x, int y, const Matrix2D &m);
9294

9395
// Pure virtual methods
94-
virtual void drawAntiAliased(Pixels &pixels) = 0;
95-
virtual void drawAliased(Pixels &pixels) = 0;
96+
virtual void drawAntiAliased(Display &pixels) = 0;
97+
virtual void drawAliased(Display &pixels) = 0;
9698
virtual Collider *defaultCollider() = 0;
9799

98100
// Drawing
99-
void draw(Pixels &pixels, const DrawOptions &options);
101+
void draw(Display &pixels, const DrawOptions &options);
100102

101103
// Position and transformation methods
102104
void setPosition(int x, int y);

include/Utils.hpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
2-
#include <cstdint>
2+
#include "esp_heap_caps.h"
3+
#include "esp_log.h"
4+
#include <cstdlib>
35
#include <vector>
46

57
struct Color {
@@ -16,19 +18,32 @@ struct Color {
1618
bool operator==(const Color &lhs, const Color &rhs);
1719
bool operator!=(const Color &lhs, const Color &rhs);
1820

19-
struct Pixel {
20-
int x;
21-
int y;
22-
Color color;
23-
24-
Pixel() : x(0), y(0), color() {}
25-
Pixel(int x, int y, Color color) : x(x), y(y), color(color) {}
21+
template <class T> struct PsramAllocator {
22+
typedef T value_type;
23+
24+
PsramAllocator() = default;
25+
template <class U>
26+
constexpr PsramAllocator(const PsramAllocator<U> &) noexcept {}
27+
28+
T *allocate(std::size_t n) {
29+
void *p = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM);
30+
if (!p) {
31+
ESP_LOGE("PSRAM",
32+
"CRITICAL: Failed to allocate %zu bytes in PSRAM!",
33+
n * sizeof(T));
34+
std::abort();
35+
}
36+
return static_cast<T *>(p);
37+
}
38+
39+
void deallocate(T *p, std::size_t) noexcept { heap_caps_free(p); }
2640
};
2741

28-
bool operator==(const Pixel &lhs, const Pixel &rhs);
29-
bool operator!=(const Pixel &lhs, const Pixel &rhs);
30-
31-
using Pixels = std::vector<Pixel>;
42+
struct Display {
43+
std::vector<Color, PsramAllocator<Color>> pixels;
44+
int width = 0;
45+
int height = 0;
46+
};
3247

3348
class Texture;
3449

0 commit comments

Comments
 (0)