-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdgfx.c
5620 lines (4823 loc) · 224 KB
/
cmdgfx.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
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
/****************************************
* Cmdgfx (c) Mikael Sollenborn 2016-20 *
****************************************/
#ifndef WINVER
#define WINVER 0x600
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#endif
//#define GDI_OUTPUT
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <process.h>
#include "gfxlib.h"
#include "tinyexpr.h"
#ifdef GDI_OUTPUT
#include "cmdfonts.h"
#endif
// Issues/ideas:
// 1. Code optimization: Re-use images used several times, same way as for 3d objects (or possibly, use binary alternative to gxy to remove parsing, might be almost as fast)
// 2. Force col for tpoly/3d? Free rotation for block?
// 3. brush op from dwself?
// 4. Allow set texture(s) for 3d obj from 3d op, to avoid having to use loads of dupl objects just to change texture
// 5. RGB: Make specified 3d colors work better with textures (should not overflow the colors, should be safe to do e.g 555555 and not overflow. Should also be correct for - . And work for bgcol too...)
// 6. RGB: Allow bgy files with alpha, to be used by various ops
// 7. Sending commands with title to cmdgfx does not seem reliable... (misses sometimes)
// 8. 3d Z-lighting mode for textures ("fog")
// 9. Wrap for text op
// 10. GDI: Scale output image with flag (to support Windows scaling)
// 11. Non-GDI: Add support for two more params to f flag for x,y of final output offset
// 12. Major: Port to Linux
int XRES, YRES, FRAMESIZE;
uchar *video;
float *ZBufVideo = NULL;
int bAllowRepeated3dTextures = 0;
float texture_offset_x = 0, texture_offset_y = 0;
int bPrintFullErrorString = 0;
int g_bUsingPixelFont = 0;
#define MAX_ERRS 64
typedef enum {ERR_NOF_ARGS, ERR_IMAGE_LOAD, ERR_OBJECT_LOAD, ERR_PARSE, ERR_MEMORY, ERR_OPTYPE, ERR_EXPRESSION } ErrorType;
typedef enum {OP_POLY=0, OP_IPOLY=1, OP_GPOLY=2, OP_TPOLY=3, OP_IMAGE=4, OP_BOX=5, OP_FBOX=6, OP_LINE=7, OP_PIXEL=8, OP_CIRCLE=9, OP_FCIRCLE=10, OP_ELLIPSE=11, OP_FELLIPSE=12, OP_TEXT=13, OP_3D=14, OP_BLOCK=15, OP_INSERT=16, OP_PLAY=17, OP_UNKNOWN=18 } OperationType;
typedef struct {
ErrorType errType[MAX_ERRS];
OperationType opType[MAX_ERRS];
int index[MAX_ERRS];
char *extras[MAX_ERRS];
char *op[MAX_ERRS];
int argNof[MAX_ERRS];
int errCnt;
} ErrorHandler;
unsigned char hexLookup[256];
unsigned char colLookup[256];
#define GetHex(v) (hexLookup[(int)v])
#define GetCol(v, old) (colLookup[(int)v] == TRANSPVAL? old : hexLookup[(int)v])
#define MAX_STR_SIZE 300000
#define MAX_OP_SIZE 128000
HANDLE g_conin, g_conout;
static HANDLE GetInputHandle() {
return CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
}
static HANDLE GetOutputHandle() {
return CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
}
// if USE_THREAD_POOL not defined, create new threads each frame for block if threading enabled
// NOTE: seriously seems like using thread pool (i.e. using waiting and/or suspend/resumethread) causes Windows antimalware service to go active, BOTH when compiling/linking (takes very long), AND first time running the exe!
// NOTE2: DON'T use ResumeThread/SuspendThread for thread pool. Got deadlocks. Use WaitForSingleObject+auto-reset events instead
#define USE_THREAD_POOL
#define MAXTHREADS 8
#ifdef USE_THREAD_POOL
HANDLE ghDoneEvents[MAXTHREADS];
HANDLE ghAwaitWorkEvents[MAXTHREADS];
HANDLE ghDoneBlitEvent;
HANDLE ghAwaitWorkBlitEvent;
int gbThreadsCreated = 0;
#endif
static unsigned __stdcall process_op(void *arg);
enum { THREAD_OP_BLOCK, THREAD_OP_FBOX, THREAD_OP_PIXELPREP };
typedef struct {
int op;
//block (and partly reused by other ops)
char *s_mode; int x; int y; int w; int h; int nx; int ny; int nw; int nh; int rz; char *transf; char *colorExpr; char *xExpr; char *yExpr; int XRES; int YRES; uchar *videoCol; uchar *videoChar; int transpchar; int bFlipX; int bFlipY; int bTo; int mvx;int mvy; int mvw; int mvh;
//fbox
int fgcol, bgcol, bWriteChars, bWriteCols, dchar;
//prepPix
int fw, fh, fontIndex, *data;
unsigned int *palFg, *palBg;
//shared
int blockThreadIndex; int threadYp;
int bExit;
} BlockData;
typedef struct {
int x; int y; int w; int h;
int bExit;
} BlitData;
static int MouseClicked(MOUSE_EVENT_RECORD mer) {
static int bReportNext = 0;
int res = 0;
switch(mer.dwEventFlags) {
case DOUBLE_CLICK: case MOUSE_WHEELED: bReportNext = 1; res = 1; break;
case 0: case MOUSE_MOVED:
if(mer.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED || mer.dwButtonState & RIGHTMOST_BUTTON_PRESSED) {
bReportNext = 1;
res = 1;
} else if (bReportNext) {
bReportNext = 0;
res = 1;
} else {
bReportNext = 0;
res = 0;
}
break;
default:
break;
}
return res;
}
static int MouseEventProc(MOUSE_EVENT_RECORD mer) {
static int bReportNext = 0;
int res;
res = (mer.dwMousePosition.X << 5) | (mer.dwMousePosition.Y << 14);
switch(mer.dwEventFlags) {
case 0: case DOUBLE_CLICK: case MOUSE_MOVED:
//printf("GOT: %d %d\n",mer.dwButtonState, mer.dwEventFlags);
if(mer.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED) {
res |= 2;
}
if(mer.dwButtonState & RIGHTMOST_BUTTON_PRESSED) {
res |= 4;
}
break;
case MOUSE_WHEELED:
if ((int)mer.dwButtonState < 0)
res |= 8;
else
res |= 16;
break;
default:
break;
}
res |= 1;
return res;
}
int consoleFgCol=-1, consoleBgCol=-1;
static void GetConsoleColor(){
CONSOLE_SCREEN_BUFFER_INFO info;
int col = 0x7;
GetConsoleScreenBufferInfo(g_conout, &info);
consoleFgCol = info.wAttributes & 0xf;
consoleBgCol = (info.wAttributes>>4) & 0xf;
}
int GXY_MAX_X = 256, GXY_MAX_Y = 256;
unsigned int *g_rgbFgPalette, *g_rgbBgPalette;
static int readGxy(char *fname, Bitmap *b_cols, Bitmap *b_chars, int *w1, int *h1, uchar color, int transpchar,int bIsFile,int bIgnoreFgColor, int bIgnoreBgColor, int bIgnoreAllCodes) {
unsigned char *text, ch;
uchar *pbcol, *pbchar;
int fr, i, j, inlen;
int x = 0, y = 0, yp=0;
int v, v16;
unsigned int fgCol, bgCol;
uchar oldColor;
uchar *cchars, *ccols;
int w = GXY_MAX_X, h = GXY_MAX_Y;
FILE *ifp;
*w1 = -1;
bgCol = (color>>BITSHL) & AND_MASK;
fgCol = color & AND_MASK;
oldColor = color;
b_cols->data = (uchar *)malloc(w*h*sizeof(uchar));
b_chars->data = (uchar *)malloc(w*h*sizeof(uchar));
text = (char *)malloc(GXY_MAX_X * GXY_MAX_Y * 6);
if (!text || !b_cols->data || !b_chars->data) { if (text) free(text); if (b_cols->data) free(b_cols->data); if(b_chars->data) free(b_chars->data); b_cols->data = b_chars->data = NULL; return 0; }
MYMEMSET(b_cols->data, 0, w*h);
MYMEMSET(b_chars->data, TRANSPVAL, w*h);
pbchar = b_chars->data;
pbcol = b_cols->data;
if (bIsFile) {
ifp=fopen(fname, "r");
if (ifp == NULL) {
free(text); free(b_cols->data); free(b_chars->data); b_cols->data = b_chars->data = NULL;
return 0;
} else {
fr = fread(text, 1, GXY_MAX_X * GXY_MAX_Y * 6, ifp);
fclose(ifp);
}
text[fr] = 0;
} else
strcpy(text, fname);
inlen =strlen(text);
for(i = 0; i < inlen; i++) {
ch = text[i];
if (ch == '\\' && !bIgnoreAllCodes) {
i++;
ch = text[i];
switch(ch) {
case '-': {
x++;
break;
}
case 'g': {
i++; v16 = GetHex(text[i]);
i++; v = 0;
if (i < inlen) {
v = GetHex(text[i]);
}
v16 = (v16*16) + v;
if (x < w) { pbchar[yp+x] = v16; pbcol[yp+x] = color; }
x++;
break;
}
case '\\': {
if (x < w) { pbchar[yp+x] = ch; pbcol[yp+x] = color; }
x++;
break;
}
case 'r': {
uchar tempC = color;
color = oldColor;
oldColor = tempC;
break;
}
case 'n': {
if (x > *w1) *w1 = x;
x = 0; y++; yp+=w;
break;
}
default: {
oldColor = color;
if (!bIgnoreFgColor) {
if (ch == 'u' || ch == 'U') {
if (consoleBgCol==-1)
GetConsoleColor();
fgCol = ch == 'u'? consoleFgCol : consoleBgCol;
} else
fgCol = GetCol(ch, fgCol);
}
i++;
if (!bIgnoreBgColor) {
ch = text[i];
if (ch == 'u' || ch == 'U') {
if (consoleBgCol==-1)
GetConsoleColor();
bgCol = ch == 'u'? consoleFgCol : consoleBgCol;
} else
bgCol = GetCol(ch, bgCol);
}
#ifndef _RGB32
color = fgCol | (bgCol<<4);
#else
if (bIgnoreFgColor) color = fgCol; else color = g_rgbFgPalette[fgCol];
if (bIgnoreBgColor) color |= (PREPCOL)bgCol<<BITSHL; else color |= ((PREPCOL)g_rgbBgPalette[bgCol]<<BITSHL);
#endif
}
}
} else {
if (y >= h) break;
if (ch == 10) {
if (x > *w1) *w1 = x;
x = 0; y++; yp+=w;
} else {
if (ch == 9) ch=32;
if (x < w) { pbchar[yp+x] = ch; pbcol[yp+x] = color; }
x++;
}
}
}
y++;
*h1 = y;
if (x > *w1) *w1 = x;
ccols = (uchar *)malloc((*w1)*y*sizeof(uchar));
cchars = (uchar *)malloc((*w1)*y*sizeof(uchar));
if (!ccols || !cchars) { free(b_cols->data); free(b_chars->data); free(text); if (ccols) free(ccols); if (cchars) free(cchars); return 0; }
for (i = 0; i < *h1; i++)
for (j = 0; j < *w1; j++) {
ccols[i*(*w1)+j] = b_cols->data[i*w+j];
cchars[i*(*w1)+j] = b_chars->data[i*w+j];
}
b_chars->xSize = *w1; b_chars->ySize = *h1;
b_cols->xSize = *w1; b_cols->ySize = *h1;
b_cols->transpVal = b_chars->transpVal = -1;
free(b_cols->data);
free(b_chars->data);
b_chars->data = cchars;
b_cols->data = ccols;
free(text);
return 1;
}
static int parseInput(char *s_fgcol, char *s_bgcol, char *s_dchar, int *fgcol, int *bgcol, int *dchar, int *bWriteChars, int *bWriteCols) {
int writeCols = 1, writeChars = 1, writeFgBg = 3, sLen;
sLen = strlen(s_fgcol);
if (sLen==1) {
*fgcol = strtol(s_fgcol, NULL, 16);
if (s_fgcol[0] == 'U') {
if (consoleBgCol==-1)
GetConsoleColor();
*fgcol = consoleBgCol;
}
else if (s_fgcol[0] == 'u') {
if (consoleFgCol==-1)
GetConsoleColor();
*fgcol = consoleFgCol;
}
else if (s_fgcol[0] == '?')
writeCols = 0, writeFgBg -= 1;
#ifndef _RGB32
} else
*fgcol = strtol(s_fgcol, NULL, 10);
#else
*fgcol = g_rgbFgPalette[*fgcol] & 0x00ffffff;
} else {
if (sLen==2) {
*fgcol = strtol(s_fgcol, NULL, 10);
if (*fgcol < 0 || *fgcol > 15) *fgcol = 0;
*fgcol = g_rgbFgPalette[*fgcol] & 0x00ffffff;
} else {
*fgcol = strtoll(s_fgcol, NULL, 16);
}
}
#endif
sLen = strlen(s_bgcol);
if (sLen==1) {
*bgcol = strtol(s_bgcol, NULL, 16);
if (s_bgcol[0] == 'U') {
if (consoleBgCol==-1)
GetConsoleColor();
*bgcol = consoleBgCol;
}
else if (s_bgcol[0] == 'u') {
if (consoleFgCol==-1)
GetConsoleColor();
*bgcol = consoleFgCol;
}
else if (s_bgcol[0] == '?')
writeCols = 0, writeFgBg -= 2;
#ifndef _RGB32
} else
*bgcol = strtol(s_bgcol, NULL, 10);
#else
*bgcol = g_rgbBgPalette[*bgcol] & 0x00ffffff;
} else {
if (sLen==2) {
*bgcol = strtol(s_bgcol, NULL, 10);
if (*bgcol < 0 || *bgcol > 15) *bgcol = 0;
*bgcol = g_rgbBgPalette[*bgcol] & 0x00ffffff;
} else
*bgcol = strtoll(s_bgcol, NULL, 16);
}
#endif
if (strlen(s_dchar)==1) {
if (s_dchar[0] == '?')
writeChars = 0;
*dchar = s_dchar[0];
} else
*dchar = strtol(s_dchar, NULL, 16);
if (bWriteChars) *bWriteChars = writeChars;
if (bWriteCols) *bWriteCols = writeCols;
return writeFgBg;
}
ErrorHandler *g_errH;
int g_opCount;
static void reportFileError(ErrorHandler *errHandler, OperationType opType, ErrorType errType, int index, char *extras, char *op);
uchar *g_videoCol, *g_videoChar;
int g_bSleepingWait = 0;
int g_bFlushAfterELwrite = 0;
static int readCmdGfxTexture(Bitmap *bmap, char *fname) {
char *orgFnameP = fname;
int res = 0;
if (!bmap || !fname) return res;
bmap->transpVal = -1;
if (strstr(fname, ".pcx")) {
char transp[16], inpname[256];
int nofargs, dum1, dum2, transpVal = -1;
nofargs = sscanf(fname, "%250s %13s", inpname, transp);
if (nofargs > 1) parseInput(transp, transp, transp, &transpVal, &dum1, &dum2, NULL, NULL);
res = PCXload(bmap, inpname);
#ifdef _RGB32
for(int i = 0; i < bmap->xSize*bmap->ySize; i++)
bmap->data[i] = g_rgbFgPalette[bmap->data[i]] & 0xffffff;
#endif
bmap->transpVal = transpVal;
bmap->bCmdBlock = 0;
bmap->extras = NULL;
bmap->extrasType = EXTRAS_NONE;
#ifdef _RGB32
} else if (strstr(fname, ".bmp")) {
char transp[16], inpname[256];
int nofargs, dum1, dum2, transpVal = -1;
nofargs = sscanf(fname, "%250s %13s", inpname, transp);
if (nofargs > 1) parseInput(transp, transp, transp, &transpVal, &dum1, &dum2, NULL, NULL);
res = BMPload(bmap, inpname);
bmap->transpVal = transpVal;
bmap->bCmdBlock = 0;
bmap->extras = NULL;
bmap->extrasType = EXTRAS_NONE;
} else if (strstr(fname, ".bxy")) {
int w, h;
char transp[16], inpname[256];
int nofargs, dum1, dum2, transpVal = -1;
nofargs = sscanf(fname, "%250s %13s", inpname, transp);
if (nofargs > 1) parseInput(transp, transp, transp, &dum1, &dum2, &transpVal, NULL, NULL);
bmap->extras = (Bitmap *) calloc(sizeof(Bitmap), 1);
if (!bmap->extras) return res;
bmap->extrasType = EXTRAS_BITMAP;
res = BXYload(bmap, (Bitmap *)bmap->extras, inpname);
bmap->transpVal = transpVal;
bmap->bCmdBlock = 0;
#endif
} else if (strstr(fname, "cmdblock")) {
int x, y, w, h, i, j, ii, vi, nofargs, transpVal = -1, blockRefresh = 0;
Bitmap *bmap2;
fname = strstr(fname, "cmdblock ") + strlen("cmdblock ");
while (*fname==32) fname++;
nofargs = sscanf(fname, "%d %d %d %d %x %d", &x, &y, &w, &h, &transpVal, &blockRefresh);
if (nofargs < 4)
return 0;
if (x < 0 || y < 0 || x >= XRES || y >= YRES)
return 0;
if (x+w >= XRES) w -= (x+w)-(XRES+0);
if (y+h >= YRES) h -= (y+h)-(YRES+0);
bmap2 = (Bitmap *) calloc(sizeof(Bitmap), 1);
bmap2->data = (uchar *) malloc( w * h * sizeof(uchar));
bmap->data = (uchar *) malloc( w * h * sizeof(uchar));
if (!bmap2 || ! bmap->data || ! bmap2->data) { if(bmap2->data) free(bmap2->data); if(bmap->data) free(bmap->data); if (bmap2) free(bmap2); return res; }
bmap->extras = bmap2;
bmap->xSize = bmap2->xSize = w;
bmap->ySize = bmap2->ySize = h;
bmap->extrasType = EXTRAS_BITMAP;
bmap->transpVal = -1;
bmap2->transpVal = transpVal;
bmap->bCmdBlock = 1;
strncpy(bmap->pathOrBlockString, orgFnameP, 64);
bmap->pathOrBlockString[64] = 0;
bmap->blockRefresh = blockRefresh == 2? 2 : 0;
for (i = 0; i < h; i++) {
ii = w*i; vi = x + y * XRES + i * XRES;
for (j = 0; j < w; j++) {
bmap->data[ii + j] = g_videoCol[vi + j];
bmap2->data[ii + j] = g_videoChar[vi + j];
}
}
res = 1;
} else if (strstr(fname, "cmdcolblock")) {
int x, y, w, h, i, j, ii, vi, nofargs, transpVal = -1, blockRefresh = 0;
fname = strstr(fname, "cmdcolblock ") + strlen("cmdcolblock ");
while (*fname==32) fname++;
nofargs = sscanf(fname, "%d %d %d %d %x %d", &x, &y, &w, &h, &transpVal, &blockRefresh);
if (nofargs < 4)
return 0;
if (x < 0 || y < 0 || x >= XRES || y >= YRES)
return 0;
if (x+w >= XRES) w -= (x+w)-(XRES+0);
if (y+h >= YRES) h -= (y+h)-(YRES+0);
bmap->data = (uchar *) malloc( w * h * sizeof(uchar));
if (!bmap->data) { if(bmap->data) free(bmap->data); return res; }
bmap->extras = NULL;
bmap->xSize = w;
bmap->ySize = h;
bmap->extrasType = EXTRAS_NONE;
bmap->transpVal = transpVal;
bmap->bCmdBlock = 1;
strncpy(bmap->pathOrBlockString, orgFnameP, 64);
bmap->pathOrBlockString[64] = 0;
bmap->blockRefresh = blockRefresh == 2? 2 : 0;
for (i = 0; i < h; i++) {
ii = w*i; vi = x + y * XRES + i * XRES;
for (j = 0; j < w; j++) {
bmap->data[ii + j] = g_videoCol[vi + j];
}
}
res = 1;
} else if (strstr(fname, "cmdpalette ")) {
char s_fgcols[34][64], s_bgcols[34][10], s_dchars[34][4];
int pchar[64], pbWriteChars[64], pbWriteCols[64];
uchar pfgbg[64], *cols;
int nofcols, i, j;
int fgcol, bgcol;
fname = strstr(fname, "cmdpalette ") + strlen("cmdpalette ");
while (*fname==32) fname++;
nofcols = sscanf(fname, "%62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s %62s %8s %2s",
s_fgcols[0], s_bgcols[0], s_dchars[0], s_fgcols[1], s_bgcols[1], s_dchars[1], s_fgcols[2], s_bgcols[2], s_dchars[2],
s_fgcols[3], s_bgcols[3], s_dchars[3], s_fgcols[4], s_bgcols[4], s_dchars[4], s_fgcols[5], s_bgcols[5], s_dchars[5],
s_fgcols[6], s_bgcols[6], s_dchars[6], s_fgcols[7], s_bgcols[7], s_dchars[7], s_fgcols[8], s_bgcols[8], s_dchars[8],
s_fgcols[9], s_bgcols[9], s_dchars[9], s_fgcols[10], s_bgcols[10], s_dchars[10], s_fgcols[11], s_bgcols[11], s_dchars[11],
s_fgcols[12], s_bgcols[12], s_dchars[12], s_fgcols[13], s_bgcols[13], s_dchars[13], s_fgcols[14], s_bgcols[14], s_dchars[14],
s_fgcols[15], s_bgcols[15], s_dchars[15], s_fgcols[16], s_bgcols[16], s_dchars[16], s_fgcols[17], s_bgcols[17], s_dchars[17],
s_fgcols[18], s_bgcols[18], s_dchars[18], s_fgcols[19], s_bgcols[19], s_dchars[19], s_fgcols[20], s_bgcols[20], s_dchars[20],
s_fgcols[21], s_bgcols[21], s_dchars[21], s_fgcols[22], s_bgcols[22], s_dchars[22], s_fgcols[23], s_bgcols[23], s_dchars[23],
s_fgcols[24], s_bgcols[24], s_dchars[24], s_fgcols[25], s_bgcols[25], s_dchars[25], s_fgcols[26], s_bgcols[26], s_dchars[26],
s_fgcols[27], s_bgcols[27], s_dchars[27], s_fgcols[28], s_bgcols[28], s_dchars[28], s_fgcols[29], s_bgcols[29], s_dchars[29],
s_fgcols[30], s_bgcols[30], s_dchars[30], s_fgcols[31], s_bgcols[31], s_dchars[31] );
if (nofcols < 3)
return res;
nofcols /= 3;
bmap->extras = (uchar *) malloc(sizeof(uchar) * (34 * 4 + 1));
if (!bmap->extras) return res;
bmap->data = NULL;
bmap->extrasType = EXTRAS_ARRAY;
bmap->bCmdBlock = 0;
for (i = 0; i < nofcols; i++) {
parseInput(s_fgcols[i%nofcols], s_bgcols[i%nofcols], s_dchars[i%nofcols], &fgcol, &bgcol, &pchar[i], &pbWriteChars[i], &pbWriteCols[i]);
pfgbg[i] = (bgcol << 4) | fgcol;
}
cols = (uchar *)bmap->extras;
j = 0; cols[j++] = nofcols;
for (i = 0; i < nofcols; i++) {
cols[j++] = pfgbg[i];
cols[j++] = pchar[i];
cols[j++] = pbWriteChars[i];
cols[j++] = pbWriteCols[i];
}
res = 1;
} else {
int w, h;
char transp[16], inpname[256];
int nofargs, dum1, dum2, transpVal = -1;
nofargs = sscanf(fname, "%250s %13s", inpname, transp);
if (nofargs > 1) parseInput(transp, transp, transp, &dum1, &dum2, &transpVal, NULL, NULL);
bmap->extras = (Bitmap *) calloc(sizeof(Bitmap), 1);
if (!bmap->extras) return res;
bmap->extrasType = EXTRAS_BITMAP;
res = readGxy(inpname, bmap, (Bitmap *)bmap->extras, &w, &h, 0, -1, 1, 0, 0, 0);
bmap->transpVal = transpVal;
bmap->bCmdBlock = 0;
}
if (!res) reportFileError(g_errH, OP_3D, ERR_IMAGE_LOAD, g_opCount, fname, NULL);
return res;
}
static CHAR_INFO * readScreenBlock() {
COORD a = { 1, 1 };
COORD b = { 0, 0 };
SMALL_RECT r;
CHAR_INFO *str;
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
int x,y, w, h;
HANDLE conout = GetOutputHandle();
GetConsoleScreenBufferInfo(conout, &screenBufferInfo);
x = y = 0;
w = screenBufferInfo.dwSize.X;
h = screenBufferInfo.dwSize.Y;
str = (CHAR_INFO *) malloc (sizeof(CHAR_INFO) * w*h);
if (!str) {
CloseHandle(conout);
return NULL;
}
// Stupid bug in ReadConsoleOutput doesn't seem to read more than ~15680 chars, then it's all garbled characters!! Have to read in smaller blocks
{
int i, j, k, l;
l = 15000 / w;
i = h / l;
for (j = 0; j <= i; j++) {
r.Left = 0;
r.Top = j*l;
r.Right = w;
if (i == j) k = h % l; else k = l;
r.Bottom = j*l+k;
a.X = r.Right;
a.Y = k;
ReadConsoleOutput(conout, str+j*l*w, a, b, &r);
}
}
CloseHandle(conout);
return str;
}
#ifndef GDI_OUTPUT
#ifndef _RGB32
static void convertToText(int XRES, int YRES, uchar *videoCol, uchar *videoChar, uchar *fgPalette, uchar *bgPalette, int x,int y, int outw, int outh) {
CHAR_INFO *str;
COORD a, b;
SMALL_RECT r;
HANDLE hCurrHandle;
int i,j,k,l;
a.X = outw; a.Y = outh;
hCurrHandle = g_conout;
str = (CHAR_INFO *) calloc (sizeof(CHAR_INFO) * (a.X * a.Y), 1);
if (!str) return;
if (fgPalette == NULL) {
for (i = 0; i < outh; i++) {
k = i * XRES; l = i * outw;
for (j = 0; j < outw; j++) {
str[l+j].Attributes = videoCol[k+j];
str[l+j].Char.AsciiChar = videoChar[k+j];
}
}
} else {
for (i = 0; i < outh; i++) {
k = i * XRES; l = i * outw;
for (j = 0; j < outw; j++) {
str[l+j].Attributes = fgPalette[videoCol[k+j] & 0xf] | (bgPalette[videoCol[k+j] >> 4] << 4);
str[l+j].Char.AsciiChar = videoChar[k+j];
}
}
}
b.X = b.Y = r.Left = r.Top = 0;
r.Right = a.X + x;
r.Bottom = a.Y + y;
r.Left = x;
r.Top = y;
WriteConsoleOutput(hCurrHandle, str, a, b, &r);
free(str);
}
#else
static void convertToVT100(int XRES, int YRES, uchar *videoCol, uchar *videoChar, int x, int y, int outw, int outh) {
unsigned int i,j,fcol,bcol,cchar, oldfcol = -1, oldbcol = -1, k=0;
unsigned char *outS = malloc(40 * XRES + 10);
for (i = 0; i < outh-1; i++) {
*outS=0; k=0;
k += sprintf(outS, "%c[%d;%dH",27,y+i+1,x);
for (j = 0; j < outw; j++) {
cchar = videoChar[j+i*XRES] SAFE_AND; if (cchar==0) cchar=255;
fcol = videoCol[j+i*XRES] & 0xffffff;
bcol = videoCol[j+i*XRES] >> BITSHL;
if (fcol != oldfcol) {
k += sprintf(&outS[k], "%c[38;2;%d;%d;%dm",27,(fcol>>16)&0xff,(fcol>>8)&0xff,fcol&0xff);
oldfcol = fcol;
}
if (bcol != oldbcol) {
k += sprintf(&outS[k], "%c[48;2;%d;%d;%dm",27,(bcol>>16)&0xff,(bcol>>8)&0xff,bcol&0xff);
oldbcol = bcol;
}
outS[k] = cchar; k++; outS[k] = 0;
}
puts(outS);
}
free(outS);
}
#endif
#endif
#ifdef GDI_OUTPUT
HWND g_hWnd = NULL;
HDC g_hDc = NULL, g_hDcBmp = NULL;
unsigned char* g_lpBitmapBits;
HBITMAP g_bitmap;
int g_blitX, g_blitY, g_blitW, g_blitH;
static void prepPixels(int fw, int fh, uchar *videoCol, uchar *videoChar, int fontIndex, int outw, int outh, int *data, int yp, unsigned int *palFg, unsigned int *palBg) {
int i,j,ccol,cchar,l,m, index;
unsigned int *outdata = NULL, *pcol, *outt, *fgcol, *bgcol, rgbfgcol, rgbbgcol;
unsigned long long rgbcoltemp;
int val;
outdata = (unsigned int *)g_lpBitmapBits;
if (fontIndex < 10) {
for (i = yp; i < outh+yp; i++) {
for (j = 0; j < outw; j++) {
cchar = videoChar[j+i*XRES] SAFE_AND;
#ifndef _RGB32
ccol = videoCol[j+i*XRES];
fgcol = &palFg[(ccol&0xf)];
bgcol = &palBg[(ccol>>4)];
#else
rgbcoltemp = videoCol[j+i*XRES];
rgbfgcol = rgbcoltemp&0xffffffff;
rgbbgcol = rgbcoltemp>>BITSHL;
#endif
for (l = 0; l < fh; l++) {
index = (j*fw + (i*fh+l)*outw*fw);
val = data[cchar*fh+l];
outt = &outdata[index];
for (m = 0; m < fw; m++) {
#ifndef _RGB32
*outt++ = (val & 1) ? *fgcol : *bgcol;
#else
*outt++ = (val & 1) ? rgbfgcol : rgbbgcol;
#endif
val >>= 1;
}
}
}
}
} else { // pixelfont
if (fw == 1) {
for (i = yp; i < outh+yp; i++) {
for (j = 0; j < outw; j++) {
cchar = videoChar[j+i*XRES] SAFE_AND;
#ifndef _RGB32
ccol = videoCol[j+i*XRES];
fgcol = &palFg[(ccol&0xf)];
bgcol = &palBg[(ccol>>4)];
pcol = fgcol; if (cchar == 0 || cchar == 32 || cchar == 255) pcol = bgcol;
outdata[j + i*outw] = *pcol;
#else
rgbcoltemp = videoCol[j+i*XRES];
if (cchar == 0 || cchar == 32 || cchar == 255) rgbfgcol = rgbcoltemp>>BITSHL; else rgbfgcol = rgbcoltemp&0xffffffff;
outdata[j + i*outw] = rgbfgcol;
#endif
}
}
} else {
for (i = yp; i < outh+yp; i++) {
for (j = 0; j < outw; j++) {
cchar = videoChar[j+i*XRES] SAFE_AND;
#ifndef _RGB32
ccol = videoCol[j+i*XRES];
fgcol = &palFg[(ccol&0xf)];
bgcol = &palBg[(ccol>>4)];
pcol = fgcol; if (cchar == 0 || cchar == 32 || cchar == 255) pcol = bgcol;
#else
rgbcoltemp = videoCol[j+i*XRES];
if (cchar == 0 || cchar == 32 || cchar == 255) rgbfgcol = rgbcoltemp>>BITSHL; else rgbfgcol = rgbcoltemp&0xffffffff;
#endif
for (l = 0; l < fh; l++) {
index = (j*fw + (i*fh+l)*outw*fw);
outt = &outdata[index];
for (m = 0; m < fw; m++) {
#ifndef _RGB32
*outt++ = *pcol;
#else
*outt++ = rgbfgcol;
#endif
}
}
}
}
}
}
}
static void convertToGdiBitmap(int XRES, int YRES, uchar *videoCol, uchar *videoChar, int fontIndex, unsigned int *cmdPaletteFg, unsigned int *cmdPaletteBg, int x, int y, int outw, int outh, int outx, int outy, int bAbsBitmapPos, int bWindowedMode, int allowedThreads, HANDLE *threadhandles, BlockData *bds) {
HBITMAP hBmp1 = NULL;
HGDIOBJ hGdiObj = NULL;
BITMAP bmp = {0};
LONG w = 0, h = 0;
int iRet = EXIT_FAILURE;
static unsigned int cmdPalette[16] = { 0xff000000, 0xff000080, 0xff008000, 0xff008080, 0xff800000, 0xff800080, 0xff808000, 0xffc0c0c0, 0xff808080, 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff };
static int *fontData[16] = { &cmd_font0_data[0][0], &cmd_font1_data[0][0], &cmd_font2_data[0][0], &cmd_font3_data[0][0], &cmd_font4_data[0][0], &cmd_font5_data[0][0], &cmd_font6_data[0][0], &cmd_font7_data[0][0], &cmd_font8_data[0][0], &cmd_font9_data[0][0], NULL, NULL, NULL };
int fontWidth[16] = { cmd_font0_w, cmd_font1_w, cmd_font2_w, cmd_font3_w, cmd_font4_w, cmd_font5_w, cmd_font6_w, cmd_font7_w, cmd_font8_w, cmd_font9_w, 1,2,3,4,5,6 };
int fontHeight[16] = { cmd_font0_h, cmd_font1_h, cmd_font3_h, cmd_font3_h, cmd_font4_h, cmd_font5_h, cmd_font6_h, cmd_font7_h, cmd_font8_h, cmd_font9_h, 1,2,3,4,5,6 };
int fw, fh, *data, val, bpp = 4;
unsigned int *palFg, *palBg;
static int oldw=-1, oldh=-1, oldFontIndex = -1, oldbWindowedMode = -1;
int i,j;
if (cmdPaletteFg == NULL) palFg = &cmdPalette[0]; else palFg = cmdPaletteFg;
if (cmdPaletteBg == NULL) palBg = &cmdPalette[0]; else palBg = cmdPaletteBg;
if (fontIndex < 0 || fontIndex > 15)
return;
fw = fontWidth[fontIndex];
fh = fontHeight[fontIndex];
data = fontData[fontIndex];
if (!bAbsBitmapPos) {
x *= fw; y *= fh;
}
w = outw * fw;
h = outh * fh;
g_blitX=x, g_blitY=y, g_blitW=w, g_blitH=h;
if (g_hDc == NULL || w != oldw || h != oldh || fontIndex != oldFontIndex || bWindowedMode != oldbWindowedMode) {
if (g_hDc != NULL) {
if (g_hDc) ReleaseDC(g_hWnd, g_hDc);
if (g_hDcBmp) DeleteDC(g_hDcBmp);
if (g_bitmap) DeleteObject(g_bitmap);
}
oldw = w; oldh = h; oldFontIndex = fontIndex; oldbWindowedMode = bWindowedMode;
if ((g_hWnd = GetConsoleWindow())) {
if ((g_hDc = GetDC(bWindowedMode? g_hWnd : NULL))) {
BITMAPINFO bi;
g_hDcBmp = CreateCompatibleDC(g_hDc);
ZeroMemory(&bi, sizeof(BITMAPINFO));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = -h; //negative so (0,0) is at top left
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
g_bitmap = CreateDIBSection(g_hDcBmp, &bi, DIB_RGB_COLORS, (VOID**)&g_lpBitmapBits, NULL, 0);
SelectObject(g_hDcBmp, g_bitmap);
}
}
}
if (g_hDcBmp)
{
if (outx < 0) outx = 0;
if (outy < 0) outy = 0;
if (outx + outw > XRES) outx = XRES - outw;
if (outy + outh > YRES) outy = YRES - outh;
videoChar = videoChar + outx + outy * XRES;
videoCol = videoCol + outx + outy * XRES;
if (allowedThreads <= 1) {
prepPixels(fw, fh, videoCol, videoChar, fontIndex, outw, outh, data, 0, palFg, palBg);
} else {
int hs, hmod, hso, y1=0;
hs = hso = outh/allowedThreads;
hmod = outh%allowedThreads;
for (j = 0; j < allowedThreads; j++) {
BlockData *bd = &bds[j];
bd->op=THREAD_OP_PIXELPREP, bd->fw=fw, bd->fh=fh, bd->w=outw, bd->h=hs, bd->fontIndex=fontIndex, bd->data=fontData[fontIndex], bd->XRES=XRES, bd->YRES=YRES, bd->videoCol=videoCol, bd->videoChar=videoChar, bd->blockThreadIndex=j, bd->threadYp = j*hso; bd->palFg=palFg, bd->palBg=palBg, bd->bExit=0;
#ifdef USE_THREAD_POOL
SetEvent(ghAwaitWorkEvents[j]);
#else
threadhandles[j] = (HANDLE)_beginthreadex(NULL, 0, process_op, &bds[j], 0, NULL);
#endif
y1+=hs; if (j==allowedThreads-2) hs+=hmod;
}
#ifdef USE_THREAD_POOL
WaitForMultipleObjects(allowedThreads, ghDoneEvents, TRUE, INFINITE);
#else
WaitForMultipleObjects(allowedThreads, threadhandles, TRUE, INFINITE);
for (j = 0; i < allowedThreads; j++)
CloseHandle(threadhandles[i]);
#endif
}
// if (BitBlt(g_hDc, (int)x, (int)y, (int)w, (int)h, g_hDcBmp, 0, 0, SRCCOPY)) {
// iRet = EXIT_SUCCESS;
// }
}
//GdiFlush();
//if (iRet == EXIT_FAILURE) printf("#ERR: Failure processing output bitmap\n");
}
static void writeGdiFontString(int XRES, int YRES, int x, int y, uchar *videoCol, uchar *videoChar, unsigned char *tstring, unsigned long fcol, unsigned long bcol, unsigned char dchar, int bIgnoreFgCol, int bIgnoreBgCol, int bWriteChars, int bIgnoreAllCodes, int fontIndex, unsigned int *cmdPaletteFg, unsigned int *cmdPaletteBg)
{
unsigned int *outdata = NULL, *pcol, *fgcol, *bgcol, rgbfgcol, rgbbgcol;
int i,j,ccol,cchar,l,m, index;
static unsigned int cmdPalette[16] = { 0xff000000, 0xff000080, 0xff008000, 0xff008080, 0xff800000, 0xff800080, 0xff808000, 0xffc0c0c0, 0xff808080, 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff };
static int *fontData[16] = { &cmd_font0_data[0][0], &cmd_font1_data[0][0], &cmd_font2_data[0][0], &cmd_font3_data[0][0], &cmd_font4_data[0][0], &cmd_font5_data[0][0], &cmd_font6_data[0][0], &cmd_font7_data[0][0], &cmd_font8_data[0][0], &cmd_font9_data[0][0] };
int fontWidth[16] = { cmd_font0_w, cmd_font1_w, cmd_font2_w, cmd_font3_w, cmd_font4_w, cmd_font5_w, cmd_font6_w, cmd_font7_w, cmd_font8_w, cmd_font9_w };
int fontHeight[16] = { cmd_font0_h, cmd_font1_h, cmd_font3_h, cmd_font3_h, cmd_font4_h, cmd_font5_h, cmd_font6_h, cmd_font7_h, cmd_font8_h, cmd_font9_h };
int fw, fh, *data, val;
unsigned int *palFg, *palBg;
uchar colfgbg, oldfgbg, coltemp;
uchar *outCh, *outCol;
int v16, xi;
int ch;
if (cmdPaletteFg == NULL) palFg = &cmdPalette[0]; else palFg = cmdPaletteFg;
if (cmdPaletteBg == NULL) palBg = &cmdPalette[0]; else palBg = cmdPaletteBg;
if (fontIndex < 0 || fontIndex > 9)
return;
fw = fontWidth[fontIndex];
fh = fontHeight[fontIndex];
data = fontData[fontIndex];
colfgbg = oldfgbg = (((PREPCOL)bcol << BITSHL) | fcol);
for (i = 0, j = 0; i < strlen(tstring); i++) {
cchar = tstring[i];
if (cchar == '\\' && !bIgnoreAllCodes) {
i++;
switch(tstring[i]) {
case 'n': y+=fh+1; j=0; if (y >= YRES) return; continue; break;
case '-': j++; continue; break;
case '\\': break;