summaryrefslogtreecommitdiff
path: root/src/game/Brush.h
blob: 1b7cf699184d5b5fe9331af0c388eabe37e19dc6 (plain)
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
/*
 * Brush.h
 *
 *  Created on: Jan 22, 2012
 *      Author: Simon
 */

#ifndef BRUSH_H_
#define BRUSH_H_

#include <iostream>
#include "interface/Point.h"

class Brush
{
protected:
	unsigned char * outline;
	unsigned char * bitmap;
	ui::Point size;
	ui::Point radius;
	void updateOutline()
	{
		if(!bitmap)
			GenerateBitmap();
		if(!bitmap)
			return;
		if(outline)
			delete[] outline;
		outline = new unsigned char[size.X*size.Y];
		for(int x = 0; x < size.X; x++)
		{
			for(int y = 0; y < size.Y; y++)
			{
				if(bitmap[y*size.X+x] && (!y || !x || y == size.X-1 || x == 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]))
					outline[y*size.X+x] = 255;
				else
					outline[y*size.X+x] = 0;
			}
		}
	}
public:
	Brush(ui::Point size_):
		bitmap(NULL),
		outline(NULL),
		radius(0, 0),
		size(0, 0)
	{
		SetRadius(size_);
	};
	
	//Radius of the brush 0x0 - infxinf (Radius of 0x0 would be 1x1, radius of 1x1 would be 3x3)
	ui::Point GetRadius()
	{
		return radius;
	}
	
	//Size of the brush bitmap mask, 1x1 - infxinf
	ui::Point GetSize()
	{
		return size;
	}
	void SetRadius(ui::Point radius)
	{
		this->radius = radius;
		this->size = radius+radius+ui::Point(1, 1);

		GenerateBitmap();
		updateOutline();
	}
	virtual ~Brush() {
		if(bitmap)
			delete[] bitmap;
		if(outline)
			delete[] outline;
	}
	virtual void RenderRect(Graphics * g, ui::Point position1, ui::Point position2)
	{
		int width, height, t;
		width = position2.X-position1.X;
		height = position2.Y-position1.Y;
		if(height<0)
		{
			position1.Y += height;
			height *= -1;
		}
		if(width<0)
		{
			position1.X += width;
			width *= -1;
		}
		g->xor_line(position1.X, position1.Y, position1.X+width, position1.Y);
		g->xor_line(position1.X, position1.Y+height, position1.X+width, position1.Y+height);
		g->xor_line(position1.X+width, position1.Y+1, position1.X+width, position1.Y+height-1);
		g->xor_line(position1.X, position1.Y+1, position1.X, position1.Y+height-1);
	}
	virtual void RenderLine(Graphics * g, ui::Point position1, ui::Point position2)
	{
		g->xor_line(position1.X, position1.Y, position2.X, position2.Y);
	}
	//Draw the brush outline onto the screen
	virtual void RenderPoint(Graphics * g, ui::Point position)
	{
		if(!outline)
			updateOutline();
		if(!outline)
			return;
		g->xor_bitmap(outline, position.X-radius.X, position.Y-radius.Y, size.X, size.Y);
	}
	virtual void RenderFill(Graphics * g, ui::Point position)
	{
		//Do nothing for now - possibly draw some sort of flood fill mask
	}
	virtual void GenerateBitmap()
	{
		if(bitmap)
			delete[] bitmap;
		bitmap = new unsigned char[size.X*size.Y];
		for(int x = 0; x < size.X; x++)
		{
			for(int y = 0; y < size.Y; y++)
			{
				bitmap[(y*size.X)+x] = 255;
			}
		}
	}
	//Get a bitmap for drawing particles
	unsigned char * GetBitmap()
	{
		if(!bitmap)
			GenerateBitmap();
		return bitmap;
	}

	unsigned char * GetOutline()
	{
		if(!outline)
			updateOutline();
		if(!outline)
			return NULL;
		return outline;
	}
};


#endif /* BRUSH_H_ */