-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayer.cpp
More file actions
100 lines (81 loc) · 2.46 KB
/
Copy pathLayer.cpp
File metadata and controls
100 lines (81 loc) · 2.46 KB
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
#include "include/Layer.hpp"
Layer::Layer(int width, int height) {
renderTexture = LoadRenderTexture(width, height);
BeginTextureMode(renderTexture);
ClearBackground(WHITE);
EndTextureMode();
}
void Layer::drawStroke(Stroke stroke) {
strokes.push_back(stroke);
BeginTextureMode(renderTexture);
stroke.render();
EndTextureMode();
}
void Layer::removeLastStroke() {
if (strokes.size() == 0) return;
Brush b = strokes[strokes.size() - 1].getBrush();
b.setColor(WHITE);
strokes[strokes.size() - 1].setBrush(b);
BeginTextureMode(renderTexture);
strokes[strokes.size() - 1].render();
strokes.pop_back();
for (Stroke s : strokes)
s.render();
EndTextureMode();
}
void Layer::restart() {
strokes.clear();
strokes.shrink_to_fit();
BeginTextureMode(renderTexture);
ClearBackground(WHITE);
EndTextureMode();
}
void Layer::fill(Vector2 mousePosition, Color fillColor) {
BeginTextureMode(renderTexture);
Image image = LoadImageFromTexture(renderTexture.texture);
Color* pixels = LoadImageColors(image);
int width = image.width;
int height = image.height;
int x = (int)mousePosition.x;
int y = height - (int)mousePosition.y;
if (x < 0 || x >= width || y < 0 || y >= height) {
UnloadImageColors(pixels);
UnloadImage(image);
EndTextureMode();
return;
}
Color targetColor = pixels[y * width + x];
if (ColorUtils::colorEqual(targetColor, fillColor)) {
UnloadImageColors(pixels);
UnloadImage(image);
EndTextureMode();
return;
}
std::stack<Vector2> stack;
stack.push({(float)x, (float)y});
while (!stack.empty()) {
Vector2 pos = stack.top();
stack.pop();
int px = (int)pos.x;
int py = (int)pos.y;
if (px < 0 || px >= width || py < 0 || py >= height) continue;
if (!ColorUtils::colorEqual(pixels[py * width + px], targetColor)) continue;
pixels[py * width + px] = fillColor;
stack.push({(float)(px + 1), (float)py});
stack.push({(float)(px - 1), (float)py});
stack.push({(float)px, (float)(py + 1)});
stack.push({(float)px, (float)(py - 1)});
}
UpdateTexture(renderTexture.texture, pixels);
UnloadImageColors(pixels);
UnloadImage(image);
EndTextureMode();
}
void Layer::render() {
DrawTextureRec(
renderTexture.texture,
(Rectangle){0, 0, (float)renderTexture.texture.width, -(float)renderTexture.texture.height},
(Vector2){0, 0},
WHITE
);
}