summaryrefslogtreecommitdiff
path: root/src/libs/graphics/tfb_prim.c
blob: f8a2df4d725cc029aa627b194b70a6e700223881 (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
// Copyright Michael Martin, 2003

/*
 *  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.
 */

/* The original Primitive routines do various elaborate checks to
 * ensure we're within bounds for the clipping.  Since clipping is
 * handled by the underlying TFB_Canvas implementation, we need not
 * worry about this. */

#include "gfxintrn.h"
#include "gfx_common.h"
#include "tfb_draw.h"
#include "tfb_prim.h"
#include "cmap.h"
#include "libs/log.h"

void
TFB_Prim_Point (POINT *p, Color color, DrawMode mode, POINT ctxOrigin)
{
	RECT r;

	// The caller must scale the origin!
	r.corner.x = p->x + ctxOrigin.x;
	r.corner.y = p->y + ctxOrigin.y;
	r.extent.width = r.extent.height = 1;

	if (_CurFramePtr->Type == SCREEN_DRAWABLE)
		TFB_DrawScreen_Rect (&r, color, mode, TFB_SCREEN_MAIN);
	else
		TFB_DrawImage_Rect (&r, color, mode, _CurFramePtr->image);
}

void
TFB_Prim_Rect (RECT *r, Color color, DrawMode mode, POINT ctxOrigin)
{
	RECT arm;
	int gscale;

	// XXX: Rect prim scaling is currently unused
	//   We scale the rect size just to be consistent with stamp prim,
	//   which does same. The caller must scale the origin!
	gscale = GetGraphicScale ();
	arm = *r;
	arm.extent.width = r->extent.width;
	arm.extent.height = 1;
	TFB_Prim_FillRect (&arm, color, mode, ctxOrigin);
	arm.extent.height = r->extent.height;
	arm.extent.width = 1;
	TFB_Prim_FillRect (&arm, color, mode, ctxOrigin);
	// rounding error correction here
	arm.corner.x += ((r->extent.width * gscale + (GSCALE_IDENTITY >> 1))
			/ GSCALE_IDENTITY) - 1;
	TFB_Prim_FillRect (&arm, color, mode, ctxOrigin);
	arm.corner.x = r->corner.x;
	arm.corner.y += ((r->extent.height * gscale + (GSCALE_IDENTITY >> 1))
			/ GSCALE_IDENTITY) - 1;
	arm.extent.width = r->extent.width;
	arm.extent.height = 1;
	TFB_Prim_FillRect (&arm, color, mode, ctxOrigin);
}

void
TFB_Prim_FillRect (RECT *r, Color color, DrawMode mode, POINT ctxOrigin)
{
	RECT rect;
	int gscale;

	rect.corner.x = r->corner.x + ctxOrigin.x;
	rect.corner.y = r->corner.y + ctxOrigin.y;
	rect.extent.width = r->extent.width;
	rect.extent.height = r->extent.height;

	// XXX: Rect prim scaling is currently unused
	//   We scale the rect size just to be consistent with stamp prim,
	//   which does same. The caller must scale the origin!
	gscale = GetGraphicScale ();
	if (gscale != GSCALE_IDENTITY)
	{	// rounding error correction here
		rect.extent.width = (rect.extent.width * gscale
				+ (GSCALE_IDENTITY >> 1)) / GSCALE_IDENTITY;
		rect.extent.height = (rect.extent.height * gscale
				+ (GSCALE_IDENTITY >> 1)) / GSCALE_IDENTITY;
	}

	if (_CurFramePtr->Type == SCREEN_DRAWABLE)
		TFB_DrawScreen_Rect (&rect, color, mode, TFB_SCREEN_MAIN);
	else
		TFB_DrawImage_Rect (&rect, color, mode, _CurFramePtr->image);
}

void
TFB_Prim_Line (LINE *line, Color color, DrawMode mode, POINT ctxOrigin)
{
	int x1, y1, x2, y2;

	// The caller must scale the origins!
	x1=line->first.x + ctxOrigin.x;
	y1=line->first.y + ctxOrigin.y;
	x2=line->second.x + ctxOrigin.x;
	y2=line->second.y + ctxOrigin.y;

	if (_CurFramePtr->Type == SCREEN_DRAWABLE)
		TFB_DrawScreen_Line (x1, y1, x2, y2, color, mode, TFB_SCREEN_MAIN);
	else
		TFB_DrawImage_Line (x1, y1, x2, y2, color, mode, _CurFramePtr->image);
}

void
TFB_Prim_Stamp (STAMP *stmp, DrawMode mode, POINT ctxOrigin)
{
	int x, y;
	FRAME SrcFramePtr;
	TFB_Image *img;
	TFB_ColorMap *cmap = NULL;

	SrcFramePtr = stmp->frame;
	if (!SrcFramePtr)
	{
		log_add (log_Warning, "TFB_Prim_Stamp: Tried to draw a NULL frame"
				" (Stamp address = %p)", (void *) stmp);
		return;
	}
	img = SrcFramePtr->image;
	
	if (!img)
	{
		log_add (log_Warning, "Non-existent image to TFB_Prim_Stamp()");
		return;
	}

	LockMutex (img->mutex);

	img->NormalHs = SrcFramePtr->HotSpot;
	// We scale the image size here, but the caller must scale the origin!
	x = stmp->origin.x + ctxOrigin.x;
	y = stmp->origin.y + ctxOrigin.y;

	if (TFB_DrawCanvas_IsPaletted(img->NormalImg) && img->colormap_index != -1)
	{
		// returned cmap is addrefed, must release later
		cmap = TFB_GetColorMap (img->colormap_index);
	}

	UnlockMutex (img->mutex);

	if (_CurFramePtr->Type == SCREEN_DRAWABLE)
	{
		TFB_DrawScreen_Image (img, x, y, GetGraphicScale (),
				GetGraphicScaleMode (), cmap, mode, TFB_SCREEN_MAIN);
	}
	else
	{
		TFB_DrawImage_Image (img, x, y, GetGraphicScale (),
				GetGraphicScaleMode (), cmap, mode, _CurFramePtr->image);
	}
}

void
TFB_Prim_StampFill (STAMP *stmp, Color color, DrawMode mode, POINT ctxOrigin)
{
	int x, y;
	FRAME SrcFramePtr;
	TFB_Image *img;

	SrcFramePtr = stmp->frame;
	if (!SrcFramePtr)
	{
		log_add (log_Warning, "TFB_Prim_StampFill: Tried to draw a NULL frame"
				" (Stamp address = %p)", (void *) stmp);
		return;
	}
	img = SrcFramePtr->image;

	if (!img)
	{
		log_add (log_Warning, "Non-existent image to TFB_Prim_StampFill()");
		return;
	}

	LockMutex (img->mutex);

	img->NormalHs = SrcFramePtr->HotSpot;
	// We scale the image size here, but the caller must scale the origin!
	x = stmp->origin.x + ctxOrigin.x;
	y = stmp->origin.y + ctxOrigin.y;

	UnlockMutex (img->mutex);

	if (_CurFramePtr->Type == SCREEN_DRAWABLE)
	{
		TFB_DrawScreen_FilledImage (img, x, y, GetGraphicScale (),
				GetGraphicScaleMode (), color, mode, TFB_SCREEN_MAIN);
	}
	else
	{
		TFB_DrawImage_FilledImage (img, x, y, GetGraphicScale (),
				GetGraphicScaleMode (), color, mode, _CurFramePtr->image);
	}
}

void
TFB_Prim_FontChar (POINT charOrigin, TFB_Char *fontChar, TFB_Image *backing,
		DrawMode mode, POINT ctxOrigin)
{
	int x, y;

	// Text prim does not scale
	x = charOrigin.x + ctxOrigin.x;
	y = charOrigin.y + ctxOrigin.y;

	if (_CurFramePtr->Type == SCREEN_DRAWABLE)
	{
		TFB_DrawScreen_FontChar (fontChar, backing, x, y, mode,
				TFB_SCREEN_MAIN);
	}
	else
	{
		TFB_DrawImage_FontChar (fontChar, backing, x, y, mode,
				_CurFramePtr->image);
	}
}

// Text rendering is in font.c, under the name _text_blt