summaryrefslogtreecommitdiff
path: root/src/libs/graphics/frame.c
blob: 0539746c3ca7fe13e1dc8752fd699ff2afbb1492 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  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 "gfxintrn.h"
#include "gfx_common.h"
#include "tfb_draw.h"
#include "tfb_prim.h"

HOT_SPOT
MAKE_HOT_SPOT (COORD x, COORD y)
{
	HOT_SPOT hs;
	hs.x = x;
	hs.y = y;
	return hs;
}

// XXX: INTERNAL_PRIMITIVE and INTERNAL_PRIM_DESC are not used
typedef union
{
	POINT Point;
	STAMP Stamp;
	BRESENHAM_LINE Line;
	TEXT Text;
	RECT Rect;
} INTERNAL_PRIM_DESC;

typedef struct
{
	PRIM_LINKS Links;
	GRAPHICS_PRIM Type;
	Color Color;
	INTERNAL_PRIM_DESC Object;
} INTERNAL_PRIMITIVE;


// pValidRect or origin may be NULL
BOOLEAN
GetContextValidRect (RECT *pValidRect, POINT *origin)
{
	RECT tempRect;
	POINT tempPt;

	if (!pValidRect)
		pValidRect = &tempRect;
	if (!origin)
		origin = &tempPt;

	// Start with a rect the size of foreground frame
	pValidRect->corner.x = 0;
	pValidRect->corner.y = 0;
	pValidRect->extent = GetFrameBounds (_CurFramePtr);
	*origin = _CurFramePtr->HotSpot;

	if (_pCurContext->ClipRect.extent.width)
	{
		// If the cliprect is completely outside of the valid frame
		// bounds we have nothing to draw
		if (!BoxIntersect (&_pCurContext->ClipRect,
				pValidRect, pValidRect))
			return (FALSE);

		// Foreground frame hotspot defines a drawing position offset
		// WRT the context cliprect
		origin->x += _pCurContext->ClipRect.corner.x;
		origin->y += _pCurContext->ClipRect.corner.y;
	}

	return (TRUE);
}

static void
ClearBackGround (RECT *pClipRect)
{
	RECT clearRect;
	Color color = _get_context_bg_color ();
	clearRect.corner.x = 0;
	clearRect.corner.y = 0;
	clearRect.extent = pClipRect->extent;
	TFB_Prim_FillRect (&clearRect, color, DRAW_REPLACE_MODE,
			pClipRect->corner);
}

void
DrawBatch (PRIMITIVE *lpBasePrim, PRIM_LINKS PrimLinks, 
		BATCH_FLAGS BatchFlags)
{
	RECT ValidRect;
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (&ValidRect, &origin))
	{
		COUNT CurIndex;
		PRIMITIVE *lpPrim;
		DrawMode mode = _get_context_draw_mode ();

		BatchGraphics ();

		if (BatchFlags & BATCH_BUILD_PAGE)
		{
			ClearBackGround (&ValidRect);
		}

		CurIndex = GetPredLink (PrimLinks);

		for (; CurIndex != END_OF_LIST;
				CurIndex = GetSuccLink (GetPrimLinks (lpPrim)))
		{
			GRAPHICS_PRIM PrimType;
			PRIMITIVE *lpWorkPrim;
			RECT ClipRect;
			Color color;

			lpPrim = &lpBasePrim[CurIndex];
			PrimType = GetPrimType (lpPrim);
			if (!ValidPrimType (PrimType))
				continue;

			lpWorkPrim = lpPrim;

			switch (PrimType)
			{
				case POINT_PRIM:
					color = GetPrimColor (lpWorkPrim);
					TFB_Prim_Point (&lpWorkPrim->Object.Point, color,
							mode, origin);
					break;
				case STAMP_PRIM:
					TFB_Prim_Stamp (&lpWorkPrim->Object.Stamp, mode, origin);
					break;
				case STAMPFILL_PRIM:
					color = GetPrimColor (lpWorkPrim);
					TFB_Prim_StampFill (&lpWorkPrim->Object.Stamp, color,
							mode, origin);
					break;
				case LINE_PRIM:
					color = GetPrimColor (lpWorkPrim);
					TFB_Prim_Line (&lpWorkPrim->Object.Line, color,
							mode, origin);
					break;
				case TEXT_PRIM:
					if (!TextRect (&lpWorkPrim->Object.Text, &ClipRect, NULL))
						continue;
					// ClipRect is relative to origin
					_text_blt (&ClipRect, &lpWorkPrim->Object.Text, origin);
					break;
				case RECT_PRIM:
					color = GetPrimColor (lpWorkPrim);
					TFB_Prim_Rect (&lpWorkPrim->Object.Rect, color,
							mode, origin);
					break;
				case RECTFILL_PRIM:
					color = GetPrimColor (lpWorkPrim);
					TFB_Prim_FillRect (&lpWorkPrim->Object.Rect, color,
							mode, origin);
					break;
			}
		}

		UnbatchGraphics ();
	}
}

void
ClearDrawable (void)
{
	RECT ValidRect;

	if (GraphicsSystemActive () && GetContextValidRect (&ValidRect, NULL))
	{
		ClearBackGround (&ValidRect);
	}
}

void
DrawPoint (POINT *lpPoint)
{
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (NULL, &origin))
	{
		Color color = GetPrimColor (&_locPrim);
		DrawMode mode = _get_context_draw_mode ();
		TFB_Prim_Point (lpPoint, color, mode, origin);
	}
}

void
DrawRectangle (RECT *lpRect)
{
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (NULL, &origin))
	{
		Color color = GetPrimColor (&_locPrim);
		DrawMode mode = _get_context_draw_mode ();
		TFB_Prim_Rect (lpRect, color, mode, origin);
	}
}

void
DrawFilledRectangle (RECT *lpRect)
{
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (NULL, &origin))
	{
		Color color = GetPrimColor (&_locPrim);
		DrawMode mode = _get_context_draw_mode ();
		TFB_Prim_FillRect (lpRect, color, mode, origin);
	}
}

void
DrawLine (LINE *lpLine)
{
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (NULL, &origin))
	{
		Color color = GetPrimColor (&_locPrim);
		DrawMode mode = _get_context_draw_mode ();
		TFB_Prim_Line (lpLine, color, mode, origin);
	}
}

void
DrawStamp (STAMP *stmp)
{
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (NULL, &origin))
	{
		DrawMode mode = _get_context_draw_mode ();
		TFB_Prim_Stamp (stmp, mode, origin);
	}
}

void
DrawFilledStamp (STAMP *stmp)
{
	POINT origin;

	if (GraphicsSystemActive () && GetContextValidRect (NULL, &origin))
	{
		Color color = GetPrimColor (&_locPrim);
		DrawMode mode = _get_context_draw_mode ();
		TFB_Prim_StampFill (stmp, color, mode, origin);
	}
}