-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.c
1435 lines (1389 loc) · 40.6 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
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
#if 0
gcc ${CFLAGS:--s -O2} -c -Wno-unused-result picture.c `sdl-config --cflags`
exit
#endif
/*
This program is part of Free Hero Mesh and is public domain.
*/
#define _BSD_SOURCE
#include "SDL.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "pcfont.h"
#include "quarks.h"
#include "heromesh.h"
#include "cursorshapes.h"
#include "keyicons.xbm"
#define PPR 128 // pictures per row
#define PPRM 127 // pictures per row bit mask (one less than pictures per row)
#define PPRS 7 // pictures per row bit shift amount (log2 of pictures per row)
SDL_Surface*screen;
Uint16 picture_size;
int left_margin;
Uint32 codepage;
static SDL_Surface*picts;
static Uint8*curpic;
static const unsigned char*fontdata;
static const Uint8 bytewidth[32];
static const char default_palette[]=
"C020FF "
"000000 222222 333333 444444 555555 666666 777777 888888 999999 AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF "
"281400 412300 5F3200 842100 A05000 C35F14 E1731E FF8232 FF9141 FFA050 FFAF5F FFBE73 FFD282 FFE191 FFF0A0 "
"321E1E 412220 5F2830 823040 A03A4C BE4658 E15464 FF6670 FF7F7B FF8E7F FF9F7F FFAF7F FFBF7F FFCF7F FFDF7F "
"280D0D 401515 602020 802A2A A03535 C04040 E04A4A FF5555 FF6764 FF6F64 FF7584 FF849D FF94B7 FF9FD1 FFAEEA "
"901400 A02000 B03000 C04000 D05000 E06000 F07000 FF8000 FF9000 FFA000 FFB000 FFC000 FFD000 FFE000 FFF000 "
"280000 400000 600000 800000 A00000 C00000 E00000 FF0000 FF2828 FF4040 FF6060 FF8080 FFA0A0 FFC0C0 FFE0E0 "
"280028 400040 600060 800080 A000A0 C000C0 E000E0 FF00FF FF28FF FF40FF FF60FF FF80FF FFA0FF FFC0FF FFE0FF "
"281428 402040 603060 804080 A050A0 C060C0 E070E0 FF7CFF FF8CFF FF9CFF FFACFF FFBCFF FFCCFF FFDCFF FFECFF "
"280050 350566 420A7C 4F0F92 5C14A8 6919BE 761ED4 8323EA 9028FF A040FF B060FF C080FF D0A0FF E0C0FF F0E0FF "
"000028 000040 000060 000080 0000A0 0000C0 0000E0 0000FF 0A28FF 284AFF 466AFF 678AFF 87AAFF A7CAFF C7EBFF "
"0F1E1E 142323 193232 1E4141 285050 325F5F 377373 418282 469191 50A0A0 5AAFAF 5FC3C3 69D2D2 73E1E1 78F0F0 "
"002828 004040 006060 008080 00A0A0 00C0C0 00E0E0 00FFFF 28FFFF 40FFFF 60FFFF 80FFFF A0FFFF C0FFFF E0FFFF "
"002800 004000 006000 008000 00A000 00C000 00E000 00FF00 28FF28 40FF40 60FF60 80FF80 A0FFA0 C0FFC0 E0FFE0 "
"002110 234123 325F32 418241 50A050 5FC35F 73E173 85FF7A 91FF6E A0FF5F B4FF50 C3FF41 D2FF32 E1FF23 F0FF0F "
"282800 404000 606000 808000 A0A000 C0C000 E0E000 FFFF00 FFFF28 FFFF40 FFFF60 FFFF80 FFFFA0 FFFFC0 FFFFE0 "
//
"442100 00FF55 0055FF FF5500 55FF00 FF0055 5500FF CA8B25 F078F0 F0F078 FF7F00 DD6D01 7AFF00 111111 "
//
"000000 0000AA 00AA00 00AAAA AA0000 AA00AA AAAA00 AAAAAA "
"555555 5555FF 55FF55 55FFFF FF5555 FF55FF FFFF55 FFFFFF "
;
void init_palette(void) {
double gamma;
int usegamma=1;
int i,j;
SDL_Color pal[256];
FILE*fp=0;
const char*v;
optionquery[1]=Q_gamma;
gamma=strtod(xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"0",0);
if(gamma<=0.0 || gamma==1.0) usegamma=0;
optionquery[1]=Q_palette;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
if(v && *v) {
fp=fopen(v,"r");
if(!fp) fatal("Unable to load palette file '%s'\n%m",v);
}
for(i=0;i<256;i++) {
if(fp) {
if(fscanf(fp,"%2hhX%2hhX%2hhX ",&pal[i].r,&pal[i].g,&pal[i].b)!=3) fatal("Invalid palette file\n");
} else {
sscanf(default_palette+i*7,"%2hhX%2hhX%2hhX ",&pal[i].r,&pal[i].g,&pal[i].b);
}
if(usegamma) {
j=(int)(255.0*pow(pal[i].r/255.0,gamma)+0.2); pal[i].r=j<0?0:j>255?255:j;
j=(int)(255.0*pow(pal[i].g/255.0,gamma)+0.2); pal[i].g=j<0?0:j>255?255:j;
j=(int)(255.0*pow(pal[i].b/255.0,gamma)+0.2); pal[i].b=j<0?0:j>255?255:j;
}
}
if(fp) fclose(fp);
SDL_SetColors(screen,pal,0,256);
SDL_SetColors(picts,pal,0,256);
}
void draw_picture(int x,int y,Uint16 img) {
// To be called only when screen is unlocked!
SDL_Rect src={(img&PPRM)*picture_size,(img>>PPRS)*picture_size,picture_size,picture_size};
SDL_Rect dst={x,y,picture_size,picture_size};
SDL_BlitSurface(picts,&src,screen,&dst);
}
void draw_cell(int x,int y) {
// To be called only when screen is unlocked!
Uint32 o;
Class*c;
int i;
SDL_Rect dst={(x-1)*picture_size+left_margin,(y-1)*picture_size,picture_size,picture_size};
if(x<1 || x>64 || y<1 || y>64) return;
SDL_FillRect(screen,&dst,back_color);
o=playfield[y*64+x-65];
while(o!=VOIDLINK) {
if(main_options['e'] || !(objects[o]->oflags&OF_INVISIBLE)) {
c=classes[objects[o]->class];
if(objects[o]->anim && (objects[o]->anim->status&ANISTAT_VISUAL)) i=objects[o]->anim->vimage; else i=objects[o]->image;
if(i<c->nimages) draw_picture((x-1)*picture_size+left_margin,(y-1)*picture_size,c->images[i]&0x7FFF);
}
o=objects[o]->up;
}
}
void draw_selection_rectangle(void) {
Uint16 pitch=screen->pitch;
Uint8*p=screen->pixels+left_margin+(editrect.x0-1)*picture_size+pitch*(editrect.y0-1)*picture_size;
int xr=(editrect.x1+1-editrect.x0)*picture_size-1;
int yr=(editrect.y1+1-editrect.y0)*picture_size-1;
int i;
if(p+xr+yr*pitch>=((Uint8*)screen->pixels)+screen->h*pitch+screen->w) return;
memset(p,0xF7,xr);
memset(p+yr*pitch,0xF7,xr);
for(i=1;i<yr;i++) {
p[i*pitch]=p[i*pitch+xr]=0xF7;
p[i*pitch+1]=p[i*pitch+xr-1]=0xF0;
}
memset(p+pitch+1,0xF0,xr-2);
memset(p+(yr-1)*pitch+1,0xF0,xr-2);
p[0]=p[xr]=p[yr*pitch]=p[yr*pitch+xr]=0xF8;
}
void draw_text(int x,int y,const unsigned char*t,int bg,int fg) {
// To be called only when screen is locked!
int len=strlen(t);
Uint8*pix=screen->pixels;
Uint8*p;
Uint16 pitch=screen->pitch;
int xx,yy;
const unsigned char*f;
if(x+8*len>screen->w) len=(screen->w-x)>>3;
if(len<=0 || y+8>screen->h) return;
pix+=y*pitch+x;
while(*t) {
f=fontdata+(*t<<3);
p=pix;
for(yy=0;yy<8;yy++) {
for(xx=0;xx<8;xx++) p[xx]=(*f<<xx)&128?fg:bg;
p+=pitch;
++f;
}
t++;
if(!--len) return;
pix+=8;
}
}
void draw_text_v(int x,int y,const unsigned char*t,int bg,int fg) {
// Draw text using sizes other than 8x8 and possible multibyte encodings.
// To be called only when screen is locked!
//TODO
}
int measure_text_v(const unsigned char*t,int len) {
// Returns number of character cells of text (t).
// If len is positive then it is the maximum number of bytes to measure.
int r=0;
int c;
while((len<0 || len--) && (c=*t++)) {
r++;
if(c&0x80) r-=(bytewidth[(c&0x7C)>>2]>>((c&3)<<1))&3;
}
return r>0?r:0;
}
int draw_text_line(int x,int y,unsigned char*t,int cur,Uint8**cp) {
// To be called only when screen is locked!
int len=strlen(t);
const unsigned char*s=t;
Uint8*pix=screen->pixels;
Uint8*p;
Uint16 pitch=screen->pitch;
int bg,fg,xx,yy;
char isimg=0;
char e=0;
const unsigned char*f;
if(!*t) return 0;
len=(screen->w-x)>>3;
if(len<=0 || y+8>screen->h) return len<1?1:len;
pix+=y*pitch+x;
for(;;) {
if(!cur) *cp=t;
if(*t==10) {
f=fontdata+(17<<3);
bg=0xF0,fg=0xF1;
e=1;
} else if(!*t) {
f=fontdata+(254<<3);
bg=0xF0,fg=0xF1;
e=1;
} else if(*t<31) {
f=fontdata+(" 01234567?NLC#IBQ?????????????D?"[*t]<<3);
if(*t==14 || *t==30) isimg=1;
bg=0xF3,fg=0xF0;
} else {
if(*t==31 && t[1]) t++;
f=fontdata+(*t<<3);
bg=0xF0,fg=isimg?0xF2:0xF7;
}
if(!cur--) bg^=15,fg^=15;
p=pix;
for(yy=0;yy<8;yy++) {
for(xx=0;xx<8;xx++) p[xx]=(*f<<xx)&128?fg:bg;
p+=pitch;
++f;
}
if(*t=='\\') isimg=0;
if(!--len || e) return t-s;
t++;
pix+=8;
}
}
int draw_text_line_v(int x,int y,unsigned char*t,int cur,Uint8**cp) {
//TODO
}
int measure_text_line_v(unsigned char*t,int len,Uint8**cp) {
//TODO
}
void draw_key(int x,int y,int k,int bg,int fg) {
// To be called only when screen is locked!
Uint8*p=screen->pixels;
Uint16 pitch=screen->pitch;
int xx,yy;
const unsigned char*f;
if(x<0 || y<0 || x+16>screen->w || y+16>screen->h) return;
p+=y*pitch+x;
f=keyicons_bits+(k<<5);
for(yy=0;yy<16;yy++) {
for(xx=0;xx<8;xx++) p[xx]=(*f>>xx)&1?fg:bg;
++f;
for(xx=0;xx<8;xx++) p[xx+8]=(*f>>xx)&1?fg:bg;
p+=pitch;
++f;
}
}
const char*screen_prompt(const char*txt) {
#ifdef CONFIG_FUNCTION_ASK_TEXT
return CONFIG_FUNCTION_ASK_TEXT(screen,txt,367,0);
#else
static char*t=0;
int n=0;
SDL_Rect r={0,0,screen->w,16};
int m=r.w>>3;
SDL_Event ev;
if(!t) {
t=malloc(m+2);
if(!t) fatal("Allocation failed\n");
}
*t=0;
SDL_FillRect(screen,&r,0xF1);
r.y=16;
r.h=1;
SDL_FillRect(screen,&r,0xF8);
SDL_LockSurface(screen);
draw_text(0,0,txt,0xF1,0xFE);
draw_text(0,8,"\xB1",0xF1,0xFB);
SDL_UnlockSurface(screen);
set_cursor(XC_iron_cross);
SDL_Flip(screen);
r.y=8;
r.h=8;
while(SDL_WaitEvent(&ev)) {
switch(ev.type) {
case SDL_QUIT:
SDL_PushEvent(&ev);
return 0;
case SDL_KEYDOWN:
SDL_FillRect(screen,&r,0xF1);
switch(ev.key.keysym.sym) {
case SDLK_RETURN: case SDLK_KP_ENTER:
r.y=0;
r.h=17;
SDL_FillRect(screen,&r,0xF0);
t[n]=0;
return t;
case SDLK_BACKSPACE: case SDLK_DELETE:
if(n) t[n--]=0;
break;
case SDLK_CLEAR:
t[n=0]=0;
break;
case SDLK_INSERT:
if(ev.key.keysym.mod&KMOD_SHIFT) {
const char*s;
FILE*fp;
int c;
paste:
optionquery[1]=Q_pasteCommand;
if((s=xrm_get_resource(resourcedb,optionquery,optionquery,2)) && (fp=popen(s,"r"))) {
for(;;) {
c=fgetc(fp);
if(c=='\t') c=' ';
if(c>=32 && n<m) t[n++]=c;
if(c=='\n' || c<0 || n>=m) break;
}
t[n]=0;
pclose(fp);
}
}
break;
default:
if(ev.key.keysym.sym==SDLK_u && (ev.key.keysym.mod&KMOD_CTRL)) {
t[n=0]=0;
} else if(ev.key.keysym.sym==SDLK_c && (ev.key.keysym.mod&KMOD_CTRL)) {
r.y=0;
r.h=17;
SDL_FillRect(screen,&r,0xF0);
return 0;
} else if(n<m && ev.key.keysym.unicode<127 && ev.key.keysym.unicode>=32 && !(ev.key.keysym.mod&KMOD_CTRL)) {
t[n++]=ev.key.keysym.unicode;
t[n]=0;
}
}
t[n]=0;
SDL_FillRect(screen,&r,0xF1);
SDL_LockSurface(screen);
draw_text(0,8,t,0xF1,0xFF);
draw_text(n<<3,8,"\xB1",0xF1,0xFB);
SDL_UnlockSurface(screen);
SDL_Flip(screen);
break;
case SDL_MOUSEBUTTONDOWN:
if(ev.button.button==2) goto paste;
break;
}
}
return 0;
#endif
}
int screen_message(const char*txt) {
int n=0;
SDL_Rect r={0,0,0,16};
SDL_Event ev;
if(!screen) {
fprintf(stderr," * %s\n",txt);
return 0;
}
r.w=screen->w;
SDL_FillRect(screen,&r,0xF4);
r.y=16;
r.h=1;
SDL_FillRect(screen,&r,0xF8);
SDL_LockSurface(screen);
draw_text(0,0,txt,0xF4,0xFE);
draw_text(0,8,"<Push ENTER to continue>",0xF4,0xF7);
SDL_UnlockSurface(screen);
set_cursor(XC_iron_cross);
SDL_Flip(screen);
r.y=8;
r.h=8;
while(SDL_WaitEvent(&ev)) {
switch(ev.type) {
case SDL_QUIT:
SDL_PushEvent(&ev);
return -1;
case SDL_KEYDOWN:
switch(ev.key.keysym.sym) {
case SDLK_RETURN: case SDLK_KP_ENTER:
r.y=0;
r.h=17;
SDL_FillRect(screen,&r,0xF0);
return 0;
}
break;
}
}
return -1;
}
void draw_lines(int x,int y) {
Uint8*pix;
int pitch;
SDL_Event ev;
int xx=(x-1)*picture_size+left_margin+picture_size/2;
int yy=(y-1)*picture_size+picture_size/2;
set_cursor(XC_cross_reverse);
if(xx>0 && yy>0 && xx<screen->w && yy<screen->h) {
SDL_LockSurface(screen);
pix=screen->pixels;
pitch=screen->pitch;
// Diagonal
for(y=0;y<screen->h;y++) {
x=xx-yy+y;
if(x>1 && x<screen->w-2) memcpy(pix+y*pitch+x-2,"\xF0\xF8\xF7\xF8\xF0",5);
x=xx+yy-y;
if(x>1 && x<screen->w-2) memcpy(pix+y*pitch+x-2,"\xF0\xF8\xF7\xF8\xF0",5);
}
// Vertical
if(xx+2<screen->w) for(y=0;y<screen->h;y++) pix[y*pitch+xx+2]=0xF0;
if(xx+1<screen->w) for(y=0;y<screen->h;y++) pix[y*pitch+xx+1]=0xF8;
for(y=0;y<screen->h;y++) pix[y*pitch+xx]=0xF7;
if(xx-1>0) for(y=0;y<screen->h;y++) pix[y*pitch+xx-1]=0xF8;
if(xx-2>0) for(y=0;y<screen->h;y++) pix[y*pitch+xx-2]=0xF0;
// Horizontal
if(yy+2<screen->h) memset(pix+(yy+2)*pitch,0xF0,screen->w);
if(yy+1<screen->h) memset(pix+(yy+1)*pitch,0xF8,screen->w);
memset(pix+yy*pitch,0xF7,screen->w);
if(yy-1>0) memset(pix+(yy-1)*pitch,0xF8,screen->w);
if(yy-2>0) memset(pix+(yy-2)*pitch,0xF0,screen->w);
SDL_UnlockSurface(screen);
}
SDL_Flip(screen);
while(SDL_WaitEvent(&ev)) switch(ev.type) {
case SDL_KEYDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_KEYUP: case SDL_MOUSEBUTTONUP:
return;
case SDL_QUIT:
SDL_PushEvent(&ev);
return;
}
}
static Uint16 decide_picture_size(int nwantsize,const Uint8*wantsize,const Uint16*havesize,int n) {
int i,j;
if(!nwantsize) fatal("Unable to determine what picture size is wanted\n");
for(i=0;i<nwantsize;i++) if(havesize[j=wantsize[i]]==n) return j;
for(i=*wantsize;i;--i) if(havesize[i]) return i;
fatal("Unable to determine what picture size is wanted\n");
}
static void load_one_picture_sub(FILE*fp,int size,int meth) {
Uint8*p=curpic;
int c,n,t,x,y;
if(meth==15) {
fread(p,size,size,fp);
return;
}
n=t=0;
y=size*size;
while(y--) {
if(!n) {
n=fgetc(fp);
if(n<85) {
// Homogeneous run
n++;
x=fgetc(fp);
if(t==1 && x==c) n*=85; else n++;
c=x;
t=1;
} else if(n<170) {
// Heterogeneous run
n-=84;
t=2;
} else {
// Copy-above run
n-=169;
if(t==3) n*=85;
t=3;
}
}
n--;
if(t==2) c=fgetc(fp);
if(t==3) c=p-curpic>=size?p[-size]:0;
*p++=c;
}
}
static void load_one_picture(FILE*fp,Uint16 img,int alt) {
int h,i,j,k,pitch,which,meth,size,psize,zoom;
Uint8 buf[32];
Uint8*pix;
*buf=fgetc(fp);
j=*buf&15;
fread(buf+1,1,j+(j>>1),fp);
k=0;
zoom=1;
for(i=1;i<=j;i++) if(buf[i]==picture_size) ++k;
if(k) {
psize=picture_size;
} else {
for(zoom=2;zoom<=picture_size;zoom++) if(!(picture_size%zoom)) {
psize=picture_size/zoom;
for(i=1;i<=j;i++) if(buf[i]==psize) ++k;
if(k) break;
}
}
alt%=k;
for(i=1;i<=j;i++) if(buf[i]==psize && !alt--) break;
which=i;
i=1;
while(which--) load_one_picture_sub(fp,size=buf[i],meth=(i==1?*buf>>4:buf[(*buf&15)+1+((i-2)>>1)]>>(i&1?4:0))&15),i++;
if(meth==5 || meth==6) meth^=3;
if(meth==15) meth=0;
SDL_LockSurface(picts);
pitch=picts->pitch;
pix=picts->pixels+((img&PPRM)+pitch*(img>>PPRS))*picture_size;
for(i=0;i<size;i++) {
for(h=0;h<zoom;h++) {
for(j=0;j<size;j++) {
if(meth&1) j=size-j-1;
if(meth&2) i=size-i-1;
for(k=0;k<zoom;k++) *pix++=curpic[meth&4?j*size+i:i*size+j];
if(meth&1) j=size-j-1;
if(meth&2) i=size-i-1;
}
pix+=pitch-picture_size;
}
}
SDL_UnlockSurface(picts);
}
static void pic_shift_right(SDL_Rect*r,int sh) {
Uint8*p;
int y;
char buf[256];
sh%=r->w;
if(!sh) return;
SDL_LockSurface(picts);
p=picts->pixels+r->y*picts->pitch+r->x;
for(y=0;y<r->h;y++) {
memcpy(buf,p,r->w);
memcpy(p,buf+r->w-sh,sh);
memcpy(p+sh,buf,r->w-sh);
p+=picts->pitch;
}
SDL_UnlockSurface(picts);
}
static void pic_shift_down(SDL_Rect*r,int sh) {
Uint8*p;
int y;
char b1[256];
char b2[256];
sh%=r->h;
SDL_LockSurface(picts);
// This implementation is probably inefficient.
while(sh--) {
p=picts->pixels+r->y*picts->pitch+r->x;
memcpy(b1,p+picts->pitch*(r->h-1),r->w);
for(y=0;y<r->h;y++) {
memcpy(b2,p,r->w);
memcpy(p,b1,r->w);
p+=picts->pitch;
memcpy(b1,b2,r->w);
}
}
SDL_UnlockSurface(picts);
}
static void pic_orientation(SDL_Rect*r,Uint8 m) {
Uint8*d=malloc(r->w*r->h);
Uint8*p;
Uint8*q;
int i,j;
if(!d) fatal("Allocation failed\n");
SDL_LockSurface(picts);
p=picts->pixels+r->y*picts->pitch+r->x;
q=d;
for(i=0;i<r->h;i++) {
for(j=0;j<r->w;j++) {
if(m&1) j=r->w-j-1;
if(m&2) i=r->h-i-1;
*q++=p[m&4?j*picts->pitch+i:i*picts->pitch+j];
if(m&1) j=r->w-j-1;
if(m&2) i=r->h-i-1;
}
}
q=d;
for(i=0;i<r->h;i++) {
memcpy(p,q,r->w);
p+=picts->pitch;
q+=r->w;
}
SDL_UnlockSurface(picts);
free(d);
}
static void load_dependent_picture(FILE*fp,Sint32 sz,Uint16 img,int alt) {
SDL_Rect src={0,0,picture_size,picture_size};
SDL_Rect dst={(img&PPRM)*picture_size,(img>>PPRS)*picture_size,picture_size,picture_size};
sqlite3_stmt*st;
int c,i,x,y;
char nam[128];
Uint8 buf[512];
Uint8*p;
if(sqlite3_prepare_v2(userdb,"SELECT `ID` FROM `PICTURES` WHERE `NAME` = ?1 AND NOT `DEPENDENT`;",-1,&st,0))
fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
i=0;
while(sz) {
c=fgetc(fp);
if(c<32) break;
if(i<127) nam[i++]=c;
sz--;
}
if(sz) ungetc(c,fp);
if(i) {
sqlite3_bind_text(st,1,nam,i,0);
i=sqlite3_step(st);
if(i==SQLITE_DONE) {
fprintf(stderr,"Cannot find base picture for a dependent picture; ignoring\n");
sqlite3_finalize(st);
return;
} else if(i!=SQLITE_ROW) {
fatal("SQL error (%d): %s\n",i,sqlite3_errmsg(userdb));
}
i=sqlite3_column_int(st,0);
sqlite3_reset(st);
src.x=(i&PPRM)*picture_size;
src.y=(i>>PPRS)*picture_size;
SDL_SetColorKey(picts,0,0);
SDL_BlitSurface(picts,&src,picts,&dst);
} else {
SDL_FillRect(picts,&dst,0);
}
while(sz-->0) switch(c=fgetc(fp)) {
case 0 ... 7: // Flip/rotate
pic_orientation(&dst,c);
break;
case 8: // *->* *->* *->*
c=fgetc(fp);
sz-=c+1;
fread(buf,1,c&255,fp);
SDL_LockSurface(picts);
p=picts->pixels+((img&PPRM)+picts->pitch*(img>>PPRS))*picture_size;
for(y=0;y<picture_size;y++) {
for(x=0;x<picture_size;x++) {
for(i=0;i<c;i+=2) {
if(p[x]==buf[i]) {
p[x]=buf[i+1];
break;
}
}
}
p+=picts->pitch;
}
SDL_UnlockSurface(picts);
break;
case 9: // *->*->*->*->*
c=fgetc(fp);
sz-=c+1;
fread(buf,1,c&255,fp);
SDL_LockSurface(picts);
p=picts->pixels+((img&PPRM)+picts->pitch*(img>>PPRS))*picture_size;
for(y=0;y<picture_size;y++) {
for(x=0;x<picture_size;x++) {
for(i=0;i<c-1;i++) {
if(p[x]==buf[i]) {
p[x]=buf[i+1];
break;
}
}
}
p+=picts->pitch;
}
SDL_UnlockSurface(picts);
break;
case 10: // *<->* *<->* *<->*
c=fgetc(fp);
sz-=c+1;
fread(buf,1,c&255,fp);
SDL_LockSurface(picts);
p=picts->pixels+((img&PPRM)+picts->pitch*(img>>PPRS))*picture_size;
for(y=0;y<picture_size;y++) {
for(x=0;x<picture_size;x++) {
for(i=0;i<c;i+=2) {
if(p[x]==buf[i]) {
p[x]=buf[i+1];
break;
} else if(p[x]==buf[i+1]) {
p[x]=buf[i];
break;
}
}
}
p+=picts->pitch;
}
SDL_UnlockSurface(picts);
break;
case 11: // Overlay
SDL_SetColorKey(picts,SDL_SRCCOLORKEY,0);
i=0;
while(sz>0) {
c=fgetc(fp);
if(c<32) break;
if(i<127) nam[i++]=c;
sz--;
}
if(sz) ungetc(c,fp);
sqlite3_bind_text(st,1,nam,i,0);
i=sqlite3_step(st);
if(i==SQLITE_DONE) {
fprintf(stderr,"Cannot find overlay for a dependent picture; ignoring\n");
break;
} else if(i!=SQLITE_ROW) {
fatal("SQL error (%d): %s\n",i,sqlite3_errmsg(userdb));
}
i=sqlite3_column_int(st,0);
sqlite3_reset(st);
src.x=(i&PPRM)*picture_size;
src.y=(i>>PPRS)*picture_size;
SDL_BlitSurface(picts,&src,picts,&dst);
break;
case 12 ... 15: // Shift (up/down/right/left)
while(sz>0) {
x=fgetc(fp);
y=fgetc(fp);
sz-=2;
if(y && !(picture_size%y)) {
if(c==12) pic_shift_down(&dst,picture_size-((x&127)*picture_size)/y);
if(c==13) pic_shift_down(&dst,((x&127)*picture_size)/y);
if(c==14) pic_shift_right(&dst,((x&127)*picture_size)/y);
if(c==15) pic_shift_right(&dst,picture_size-((x&127)*picture_size)/y);
break;
}
if(x&128) break;
}
while(sz>0 && x<128) {
x=fgetc(fp);
fgetc(fp);
sz-=2;
}
break;
case 16: // Hue/shade
fread(buf,1,2,fp);
c=(Sint8)buf[1];
sz-=2;
SDL_LockSurface(picts);
p=picts->pixels+((img&PPRM)+picts->pitch*(img>>PPRS))*picture_size;
for(y=0;y<picture_size;y++) {
for(x=0;x<picture_size;x++) {
if(p[x] && p[x]<=225) {
i=(p[x]-1)%15+c;
p[x]=15*(((p[x]-1)/15+buf[0])%15)+1;
if(i<0) p[x]=1; else if(i>14) p[x]=15; else p[x]+=i;
}
}
p+=picts->pitch;
}
SDL_UnlockSurface(picts);
break;
default:
fprintf(stderr,"Unrecognized command in dependent picture (%d)\n",c);
goto done;
}
if(sz<-1) fprintf(stderr,"Lump size of dependent picture is too short\n");
done: sqlite3_finalize(st);
}
static int find_multidependent(sqlite3_stmt*st,FILE*fp,int len,int npic) {
sqlite3_stmt*s1;
int at=4;
long rew=ftell(fp);
Uint8*mem=malloc(len+1);
int i,j,zt;
if(!mem) fatal("Allocation failed\n");
fread(mem,1,len,fp);
mem[len]=0;
fseek(fp,rew+len,SEEK_SET);
if(sqlite3_exec(userdb,"CREATE TEMPORARY TABLE IF NOT EXISTS `_MUL`(`X`,`Y`,`N`,`Z`);",0,0,0))
fatal("SQL error: %s\n",sqlite3_errmsg(userdb));
if(sqlite3_prepare_v2(userdb,"INSERT INTO `_MUL`(`X`,`Y`,`N`,`Z`) VALUES(?1,?2,SUBSTR('._-',?5,1)||?3,?4);",-1,&s1,0))
fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
sqlite3_bind_int(s1,2,0);
sqlite3_bind_text(s1,3,"",0,0);
sqlite3_bind_int(s1,5,4);
for(i=0;i<4;i++) if((mem[i]&128) || !mem[i]) {
sqlite3_reset(s1);
sqlite3_bind_int(s1,1,i);
sqlite3_bind_blob(s1,4,"",i?0:1,0);
while(sqlite3_step(s1)==SQLITE_ROW);
}
sqlite3_reset(s1);
sqlite3_bind_int(s1,1,0);
for(i=0;i<(*mem&63);i++) {
if(at>=len) fatal("Malformed multidependent picture lump\n");
sqlite3_reset(s1);
sqlite3_bind_int(s1,2,i+1);
j=strlen(mem+at);
sqlite3_bind_text(s1,3,mem+at,j,0);
sqlite3_bind_blob(s1,4,mem+at,j+1,0);
at+=j+1;
while(sqlite3_step(s1)==SQLITE_ROW);
}
zt=at;
for(j=1;j<3;j++) for(i=0;i<(mem[j]&63);i++) {
if(zt+2>=len) fatal("Malformed multidependent picture lump\n");
zt+=((mem[zt+1]>>3)&7)+2;
}
for(j=1;j<3;j++) for(i=0;i<(mem[j]&63);i++) {
sqlite3_reset(s1);
sqlite3_bind_int(s1,1,j);
sqlite3_bind_int(s1,2,i+1);
sqlite3_bind_text(s1,3,mem+at+2,(mem[at+1]>>3)&7,0);
if(zt+mem[at]+((mem[at+1]&7)<<8)>len) fatal("Malformed multidependent picture lump\n");
sqlite3_bind_blob(s1,4,mem+zt,mem[at]|((mem[at+1]&7)<<8),0);
sqlite3_bind_int(s1,5,4-(mem[at+1]>>6));
zt+=mem[at]|((mem[at+1]&7)<<8);
at+=((mem[at+1]>>3)&7)+2;
while(sqlite3_step(s1)==SQLITE_ROW);
}
sqlite3_finalize(s1);
free(mem);
if(sqlite3_prepare_v2(userdb,"SELECT A.N||B.N||C.N||D.N,BCAT(A.Z,B.Z,C.Z,D.Z) FROM _MUL A,_MUL B,_MUL C,_MUL D WHERE A.X=0 AND B.X=1 AND C.X=2 AND D.X=3 AND B.Y+C.Y+D.Y<>0;",-1,&s1,0))
fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
while((j=sqlite3_step(s1))==SQLITE_ROW) {
if(npic++==32768) fatal("Too many pictures\n");
sqlite3_reset(st);
sqlite3_bind_int(st,1,npic);
sqlite3_bind_value(st,2,sqlite3_column_value(s1,0));
sqlite3_bind_value(st,5,sqlite3_column_value(s1,1));
while(sqlite3_step(st)==SQLITE_ROW);
}
if(j!=SQLITE_DONE) fatal("SQL error (%d): %s\n",j,sqlite3_errmsg(userdb));
sqlite3_finalize(s1);
if(sqlite3_exec(userdb,"DELETE FROM `_MUL`;",0,0,0))
fatal("SQL error: %s\n",sqlite3_errmsg(userdb));
return npic;
}
static void load_multidependent(int n,const char*data,int len) {
FILE*fp=fmemopen((char*)data,len,"r");
if(!fp) fatal("Allocation failed\n");
load_dependent_picture(fp,len,n,0);
fclose(fp);
}
void load_pictures(void) {
sqlite3_stmt*st=0;
FILE*fp;
Uint8 wantsize[32];
Uint8 nwantsize=0;
Uint8 altImage;
Uint8 havesize1[256];
Uint16 havesize[256];
char*nam=sqlite3_mprintf("%s.xclass",basefilename);
const char*v;
int i,j,n;
if(!nam) fatal("Allocation failed\n");
printStatus("Loading pictures...\n");
fp=main_options['z']?composite_slice(".xclass",1):fopen(nam,"r");
if(!fp) fatal("Failed to open xclass file (%m)\n");
sqlite3_free(nam);
optionquery[1]=Q_altImage;
altImage=strtol(xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"0",0,10);
optionquery[1]=Q_imageSize;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
if(v) while(nwantsize<32) {
i=j=0;
sscanf(v," %d %n",&i,&j);
if(!j) break;
if(i<2 || i>255) fatal("Invalid picture size %d\n",i);
wantsize[nwantsize++]=i;
v+=j;
}
if(n=sqlite3_exec(userdb,"BEGIN;",0,0,0)) fatal("SQL error (%d): %s\n",n,sqlite3_errmsg(userdb));
if(sqlite3_prepare_v2(userdb,"INSERT INTO `PICTURES`(`ID`,`NAME`,`OFFSET`,`DEPENDENT`,`MISC`) VALUES(?1,?2,?3,?4,?5);",-1,&st,0))
fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
nam=malloc(256);
if(!nam) fatal("Allocation failed\n");
n=0;
memset(havesize,0,256*sizeof(Uint16));
while(!feof(fp)) {
i=0;
while(j=fgetc(fp)) {
if(j==EOF) goto nomore1;
if(i<255) nam[i++]=j;
}
nam[i]=0;
if(i>4 && (!memcmp(".IMG",nam+i-4,4) || !memcmp(".DEP",nam+i-4,4))) {
if(nam[i-3]=='I') j=1; else j=0;
if(n++==32768) fatal("Too many pictures\n");
sqlite3_reset(st);
sqlite3_bind_int(st,1,n);
sqlite3_bind_text(st,2,nam,i-4,SQLITE_TRANSIENT);
sqlite3_bind_int64(st,3,ftell(fp)+4);
sqlite3_bind_int(st,4,j^1);
sqlite3_bind_null(st,5);
while((i=sqlite3_step(st))==SQLITE_ROW);
if(i!=SQLITE_DONE) fatal("SQL error (%d): %s\n",i,sqlite3_errmsg(userdb));
} else if(i>4 && !memcmp(".MUL",nam+i-4,4)) {
j=2;
} else {
j=0;
}
i=fgetc(fp)<<16;
i|=fgetc(fp)<<24;
i|=fgetc(fp)<<0;
i|=fgetc(fp)<<8;
if(j==1 && i>1) {
memset(havesize1,0,256);
i-=j=fgetc(fp)&15;
while(j--) havesize1[fgetc(fp)&255]=1;
fseek(fp,i-1,SEEK_CUR);
for(i=1;i<256;i++) if(havesize1[i]) for(j=i+i;j<256;j+=i) havesize1[j]=1;
for(j=1;j<256;j++) havesize[j]+=havesize1[j];
} else if(j==2 && i>4) {
sqlite3_reset(st);
sqlite3_bind_int64(st,3,ftell(fp));
sqlite3_bind_int(st,4,1);
n=find_multidependent(st,fp,i,n);
} else {
fseek(fp,i,SEEK_CUR);
}
}
nomore1:
if(!n) fatal("Cannot find any pictures in this puzzle set\n");
free(nam);
sqlite3_finalize(st);
rewind(fp);
for(i=255;i;--i) if(havesize[i]) {
curpic=malloc(i*i);
break;
}
if(!curpic) fatal("Allocation failed\n");
picture_size=decide_picture_size(nwantsize,wantsize,havesize,n);
if(main_options['x']) goto done;
if(sqlite3_prepare_v2(userdb,"SELECT `ID`, `OFFSET` FROM `PICTURES` WHERE NOT `DEPENDENT`;",-1,&st,0))
fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
optionquery[1]=Q_screenFlags;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
i=v&&strchr(v,'h');
picts=SDL_CreateRGBSurface((i?SDL_HWSURFACE:SDL_SWSURFACE)|SDL_SRCCOLORKEY,picture_size<<PPRS,picture_size*((n+PPR)>>PPRS),8,0,0,0,0);
if(!picts) fatal("Error allocating surface for pictures: %s\n",SDL_GetError());
init_palette();
for(i=0;i<n;i++) {
if((j=sqlite3_step(st))!=SQLITE_ROW) {
if(j==SQLITE_DONE) break;
fatal("SQL error (%d): %s\n",j,sqlite3_errmsg(userdb));
}
fseek(fp,sqlite3_column_int64(st,1),SEEK_SET);
load_one_picture(fp,sqlite3_column_int(st,0),altImage);
}
sqlite3_finalize(st);
if(sqlite3_prepare_v2(userdb,"SELECT `ID`, `OFFSET`, `MISC` FROM `PICTURES` WHERE `DEPENDENT`;",-1,&st,0))
fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
for(i=0;i<n;i++) {
if((j=sqlite3_step(st))!=SQLITE_ROW) {
if(j==SQLITE_DONE) break;
fatal("SQL error (%d): %s\n",j,sqlite3_errmsg(userdb));
}
if(sqlite3_column_type(st,2)==SQLITE_NULL) {
fseek(fp,sqlite3_column_int64(st,1)-4,SEEK_SET);
j=fgetc(fp)<<16;
j|=fgetc(fp)<<24;
j|=fgetc(fp);
j|=fgetc(fp)<<8;
load_dependent_picture(fp,j,sqlite3_column_int(st,0),0);
} else {
v=sqlite3_column_blob(st,2);
j=sqlite3_column_bytes(st,2);
load_multidependent(sqlite3_column_int(st,0),v,j);
}
}
sqlite3_finalize(st);
fclose(fp);
SDL_SetColorKey(picts,SDL_SRCCOLORKEY|SDL_RLEACCEL,0);
done:
if(n=sqlite3_exec(userdb,"COMMIT;",0,0,0)) fatal("SQL error (%d): %s\n",n,sqlite3_errmsg(userdb));
printStatus("Done\n");
}
void init_screen(void) {
const char*v;
int w,h,i;
if(main_options['x']) return;
if(!fontdata) fontdata=pcfont;
optionquery[1]=Q_screenWidth;
w=strtol(xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"800",0,10);
optionquery[1]=Q_screenHeight;
h=strtol(xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"600",0,10);
optionquery[1]=Q_screenFlags;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"";
if(SDL_Init(SDL_INIT_VIDEO|(strchr(v,'z')?SDL_INIT_NOPARACHUTE:0)|SDL_INIT_TIMER)) fatal("Error initializing SDL: %s\n",SDL_GetError());
#ifdef CONFIG_EXTRA_SCREEN_INIT
do{ CONFIG_EXTRA_SCREEN_INIT }while(0);
#endif
atexit(SDL_Quit);
i=0;
while(*v) switch(*v++) {
case 'd': i|=SDL_DOUBLEBUF; break;
case 'f': i|=SDL_FULLSCREEN; break;
case 'h': i|=SDL_HWSURFACE; break;
case 'n': i|=SDL_NOFRAME; break;
case 'p': i|=SDL_HWPALETTE; break;
case 'r': i|=SDL_RESIZABLE; break;
case 'y': i|=SDL_ASYNCBLIT; break;
}
if(!(i&SDL_HWSURFACE)) i|=SDL_SWSURFACE;
screen=SDL_SetVideoMode(w,h,8,i);