1 #include "Brush.h"
2 #include "graphics/Renderer.h"
3
Brush(ui::Point size_)4 Brush::Brush(ui::Point size_):
5 outline(NULL),
6 bitmap(NULL),
7 size(0, 0),
8 radius(0, 0)
9 {
10 SetRadius(size_);
11 };
12
~Brush()13 Brush::~Brush()
14 {
15 delete[] bitmap;
16 delete[] outline;
17 }
18
updateOutline()19 void Brush::updateOutline()
20 {
21 if(!bitmap)
22 GenerateBitmap();
23 if(!bitmap)
24 return;
25 delete[] outline;
26 outline = new unsigned char[size.X*size.Y];
27 for(int x = 0; x < size.X; x++)
28 {
29 for(int y = 0; y < size.Y; y++)
30 {
31 if(bitmap[y*size.X+x] && (!y || !x || x == size.X-1 || y == size.Y-1 || !bitmap[y*size.X+(x+1)] || !bitmap[y*size.X+(x-1)] || !bitmap[(y-1)*size.X+x] || !bitmap[(y+1)*size.X+x]))
32 {
33 outline[y*size.X+x] = 255;
34 }
35 else
36 outline[y*size.X+x] = 0;
37 }
38 }
39 }
40
SetRadius(ui::Point radius)41 void Brush::SetRadius(ui::Point radius)
42 {
43 this->radius = radius;
44 this->size = radius+radius+ui::Point(1, 1);
45
46 GenerateBitmap();
47 updateOutline();
48 }
49
GenerateBitmap()50 void Brush::GenerateBitmap()
51 {
52 delete[] bitmap;
53 bitmap = new unsigned char[size.X*size.Y];
54 for(int x = 0; x < size.X; x++)
55 {
56 for(int y = 0; y < size.Y; y++)
57 {
58 bitmap[(y*size.X)+x] = 255;
59 }
60 }
61 }
62
GetBitmap()63 unsigned char *Brush::GetBitmap()
64 {
65 if(!bitmap)
66 GenerateBitmap();
67 return bitmap;
68 }
69
GetOutline()70 unsigned char *Brush::GetOutline()
71 {
72 if(!outline)
73 updateOutline();
74 if(!outline)
75 return NULL;
76 return outline;
77 }
78
RenderRect(Renderer * ren,ui::Point position1,ui::Point position2)79 void Brush::RenderRect(Renderer * ren, ui::Point position1, ui::Point position2)
80 {
81 int width, height;
82 width = position2.X-position1.X;
83 height = position2.Y-position1.Y;
84 if(height<0)
85 {
86 position1.Y += height;
87 height *= -1;
88 }
89 if(width<0)
90 {
91 position1.X += width;
92 width *= -1;
93 }
94
95 ren->xor_line(position1.X, position1.Y, position1.X+width, position1.Y);
96 if(height>0){
97 ren->xor_line(position1.X, position1.Y+height, position1.X+width, position1.Y+height);
98 if(height>1){
99 ren->xor_line(position1.X+width, position1.Y+1, position1.X+width, position1.Y+height-1);
100 if(width>0)
101 ren->xor_line(position1.X, position1.Y+1, position1.X, position1.Y+height-1);
102 }
103 }
104 }
105
RenderLine(Renderer * ren,ui::Point position1,ui::Point position2)106 void Brush::RenderLine(Renderer * ren, ui::Point position1, ui::Point position2)
107 {
108 ren->xor_line(position1.X, position1.Y, position2.X, position2.Y);
109 }
110
RenderPoint(Renderer * ren,ui::Point position)111 void Brush::RenderPoint(Renderer * ren, ui::Point position)
112 {
113 if(!outline)
114 updateOutline();
115 if(!outline)
116 return;
117 ren->xor_bitmap(outline, position.X-radius.X, position.Y-radius.Y, size.X, size.Y);
118 }
119
RenderFill(Renderer * ren,ui::Point position)120 void Brush::RenderFill(Renderer * ren, ui::Point position)
121 {
122 ren->xor_line(position.X-5, position.Y, position.X-1, position.Y);
123 ren->xor_line(position.X+5, position.Y, position.X+1, position.Y);
124 ren->xor_line(position.X, position.Y-5, position.X, position.Y-1);
125 ren->xor_line(position.X, position.Y+5, position.X, position.Y+1);
126 }
127