-
Notifications
You must be signed in to change notification settings - Fork 1
/
tigr-cpp.h
182 lines (144 loc) · 4.64 KB
/
tigr-cpp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
USAGE:
auto t = std::make_shared<tigr::Window>(200, 200, "Example", (int)tigr::WindowFlag::AUTO);
auto image = std::make_shared<tigr::Bitmap>(200, 200);
while (!t->isClosed()) {
t->clear(0, 0, 0);
for (int y = 0; y < 200; ++y) {
for (int x = 0; x < 200; ++x) {
if ((x % 2 == 0 && y % 2 != 0) || (x % 2 != 0 && y % 2 == 0))
image->setPixel(x, y, tigr::RGBA{ 255, 255, 255 });
else
image->setPixel(x, y, tigr::RGBA{ 0, 0, 0 });
}
}
t->blitTint(image, tigr::RGBA{ 255, 0, 0 });
t->blitTint(image, 10, 10, 0, 0, 180, 180, tigr::RGBA{ 0, 0, 255 });
t->blitTint(image, 20, 20, 0, 0, 160, 160, tigr::RGBA{ 0, 255, 0 });
t->print(0, 0, tigr::RGBA{ 255, 255, 255 }, "Hello world");
t->update();
}
Not yet implemented functions:
tigrPlot
tigrPosition
tigrEnforceScale
tigrResize
tigrPrint
*/
#pragma once
#include <tigr.h>
#include <algorithm>
#include <memory>
#include <cassert>
#include <cstdarg>
namespace tigr {
enum class WindowFlag : int {
FIXED = 0,
AUTO = 1,
A2X = 2,
A3X = 4,
A4X = 8,
RETINA = 16,
};
struct RGBA {
unsigned char r, g, b, a;
RGBA(TPixel pixel) : r(pixel.r), g(pixel.g), b(pixel.b), a(pixel.a) {
}
explicit RGBA(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 255) : r(r), g(g), b(b), a(a) {
}
operator TPixel() const {
return tigrRGBA(r, g, b, a);
}
};
class Bitmap {
protected:
Tigr* b = nullptr;
public:
Bitmap() {
}
Bitmap(int w, int h) {
b = tigrBitmap(w, h);
}
virtual ~Bitmap() {
tigrFree(b);
b = nullptr;
}
TPixel& at(int x, int y) {
return b->pix[y * b->w + x];
}
void clear(unsigned char r, unsigned char g, unsigned char w) {
clear(tigrRGB(r, g, w));
}
void clear(const RGBA& color) {
tigrClear(b, color);
}
void setPixel(int x, int y, const RGBA& color) {
at(x, y) = color;
}
void setRectWH(int x, int y, int w, int h, const RGBA& color) {
tigrRect(b, x, y, w, h, color);
}
void setRect(int x0, int y0, int x1, int y1, const RGBA& color) {
assert(x1 > x0 && y1 > y0);
tigrRect(b, x0, y0, x1 - x0, y1 - y0, color);
}
void setFillWH(int x, int y, int w, int h, const RGBA& color) {
tigrFill(b, x, y, w, h, color);
}
void setFill(int x0, int y0, int x1, int y1, const RGBA& color) {
assert(x1 > x0 && y1 > y0);
tigrFill(b, x0, y0, x1 - x0, y1 - y0, color);
}
void setLine(int x0, int y0, int x1, int y1, const RGBA& color) {
tigrLine(b, x0, y0, x1, y1, color);
}
void blit(std::shared_ptr<Bitmap> source, int dx, int dy, int sx, int sy, int w, int h) {
tigrBlit(b, source->b, dx, dy, sx, sy, w, h);
}
void blit(std::shared_ptr<Bitmap> source) {
int minW = std::min(b->w, source->b->w);
int minH = std::min(b->h, source->b->h);
blit(source, 0, 0, 0, 0, minW, minH);
}
void blitTint(std::shared_ptr<Bitmap> source, int dx, int dy, int sx, int sy, int w, int h, const RGBA& tint) {
tigrBlitTint(b, source->b, dx, dy, sx, sy, w, h, tint);
}
void blitTint(std::shared_ptr<Bitmap> source, const RGBA& tint) {
int minW = std::min(b->w, source->b->w);
int minH = std::min(b->h, source->b->h);
tigrBlitTint(b, source->b, 0, 0, 0, 0, minW, minH, tint);
}
void blitAlpha(std::shared_ptr<Bitmap> source, int dx, int dy, int sx, int sy, int w, int h, float alpha) {
tigrBlitAlpha(b, source->b, dx, dy, sx, sy, w, h, alpha);
}
void blitAlpha(std::shared_ptr<Bitmap> source, float alpha) {
int minW = std::min(b->w, source->b->w);
int minH = std::min(b->h, source->b->h);
tigrBlitAlpha(b, source->b, 0, 0, 0, 0, minW, minH, alpha);
}
void print(TigrFont* font, int x, int y, const RGBA& color, const char* text, ...) {
va_list args;
va_start(args, text);
tigrPrint(b, font, x, y, color, text, args);
va_end(args);
}
void print(int x, int y, const RGBA& color, const char* text, ...) {
va_list args;
va_start(args, text);
tigrPrint(b, tfont, x, y, color, text, args);
va_end(args);
}
void update() {
tigrUpdate(b);
}
};
class Window final : public Bitmap {
public:
Window(int w, int h, const char* title, int flags) {
b = tigrWindow(w, h, title, flags);
}
bool isClosed() {
return tigrClosed(b);
}
};
} // namespace tigr