1 /*
2 	r_progs.c
3 
4 	QC interface to the renderer
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the GNU General Public License for more details.
18 
19 	You should have received a copy of the GNU General Public License
20 	along with this program; if not, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif
37 #include <stdlib.h>
38 
39 #include "QF/draw.h"
40 #include "QF/hash.h"
41 #include "QF/progs.h"
42 #include "QF/render.h"
43 #include "QF/sys.h"
44 
45 #include "r_internal.h"
46 
47 typedef struct {
48 	pr_int_t    width;
49 	pr_int_t    height;
50 	pr_int_t    pic_handle;
51 } bi_qpic_t;
52 
53 typedef struct qpic_res_s {
54 	struct qpic_res_s *next;
55 	struct qpic_res_s **prev;
56 	bi_qpic_t  *bq;
57 	qpic_t     *pic;
58 	int         cached;
59 	char       *name;
60 } qpic_res_t;
61 
62 typedef struct {
63 	PR_RESMAP (qpic_res_t) qpic_map;
64 	qpic_res_t *qpics;
65 	hashtab_t  *pic_hash;
66 } draw_resources_t;
67 
68 static qpic_res_t *
qpic_new(draw_resources_t * res)69 qpic_new (draw_resources_t *res)
70 {
71 	PR_RESNEW (qpic_res_t, res->qpic_map);
72 }
73 
74 static void
bi_draw_free_qpic(qpic_res_t * qp)75 bi_draw_free_qpic (qpic_res_t *qp)
76 {
77 	if (qp->cached)
78 		r_funcs->Draw_UncachePic (qp->name);
79 	else
80 		r_funcs->Draw_DestroyPic (qp->pic);
81 	if (qp->name)
82 		free (qp->name);
83 }
84 
85 static void
qpic_free(draw_resources_t * res,qpic_res_t * qp)86 qpic_free (draw_resources_t *res, qpic_res_t *qp)
87 {
88 	bi_draw_free_qpic (qp);
89 	PR_RESFREE (qpic_res_t, res->qpic_map, qp);
90 }
91 
92 static void
qpic_reset(draw_resources_t * res)93 qpic_reset (draw_resources_t *res)
94 {
95 	PR_RESRESET (qpic_res_t, res->qpic_map);
96 }
97 
98 static inline qpic_res_t *
qpic_get(draw_resources_t * res,int index)99 qpic_get (draw_resources_t *res, int index)
100 {
101 	PR_RESGET (res->qpic_map, index);
102 }
103 
104 static inline int
qpic_index(draw_resources_t * res,qpic_res_t * qp)105 qpic_index (draw_resources_t *res, qpic_res_t *qp)
106 {
107 	PR_RESINDEX (res->qpic_map, qp);
108 }
109 
110 static qpic_res_t *
get_qpic(progs_t * pr,const char * name,int qpic_handle)111 get_qpic (progs_t *pr, const char *name, int qpic_handle)
112 {
113 	draw_resources_t *res = PR_Resources_Find (pr, "Draw");
114 	qpic_res_t *qp = qpic_get (res, qpic_handle);
115 
116 	if (!qp)
117 		PR_RunError (pr, "invalid qpic handle passed to %s", name + 3);
118 	return qp;
119 }
120 
121 static void
bi_Draw_FreePic(progs_t * pr)122 bi_Draw_FreePic (progs_t *pr)
123 {
124 	draw_resources_t *res = PR_Resources_Find (pr, "Draw");
125 	bi_qpic_t  *bq = &P_STRUCT (pr, bi_qpic_t, 0);
126 	qpic_res_t *qp = get_qpic (pr, __FUNCTION__, bq->pic_handle);
127 
128 	PR_Zone_Free (pr, qp->bq);
129 	qpic_free (res, qp);
130 }
131 
132 static void
bi_Draw_MakePic(progs_t * pr)133 bi_Draw_MakePic (progs_t *pr)
134 {
135 	draw_resources_t *res = PR_Resources_Find (pr, "Draw");
136 	int         width = P_INT (pr, 0);
137 	int         height = P_INT (pr, 1);
138 	byte       *data = (byte *) P_GSTRING (pr, 2);
139 	qpic_t     *pic;
140 	qpic_res_t *qp;
141 	bi_qpic_t  *bq;
142 
143 	pic = r_funcs->Draw_MakePic (width, height, data);
144 	qp = qpic_new (res);
145 	qp->name = 0;
146 	qp->pic = pic;
147 	qp->cached = 1;
148 	bq = PR_Zone_Malloc (pr, sizeof (bi_qpic_t));
149 	bq->width = pic->width;
150 	bq->height = pic->height;
151 	bq->pic_handle = qpic_index (res, qp);
152 	qp->bq = bq;
153 	RETURN_POINTER (pr, qp->bq);
154 }
155 
156 static void
bi_Draw_CachePic(progs_t * pr)157 bi_Draw_CachePic (progs_t *pr)
158 {
159 	draw_resources_t *res = PR_Resources_Find (pr, "Draw");
160 	const char *name = P_GSTRING (pr, 0);
161 	int         alpha = P_INT (pr, 1);
162 	qpic_t     *pic;
163 	qpic_res_t *qp;
164 	bi_qpic_t  *bq;
165 
166 	pic = r_funcs->Draw_CachePic (name, alpha);
167 	qp = Hash_Find (res->pic_hash, name);
168 	if (qp) {
169 		RETURN_POINTER (pr, qp->bq);
170 		return;
171 	}
172 	qp = qpic_new (res);
173 	qp->name = strdup (name);
174 	qp->pic = pic;
175 	qp->cached = 1;
176 	bq = PR_Zone_Malloc (pr, sizeof (bi_qpic_t));
177 	bq->width = pic->width;
178 	bq->height = pic->height;
179 	bq->pic_handle = qpic_index (res, qp);
180 	qp->bq = bq;
181 	Hash_Add (res->pic_hash, qp);
182 	RETURN_POINTER (pr, qp->bq);
183 }
184 
185 static void
bi_Draw_Pic(progs_t * pr)186 bi_Draw_Pic (progs_t *pr)
187 {
188 	int         x = P_INT (pr, 0);
189 	int         y = P_INT (pr, 1);
190 	bi_qpic_t  *bq = &P_STRUCT (pr, bi_qpic_t, 2);
191 	qpic_res_t *qp = get_qpic (pr, __FUNCTION__, bq->pic_handle);
192 	qpic_t     *pic = qp->pic;
193 
194 	r_funcs->Draw_Pic (x, y, pic);
195 }
196 
197 static void
bi_Draw_Picf(progs_t * pr)198 bi_Draw_Picf (progs_t *pr)
199 {
200 	float       x = P_FLOAT (pr, 0);
201 	float       y = P_FLOAT (pr, 1);
202 	bi_qpic_t  *bq = &P_STRUCT (pr, bi_qpic_t, 2);
203 	qpic_res_t *qp = get_qpic (pr, __FUNCTION__, bq->pic_handle);
204 	qpic_t     *pic = qp->pic;
205 
206 	r_funcs->Draw_Picf (x, y, pic);
207 }
208 
209 static void
bi_Draw_SubPic(progs_t * pr)210 bi_Draw_SubPic (progs_t *pr)
211 {
212 	int         x = P_INT (pr, 0);
213 	int         y = P_INT (pr, 1);
214 	bi_qpic_t  *bq = &P_STRUCT (pr, bi_qpic_t, 2);
215 	qpic_res_t *qp = get_qpic (pr, __FUNCTION__, bq->pic_handle);
216 	qpic_t     *pic = qp->pic;
217 	int         srcx = P_INT (pr, 3);
218 	int         srcy = P_INT (pr, 4);
219 	int         width = P_INT (pr, 5);
220 	int         height = P_INT (pr, 6);
221 
222 	r_funcs->Draw_SubPic (x, y, pic, srcx, srcy, width, height);
223 }
224 
225 static void
bi_Draw_CenterPic(progs_t * pr)226 bi_Draw_CenterPic (progs_t *pr)
227 {
228 	int         x = P_INT (pr, 0);
229 	int         y = P_INT (pr, 1);
230 	bi_qpic_t  *bq = &P_STRUCT (pr, bi_qpic_t, 2);
231 	qpic_res_t *qp = get_qpic (pr, __FUNCTION__, bq->pic_handle);
232 	qpic_t     *pic = qp->pic;
233 
234 	r_funcs->Draw_Pic (x - pic->width / 2, y, pic);
235 }
236 
237 static void
bi_Draw_Character(progs_t * pr)238 bi_Draw_Character (progs_t *pr)
239 {
240 	int         x = P_INT (pr, 0);
241 	int         y = P_INT (pr, 1);
242 	int         c = P_INT (pr, 2);
243 
244 	r_funcs->Draw_Character (x, y, c);
245 }
246 
247 static void
bi_Draw_String(progs_t * pr)248 bi_Draw_String (progs_t *pr)
249 {
250 	int         x = P_INT (pr, 0);
251 	int         y = P_INT (pr, 1);
252 	const char *text = P_GSTRING (pr, 2);
253 
254 	r_funcs->Draw_String (x, y, text);
255 }
256 
257 static void
bi_Draw_nString(progs_t * pr)258 bi_Draw_nString (progs_t *pr)
259 {
260 	int         x = P_INT (pr, 0);
261 	int         y = P_INT (pr, 1);
262 	const char *text = P_GSTRING (pr, 2);
263 	int         n = P_INT (pr, 3);
264 
265 	r_funcs->Draw_nString (x, y, text, n);
266 }
267 
268 static void
bi_Draw_AltString(progs_t * pr)269 bi_Draw_AltString (progs_t *pr)
270 {
271 	int         x = P_INT (pr, 0);
272 	int         y = P_INT (pr, 1);
273 	const char *text = P_GSTRING (pr, 2);
274 
275 	r_funcs->Draw_AltString (x, y, text);
276 }
277 
278 /*
279 	bi_Draw_Fill
280 
281 	Draws a filled area with a color
282 	(only a wrapper function to original qf-api)
283 */
284 static void
bi_Draw_Fill(progs_t * pr)285 bi_Draw_Fill (progs_t *pr)
286 {
287 	int         x = P_INT (pr, 0);
288 	int         y = P_INT (pr, 1);
289 	int         w = P_INT (pr, 2);
290 	int         h = P_INT (pr, 3);
291 	int         color = P_INT (pr, 4);
292 
293 	r_funcs->Draw_Fill (x, y, w, h, color);
294 }
295 
296 static void
bi_Draw_Crosshair(progs_t * pr)297 bi_Draw_Crosshair (progs_t *pr)
298 {
299 	int         ch = P_INT (pr, 0);
300 	int         x = P_INT (pr, 1);
301 	int         y = P_INT (pr, 2);
302 
303 	r_funcs->Draw_CrosshairAt (ch, x, y);
304 }
305 
306 static const char *
bi_draw_get_key(const void * p,void * unused)307 bi_draw_get_key (const void *p, void *unused)
308 {
309 	return ((qpic_res_t *) p)->name;
310 }
311 
312 static void
bi_draw_clear(progs_t * pr,void * data)313 bi_draw_clear (progs_t *pr, void *data)
314 {
315 	draw_resources_t *res = (draw_resources_t *) data;
316 	qpic_res_t *qp;
317 
318 	for (qp = res->qpics; qp; qp = qp->next) {
319 		bi_draw_free_qpic (qp);
320 	}
321 	res->qpics = 0;
322 
323 	qpic_reset (res);
324 	Hash_FlushTable (res->pic_hash);
325 }
326 
327 static builtin_t builtins[] = {
328 	{"Draw_FreePic",	bi_Draw_FreePic,	-1},
329 	{"Draw_MakePic",	bi_Draw_MakePic,	-1},
330 	{"Draw_CachePic",	bi_Draw_CachePic,	-1},
331 	{"Draw_Pic",		bi_Draw_Pic,		-1},
332 	{"Draw_Picf",		bi_Draw_Picf,		-1},
333 	{"Draw_SubPic",		bi_Draw_SubPic,		-1},
334 	{"Draw_CenterPic",	bi_Draw_CenterPic,	-1},
335 	{"Draw_Character",	bi_Draw_Character,	-1},
336 	{"Draw_String",		bi_Draw_String,		-1},
337 	{"Draw_nString",	bi_Draw_nString,	-1},
338 	{"Draw_AltString",	bi_Draw_AltString,	-1},
339 	{"Draw_Fill",		bi_Draw_Fill,		-1},
340 	{"Draw_Crosshair",	bi_Draw_Crosshair,	-1},
341 	{0}
342 };
343 
344 void
R_Progs_Init(progs_t * pr)345 R_Progs_Init (progs_t *pr)
346 {
347 	draw_resources_t *res = calloc (1, sizeof (draw_resources_t));
348 	res->pic_hash = Hash_NewTable (61, bi_draw_get_key, 0, 0);
349 
350 	PR_Resources_Register (pr, "Draw", res, bi_draw_clear);
351 	PR_RegisterBuiltins (pr, builtins);
352 }
353