summaryrefslogtreecommitdiff
path: root/src/libs/graphics/bbox.c
blob: ce57d32d74c09ed1f02c93b875e84582237d6c70 (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
/*
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "port.h"
#include "libs/graphics/bbox.h"

TFB_BoundingBox TFB_BBox;
int maxWidth;
int maxHeight;

void
TFB_BBox_Init (int width, int height)
{
	maxWidth = width;
	maxHeight = height;
	TFB_BBox.clip.extent.width = width;
	TFB_BBox.clip.extent.height = height;
}

void
TFB_BBox_Reset (void)
{
	TFB_BBox.valid = 0;
}

void
TFB_BBox_SetClipRect (const RECT *r)
{
	if (!r)
	{	/* No clipping -- full rect */
		TFB_BBox.clip.corner.x = 0;
		TFB_BBox.clip.corner.y = 0;
		TFB_BBox.clip.extent.width = maxWidth;
		TFB_BBox.clip.extent.height = maxHeight;
		return;
	}

	TFB_BBox.clip = *r;

	/* Make sure the cliprect is sane */
	if (TFB_BBox.clip.corner.x < 0)
		TFB_BBox.clip.corner.x = 0;

	if (TFB_BBox.clip.corner.y < 0)
		TFB_BBox.clip.corner.y = 0;

	if (TFB_BBox.clip.corner.x + TFB_BBox.clip.extent.width > maxWidth)
		TFB_BBox.clip.extent.width = maxWidth - TFB_BBox.clip.corner.x;
	
	if (TFB_BBox.clip.corner.y + TFB_BBox.clip.extent.height > maxHeight)
		TFB_BBox.clip.extent.height = maxHeight - TFB_BBox.clip.corner.y;
}

void
TFB_BBox_RegisterPoint (int x, int y) 
{
	int x1 = TFB_BBox.clip.corner.x; 
	int y1 = TFB_BBox.clip.corner.y;
	int x2 = TFB_BBox.clip.corner.x + TFB_BBox.clip.extent.width - 1;
	int y2 = TFB_BBox.clip.corner.y + TFB_BBox.clip.extent.height - 1;

	/* Constrain coordinates */
	if (x < x1) x = x1;
	if (x >= x2) x = x2;
	if (y < y1) y = y1;
	if (y >= y2) y = y2;

	/* Is this the first point?  If so, set a pixel-region and return. */
	if (!TFB_BBox.valid)
	{
		TFB_BBox.valid = 1;
		TFB_BBox.region.corner.x = x;
		TFB_BBox.region.corner.y = y;
		TFB_BBox.region.extent.width = 1;
		TFB_BBox.region.extent.height = 1;
		return;
	}

	/* Otherwise expand the rectangle if necessary. */
	x1 = TFB_BBox.region.corner.x; 
	y1 = TFB_BBox.region.corner.y;
	x2 = TFB_BBox.region.corner.x + TFB_BBox.region.extent.width - 1;
	y2 = TFB_BBox.region.corner.y + TFB_BBox.region.extent.height - 1;

	if (x < x1) {
		TFB_BBox.region.corner.x = x;
		TFB_BBox.region.extent.width += x1 - x;
	}
	if (y < y1) {
		TFB_BBox.region.corner.y = y;
		TFB_BBox.region.extent.height += y1 - y;
	}
	if (x > x2) {
		TFB_BBox.region.extent.width += x - x2;
	}
	if (y > y2) {
		TFB_BBox.region.extent.height += y - y2;
	}
}

void
TFB_BBox_RegisterRect (const RECT *r)
{
	/* RECT will still register as a corner point of the cliprect even
	 * if it does not intersect with the cliprect at all. This is not
	 * a problem, as more is not less. */
	TFB_BBox_RegisterPoint (r->corner.x, r->corner.y);
	TFB_BBox_RegisterPoint (r->corner.x + r->extent.width - 1,
			r->corner.y + r->extent.height - 1);
}

void
TFB_BBox_RegisterCanvas (TFB_Canvas c, int x, int y)
{
	RECT r;
	r.corner.x = x;
	r.corner.y = y;
	TFB_DrawCanvas_GetExtent (c, &r.extent);
	TFB_BBox_RegisterRect (&r);
}