aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gfx/resource/sci_picfill.cpp
blob: 0867b84c6e9b2b4adc0802802f6e6adf654f56b0 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "sci/gfx/gfx_resource.h"

namespace Sci {

/* Generic pic filling code, to be included by sci_pic_0.c
 *
 *
 * To use, define the following:
 *    FILL_FUNCTION: Name of the exported floodfill function
 *    FILL_FUNCTION_RECURSIVE: Name of the helper function
 *
 * Define DRAW_SCALED to support scaled drawing, or leave it out for faster
 * processing.
 *
 */

#ifdef FILL_RECURSIVE_DEBUG
#  define PRINT_DEBUG0(s) if (!fillmagc) fprintf(stderr, s)
#  define PRINT_DEBUG1(s,p1) if (!fillmagc) fprintf(stderr, s, p1)
#  define PRINT_DEBUG4(s,p1,p2,p3,p4) if (!fillmagc) fprintf(stderr, s, p1)
#else
#  define PRINT_DEBUG0(s)
#  define PRINT_DEBUG1(s,p1)
#  define PRINT_DEBUG4(s,p1,p2,p3,p4)
#endif

#ifdef DRAW_SCALED
#  define SCALED_CHECK(x) (x)
#  define IS_BOUNDARY(x, y, index)  (((index) & legalmask) != legalcolor)
#else
#  define SCALED_CHECK(x) 1
#  define IS_BOUNDARY(x, y, index) (								\
	(((x)+(y)) & 1)? /* figure out which part of the mask to use, to simulate dithering */	\
	   ((((index)) & ((legalmask) )) != ((legalcolor) & ((legalmask)))) /* odd coordinate */			\
	 : ((((index)) & ((legalmask) >> 8)) != ((legalcolor) & ((legalmask) >> 8))) /* even coordinate */			\
	)
#endif

static void FILL_FUNCTION_RECURSIVE(gfxr_pic_t *pic, int old_xl, int old_xr, int y, int dy, byte *bounds,
                        int legalcolor, int legalmask, int color, int priority, int drawenable, int sci_titlebar_size) {
	int linewidth = pic->mode->xfact * 320;
	int miny = pic->mode->yfact * sci_titlebar_size;
	int maxy = pic->mode->yfact * 200;
	int xl, xr;
	int oldytotal = y * linewidth;
#ifdef DRAW_SCALED
	int old_proj_y = -42;
	int proj_y;
	int proj_ytotal;
	int proj_x;
	int proj_xl_bound = 0;
	int proj_xr_bound = 0;
#endif

	do {
		int ytotal = oldytotal + (linewidth * dy);
		int xcont;
		int state;

		y += dy;

#ifdef FILL_RECURSIVE_DEBUG
		if (!fillc)
			return;
		else if (!fillmagc) {
			--fillc;
		}
#endif

		if (y < miny || y >= maxy) {
			PRINT_DEBUG0("ABRT on failed initial assertion!\n");
			return;
		}
#ifdef DRAW_SCALED
		proj_y = y / pic->mode->yfact;

		if (proj_y != old_proj_y) {
			// First, find the projected coordinates, unless known already:
			proj_ytotal = proj_y * 320;
			proj_x = old_xl / pic->mode->xfact;

			proj_xl_bound = proj_x;
			if (SCALED_CHECK(pic->aux_map[proj_ytotal + proj_xl_bound] & FRESH_PAINT)) {
				while (proj_xl_bound && pic->aux_map[proj_ytotal + proj_xl_bound - 1] & FRESH_PAINT)
					--proj_xl_bound;
			} else {
				while (proj_xl_bound < 319 && !(pic->aux_map[proj_ytotal + proj_xl_bound + 1] & FRESH_PAINT))
					++proj_xl_bound;

				if (proj_xl_bound < 319)
					++proj_xl_bound;
			}

			if (proj_xl_bound == 319
			        && !(pic->aux_map[proj_ytotal + proj_xl_bound] & FRESH_PAINT)) {
				PRINT_DEBUG0("ABRT because proj_xl_bound couldn't be found\n");
				return;
			}

			proj_xr_bound = (proj_xl_bound > proj_x) ? proj_xl_bound : proj_x;
			while ((proj_xr_bound < 319)
			        && pic->aux_map[proj_ytotal + proj_xr_bound + 1] & FRESH_PAINT)
				++proj_xr_bound;

#ifdef FILL_RECURSIVE_DEBUG
			if (!fillmagc) {
				fprintf(stderr, "l%d: {%d,%d} | ", proj_y, proj_xl_bound, proj_xr_bound);
				pic->aux_map[proj_y*320 + proj_xl_bound] |= 0x2;
				pic->aux_map[proj_y*320 + proj_xr_bound] |= 0x2;
			}
#endif

			proj_xl_bound *= pic->mode->xfact;
			if (proj_xl_bound)
				proj_xl_bound -= pic->mode->xfact - 1;

			if (proj_xr_bound < 319)
				++proj_xr_bound;
			proj_xr_bound *= pic->mode->xfact;
			proj_xr_bound += pic->mode->xfact - 1;

			old_proj_y = proj_y;
		}
#else
#  define proj_xl_bound 0
#  define proj_xr_bound 319
#endif

		// Now we have the projected limits, get the real ones:

		xl = (old_xl > proj_xl_bound) ? old_xl : proj_xl_bound;
		if (!IS_BOUNDARY(xl, y + 1, bounds[ytotal + xl])) { // go left as far as possible
			while (xl > proj_xl_bound && (!IS_BOUNDARY(xl - 1, y + 1, bounds[ytotal + xl - 1])))
				--xl;
		} else // go right until the fillable area starts
			while (xl < proj_xr_bound && (IS_BOUNDARY(xl, y + 1, bounds[ytotal + xl])))
				++xl;


		PRINT_DEBUG1("<%d,", xl);

		if ((xl > proj_xr_bound)
		        || (xl > old_xr)) {
			PRINT_DEBUG0("ABRT because xl > xr_bound\n");
			return;
		}

		xr = (xl > old_xl) ? xl : old_xl;
		while (xr < proj_xr_bound && (!IS_BOUNDARY(xr + 1, y + 1, bounds[ytotal + xr + 1])))
			++xr;

		PRINT_DEBUG1("%d> -> ", xr);

		if (IS_BOUNDARY(xl, y + 1,  bounds[ytotal + xl])) {
			PRINT_DEBUG0("ABRT because xl illegal\n");
			return;
		}

#ifdef DRAW_SCALED
		PRINT_DEBUG4("[%d[%d,%d]%d]\n", proj_xl_bound, xl, xr, proj_xr_bound);

		if (xl < proj_xl_bound && xr - 3*pic->mode->xfact < proj_xl_bound) {
			PRINT_DEBUG0("ABRT interval left of zone\n");
			return;
		}

		if (xr > proj_xr_bound && xl + 3*pic->mode->xfact > proj_xr_bound) {
			PRINT_DEBUG0("ABRT because interval right of zone\n");
			return;
		}
#endif

		if (drawenable & GFX_MASK_VISUAL)
			memset(pic->visual_map->index_data + ytotal + xl, color, xr - xl + 1);

		if (drawenable & GFX_MASK_PRIORITY)
			memset(pic->priority_map->index_data + ytotal + xl, priority, xr - xl + 1);


		// Check whether we need to recurse on branches in the same direction
		state = 0;
		xcont = xr + 1;
		while (xcont <= old_xr) {
			if (IS_BOUNDARY(xcont, y + 1, bounds[ytotal + xcont]))
				state = xcont;
			else if (state) { // recurse
				PRINT_DEBUG4("[%d[%d,%d],%d]: ", old_xl, xl, xr, old_xr);
				PRINT_DEBUG4("rec BRANCH %d [%d,%d] l%d\n", dy, state, xcont, y - dy);

				FILL_FUNCTION_RECURSIVE(pic, state, xcont, y - dy, dy, bounds, legalcolor,
				                        legalmask, color, priority, drawenable, sci_titlebar_size);
				state = 0;
			}
			++xcont;
		}

		// Check whether we need to recurse on backward branches:
		// left
		if (xl < old_xl - 1) {
			state = 0;
			for (xcont = old_xl - 1; xcont >= xl; xcont--) {
				if (IS_BOUNDARY(xcont, y, bounds[oldytotal + xcont]))
					state = xcont;
				else if (state) { // recurse
					PRINT_DEBUG4("[%d[%d,%d],%d]: ", old_xl, xl, xr, old_xr);
					PRINT_DEBUG4("rec BACK-LEFT %d [%d,%d] l%d\n", -dy, state, xcont, y);

					FILL_FUNCTION_RECURSIVE(pic, xcont, state, y, -dy, bounds,
					                        legalcolor, legalmask, color, priority, drawenable,
					                        sci_titlebar_size);
					state = 0;
				}
			}
		}

		// right
		if (xr > old_xr + 1) {
			state = 0;
			for (xcont = old_xr + 1; xcont <= xr; xcont++) {
				if (IS_BOUNDARY(xcont, y, bounds[oldytotal + xcont]))
					state = xcont;
				else if (state) { // recurse
					PRINT_DEBUG4("[%d[%d,%d],%d]: ", old_xl, xl, xr, old_xr);
					PRINT_DEBUG4("rec BACK-RIGHT %d [%d,%d] l%d\n", -dy, state, xcont, y);

					FILL_FUNCTION_RECURSIVE(pic, state, xcont, y, -dy, bounds,
					                        legalcolor, legalmask, color, priority, drawenable,
					                        sci_titlebar_size);
					state = 0;
				}
			}
		}

		oldytotal = ytotal;
		old_xl = xl;
		old_xr = xr;

	} while (1);
}


static void FILL_FUNCTION(gfxr_pic_t *pic, int x_320, int y_200, int color, int priority, int control, int drawenable,
              int sci_titlebar_size) {
	int linewidth = pic->mode->xfact * 320;
	int x, y;
	int xl, xr;
	int ytotal;
	int bitmask;
	byte *bounds = NULL;
	int legalcolor, legalmask;
#ifdef DRAW_SCALED
	int min_x, min_y, max_x, max_y;
#endif
	int original_drawenable = drawenable; // Backup, since we need the unmodified value
					      // for filling the aux and control map

	// Restrict drawenable not to restrict itself to zero
	if (pic->control_map->index_data[y_200 * 320 + x_320] != 0)
		drawenable &= ~GFX_MASK_CONTROL;

	if (color == 0xff)
		drawenable &= ~GFX_MASK_VISUAL;

	if (priority == 0) {
		drawenable &= ~GFX_MASK_PRIORITY;
		original_drawenable &= ~GFX_MASK_PRIORITY;
	}

	AUXBUF_FILL(pic, x_320, y_200, original_drawenable, (drawenable & GFX_MASK_CONTROL) ? control : 0,
	            sci_titlebar_size);

#ifdef DRAW_SCALED
	_gfxr_auxbuf_spread(pic, &min_x, &min_y, &max_x, &max_y);

	if (_gfxr_find_fill_point(pic, min_x, min_y, max_x, max_y, x_320, y_200, color, drawenable, &x, &y)) {
		//GFXWARN("Could not find scaled fill point, but unscaled fill point was available!\n");
		drawenable &= GFX_MASK_PRIORITY;
		if (!drawenable)
			_gfxr_auxbuf_propagate_changes(pic, 0);
	}
#else
	x = x_320;
	y = y_200;
#endif

	ytotal = y * linewidth;

	if (!drawenable)
		return;

	if (drawenable & GFX_MASK_VISUAL) {
		bounds = pic->visual_map->index_data;
#if 0
		// Code disabled, as removing it fixes qg1 pic.095 (unscaled). However,
		// it MAY be of relevance to scaled pic drawing...

		if ((color & 0xf) == 0xf // When dithering with white, do more
					 // conservative checks
		        || (color & 0xf0) == 0xf0)
			legalcolor = 0xff;
		else
			legalcolor = 0xf0; // Only check the second color
#endif
#ifdef DRAW_SCALED
		legalcolor = 0xff;
		legalmask = legalcolor;
#else
		legalmask = 0x0ff0;
		legalcolor = 0xff;
#endif
	} else if (drawenable & GFX_MASK_PRIORITY) {
		bounds = pic->priority_map->index_data;
		legalcolor = 0;
		legalmask = 0x0f0f;
	} else {
		legalcolor = 0;
		legalmask = 0x0f0f;
	}

	if (!bounds || IS_BOUNDARY(x, y, bounds[ytotal + x]))
		return;

	if (bounds) {
#ifdef DRAW_SCALED
		int proj_y = y_200;
		int proj_ytotal = proj_y * 320;
		int proj_x = x_320;
		int proj_xl_bound;
		int proj_xr_bound;
		int proj_xl, proj_xr;

		ytotal = y * linewidth;

		proj_xl_bound = proj_x;
		if (SCALED_CHECK(pic->aux_map[proj_ytotal + proj_xl_bound] & FRESH_PAINT)) {
			while (proj_xl_bound && SCALED_CHECK(pic->aux_map[proj_ytotal + proj_xl_bound - 1] & FRESH_PAINT))
				--proj_xl_bound;
		} else
			while (proj_xl_bound < 319 && SCALED_CHECK(!(pic->aux_map[proj_ytotal + proj_xl_bound + 1] & FRESH_PAINT)))
				++proj_xl_bound;

		proj_xr_bound = (proj_xl_bound > proj_x) ? proj_xl_bound : proj_x;
		while ((proj_xr_bound < 319) && SCALED_CHECK(pic->aux_map[proj_ytotal + proj_xr_bound + 1] & FRESH_PAINT))
			++proj_xr_bound;

		proj_xl = proj_xl_bound;
		proj_xr = proj_xr_bound;

		proj_xl_bound *= pic->mode->xfact;
		if (proj_xl_bound)
			proj_xl_bound -= pic->mode->xfact - 1;

		if (proj_xr_bound < 319)
			++proj_xr_bound;
		proj_xr_bound *= pic->mode->xfact;
		proj_xr_bound += pic->mode->xfact - 1;
#endif
		xl = x;
		while (xl > proj_xl_bound && (!IS_BOUNDARY(xl - 1, y, bounds[ytotal + xl -1])))
			--xl;

		while (x < proj_xr_bound && (!IS_BOUNDARY(x + 1, y, bounds[ytotal + x + 1])))
			++x;
		xr = x;

		if (drawenable & GFX_MASK_VISUAL)
			memset(pic->visual_map->index_data + ytotal + xl, color, xr - xl + 1);

		if (drawenable & GFX_MASK_PRIORITY)
			memset(pic->priority_map->index_data + ytotal + xl, priority, xr - xl + 1);

		FILL_FUNCTION_RECURSIVE(pic, xl, xr, y, -1, bounds, legalcolor, legalmask, color, priority, drawenable,
		                        sci_titlebar_size);
		FILL_FUNCTION_RECURSIVE(pic, xl, xr, y, + 1, bounds, legalcolor, legalmask, color, priority, drawenable,
		                        sci_titlebar_size);
	}

	// Now finish the aux buffer
	bitmask = drawenable & (((color != 0xff) ? 1 : 0) | ((priority) ? 2 : 0) | ((control) ? 4 : 0));

#ifdef DRAW_SCALED
#  ifdef FILL_RECURSIVE_DEBUG
	if (fillmagc)
#  endif
		_gfxr_auxbuf_propagate_changes(pic, bitmask);
#endif
}

#undef SCALED_CHECK
#undef IS_BOUNDARY

#ifndef DRAW_SCALED
#  undef proj_xl_bound
#  undef proj_xr_bound
#endif

} // End of namespace Sci