This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Framebuffer.h
244 lines (205 loc) · 7.84 KB
/
Framebuffer.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
This file is part of duckOS.
duckOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
duckOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with duckOS. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) Byteduck 2016-2021. All rights reserved.
*/
#pragma once
#include <sys/types.h>
#include "Geometry.h"
#include "Color.h"
#include <libduck/Serializable.h>
#define IMGSIZE(width, height) (sizeof(uint32_t) * (width) * (height))
#define IMGPIXEL(img, x, y) (img).data[(x) + (y) * (img).width]
#define IMGPTRPIXEL(img, x, y) (img)->data[(x) + (y) * (img)->width]
namespace Gfx {
class Font;
class Framebuffer: public Duck::Serializable {
public:
Framebuffer();
Framebuffer(Color* buffer, int width, int height);
Framebuffer(int width, int height);
Framebuffer(Framebuffer&& other) noexcept;
Framebuffer(Framebuffer& other) noexcept;
~Framebuffer() noexcept;
Framebuffer& operator=(const Framebuffer& other);
Framebuffer& operator=(Framebuffer&& other) noexcept;
Color* data = nullptr;
int width = 0;
int height = 0;
bool should_free = false;
/**
* Frees the data associated with the Image.
*/
void free();
/**
* Copies a part of another Image to this one.
* @param other The other Image to copy from.
* @param other_area The area of the other Image to copy.
* @param pos The position of this Image to copy to.
*/
void copy(const Framebuffer& other, Rect other_area, const Point& pos) const;
/**
* Copies a part of another Image to this one, ignoring alpha.
* @param other The other Image to copy from.
* @param other_area The area of the other Image to copy.
* @param pos The position of this Image to copy to.
*/
void copy_noalpha(const Framebuffer& other, Rect other_area, const Point& pos) const;
/**
* Copies a part of another Image to this one, with alpha blending.
* @param other The other Image to copy from.
* @param other_area The area of the other Image to copy.
* @param pos The position of this Image to copy to.
*/
void copy_blitting(const Framebuffer& other, Rect other_area, const Point& pos) const;
/**
* Copies a part of another Image to this one, with alpha blending, flipped horizontally.
* @param other The other Image to copy from.
* @param other_area The area of the other Image to copy.
* @param pos The position of this Image to copy to.
* @param flip_h Whether to flip vertically.
* @param flip_v Whether to flip horizontally.
*/
void copy_blitting_flipped(const Framebuffer& other, Rect other_area, const Point& pos, bool flip_h, bool flip_v) const;
/**
* Identical to ::copy(), but will tile the Image.
*/
void copy_tiled(const Framebuffer& other, Rect other_area, const Point& pos) const;
/**
* Draws another Image on top of this one with alpha blending.
* @param other The Image to draw.
* @param other_area The area of the other Image to draw.
* @param pos The position on this Image to draw on.
*/
void draw_image(const Framebuffer& other, Rect other_area, const Point& pos) const;
/**
* Draws another Image on top of this one with alpha blending.
* @param other The Image to draw.
* @param pos The position on this Image to draw on.
*/
void draw_image(const Framebuffer& other, const Point& pos) const;
/**
* Draws another Image on top of this one with alpha blending, scaled to
* fit inside of the specified rect.
* @param other The Image to draw.
* @param size The rect on this Image to scale the image to and draw on.
*/
void draw_image_scaled(const Framebuffer& other, const Rect& rect) const;
/**
* Fills an area of the Image with a color.
* @param area The area to fill.
* @param color The color to fill the area with.
*/
void fill(Rect area, Color color) const;
/**
* Fills an area of the Image with a color, calculating transparency.
* @param area The area to fill.
* @param color The color to fill the area with.
*/
void fill_blitting(Rect area, Color color) const;
/**
* Fills an area of the Image with a horizontal gradient (left to right).
* @param area The area to fill.
* @param color_a The start color.
* @param color_b The end color.
*/
void fill_gradient_h(Rect area, Color color_a, Color color_b) const;
/**
* Fills an area of the Image with a vertical gradient (top to bottom).
* @param area The area to fill.
* @param color_a The start color.
* @param color_b The end color.
*/
void fill_gradient_v(Rect area, Color color_a, Color color_b) const;
/**
* Inverts an area of the Image.
* @param area The area to invert.
*/
void invert(Rect area) const;
/**
* Inverts an area of the Image with a checkered pattern.
* @param area The area to invert.
*/
void invert_checkered(Rect area) const;
/**
* Draws the outline of an area on the Image.
* @param area The rect of the area to outline.
* @param color The color to draw the outline in.
*/
void outline(Rect area, Color color) const;
/**
* Draws the outline of an area on the Image, calculating transparency.
* @param area The rect of the area to outline.
* @param color The color to draw the outline in.
*/
void outline_blitting(Rect area, Color color) const;
/**
* Outlines an area on the Framebuffer by inverting the colors.
* @param area The area to outline.
*/
void outline_inverting(Rect area) const;
/**
* Outlines an area on the Framebuffer by inverting the colors with a checker pattern.
* @param area The area to outline.
*/
void outline_inverting_checkered(Rect area) const;
/**
* Draws text on the Image with a certain color.
* @param str The string to draw.
* @param pos The top-left position of where to draw.
* @param font The font to use.
* @param color The color to draw in.
*/
Point draw_text(const char* str, const Point& pos, Font* font, Color color) const;
/**
* Draws text on the Image with a certain color.
* @param str The string to draw. Does not need to be zero-terminated.
* @param len The length of the string to draw.
* @param pos The top-left position of where to draw.
* @param font The font to use.
* @param color The color to draw in.
*/
Point draw_text(const char* str, size_t len, const Point& pos, Font* font, Color color) const;
/**
* Draws a glyph on the Image with a certain color.
* @param font The font to use.
* @param codepoint The codepoint of the character.
* @param pos The position to draw the glyph at.
* @param color The color to draw the glyph in.
* @return The position where the next character should be drawn.
*/
Point draw_glyph(Font* font, uint32_t codepoint, const Point& pos, Color color) const;
/**
* Multiplies the image with a certain color.
* @param color The color to multiply by.
*/
void multiply(Color color);
/**
* Returns a pointer to the Image buffer at a certain position. Returns NULL if outside the constraints.
*/
Color* at(const Point& position) const;
/**
* Returns a reference to the Image buffer at a certain position without bounds checking.
*/
inline constexpr Color& ref_at(const Point& position) const {
return data[position.x + position.y * width];
}
/**
* Puts a pixel at a point.
*/
void put(Gfx::Point point, Gfx::Color color) const;
/// Serializable
size_t serialized_size() const override;
void serialize(uint8_t*& buf) const override;
void deserialize(const uint8_t*& buf) override;
};
}