This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgamegraphics.cpp
3426 lines (3134 loc) · 107 KB
/
gamegraphics.cpp
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fstream>
#include "gamegraphics.h"
#include "utils.h"
#include "main.h"
//#define STB_IMAGE_WRITE_IMPLEMENTATION
//#include "stb-master\stb_image_write.h"
char path_uw0[100];
char path_uw1[100];
char path_uw2[100];
char path_shock[100];
//For use in the cutscene decoder.
typedef __int32 unsigned Uint32;
typedef __int16 unsigned Uint16;
typedef __int8 unsigned Uint8;
typedef __int32 signed Sint32;
typedef __int16 signed Sint16;
typedef __int8 signed Sint8;
typedef struct {
//Borrowed from Underworld Adventures for extracting cutscenes.
Uint32 id; /* 4 character ID == "LPF " */
Uint16 maxLps; /* max # largePages allowed. 256 FOR NOW. */
Uint16 nLps; /* # largePages in this file. */
Uint32 nRecords; /* # records in this file. 65534 is current limit plus */
/* one for last-to-first delta for looping the animation */
Uint16 maxRecsPerLp; /* # records permitted in an lp. 256 FOR NOW. */
Uint16 lpfTableOffset; /* Absolute Seek position of lpfTable. 1280 FOR NOW.
The lpf Table is an array of 256 large page structures
that is used to facilitate finding records in an anim
file without having to seek through all of the Large
Pages to find which one a specific record lives in. */
Uint32 contentType; /* 4 character ID == "ANIM" */
Uint16 width; /* Width of screen in pixels. */
Uint16 height; /* Height of screen in pixels. */
Uint8 variant; /* 0==ANIM. */
Uint8 version; /* 0==frame rate is multiple of 18 cycles/sec.
1==frame rate is multiple of 70 cycles/sec. */
Uint8 hasLastDelta; /* 1==Last record is a delta from last-to-first frame. */
Uint8 lastDeltaValid; /* 0==The last-to-first delta (if present) hasn't been
updated to match the current first&last frames, so it
should be ignored. */
Uint8 pixelType; /* 0==256 color. */
Uint8 CompressionType; /* 1==(RunSkipDump) Only one used FOR NOW. */
Uint8 otherRecsPerFrm; /* 0 FOR NOW. */
Uint8 bitmaptype; /* 1==320x200, 256-color. Only one implemented so far. */
Uint8 recordTypes[32]; /* Not yet implemented. */
Uint32 nFrames; /* In case future version adds other records at end of
file, we still know how many actual frames.
NOTE: DOES include last-to-first delta when present. */
Uint16 framesPerSecond; /* Number of frames to play per second. */
Uint16 pad2[29]; /* 58 bytes of filler to round up to 128 bytes total. */
} lpfileheader;
/* this is the format of an large page structure
Borrowed from Underworld adventures
*/
typedef struct {
Uint16 baseRecord; /* Number of first record in this large page. */
Uint16 nRecords; /* Number of records in lp.
bit 15 of "nRecords" == "has continuation from previous lp".
bit 14 of "nRecords" == "final record continues on next lp". */
Uint16 nBytes; /* Total number of bytes of contents, excluding header. */
} lp_descriptor;
void myPlayRunSkipDump(Uint8 *srcP, Uint8 *dstP);
void extractUW2Bitmaps(char filePathIn[255],char PaletteFile[255],int PaletteNo,char OutFileName[255], int useTGA)
{
unsigned char *textureFile; // Pointer to our buffered data (little endian format)
int i;
long NoOfTextures;
FILE *file = NULL; // File pointer
if ((file = fopen(filePathIn, "rb")) == NULL)
{ fprintf(LOGFILE,"Could not open specified file\n"); return;}
// Get the size of the file in bytes
long fileSize = getFileSize(file);
textureFile = new unsigned char[fileSize];
fread(textureFile, fileSize, 1,file);
fclose(file);
for (int p = 0; p <= 16; p++)
{
palette *pal;
pal = new palette[256];
//getPalette(PaletteFile, pal, PaletteNo);
getPalette(PaletteFile, pal, p);
NoOfTextures = getValAtAddress(textureFile, 0, 8);
char OutFileNameToUse[255];
sprintf_s(OutFileNameToUse, 255, "%d_%s_",p, OutFileName);
for (i = 0; i <NoOfTextures; i++)
{
long textureOffset = getValAtAddress(textureFile, (i * 4) + 6, 32);
if (textureOffset != 0)
{
int compressionFlag = getValAtAddress(textureFile, ((i * 4) + 6) + (NoOfTextures * 4), 32);
int isCompressed = (compressionFlag >> 1) & 0x01;
if (isCompressed == 1)
{
int datalen;
unsigned char *outputImg = unpackUW2(textureFile, textureOffset, &datalen);
if (useTGA == 1)
{
writeTGA(outputImg, 0, 320, 200, i, pal, OutFileNameToUse, 0);
}
else
{
writeBMP(outputImg, 0, 320, 200, i, pal, OutFileNameToUse);
}
}
else
{
if (useTGA == 1)
{
writeTGA(textureFile, textureOffset, 320, 200, i, pal, OutFileNameToUse, 0);
}
else
{
writeBMP(textureFile, textureOffset, 320, 200, i, pal, OutFileNameToUse);
}
}
}
}
}
}
void extractTextureBitmap(int ImageCount, char filePathIn[255], char PaletteFile[255], int PaletteNo, int BitmapSize, int FileType, char OutFileName[255],char auxPalPath[255],int useTGA)
{
unsigned char *textureFile; // Pointer to our buffered data (little endian format)
int i;
long NoOfTextures;
int MaxHeight=0; int MaxWidth=0;
FILE *file = NULL; // File pointer
if ((file = fopen(filePathIn, "rb")) == NULL)
{ fprintf(LOGFILE,"Could not open specified file\n"); return;}
// Get the size of the file in bytes
long fileSize = getFileSize(file);
palette *pal;
pal = new palette[256];
//Extract all palettes
unsigned char Allpalettefile[256*16];
for (int j = 0; j < 16; j++)
{
for (int i = 0; i < 256; i++)
{//create a blank image with incremental values
Allpalettefile[i+j*256] = i;
}
}
for (int p = 0; p < 1; p++)
{//Get the 8 palettes
getPalette(PaletteFile, pal, p);
//Not to cycle the palettes. To keep them synced I need 28 cycles? (4 water * 7 fire frames?)
for (int paletteFrame = 0; paletteFrame < 1; paletteFrame++)
{
// CyclePalette(16, 22);//fire
//CyclePalette(48, 51);//water
//cyclePalette(pal, 48, 4);
//cyclePalette(pal, 16, 7);//Reverse direction.
char paletteOutname[256];
sprintf(paletteOutname, "Palette_%d_frame", p);
writeTGA(Allpalettefile, 0, 256,16, paletteFrame, pal, paletteOutname, 1);
}
}
//getPalette(PaletteFile, pal, p);
if (GREYSCALE == 1)
{
getPaletteIndex(PaletteFile,pal,PaletteNo);
}
else
{
getPalette(PaletteFile, pal, PaletteNo);
}
// Allocate space in the buffer for the whole file
//BigEndBuf = new unsigned char[fileSize];
textureFile = new unsigned char[fileSize];
// Read the file in to the buffer
fread(textureFile, fileSize, 1,file);
fclose(file);
//long PrevAddress = 0;
switch (FileType)
{
case UW_GRAPHICS_BITMAPS: //BYT
if (useTGA==1)
{
writeTGA(textureFile, 0, 320, 200, 0, pal,OutFileName,ALPHA);
}
else
{
writeBMP(textureFile, 0, 320, 200, 0, pal,OutFileName);
}
break;
case UW_GRAPHICS_TEXTURES : //.tr
fprintf(LOGFILE,"File Type :%d\n", textureFile[0]);
fprintf(LOGFILE,"xy resolution:%d\n", textureFile[1]);
fprintf(LOGFILE,"No of textures:%d\n", textureFile[3] << 8 | textureFile[2]);
if (ImageCount == -1) //All the images.
{
NoOfTextures = textureFile[3] << 8 | textureFile[2];
NoOfTextures=256;//Count is wrong???
}
else
{
NoOfTextures = ImageCount;
}
for (i = 0; i <NoOfTextures; i++)
{
long textureOffset = getValAtAddress(textureFile, (i * 4) + 4, 32);
if (useTGA==1)
{
writeTGA(textureFile, textureOffset, BitmapSize, BitmapSize, i, pal, OutFileName,0);
}
else
{
writeBMP(textureFile, textureOffset, BitmapSize, BitmapSize, i, pal, OutFileName);
}
}
break;
case UW_GRAPHICS_GR : //.gr
case UW_GRAPHICS_TR :
case UW_GRAPHICS_CR :
case UW_GRAPHICS_SR :
case UW_GRAPHICS_AR :
fprintf(LOGFILE,"File Type (should be %d):%d\n", FileType, textureFile[0]);
fprintf(LOGFILE,"No of textures:%d\n", textureFile[2] << 8 | textureFile[1]);
if (ImageCount == -1) //All the images.
{
NoOfTextures = textureFile[2] << 8 | textureFile[1];
}
else
{
NoOfTextures = ImageCount;
}
for (i = 0; i < NoOfTextures; i++)
{
long textureOffset = getValAtAddress(textureFile, (i * 4) + 3, 32);
//textureOffset = 679 + i * 1000;
int BitMapWidth = getValAtAddress(textureFile,textureOffset+1, 8);
int BitMapHeight = getValAtAddress(textureFile, textureOffset+2, 8);
fprintf(LOGFILE, "\tImage %d \t %d\n", i, textureOffset);
if (MaxHeight < BitMapHeight)
{MaxHeight=BitMapHeight;
}
if (MaxWidth < BitMapWidth)
{
MaxWidth = BitMapWidth;
}
int datalen;
palette auxpal[16];
int auxPalIndex;
unsigned char *imgNibbles;
unsigned char *outputImg;
switch (getValAtAddress(textureFile, textureOffset, 8))
{
case 0x4://8 bit uncompressed
//fprintf(LOGFILE,"8 bit uncompressed\n");
//fprintf(LOGFILE,"Width = %d\n",getValAtAddress(textureFile, textureOffset + 1, 8));
//fprintf(LOGFILE,"Height = %d\n", getValAtAddress(textureFile, textureOffset + 2, 8));
textureOffset = textureOffset + 5;
if (useTGA==1)
{
writeTGA(textureFile, textureOffset, BitMapWidth, BitMapHeight, i, pal, OutFileName, ALPHA);
}
else
{
writeBMP(textureFile, textureOffset, BitMapWidth, BitMapHeight, i, pal, OutFileName);
}
break;
case 0x8://4 bit run-length
fprintf(LOGFILE,"4 bit run-length\n");
//fprintf(LOGFILE,"Width = %d\n", getValAtAddress(textureFile, textureOffset + 1, 8));
//fprintf(LOGFILE,"Height = %d\n", getValAtAddress(textureFile, textureOffset + 2, 8));
auxPalIndex = getValAtAddress(textureFile, textureOffset + 3, 8);
//fprintf(LOGFILE,"Aux Pal = %d\n", auxPalIndex);
//fprintf(LOGFILE,"Data length = %d\n", getValAtAddress(textureFile, textureOffset + 4, 16));
datalen = getValAtAddress(textureFile,textureOffset+4,16);
imgNibbles = new unsigned char[BitMapWidth*BitMapHeight*2];
textureOffset = textureOffset + 6; //Start of raw data.
copyNibbles(textureFile, imgNibbles, datalen, textureOffset);
LoadAuxilaryPal(auxPalPath, auxpal, pal,auxPalIndex);
outputImg = new unsigned char[BitMapWidth*BitMapHeight];
// DecodeRLEBitmap(imgNibbles, datalen, BitMapWidth, BitMapHeight, outputImg, auxpal, i, 4,OutFileName);
DecodeRLEBitmap(imgNibbles, datalen, BitMapWidth, BitMapHeight, outputImg,4);
if (useTGA==1)
{
writeTGA(outputImg, 0, BitMapWidth, BitMapHeight, i, auxpal, OutFileName, ALPHA);
}
else
{
writeBMP(outputImg, 0, BitMapWidth, BitMapHeight, i, auxpal, OutFileName);
}
break;
case 0xA://4 bit uncompressed
fprintf(LOGFILE,"4 bit uncompressed\n");
//fprintf(LOGFILE,"Width = %d\n", getValAtAddress(textureFile, textureOffset + 1, 8));
//fprintf(LOGFILE,"Height = %d\n", getValAtAddress(textureFile, textureOffset + 2, 8));
auxPalIndex = getValAtAddress(textureFile, textureOffset + 3, 8);
//fprintf(LOGFILE,"Aux Pal = %d\n", auxPalIndex);
//fprintf(LOGFILE,"Data length = %d\n", getValAtAddress(textureFile, textureOffset + 4, 16));
datalen = getValAtAddress(textureFile, textureOffset + 4, 16);
imgNibbles = new unsigned char[BitMapWidth*BitMapHeight * 2];
textureOffset = textureOffset + 6; //Start of raw data.
copyNibbles(textureFile, imgNibbles, datalen, textureOffset);
LoadAuxilaryPal(auxPalPath, auxpal, pal, auxPalIndex);
//outputImg = new unsigned char[BitMapWidth*BitMapHeight];
if (useTGA==1)
{
writeTGA(imgNibbles, 0, BitMapWidth, BitMapHeight, i, auxpal, OutFileName, ALPHA);
}
else
{
writeBMP(imgNibbles, 0, BitMapWidth, BitMapHeight, i, auxpal, OutFileName);
}
break;
default:
fprintf(LOGFILE,"Unknown file type : %d\n", getValAtAddress(textureFile, textureOffset, 8));
break;
}
}
break;
}
//NoOfTextures=0;
//fprintf(LOGFILE,"Address of first block:%d\n", (textureFile[7]<<16 | textureFile[6]<<32 | textureFile[5]<<8 | textureFile[4]));
printf("Max Width: %d, Max Height: %d", MaxWidth, MaxHeight);
}
void getPalette(char filePathPal[255], palette *pal, int paletteNo)
{
FILE *filePal = NULL;
unsigned char *palf;
int j; int i;
//const char *filePathPal = GRAPHICS_PAL_FILE ; //"C:\\Games\\Ultima\\UW1\\DATA\\pals.dat";
filePal = fopen(filePathPal,"rb");
long fileSizePal = getFileSize(filePal);
palf = new unsigned char[fileSizePal];
fread(palf, fileSizePal, 1, filePal);
fclose (filePal);
i = 0;
int palAddr = paletteNo * 256;
for (j = 0; j < 256; j++) {
pal[i].red = palf[palAddr + 0]<<2;//getValAtAddress(palf, palAddr + 0, 8);//palf[j * 3 + 2] << 2;
pal[i].green = palf[palAddr + 1] << 2;//getValAtAddress(palf, palAddr +1, 8);//palf[j * 3 + 0] << 2;
pal[i].blue = palf[palAddr + 2] << 2;//getValAtAddress(palf, palAddr + 2, 8);//palf[j * 3 + 1] << 2;
pal[i].reserved = 0;
palAddr = palAddr +3;
i++;
}
unsigned char *xfer;
filePal = fopen("c:\\games\\uw2\\data\\xfer.dat", "rb");
fileSizePal = getFileSize(filePal);
xfer = new unsigned char[fileSizePal];
fread(xfer , fileSizePal, 1, filePal);
unsigned char *xferOut = new unsigned char[fileSizePal*16];
fclose(filePal);
writeTGA(xfer, 0, fileSizePal, 1, 0, pal, "xferpal.tga",0);
return;
}
void getPaletteIndex(char filePathPal[255], palette *pal, int paletteNo)
{
//Returns a palette comprising of just index numbers
FILE *filePal = NULL;
unsigned char *palf;
short j; int i;
//const char *filePathPal = GRAPHICS_PAL_FILE ; //"C:\\Games\\Ultima\\UW1\\DATA\\pals.dat";
filePal = fopen(filePathPal, "rb");
long fileSizePal = getFileSize(filePal);
palf = new unsigned char[fileSizePal];
fread(palf, fileSizePal, 1, filePal);
fclose(filePal);
//FILE *PALOUT_FILE = NULL;
//fopen_s(&PALOUT_FILE, "pal.txt", "w");
i = 0;
int palAddr = paletteNo * 256;
for (j = 0; j < 256; j++) {
int red;
int green;
int blue;
pal[i].red = j;//<<2;//getValAtAddress(palf, palAddr + 0, 8);//palf[j * 3 + 2] << 2;
pal[i].green = j;// << 2;//getValAtAddress(palf, palAddr +1, 8);//palf[j * 3 + 0] << 2;
pal[i].blue = j ;//<< 2;//getValAtAddress(palf, palAddr + 2, 8);//palf[j * 3 + 1] << 2;
pal[i].reserved = 0;
red = palf[palAddr + 0] << 2;//getValAtAddress(palf, palAddr + 0, 8);//palf[j * 3 + 2] << 2;
green = palf[palAddr + 1] << 2;//getValAtAddress(palf, palAddr +1, 8);//palf[j * 3 + 0] << 2;
blue = palf[palAddr + 2] << 2;//getValAtAddress(palf, palAddr + 2, 8);//palf[j * 3 + 1] << 2;
//fprintf(PALOUT_FILE,"SetPal(%d,%d,%d,%d);\n",i,red,green,blue);
palAddr = palAddr + 3;
i++;
}
//fclose(PALOUT_FILE);
return;
}
void writeBMP(unsigned char *bits, long Start, long SizeH, long SizeV, int index, palette *pal, char OutFileName[255])
{
if (ACTUALLY_EXTRACT_FILES == 0)
{
return;
}
BitMapHeader bmhead;
BitMapInfoHeader bmihead;
bmhead.bfType = 19778;
bmhead.bfReserved1 = 0;
bmhead.bfReserved2 = 0;
bmhead.bfOffBits = 1078;
bmihead.biSize = 40;
bmihead.biPlanes = 1;
bmihead.biBitCount = 8;
bmihead.biCompression = 0;
bmihead.biXPelsPerMeter = 0;
bmihead.biYPelsPerMeter = 0;
bmihead.biClrUsed = 0;
bmihead.biClrImportant = 0;
bmhead.bfOffBits = 1078; // Set up the .bmp header info
bmihead.biBitCount = 8;
bmihead.biClrUsed = 0;
bmihead.biClrImportant = 0;
//unsigned char *bits = unpackdat + substart + 28;
bmihead.biWidth = SizeH; //bm->width;
bmihead.biHeight =SizeV; // bm->height;
int imwidth = SizeH; //bmihead.biWidth;
imwidth += (4-(imwidth%4));
bmihead.biSizeImage = imwidth * bmihead.biHeight;
bmhead.bfSize = bmihead.biSizeImage + 54;
char outFile[255];
sprintf_s(outFile, 255, "%s_%04d.bmp", OutFileName, index);
FILE *outf ;
outf = fopen(outFile,"wb");
fwrite(&bmhead.bfType,2,1,outf);
fwrite(&bmhead.bfSize,4,1,outf);
fwrite(&bmhead.bfReserved1,2,1,outf);
fwrite(&bmhead.bfReserved2,2,1,outf);
fwrite(&bmhead.bfOffBits,4,1,outf);
fwrite(&bmihead,sizeof(BitMapInfoHeader),1,outf);
fwrite(pal,256*4,1,outf);
char ch = 0;
for (int k = bmihead.biHeight-1; k >= 0; k--) {
fwrite(Start+bits+(k*bmihead.biWidth),1,bmihead.biWidth,outf);
if (bmihead.biWidth%4 != 0)
for (int buf = 4; buf > bmihead.biWidth%4; buf--)
fwrite(&ch,1,1,outf);
}
//fprintf(LOGFILE,".");
fclose(outf);
}
void writeBMPBW(unsigned char *bits, long Start, long SizeH, long SizeV, int index, palette *pal, char OutFileName[255])
{
if (ACTUALLY_EXTRACT_FILES == 0)
{
return;
}
BitMapHeader bmhead;
BitMapInfoHeader bmihead;
bmhead.bfType = 19778;
bmhead.bfReserved1 = 0;
bmhead.bfReserved2 = 0;
bmhead.bfOffBits = 62;
bmihead.biSize = 40;
bmihead.biPlanes = 1;
bmihead.biBitCount = 8;
bmihead.biCompression = 0;
bmihead.biXPelsPerMeter = 0;
bmihead.biYPelsPerMeter = 0;
bmihead.biClrUsed = 0;
bmihead.biClrImportant = 0;
bmhead.bfOffBits = 1078; // Set up the .bmp header info
bmihead.biBitCount = 1;
bmihead.biClrUsed = 0;
bmihead.biClrImportant = 0;
//unsigned char *bits = unpackdat + substart + 28;
bmihead.biWidth = SizeH; //bm->width;
bmihead.biHeight = SizeV; // bm->height;
int imwidth = SizeH; //bmihead.biWidth;
imwidth += (4 - (imwidth % 4));
bmihead.biSizeImage = imwidth * bmihead.biHeight;
bmhead.bfSize = bmihead.biSizeImage + 54;
char outFile[255];
sprintf_s(outFile, 255, "%s_%04d.bmp", OutFileName, index);
FILE *outf;
outf = fopen(outFile, "wb");
fwrite(&bmhead.bfType, 2, 1, outf);
fwrite(&bmhead.bfSize, 4, 1, outf);
fwrite(&bmhead.bfReserved1, 2, 1, outf);
fwrite(&bmhead.bfReserved2, 2, 1, outf);
fwrite(&bmhead.bfOffBits, 4, 1, outf);
fwrite(&bmihead, sizeof(BitMapInfoHeader), 1, outf);
//fwrite(pal, 256 * 4, 1, outf);
//Black&White palette?
short int tmpshrt = 0;
fwrite(&tmpshrt, 2, 1, outf);
fwrite(&tmpshrt, 2, 1, outf);
tmpshrt = -1;
fwrite(&tmpshrt, 2, 1, outf);
tmpshrt = 255;
fwrite(&tmpshrt, 2, 1, outf);
char ch = 0;
for (int k = bmihead.biHeight - 1; k >= 0; k--) {
fwrite(Start + bits + (k*bmihead.biWidth), 1, bmihead.biWidth, outf);
if (bmihead.biWidth % 4 != 0)
for (int buf = 4; buf > bmihead.biWidth % 4; buf--)
fwrite(&ch, 1, 1, outf);
}
//fprintf(LOGFILE,".");
fclose(outf);
}
void LoadAuxilaryPal(char auxpalPath[255], palette auxpal[16], palette gamepal[256], int PalIndex)
{
FILE *filePal = NULL;
unsigned char *palf;
//const char *filePathPal = GRAPHICS_PAL_FILE ; //"C:\\Games\\Ultima\\UW1\\DATA\\pals.dat";
filePal = fopen(auxpalPath, "rb");
long fileSizePal = getFileSize(filePal);
palf = new unsigned char[fileSizePal];
fread(palf, fileSizePal, 1, filePal);
fclose(filePal);
for (int j = 0; j < 16; j++)
{
//auxpal[j]. = getValAtAddress(palf,PalIndex*0xf + j,8);
int value = getValAtAddress(palf, PalIndex * 16 + j, 8);
//auxpal[j].green = ((value) & 0xF) <<1;
//auxpal[j].blue = ((value) >> 4 & 0xF) << 1;
//auxpal[j].red = ((value) >> 8 & 0xF) << 1;
//auxpal[j].reserved = 0;
auxpal[j].green = gamepal[value].green;
auxpal[j].blue = gamepal[value].blue;
auxpal[j].red = gamepal[value].red;
auxpal[j].reserved = gamepal[value].reserved;
//fprintf(LOGFILE,"%d\n", auxpal[j]);
}
return;
}
void DecodeRLEBitmap(unsigned char *imageData, int datalen, int imageWidth, int imageHeight, unsigned char *outputImg , int BitSize)
//, palette *auxpal, int index, int BitSize, char OutFileName[255])
{
int state=0;
int curr_pxl=0;
int count=0;
int repeatcount=0;
char nibble;
int add_ptr=0;
while ((curr_pxl<imageWidth*imageHeight) || (add_ptr<=datalen))
{
switch (state)
{
case repeat_record_start:
{
count = getcount(imageData, &add_ptr,BitSize);
if (count == 1)
{
state = run_record;
}
else if (count == 2)
{
repeatcount = getcount(imageData, &add_ptr, BitSize) - 1;
state = repeat_record_start;
}
else
{
state = repeat_record;
}
break;
}
case repeat_record:
{
//fprintf(LOGFILE,"\nRepeatRecord\n");
//fprintf(LOGFILE,"Count is %d\n",count);
//get nibble for the palette;
nibble = getNibble(imageData, &add_ptr);
//for count times copy the palette data to the image at the output pointer
if (imageWidth*imageHeight - curr_pxl < count)
{
count = imageWidth*imageHeight - curr_pxl;
}
for (int i = 0; i < count; i++)
{
//fprintf(LOGFILE,"%d=%d\n", curr_pxl, nibble);
outputImg[curr_pxl++] = nibble;
}
if (repeatcount == 0 )
{
state = run_record;
}
else
{
state = repeat_record_start;
repeatcount--;
}
break;
}
case 2: //runrecord
{
//fprintf(LOGFILE,"\nRunRecord\n");
count = getcount(imageData, &add_ptr, BitSize);
if (imageWidth*imageHeight - curr_pxl < count)
{
count = imageWidth*imageHeight - curr_pxl;
}
//fprintf(LOGFILE,"Count is %d\n", count);
//for that count copy the data / pal as it is
for (int i = 0; i < count; i++)
{
//get nibble for the palette;
nibble = getNibble(imageData, &add_ptr);
//fprintf(LOGFILE,"%d=%d\n", curr_pxl, nibble);
outputImg[curr_pxl++] = nibble;
}
state = repeat_record_start;
break;
}
}
}
}
int getcount(unsigned char *nibbles, int *addr_ptr , int size)
{
int n1;
int n2;
int n3;
//int inc_ptr;
int count=0;
n1 = getNibble(nibbles,addr_ptr);
count = n1;
if (count==0)
{
n1 = getNibble(nibbles, addr_ptr);
n2 = getNibble(nibbles, addr_ptr);
count = (n1 << size) | n2;
}
if (count == 0)
{
n1 = getNibble(nibbles, addr_ptr);
n2 = getNibble(nibbles, addr_ptr);
n3 = getNibble(nibbles, addr_ptr);
count = (((n1 << size) | n2) << size) | n3;
}
return count;
}
int getNibble(unsigned char *nibbles, int *addr_ptr)
{
int n1 = nibbles[*addr_ptr];
*addr_ptr = *addr_ptr + 1;
return n1;
}
void copyNibbles(unsigned char *InputData, unsigned char *OutputData, int NoOfNibbles, int add_ptr)
{
//Split the data up into in's nibbles.
int i = 0;
NoOfNibbles=NoOfNibbles*2;
while (NoOfNibbles > 1)
{
OutputData[i] = (getValAtAddress(InputData, add_ptr, 8) >> 4) & 0x0F; //High nibble
OutputData[i + 1] = (getValAtAddress(InputData, add_ptr, 8)) & 0xf; //Low nibble
//fprintf(LOGFILE,"%d,%d\n", OutputData[i], OutputData[i+1]);
i=i+2;
add_ptr++;
NoOfNibbles = NoOfNibbles-2;
}
if (NoOfNibbles == 1)
{ //Odd nibble out.
OutputData[i] = (getValAtAddress(InputData, add_ptr, 8) >> 4) & 0x0F;
}
}
void copyNibbles5Bit(unsigned char *InputData, unsigned char *OutputData, int NoOfNibbles, int add_ptr)
{
int bit_ptr=0;
int bits_needed=5;
int bits_avail=7;
int mask[6];
int i=0;
mask[0] = 0x0;
mask[1] = 0x1;
mask[2] = 0x3;
mask[3] = 0x7;
mask[4] = 0xF;
mask[5] = 0x1F;
//mask[6] = 0x3F;
//mask[7] = 0xFF;
while (NoOfNibbles > 0)
{
unsigned char buf;
fprintf(LOGFILE,"\n**%d***\n", i);
//read in my byte and take the needed bits from it
if (bit_ptr<=2)
{
bits_avail=5;
}
else
{
bits_avail = 8 - bit_ptr;
}
if (bits_avail == 0)
{
add_ptr++;
bit_ptr=0;
bits_avail=5;
}
fprintf(LOGFILE,"\nReading in %d bits @ %d",bits_avail,bit_ptr);
buf = (InputData[add_ptr] >> (bit_ptr)) & mask[bits_avail];
bit_ptr=bit_ptr+bits_avail;
bits_needed= bits_needed-bits_avail;
if (bits_needed > 0)
{//I need more bits. Read in the next byte and take whats there.
fprintf(LOGFILE,"\nI need %d bits", bits_needed);
add_ptr++;
bit_ptr = 0;
fprintf(LOGFILE,"\nReading in %d bits @ %d", bits_needed, bit_ptr);
buf = ((InputData[add_ptr] & mask[bits_needed])<<bits_needed) | buf;
bit_ptr=bits_needed;
}
OutputData[i]=buf;
i++;
bits_needed=5;
NoOfNibbles--;
}
}
void extractPanels(int ImageCount, char filePathIn[255], char PaletteFile[255], int PaletteNo, int BitmapSize, int FileType, int game, char OutFileName[255], int useTGA)
{
//const char *filePathIn = GRAPHICS_FILE ; //"C:\\Games\\Ultima\\UW1\\DATA\\W64.tr";
// int indexNo;
//unsigned char *BigEndBuf; // Pointer to our buffered data (big endian format)
unsigned char *textureFile; // Pointer to our buffered data (little endian format)
int i;
long NoOfTextures;
FILE *file = NULL; // File pointer
if ((file = fopen(filePathIn, "rb")) == NULL)
{
fprintf(LOGFILE,"Could not open specified file\n"); return;
}
// Get the size of the file in bytes
long fileSize = getFileSize(file);
palette *pal;
pal = new palette[256];
getPalette(PaletteFile, pal, PaletteNo);
// Allocate space in the buffer for the whole file
//BigEndBuf = new unsigned char[fileSize];
textureFile = new unsigned char[fileSize];
// Read the file in to the buffer
fread(textureFile, fileSize, 1, file);
fclose(file);
switch (FileType)
{
case UW_GRAPHICS_GR: //.gr
case UW_GRAPHICS_TR:
case UW_GRAPHICS_CR:
case UW_GRAPHICS_SR:
case UW_GRAPHICS_AR:
fprintf(LOGFILE,"File Type (should be %d):%d\n", FileType, textureFile[0]);
fprintf(LOGFILE,"No of textures:%d\n", textureFile[2] << 8 | textureFile[1]);
if (ImageCount == -1) //All the images.
{
NoOfTextures = textureFile[2] << 8 | textureFile[1];
}
else
{
NoOfTextures = ImageCount;
}
//NoOfTextures=1;
for (i = 0; i < NoOfTextures; i++)
{
long textureOffset = getValAtAddress(textureFile, (i * 4) + 3, 32);
int BitMapWidth = 83; //getValAtAddress(textureFile, textureOffset + 1, 8);
int BitMapHeight = 114; // getValAtAddress(textureFile, textureOffset + 2, 8);
if (game == UW2)
{
BitMapWidth=79;
BitMapHeight = 112;
}
// int datalen;
// palette auxpal[16];
// int auxPalIndex;
// unsigned char *imgNibbles;
// unsigned char *outputImg;
//switch (getValAtAddress(textureFile, textureOffset, 8))
//{
//default://8 bit uncompressed
fprintf(LOGFILE,"8 bit uncompressed\n");
fprintf(LOGFILE,"Width = %d\n", getValAtAddress(textureFile, textureOffset + 1, 8));
fprintf(LOGFILE,"Height = %d\n", getValAtAddress(textureFile, textureOffset + 2, 8));
//textureOffset = 1;//textureOffset + 1;
if (useTGA == 1)
{
writeTGA(textureFile, textureOffset, BitMapWidth, BitMapHeight, i, pal, OutFileName,0);
}
else
{
writeBMP(textureFile, textureOffset, BitMapWidth, BitMapHeight, i, pal, OutFileName);
}
//break;
//}
}
break;
}
//NoOfTextures=0;
//fprintf(LOGFILE,"Address of first block:%d\n", (textureFile[7]<<16 | textureFile[6]<<32 | textureFile[5]<<8 | textureFile[4]));
return;
}
void extractCrittersUW1(char fileAssoc[255], char fileCrit[255], char PaletteFile[255], int PaletteNo, int BitmapSize, int FileType, int game, int CritterNo, char OutFileName[255], int useTGA, int SkipFileOutput, int ItemId, int fileXX, int fileYY)
{
int slotIndices[128];
int slotBase = 0;//I offset the animations slots by this number!!!!
palette *pal;
int AddressPointer;
unsigned char *critterFile;
unsigned char auxpalval[32];
palette auxpal[32];
int auxPalNo = PaletteNo;
long fileSize;
unsigned char *assocFile;
pal = new palette[256];
if (GREYSCALE == 1)
{
getPaletteIndex(PaletteFile, pal, PaletteNo);
}
else
{
getPalette(PaletteFile, pal, PaletteNo);
}
//getPalette(PaletteFile, pal, 0);//always palette 0?
FILE *file = NULL; // File pointer
if ((file = fopen(fileAssoc, "rb")) == NULL)
{
fprintf(LOGFILE, "//\nArchive %s not found!\n", fileAssoc);
return;
}
fileSize = getFileSize(file);
assocFile = new unsigned char[fileSize];
fread(assocFile, fileSize, 1, file);
fclose(file);
if ((file = fopen(fileCrit, "rb")) == NULL)
{
fprintf(LOGFILE, "//\nArchive %s not found!\n", fileCrit);
return;
}
fileSize = getFileSize(file);
critterFile = new unsigned char[fileSize];
fread(critterFile, fileSize, 1, file);
fclose(file);
AddressPointer = 0;
fprintf(LOGFILE, "\n//\tFile:%s, Palette = %d", fileCrit, PaletteNo);
slotBase = getValAtAddress(critterFile, AddressPointer, 8);
//fprintf(LOGFILE, "\n//\tSlot base %d\n", slotBase);
int NoOfSlots = getValAtAddress(critterFile, AddressPointer + 1, 8);
//fprintf(LOGFILE, "\t//No of Slots %d\n", NoOfSlots);
AddressPointer = 2;
int k = 0;
for (int i = 0; i < NoOfSlots; i++)
{
if (getValAtAddress(critterFile, AddressPointer, 8) != 255)
{
slotIndices[k++] = i;//getValAtAddress(critterFile, AddressPointer, 8);
//fprintf(LOGFILE, "\n//\tIndex %d = %d", i, getValAtAddress(critterFile, AddressPointer, 8));
}
AddressPointer++;
}
int NoOfSegs = getValAtAddress(critterFile, AddressPointer, 8);
//fprintf(LOGFILE, "\n//\tNo of anim segments=%d", NoOfSegs);
AddressPointer++;
for (int i = 0; i < NoOfSegs; i++)
{
fprintf(LOGFILE, "\n\tCreateAnimationUW(\"%d_", ItemId, slotIndices[i] + slotBase);
PrintAnimName(game, slotIndices[i] + slotBase);
fprintf(LOGFILE, "\",");
int ValidCount=0;
for (int j = 0; j < 8; j++)
{
if (getValAtAddress(critterFile, AddressPointer, 8) != 255)
{
//fprintf(LOGFILE, "\n\tAnim Frame %d is %d %s_%04d", j, getValAtAddress(critterFile, AddressPointer, 8), fileCrit, getValAtAddress(critterFile, AddressPointer, 8));
//printf(" \"CR%02oPAGE_N%02d_%d_%04d\" ,", fileXX, fileYY, auxPalNo, getValAtAddress(critterFile, AddressPointer, 8));
fprintf(LOGFILE, " \"CR%02oPAGE_N%02d_%d_%04d\" ,", fileXX, fileYY, auxPalNo, getValAtAddress(critterFile, AddressPointer, 8));
ValidCount++;
}
else
{
fprintf(LOGFILE," \"\" ,");
}
AddressPointer++;
}
fprintf(LOGFILE, "%d, _RES + \"/Sprites/Critters\" , 0.2f);",ValidCount);
}
int NoOfPals = getValAtAddress(critterFile, AddressPointer, 8);
AddressPointer++;
//fprintf(LOGFILE, "\n//\tNo of Palettes %d", NoOfPals);
for (int i = 0; i < 32; i++)
{
int value = getValAtAddress(critterFile, (AddressPointer)+(auxPalNo * 32) + i, 8);
auxpalval[i] = value;
auxpal[i].green = pal[value].green;
auxpal[i].blue = pal[value].blue;
auxpal[i].red = pal[value].red;