forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grabcmds.c
899 lines (741 loc) · 14.9 KB
/
grabcmds.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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// gengrab.c
#include "lumpy.h"
typedef struct
{
byte width,height;
byte data;
} pic_t;
typedef struct
{
short width,height;
short orgx,orgy;
byte data;
} lpic_t;
typedef struct
{
short height;
char width[256];
short charofs[256];
byte data; // as much as required
} font_t;
typedef struct
{
byte topdelta; // pixels from top,a -1 is the last post in the collumn
byte length;
// length data bytes follows
} post_t;
typedef struct
{
short width; // bounding box size
short height;
short leftoffset; // pixels to the left of origin
short topoffset; // pixels above the origin
int collumnofs[256]; // only [width] used, the [0] is &collumnofs[width]
} patch_t;
#define SCRN(x,y) (*(byteimage+(y)*byteimagewidth+x))
/*
==============
=
= GrabRaw
=
= filename RAW x y width height
=
==============
*/
void GrabRaw (void)
{
int x,y,xl,yl,xh,yh,w,h;
byte *screen_p;
int linedelta;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
w = atoi (token);
GetToken (false);
h = atoi (token);
xh = xl+w;
yh = yl+h;
screen_p = byteimage + yl*byteimagewidth + xl;
linedelta = byteimagewidth - w;
for (y=yl ; y<yh ; y++)
{
for (x=xl ; x<xh ; x++)
{
*lump_p++ = *screen_p;
*screen_p++ = 0;
}
screen_p += linedelta;
}
}
/*
==============
=
= GrabVRaw
=
= filename VRAW x y width height
=
==============
*/
void GrabVRaw (void)
{
int x,y,xl,yl,xh,yh,w,h;
byte *screen_p;
int linedelta;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
w = atoi (token);
GetToken (false);
h = atoi (token);
xh = xl+w;
yh = yl+h;
screen_p = byteimage + yl*byteimagewidth + xl;
linedelta = byteimagewidth - w;
for (x=xl ; x<xh ; x++)
{
for (y=yl ; y<yh ; y++)
{
*lump_p++ = SCRN(x,y);
SCRN(x,y) = 0;
}
}
}
/*
==============
=
= GrabPic
=
= filename PIC x y width height [orgx orgy [hitxl hityl hitxh hityh]]
=
==============
*/
void GrabPic (void)
{
int x,y,xl,yl,w,h,p;
int clipplane;
pic_t *header;
byte *screen_p;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
w = atoi (token);
GetToken (false);
h = atoi (token);
clipplane = w&3; // pad with 0 at end of x after this plane
if (!clipplane)
clipplane = 4;
w = (w+3)/4;
header = (pic_t *)lump_p;
header->width = w;
header->height = h;
lump_p =&header->data;
#if 0
//
// optional origin offset
//
if (TokenAvailable())
{
GetToken (false);
header->orgx = atoi (token);
GetToken (false);
header->orgy = atoi (token);
lump_p = (byte *)&header->hitxl;
//
// optional hit rectangle
//
if (TokenAvailable())
{
GetToken (false);
header->hitxl = atoi (token);
GetToken (false);
header->hitxh = atoi (token);
GetToken (false);
header->hityl = atoi (token);
GetToken (false);
header->hityh = atoi (token);
lump_p = ((byte *)&header->hityh)+1;
}
}
#endif
//
// grab in munged format
//
for (p=0 ; p<4 ; p++)
{
for (y=0 ; y<h ; y++)
{
screen_p = byteimage + (y+yl)*byteimagewidth + xl + p;
for (x=0 ; x<w ; x++)
{
if (x==w-1 && p>=clipplane)
*lump_p++ = 0; // pad with transparent
else
{
*lump_p++ = *screen_p;
*screen_p = 0;
}
screen_p += 4;
}
}
}
}
/*
==============
=
= GrabLinearPic
=
= filename LPIC x y width height [orgx orgy]
=
==============
*/
void GrabLinearPic (void)
{
int x,y,xl,yl,w,h;
lpic_t *header;
byte *screen_p;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
w = atoi (token);
GetToken (false);
h = atoi (token);
header = (lpic_t *)lump_p;
header->width = LittleShort(w);
header->height = LittleShort(h);
//
// optional origin offset
//
if (TokenAvailable())
{
GetToken (false);
header->orgx = LittleShort (atoi (token));
GetToken (false);
header->orgy = LittleShort (atoi (token));
}
else
header->orgx = header->orgy = 0;
lump_p = (byte *)&header->data;
//
// grab it
//
for (y=0 ; y<h ; y++)
{
screen_p = byteimage + (y+yl)*byteimagewidth + xl;
for (x=0 ; x<w ; x++)
{
*lump_p++ = *screen_p;
*screen_p++ = 0xff;
}
}
}
/*
==============
=
= GrabPalette
=
= filename PALETTE [startcolor endcolor]
=
==============
*/
void GrabPalette (void)
{
int start,end,length;
if (TokenAvailable())
{
GetToken (false);
start = atoi (token);
GetToken (false);
end = atoi (token);
}
else
{
start = 0;
end = 255;
}
length = 3*(end-start+1);
memcpy (lump_p, lbmpalette+start*3, length);
lump_p += length;
}
/*
=============================================================================
FONT GRABBING
=============================================================================
*/
font_t *font;
int sx,sy,ch;
/*
===================
=
= GrabChar
=
= Grabs the next character after sx,sy of height font->height, and
= advances sx,sy
=
===================
*/
void GrabChar (void)
{
int y;
byte *screen_p,b;
int count;
//
// look for a vertical line with a source pixel
//
do
{
screen_p = byteimage + sy*byteimagewidth+sx;
for (y=0 ; y<font->height ; y++)
if (screen_p[y*byteimagewidth])
goto startgrabing;
if (++sx == byteimagewidth)
{
sx=0;
sy += font->height+1;
if (sy+font->height > byteimagewidth)
Error ("Ran out of characters at char %i\n",ch);
}
} while (1);
startgrabing:
//
// grab the character
//
font->width[ch] = 0;
font->charofs[ch] = LittleShort (lump_p - (byte *)font );
do
{
font->width[ch]++;
screen_p = byteimage + sy*byteimagewidth+sx;
count = 0;
for (y=0 ; y<font->height ; y++)
{
b = *screen_p;
if (b)
count++;
if (b==254) // 254 is a grabbable 0
b = 0;
*lump_p++ = b;
screen_p += byteimagewidth;
}
if (count) // color the grabbed collumn
for (y=0 ; y<font->height ; y++)
{
screen_p -= byteimagewidth;
*screen_p = 1;
}
if (++sx == byteimagewidth)
{
sx=0;
sy += font->height+1;
return;
}
if (!count) // hit a blank row?
{
lump_p -= font->height;
font->width[ch]--;
return;
}
} while (1);
}
/*
===================
=
= GrabFont
=
= filename FONT startchar endchar [startchar endchar [...]]
=
===================
*/
void GrabFont (void)
{
int y;
byte *screen_p;
int top,bottom;
int startchar,endchar;
font = (font_t *)lump_p;
memset (font,0,sizeof(*font));
//
// find the height of the font by scanning for quide lines (color 255)
//
screen_p = byteimage;
top = -1;
for (y=0;y<10;y++)
if (screen_p[y*byteimagewidth] == 255)
{
top = y;
break;
}
if (top == -1)
Error ("No color 255 top guideline found!\n");
bottom = -1;
for ( y++ ; y<100 ; y++)
if (screen_p[y*byteimagewidth] == 255)
{
bottom = y;
break;
}
if (bottom == -1)
Error ("No color 255 bottom guideline found!\n");
font->height = bottom-top-1; // LittleShort after grabbing everything
lump_p = &font->data;
sx = 0;
sy = top+1;
//
// grab ranges of characters
//
do
{
GetToken (false);
startchar = atoi (token);
GetToken (false);
endchar = atoi (token);
for (ch=startchar ; ch<=endchar ; ch++)
GrabChar ();
} while (TokenAvailable ());
font->height = LittleShort(font->height);
}
/*
==============
=
= GrabPatch
=
= filename patch x y width height [originx [originy]]
=
==============
*/
void GrabPatch255 (void)
{
int x,y,xl,yl,xh,yh;
int orgx, orgy;
int width;
byte pixel, oldpixel, transcolor, *length_p;
patch_t *header;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
xh = xl-1+atoi (token);
GetToken (false);
yh = yl-1+atoi (token);
if (xh<xl || yh<yl || xl < 0 || yl<0 || xh>319 || yh>199)
Error ("Bad size: %i, %i, %i, %i",xl,yl,xh,yh);
if (TokenAvailable ())
{
GetToken (false);
orgx = xl+atoi (token);
if (TokenAvailable ())
{
GetToken (false);
orgy = yl+atoi (token);
}
else
orgy = yh;
}
else
{
orgx = (xh+xl)/2;
orgy = yh-4;
}
//
// find exact bounding box
//
transcolor = 255;
while (1)
{
for (y=yl ; y<= yh ; y++)
if (SCRN(xl,y) != transcolor)
goto gotxl;
for (y=yl ; y<= yh ; y++)
SCRN(xl,y) = 0xff;
xl++;
if (xl > xh)
Error ("GrabPatch: Nothing in block");
}
gotxl:
while (1)
{
for (y=yl ; y<= yh ; y++)
if (SCRN(xh,y) != transcolor)
goto gotxh;
for (y=yl ; y<= yh ; y++)
SCRN(xh,y) = 0xff;
xh--;
}
gotxh:
while (1)
{
for (x=xl ; x<= xh ; x++)
if (SCRN(x,yl) != transcolor)
goto gotyl;
for (x=xl ; x<= xh ; x++)
SCRN(x,yl) = 0xff;
yl++;
}
gotyl:
while (1)
{
for (x=xl ; x<= xh ; x++)
if (SCRN(x,yh) != transcolor)
goto gotyh;
for (x=xl ; x<= xh ; x++)
SCRN(x,yh) = 0xff;
yh--;
}
gotyh:
//
// fill in header
//
header = (patch_t *)lump_p;
header->leftoffset = orgx- xl;
header->topoffset = orgy-yl;
width = xh-xl+1;
header->width = width;
header->height = yh-yl+1;
//
// start grabbing posts
//
lump_p = (byte *)&header->collumnofs[header->width];
for (x=xl ; x<= xh ; x++)
{
header->collumnofs[x-xl] = LittleLong(lump_p - (byte *)header);
y = yl;
while (1)
{
// grab a segment
// an extra pixel is grabbed at the start and end of the column
// to keep a round off error from grabbing a strange pixel
// don't grab more than 128 high, because the texture mapper wraps
// fake it with two contiguous columns
while (SCRN(x,y) == transcolor && y<= yh)
y++;
if (y > yh)
{
// end of row
*lump_p++ = 0xff;
break;
}
*lump_p++ = y-yl; // delta from top of shape
length_p = lump_p++;
*length_p=0;
*lump_p++ = SCRN(x,y);
// write an extra copy of first pixel
while ( (pixel = SCRN(x,y)) != transcolor && y<= yh
&& *length_p != 128)
{
*lump_p++ = pixel;
SCRN(x,y) = 0;
(*length_p)++; // inc pixel count
y++;
oldpixel = pixel;
}
*lump_p++ = oldpixel;
// write an extra copy of last pixel
}
}
}
//=============================================================================
// this was used for character sprites in wolfjag, but it wasn't thought
// out well enough for general use
/*
==============
=
= GrabJagBlock
=
= name x y width height
=
==============
*/
#define MASKCOLOR 0
int grabsolid;
void GrabJagBlock (void);
void GrabJagSolid (void)
{
grabsolid = 1;
GrabJagBlock ();
grabsolid = 0;
}
void GrabJagBlock (void)
{
int x,y,xl,yl,xh,yh,pix;
int width, blockxh, blockyl, blockxl;
GetToken (false);
xl = blockxl = atoi (token);
GetToken (false);
yl = blockyl = atoi (token);
GetToken (false);
xh = blockxh = xl-1+atoi (token);
GetToken (false);
yh = yl-1+atoi (token);
//
// find first and last collumn
//
if (!grabsolid)
{
for ( ; xl<=xh ; xl++)
for (y=yl ; y<=yh;y++)
if (SCRN(xl,y) != MASKCOLOR)
goto gotfirst;
gotfirst:
for ( ; xh>=xl ; xh--)
for (y=yl ; y<=yh;y++)
if (SCRN(xh,y) != MASKCOLOR)
goto gotlast;
gotlast:
for ( ; yl<=yh ; yl++)
for (x=xl ; x<=xh;x++)
if (SCRN(x,yl) != MASKCOLOR)
goto gottop;
gottop:
for ( ; yh>=yl ; yh--)
for (x=xl ; x<=xh;x++)
if (SCRN(x,yh) != MASKCOLOR)
goto gotbottom;
gotbottom: ;
}
width = (xh-xl+8)&~7;
xh = xl + width -1;
*lump_p++ = xl - blockxl;
*lump_p++ = yl - blockyl;
*lump_p++ = width;
*lump_p++ = yh-yl+1;
*lump_p++ = 0;
*lump_p++ = 0;
*lump_p++ = 0;
*lump_p++ = 0;
for (y=yl ; y<=yh ; y++)
{
for (x=xl ; x <=xh ; x++)
{
if (x>blockxh)
pix = MASKCOLOR;
else
pix = SCRN(x,y);
if (pix==MASKCOLOR)
{
pix = 0;
SCRN(x,y) = 0;
}
else
SCRN(x,y) = MASKCOLOR;
*lump_p++ = pix;
}
}
}
//=============================================================================
/*
=================
=
= GrabTile
=
= x/y are tile coordinates (pixels*8)
=================
*/
void GrabTile (int x, int y, int planes)
{
int sx,sy;
byte tile[8][8], b;
byte *dest;
//
// grab it
//
for (sy = 0 ; sy< 8 ; sy++)
for (sx = 0 ; sx< 8 ; sx++)
{
tile[sy][sx] = SCRN(x*8+sx,y*8+sy);
SCRN(x*8+sx,y*8+sy) = 0xff;
}
if (planes == 7)
{ // mode 7 tiles are just bytes
memcpy (lump_p, tile, 64);
lump_p += 64;
return;
}
//
// munge it
//
memset (lump_p,0,64);
for (sy = 0 ; sy< 8 ; sy++)
for (sx = 0 ; sx< 8 ; sx++)
{
b = tile[sy][sx];
if (planes == 1)
{ // for jaguar
lump_p[sy] <<=1;
lump_p[sy] |= b&1;
continue;
}
if (planes == 2)
{ // for jaguar
dest = lump_p + sy*2 + (sx>>2);
*dest <<= 2;
*dest |= b&3;
continue;
}
lump_p[sy*2] <<=1;
lump_p[sy*2] |= b&1;
b>>=1;
lump_p[sy*2+1] <<=1;
lump_p[sy*2+1] |= b&1;
b>>=1;
if (planes == 2)
continue;
lump_p[1*16+sy*2] <<=1;
lump_p[1*16+sy*2] |= b&1;
b>>=1;
lump_p[1*16+sy*2+1] <<=1;
lump_p[1*16+sy*2+1] |= b&1;
b>>=1;
if (planes == 4)
continue;
lump_p[2*16+sy*2] <<=1;
lump_p[2*16+sy*2] |= b&1;
b>>=1;
lump_p[2*16+sy*2+1] <<=1;
lump_p[2*16+sy*2+1] |= b&1;
b>>=1;
lump_p[3*16+sy*2] <<=1;
lump_p[3*16+sy*2] |= b&1;
b>>=1;
lump_p[3*16+sy*2+1] <<=1;
lump_p[3*16+sy*2+1] |= b&1;
b>>=1;
}
lump_p += 8*planes;
}
/*
==============
=
= GrabSnesBlock
=
= planes x y width height
=
= Coordinates are in tiles
= Chunks up into 8*8 tiles, and can grab different bit depths
==============
*/
void GrabSnesBlock (void)
{
int x,y,w,h, planes;
int xs,ys;
GetToken (false);
planes = atoi (token);
GetToken (false);
x = atoi (token);
GetToken (false);
y = atoi (token);
GetToken (false);
w = atoi (token);
GetToken (false);
h = atoi (token);
for (ys = y ; ys<y+h ; ys++)
for (xs = x ; xs<x+w ; xs++)
GrabTile (xs,ys, planes);
}