-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlayer.cpp
98 lines (79 loc) · 2.15 KB
/
layer.cpp
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
#include "layer.h"
#include "stb_image.h"
Layer::Layer()
{
_size[0] = _size[1] = 0;
}
void Layer::setSize(int size[2])
{
byte pixel[4] = {255, 255, 255, 255};
setSize(size[0], size[1], pixel);
}
void Layer::setSize(int w, int h)
{
byte pixel[4] = {255, 255, 255, 255};
setSize(w, h, pixel);
}
void Layer::setSize(int size[2], const byte pixel[])
{
setSize(size[0], size[1], pixel);
}
void Layer::setSize(int w, int h, const byte pixel[])
{
this->_offset[0] = 0;
this->_offset[1] = 0;
this->_bpp = 4;
if (w != _size[0] || h != _size[1])
{
this->_size[0] = w;
this->_size[1] = h;
if (this->_data != nullptr)
{
delete[] this->_data;
}
this->_data = new unsigned char[dataSize()];
}
for (int y = 0; y < this->_size[1]; y++)
{
for (int x = 0; x < this->_size[0]; x++)
{
setPixel<4>(x, y, pixel);
}
}
}
int Layer::dataSize() const
{
return this->_size[0] * this->_size[1] * this->_bpp;
}
Layer *Layer::defaultLayer(int size[2], const byte pixel[])
{
auto layer = new Layer();
layer->setSize(size, pixel);
return layer;
}
Layer *Layer::fromFile(const char *filename)
{
auto layer = new Layer();
layer->_data = stbi_load(filename, &(layer->_size[0]), &(layer->_size[1]), &(layer->_bpp), 0);
return layer;
}
void Layer::overwrite(Layer *a, Layer *b)
{
for (int y = 0; y < b->_size[1]; y++)
{
for (int x = 0; x < b->_size[0]; x++)
{
if (x + a->_offset[0] < 0) continue;
if (y + a->_offset[1] < 0) continue;
if (x + b->_offset[0] < 0) continue;
if (y + b->_offset[1] < 0) continue;
if (x + a->_offset[0] >= a->_size[0]) continue;
if (y + a->_offset[1] >= a->_size[1]) continue;
if (x + b->_offset[0] >= b->_size[0]) continue;
if (y + b->_offset[1] >= b->_size[1]) continue;
auto bpixel = b->pixel<4>(x + b->_offset[0], y + b->_offset[1]);
if (bpixel[3] == 0) continue;
a->setPixel<4>(x + a->_offset[0], y + a->_offset[1], bpixel);
}
}
}