Skip to content

Commit cfb6d40

Browse files
committed
feat: adds sprite support
1 parent fe99a97 commit cfb6d40

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

rxterm/include/image.hpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <rxterm/pixel.hpp>
88
#include <rxterm/style.hpp>
9+
#include <rxterm/utils.hpp>
910

1011
namespace rxterm {
1112

@@ -67,10 +68,19 @@ struct Image {
6768
}
6869
};
6970

71+
struct Sprite {
72+
Image image;
73+
int x = 0;
74+
int y = 0;
75+
};
76+
7077
Image drawOnBackground(Image canvas, unsigned const& sx, unsigned const& sy, Image const& fg) {
71-
for(int y=0; y < fg.height; ++y) {
72-
for(int x=0; x < fg.width; ++x) {
73-
auto& p = canvas(sx+x, sy+y);
78+
for (int y=0; y < fg.height; ++y) {
79+
for (int x=0; x < fg.width; ++x) {
80+
auto& p = canvas(
81+
clip(sx+x, 0u, canvas.width),
82+
clip(sy+y, 0u, canvas.height)
83+
);
7484
auto const& q = fg(x, y);
7585
p = Pixel{
7686
(q.c)? q.c : p.c,
@@ -85,6 +95,31 @@ Image drawOnBackground(Image canvas, unsigned const& sx, unsigned const& sy, Ima
8595
return canvas;
8696
}
8797

98+
Image drawOnBackground(Image canvas, Sprite const& s) {
99+
return drawOnBackground(canvas, s.x, s.y, s.image);
100+
}
101+
102+
Image drawOnBackground(Image const& canvas) {
103+
return canvas;
104+
}
105+
106+
template<class X, class...Xs>
107+
auto drawOnBackground(Image const& canvas, X const& s, Xs const&...xs)
108+
-> decltype( s.image, s.x, s.y , drawOnBackground(canvas, xs...)) {
109+
return drawOnBackground(
110+
drawOnBackground(canvas, s),
111+
xs...
112+
);
113+
}
114+
115+
Image drawOnBackground(Image canvas, std::vector<Sprite> const& sprites) {
116+
for (auto const& s: sprites) {
117+
canvas = drawOnBackground(canvas, s);
118+
}
119+
120+
return canvas;
121+
}
122+
88123
}
89124

90125

rxterm/include/utils.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ std::string clearLines(unsigned n = 1) {
7575
return "\e[0m" + clearBeforeCursor() + ((n) ? repeat(n, clearLine() + moveUp()) : std::string(""));
7676
}
7777

78+
template <typename T>
79+
T clip(const T& n, const T& lower, const T& upper) {
80+
return std::max(lower, std::min(n, upper));
81+
}
82+
7883
}
7984

8085
#endif

0 commit comments

Comments
 (0)