-
Notifications
You must be signed in to change notification settings - Fork 1
/
picture.c
843 lines (694 loc) · 17 KB
/
picture.c
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/* Sarien - A Sierra AGI resource interpreter engine
* Copyright (C) 1999-2001 Stuart George and Claudio Matsuoka
*
* $Id: picture.c,v 1.64 2003/07/05 23:29:38 cmatsuoka Exp $
*
* 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; see docs/COPYING for further details.
*/
#include <stdio.h>
#include <string.h>
#include "sarien.h"
#include "agi.h"
#include "graphics.h"
#include "rand.h"
#include "savegame.h"
#define next_byte Pdata[foffs++]
static UINT8 *Pdata;
static UINT32 flen;
static UINT32 foffs;
static UINT8 pat_code;
static UINT8 pat_num;
static UINT8 pri_on;
static UINT8 scr_on;
static UINT8 scr_colour;
static UINT8 pri_colour;
static UINT8 circles[][15] = { /* agi circle bitmaps */
{ 0x80 },
{ 0xfc },
{ 0x5f, 0xf4 },
{ 0x66, 0xff, 0xf6, 0x60 },
{ 0x23, 0xbf, 0xff, 0xff, 0xee, 0x20 },
{ 0x31, 0xe7, 0x9e, 0xff, 0xff, 0xde, 0x79, 0xe3, 0x00 },
{ 0x38, 0xf9, 0xf3, 0xef, 0xff, 0xff,
0xff, 0xfe, 0xf9, 0xf3, 0xe3, 0x80 },
{ 0x18, 0x3c, 0x7e, 0x7e, 0x7e, 0xff, 0xff,
0xff, 0xff, 0xff, 0x7e, 0x7e, 0x7e, 0x3c, 0x18 }
};
static UINT8 splatter_map[32] = { /* splatter brush bitmaps */
0x20, 0x94, 0x02, 0x24, 0x90, 0x82, 0xa4, 0xa2,
0x82, 0x09, 0x0a, 0x22, 0x12, 0x10, 0x42, 0x14,
0x91, 0x4a, 0x91, 0x11, 0x08, 0x12, 0x25, 0x10,
0x22, 0xa8, 0x14, 0x24, 0x00, 0x50, 0x24, 0x04
};
static UINT8 splatter_start[128] = { /* starting bit position */
0x00, 0x18, 0x30, 0xc4, 0xdc, 0x65, 0xeb, 0x48,
0x60, 0xbd, 0x89, 0x05, 0x0a, 0xf4, 0x7d, 0x7d,
0x85, 0xb0, 0x8e, 0x95, 0x1f, 0x22, 0x0d, 0xdf,
0x2a, 0x78, 0xd5, 0x73, 0x1c, 0xb4, 0x40, 0xa1,
0xb9, 0x3c, 0xca, 0x58, 0x92, 0x34, 0xcc, 0xce,
0xd7, 0x42, 0x90, 0x0f, 0x8b, 0x7f, 0x32, 0xed,
0x5c, 0x9d, 0xc8, 0x99, 0xad, 0x4e, 0x56, 0xa6,
0xf7, 0x68, 0xb7, 0x25, 0x82, 0x37, 0x3a, 0x51,
0x69, 0x26, 0x38, 0x52, 0x9e, 0x9a, 0x4f, 0xa7,
0x43, 0x10, 0x80, 0xee, 0x3d, 0x59, 0x35, 0xcf,
0x79, 0x74, 0xb5, 0xa2, 0xb1, 0x96, 0x23, 0xe0,
0xbe, 0x05, 0xf5, 0x6e, 0x19, 0xc5, 0x66, 0x49,
0xf0, 0xd1, 0x54, 0xa9, 0x70, 0x4b, 0xa4, 0xe2,
0xe6, 0xe5, 0xab, 0xe4, 0xd2, 0xaa, 0x4c, 0xe3,
0x06, 0x6f, 0xc6, 0x4a, 0xa4, 0x75, 0x97, 0xe1
};
static void fix_pixel_bothsides (int x, int y);
static void put_virt_pixel (int x, int y, int res)
{
UINT8 *p;
int width = _WIDTH * res;
if (x < 0 || y < 0 || x >= width || y >= _HEIGHT)
return;
p =
#ifdef USE_HIRES
res > 1 ? &game.hires[y * width + x] :
#endif
&game.sbuf[y * width + x];
if (pri_on) *p = (pri_colour << 4) | (*p & 0x0f);
if (scr_on) *p = scr_colour | (*p & 0xf0);
}
/* For the flood fill routines */
/* MH2 needs stack size > 300 */
#define STACK_SIZE 512
static unsigned int stack_ptr;
static UINT16 stack[STACK_SIZE];
static INLINE void _PUSH (UINT16 c)
{
assert (stack_ptr < STACK_SIZE);
stack[stack_ptr] = c;
stack_ptr++;
}
static INLINE UINT16 _POP ()
{
if (stack_ptr == 0)
return 0xffff;
stack_ptr--;
return stack[stack_ptr];
}
/**
* Draw an AGI line.
* A line drawing routine sent by Joshua Neal, modified by Stuart George
* (fixed >>2 to >>1 and some other bugs like x1 instead of y1, etc.)
* @param x1 x coordinate of start point
* @param y1 y coordinate of start point
* @param x2 x coordinate of end point
* @param y2 y coordinate of end point
* @param res horizontal resolution multiplier
*/
static void draw_line (int x1, int y1, int x2, int y2, int res)
{
int i, x, y, deltaX, deltaY, stepX, stepY, errorX, errorY, detdelta;
int width = _WIDTH * res;
/* CM: Do clipping */
#define clip(x, y) if((x)>=(y)) (x)=(y)
clip (x1, width - 1);
clip (x2, width - 1);
clip (y1, _HEIGHT-1);
clip (y2, _HEIGHT-1);
/* Vertical line */
if (x1 == x2) {
if(y1 > y2) {
y = y1;
y1 = y2;
y2 = y;
}
for ( ; y1 <= y2; y1++) {
put_virt_pixel (x1, y1, res);
#ifdef USE_HIRES
if (res > 1)
fix_pixel_bothsides (x1, y1);
#endif
}
return;
}
/* Horizontal line */
if (y1 == y2) {
if (x1 > x2) {
x = x1;
x1 = x2;
x2 = x;
}
#ifdef USE_HIRES
if (res > 1)
fix_pixel_bothsides (x1, y1);
#endif
for( ; x1 <= x2; x1++)
put_virt_pixel (x1, y1, res);
#ifdef USE_HIRES
if (res > 1) {
put_virt_pixel (x1, y1, res);
fix_pixel_bothsides (x1, y1);
}
#endif
return;
}
y = y1;
x = x1;
stepY = 1; deltaY = y2-y1;
if (deltaY < 0) {
stepY = -1;
deltaY = -deltaY;
}
stepX = 1; deltaX = x2 - x1;
if (deltaX < 0) {
stepX = -1;
deltaX = -deltaX;
}
if (deltaY > deltaX) {
i = deltaY;
detdelta = deltaY;
errorX = deltaY / 2;
errorY = 0;
} else {
i = deltaX;
detdelta = deltaX;
errorX = 0;
errorY = deltaX / 2;
}
put_virt_pixel (x, y, res);
#ifdef USE_HIRES
if (res > 1)
fix_pixel_bothsides (x, y);
#endif
do {
errorY += deltaY;
if (errorY >= detdelta) {
errorY -= detdelta;
y += stepY;
}
errorX += deltaX;
if (errorX >= detdelta) {
errorX -= detdelta;
x += stepX;
}
put_virt_pixel (x, y, res);
#ifdef USE_HIRES
if (res > 1)
fix_pixel_bothsides (x, y);
#endif
i--;
} while (i > 0);
#ifdef USE_HIRES
if (res > 1) {
put_virt_pixel (x, y, res);
fix_pixel_bothsides (x, y);
}
#endif
}
/**
* Draw a relative AGI line.
* Draws short lines relative to last position. (drawing action 0xF7)
* @param res horizontal resolution multiplier
*/
static void dynamic_draw_line (int res)
{
int x1, y1, disp, dx, dy;
x1 = next_byte * res;
y1 = next_byte;
put_virt_pixel (x1, y1, res);
while (42) {
if ((disp = next_byte) >= 0xf0)
break;
dx= ((disp & 0xf0) >> 4) & 0x0f;
dy= (disp & 0x0f);
if (dx & 0x08) dx = -(dx & 0x07);
if (dy & 0x08) dy = -(dy & 0x07);
dx *= res;
draw_line (x1, y1, x1 + dx, y1 + dy, res);
x1 += dx;
y1 += dy;
}
foffs--;
}
/**************************************************************************
** absoluteLine
**
** Draws long lines to actual locations (cf. relative) (drawing action 0xF6)
**************************************************************************/
static void absolute_draw_line (int res)
{
int x1, y1, x2, y2;
x1 = next_byte * res;
y1 = next_byte;
put_virt_pixel (x1, y1, res);
while (42) {
if ((x2 = next_byte) >= 0xf0)
break;
if ((y2 = next_byte) >= 0xf0)
break;
x2 *= res;
draw_line (x1, y1, x2, y2, res);
x1 = x2;
y1 = y2;
}
foffs--;
}
/**************************************************************************
** okToFill
**************************************************************************/
static INLINE int is_ok_fill_here (int x, int y)
{
UINT8 p;
if (x < 0 || x >= _WIDTH || y < 0 || y >= _HEIGHT)
return FALSE;
if (!scr_on && !pri_on)
return FALSE;
p = game.sbuf[y * _WIDTH + x];
if (!pri_on && scr_on && scr_colour != 15)
return (p & 0x0f) == 15;
if (pri_on && !scr_on && pri_colour != 4)
return (p >> 4) == 4;
return (scr_on && (p & 0x0f) == 15 && scr_colour != 15);
}
/**************************************************************************
** agi_fill
**************************************************************************/
static void fill_scanline (int x, int y)
{
unsigned int c;
int newspan_up, newspan_down;
if (!is_ok_fill_here (x, y))
return;
/* Scan for left border */
for (c = x - 1; is_ok_fill_here (c, y); c--);
newspan_up = newspan_down = 1;
for (c++; is_ok_fill_here (c, y); c++) {
put_virt_pixel (c, y, 1);
if (is_ok_fill_here (c, y - 1)) {
if (newspan_up) {
_PUSH (c + 320 * (y - 1));
newspan_up = 0;
}
} else {
newspan_up = 1;
}
if (is_ok_fill_here (c, y + 1)) {
if (newspan_down) {
_PUSH (c + 320 * (y + 1));
newspan_down = 0;
}
} else {
newspan_down = 1;
}
}
}
static void agi_fill (unsigned int x, unsigned int y)
{
_PUSH (x + 320 * y);
while (42) {
UINT16 c = _POP();
/* Exit if stack is empty */
if (c == 0xffff)
break;
x = c % 320;
y = c / 320;
fill_scanline (x, y);
}
stack_ptr = 0;
}
/**************************************************************************
** xCorner
**
** Draws an xCorner (drawing action 0xF5)
**************************************************************************/
static void x_corner (int res)
{
int x1, x2, y1, y2;
x1 = next_byte * res;
y1 = next_byte;
put_virt_pixel (x1, y1, res);
while (42) {
x2 = next_byte;
if (x2 >= 0xf0)
break;
x2 *= res;
draw_line (x1, y1, x2, y1, res);
x1 = x2;
y2 = next_byte;
if (y2 >= 0xf0)
break;
draw_line (x1, y1, x1, y2, res);
y1 = y2;
}
foffs--;
}
/**************************************************************************
** yCorner
**
** Draws an yCorner (drawing action 0xF4)
**************************************************************************/
static void y_corner (int res)
{
int x1, x2, y1, y2;
x1 = next_byte * res;
y1 = next_byte;
put_virt_pixel (x1, y1, res);
while (42) {
y2 = next_byte;
if (y2 >= 0xF0)
break;
draw_line (x1, y1, x1, y2, res);
y1 = y2;
x2 = next_byte;
if (x2 >= 0xf0)
break;
x2 *= res;
draw_line (x1, y1, x2, y1, res);
x1 = x2;
}
foffs--;
}
/**************************************************************************
** fill
**
** AGI flood fill. (drawing action 0xF8)
**************************************************************************/
static void fill ()
{
int x1, y1;
while ((x1 = next_byte) < 0xF0 && (y1 = next_byte) < 0xf0)
agi_fill (x1, y1);
foffs--;
}
/**************************************************************************
** plotPattern
**
** Draws pixels, circles, squares, or splatter brush patterns depending
** on the pattern code.
**************************************************************************/
static int plot_pattern_point (int x, int y, int bitpos, int res)
{
if (pat_code & 0x20) {
if ((splatter_map[bitpos >> 3] >> (7 - (bitpos & 7))) & 1) {
#ifdef USE_HIRES
if (res > 1) {
/* extra randomness in hi-res brush fill
*/
if (rnd(4)) put_virt_pixel(x * 2, y, 2);
if (!rnd(4)) put_virt_pixel(x * 2 + 1, y, 2);
} else
#endif
{
put_virt_pixel (x, y, 1);
}
}
bitpos++;
if (bitpos == 0xff)
bitpos=0;
} else {
#ifdef USE_HIRES
if (res > 1) {
/* double width pixels make MUMG and others
* look nicer
*/
put_virt_pixel (x * 2, y, 2);
put_virt_pixel (x * 2 + 1, y, 2);
} else
#endif
{
put_virt_pixel (x, y, 1);
}
}
return bitpos;
}
static void plot_pattern (int x, int y, int res)
{
SINT32 circlePos = 0;
UINT32 x1, y1, pensize, bitpos = splatter_start[pat_num];
pensize = (pat_code & 7);
if (x < pensize)
x = pensize-1;
if (y < pensize)
y = pensize;
for (y1 = y - pensize; y1 <= y + pensize; y1++) {
for (x1 = x - (pensize+1)/2; x1 <= x + pensize / 2; x1++) {
if (pat_code & 0x10) { /* Square */
bitpos = plot_pattern_point
(x1, y1, bitpos, res);
} else { /* Circle */
if ((circles[pat_code&7][circlePos>>3] >>
(7-(circlePos&7)))&1)
{
bitpos = plot_pattern_point
(x1, y1, bitpos, res);
}
circlePos++;
}
}
}
}
/**************************************************************************
** plotBrush
**
** Plots points and various brush patterns.
**************************************************************************/
static void plot_brush (int res)
{
int x1, y1;
while (42) {
if (pat_code & 0x20) {
if ((pat_num = next_byte) >= 0xF0)
break;
pat_num = (pat_num >> 1) & 0x7f;
}
if ((x1 = next_byte) >= 0xf0)
break;
if ((y1 = next_byte) >= 0xf0)
break;
plot_pattern (x1, y1, res);
}
foffs--;
}
#ifdef USE_HIRES
#include "hirespic.c"
#endif
static void draw_picture ()
{
UINT8 act;
int drawing;
#ifdef USE_HIRES
int save_foffs;
#endif
pat_code = 0;
pat_num = 0;
pri_on = scr_on = FALSE;
scr_colour = 0xf;
pri_colour = 0x4;
drawing = 1;
_D (_D_WARN "Drawing picture");
for (drawing = 1; drawing && foffs < flen; ) {
#ifdef USE_HIRES
save_foffs = foffs;
#endif
act = next_byte;
switch(act) {
case 0xf0: /* set colour on screen */
scr_colour = next_byte;
scr_colour &= 0xF; /* for v3 drawing diff */
scr_on = TRUE;
break;
case 0xf1: /* disable screen drawing */
scr_on = FALSE;
break;
case 0xf2: /* set colour on priority */
pri_colour = next_byte;
pri_colour &= 0xf; /* for v3 drawing diff */
pri_on = TRUE;
break;
case 0xf3: /* disable priority screen */
pri_on = FALSE;
break;
case 0xf4: /* y-corner */
y_corner (1);
break;
case 0xf5: /* x-corner */
x_corner (1);
break;
case 0xf6: /* absolute draw lines */
absolute_draw_line (1);
break;
case 0xf7: /* dynamic draw lines */
dynamic_draw_line (1);
break;
case 0xf8: /* fill */
fill ();
break;
case 0xf9: /* set pattern */
pat_code = next_byte;
break;
case 0xfA: /* plot brush */
plot_brush (1);
break;
case 0xFF: /* end of pic data */
default:
drawing = 0;
break;
}
#ifdef USE_HIRES
foffs = save_foffs;
act = next_byte;
switch(act) {
case 0xf0: /* set colour on screen */
scr_colour = next_byte;
scr_colour &= 0xF; /* for v3 drawing diff */
scr_on = TRUE;
break;
case 0xf1: /* disable screen drawing */
scr_on = FALSE;
break;
case 0xf2: /* set colour on priority */
pri_colour = next_byte;
pri_colour &= 0xf; /* for v3 drawing diff */
pri_on = TRUE;
break;
case 0xf3: /* disable priority screen */
pri_on = FALSE;
break;
case 0xf4: /* y-corner */
y_corner (2);
break;
case 0xf5: /* x-corner */
x_corner (2);
break;
case 0xf6: /* absolute draw lines */
absolute_draw_line (2);
break;
case 0xf7: /* dynamic draw lines */
dynamic_draw_line (2);
break;
case 0xf8: /* fill */
hires_fill ();
break;
case 0xf9: /* set pattern */
pat_code = next_byte;
break;
case 0xfA: /* plot brush */
plot_brush (2);
break;
case 0xFF: /* end of pic data */
default:
drawing = 0;
break;
}
#endif
}
}
/*
* Public functions
*/
//#if !defined(PALMOS) && !defined(FAKE_PALMOS)
/**
*
*/
UINT8* convert_v3_pic (UINT8 *data, UINT32 len)
{
UINT8 d, old = 0, x, *in, *xdata, *out, mode = 0;
UINT32 i, ulen;
xdata = malloc (len + len / 2);
out = xdata;
in = Pdata;
for (i = ulen = 0; i < len; i++, ulen++) {
d = *in++;
*out++ = x = mode ? ((d & 0xF0) >> 4) + ((old & 0x0F) << 4) : d;
if (x == 0xFF) {
ulen++;
break;
}
if (x == 0xf0 || x == 0xf2) {
if (mode) {
*out++ = d & 0x0F;
ulen++;
} else {
d = *in++;
*out++ = (d & 0xF0) >> 4;
i++, ulen++;
}
mode = !mode;
}
old = d;
}
free (Pdata);
xdata = realloc (xdata, ulen);
return xdata;
}
//#endif /* !PALMOS && !FAKE_PALMOS */
/**
* Decode an AGI picture resource.
* This function decodes an AGI picture resource into the correct slot
* and draws it on the AGI screen, optionally cleaning the screen before
* drawing.
* @param n AGI picture resource number
* @param clear clear AGI screen before drawing
*/
int decode_picture (int n, int clear)
{
_D (_D_WARN "(%d)", n);
pat_code = 0;
pat_num = 0;
pri_on = scr_on = FALSE;
scr_colour = 0xF;
pri_colour = 0x4;
Pdata = game.pictures[n].rdata;
flen = game.dir_pic[n].len;
foffs = 0;
if (clear) {
memset (game.sbuf, 0x4f, _WIDTH * _HEIGHT);
#ifdef USE_HIRES
memset (game.hires, 0x4f, _WIDTH * 2 * _HEIGHT);
#endif
}
draw_picture ();
#ifdef USE_HIRES
fix_hires_picture ();
#endif
if (clear)
clear_image_stack();
record_image_stack_call (ADD_PIC, n, clear, 0, 0, 0, 0, 0);
return err_OK;
}
/**
* Unload an AGI picture resource.
* This function unloads an AGI picture resource and deallocates
* resource data.
* @param n AGI picture resource number
*/
int unload_picture (int n)
{
/* remove visual buffer & priority buffer if they exist */
if (game.dir_pic[n].flags & RES_LOADED) {
free (game.pictures[n].rdata);
game.dir_pic[n].flags &= ~RES_LOADED;
}
return err_OK;
}
/**
* Show AGI picture.
* This function copies a ``hidden'' AGI picture to the output device.
*/
void show_pic ()
{
int i, y;
int offset;
_D (_D_WARN "Show picture!");
#ifdef USE_HIRES
if (opt.hires) {
show_hires_pic ();
return;
}
#endif
i = 0;
offset = game.line_min_print * CHAR_LINES;
for (y = 0; y < _HEIGHT; y++) {
put_pixels_a (0, y + offset, _WIDTH, &game.sbuf[i]);
i += _WIDTH;
}
flush_screen ();
}
/* end: picture.c */