forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasegraph.cpp
More file actions
1125 lines (903 loc) · 27.2 KB
/
Copy pathbasegraph.cpp
File metadata and controls
1125 lines (903 loc) · 27.2 KB
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
unsigned backcolor = 0;
unsigned bordcolor = 0;
unsigned forecolor = 0xFFFFFF;
int utfsize(char c) {
unsigned char cu = c;
if(cu < 128) return 1;
if(cu < 224) return 2;
if(cu < 0xF0) return 3;
return 4;
}
bool eqs(const char* x, const char* y) {
return *y? *x==*y?eqs(x+1,y+1):false:true;
}
int getnext(const char* s, int& i) {
int siz = utfsize(s[i]);
// if(fontdeb) printf("s=%s i=%d siz=%d\n", s, i, siz);
if(siz == 1) return s[i++];
for(int k=0; k<NUMEXTRA; k++)
if(eqs(s+i, natchars[k])) {
i += siz; return 128+k;
}
printf("Unknown character in: '%s'\n", s);
i ++; return '?';
}
#if CAP_SDLTTF
TTF_Font *font[256];
#endif
#if CAP_SDL
SDL_Surface *s;
int ZZ;
int& qpixel(SDL_Surface *surf, int x, int y) {
if(x<0 || y<0 || x >= surf->w || y >= surf->h) return ZZ;
char *p = (char*) surf->pixels;
p += y * surf->pitch;
int *pi = (int*) (p);
return pi[x];
}
int qpixel3(SDL_Surface *surf, int x, int y) {
if(x<0 || y<0 || x >= surf->w || y >= surf->h) return ZZ;
char *p = (char*) surf->pixels;
p += y * surf->pitch;
p += x;
int *pi = (int*) (p);
return pi[0];
}
#endif
#if CAP_SDLTTF
void loadfont(int siz) {
if(!font[siz]) {
font[siz] = TTF_OpenFont(ISWEB ? "sans-serif" : HYPERPATH "DejaVuSans-Bold.ttf", siz);
// Destination set by ./configure (in the GitHub repository)
#ifdef FONTDESTDIR
if (font[siz] == NULL) {
font[siz] = TTF_OpenFont(FONTDESTDIR, siz);
}
#endif
if (font[siz] == NULL) {
printf("error: Font file not found\n");
exit(1);
}
}
}
#endif
#if !ISFAKEMOBILE && !ISANDROID & !ISIOS
int textwidth(int siz, const string &str) {
if(size(str) == 0) return 0;
#if CAP_SDLTTF
loadfont(siz);
int w, h;
TTF_SizeUTF8(font[siz], str.c_str(), &w, &h);
// printf("width = %d [%d]\n", w, size(str));
return w;
#elif CAP_GL
return gl_width(siz, str.c_str());
#else
return 0;
#endif
}
#endif
#if ISIOS
int textwidth(int siz, const string &str) {
return mainfont->getSize(str, siz / 36.0).width;
}
#endif
int darkenedby(int c, int lev) {
for(int i=0; i<lev; i++)
c = ((c & 0xFEFEFE) >> 1);
return c;
}
int darkened(int c) {
#ifdef EXTRA_FADEOUT
c = gradient(backcolor, c, 0, extra::fadeout, 1);
#endif
if(inmirrorcount&1)
c = gradient(c, winf[waMirror].color, 0, 0.5, 1);
else if(inmirrorcount)
c = gradient(c, winf[waCloud].color, 0, 0.5, 1);
for(int i=0; i<darken; i++)
c = ((c & 0xFEFEFE) >> 1) + ((backcolor & 0xFEFEFE) >> 1);
return c;
}
int darkena(int c, int lev, int a) {
return (darkenedby(c, lev) << 8) + a;
}
#if !CAP_GL
void setcameraangle(bool b) { }
#else
bool cameraangle_on;
void setcameraangle(bool b) {
if(cameraangle_on != b) {
glMatrixMode(GL_PROJECTION);
cameraangle_on = b;
ld cam = vid.camera_angle * M_PI / 180;
GLfloat cc = cos(cam);
GLfloat ss = sin(cam * (b?1:-1));
GLfloat yzspin[16] = {
1, 0, 0, 0,
0, cc, ss, 0,
0, -ss, cc, 0,
0, 0, 0, 1
};
glMultMatrixf(yzspin);
}
}
void selectEyeGL(int ed) {
DEBB(DF_GRAPH, (debugfile,"selectEyeGL\n"));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glTranslatef((vid.xcenter*2.)/vid.xres - 1, 1 - (vid.ycenter*2.)/vid.yres, 0);
if(pmodel) {
vid.scrdist = 4 * vid.radius;
// simulate glOrtho
GLfloat ortho[16] = {
GLfloat(2. / vid.xres), 0, 0, 0,
0, GLfloat(-2. / vid.yres), 0, 0,
0, 0, GLfloat(.4 / vid.scrdist), 0,
0, 0, 0, 1};
vid.scrdist = -vid.scrdist;
glMultMatrixf(ortho);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
else {
float ve = ed*vid.eye;
ve *= 2; // vid.xres; ve /= vid.radius;
if(ve)
glTranslatef(-(ve * vid.radius) * (vid.alpha - (vid.radius*1./vid.xres) * vid.eye) / vid.xres, 0, 0);
float lowdepth = .1;
float hidepth = 1e9;
// simulate glFrustum
GLfloat frustum[16] = {
GLfloat(vid.yres * 1./vid.xres), 0, 0, 0,
0, 1, 0, 0,
0, 0, -(hidepth+lowdepth)/(hidepth-lowdepth), -1,
0, 0, -2*lowdepth*hidepth/(hidepth-lowdepth), 0};
glMultMatrixf(frustum);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat sc = vid.radius / (vid.yres/2.);
GLfloat mat[16] = {sc,0,0,0, 0,-sc,0,0, 0,0,-1,0, 0,0, 0,1};
glMultMatrixf(mat);
if(ve) glTranslatef(ve, 0, vid.eye);
vid.scrdist = vid.yres * sc / 2;
}
cameraangle_on = false;
}
void selectEyeMask(int ed) {
if(ed == 0) {
glColorMask( GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE );
}
else if(ed == 1) {
glColorMask( GL_TRUE,GL_FALSE,GL_FALSE,GL_TRUE );
}
else if(ed == -1) {
glColorMask( GL_FALSE,GL_TRUE,GL_TRUE,GL_TRUE );
}
}
void setGLProjection() {
DEBB(DF_GRAPH, (debugfile,"setGLProjection\n"));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
unsigned char *c = (unsigned char*) (&backcolor);
glClearColor(c[2] / 255.0, c[1] / 255.0, c[0]/255.0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
if(vid.antialias & AA_LINES) {
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
}
else glDisable(GL_LINE_SMOOTH);
glLineWidth(vid.linewidth);
#if !ISMOBILE
if(vid.antialias & AA_POLY) {
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
else glDisable(GL_POLYGON_SMOOTH);
#endif
//glLineWidth(1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if(pmodel == mdBall || pmodel == mdHyperboloid) {
#ifdef GL_ES
glClearDepthf(1.0f);
#else
glClearDepth(1.0f);
#endif
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
else
glDisable(GL_DEPTH_TEST);
selectEyeGL(0);
}
#if CAP_GLFONT
struct glfont_t {
GLuint * textures; // Holds The Texture Id's
//GLuint list_base; // Holds The First Display List ID
int widths[128+NUMEXTRA];
int heights[128+NUMEXTRA];
float tx[128+NUMEXTRA];
float ty[128+NUMEXTRA];
};
glfont_t *glfont[256];
inline int next_p2 (int a )
{
int rval=1;
// rval<<=1 Is A Prettier Way Of Writing rval*=2;
while(rval<a) rval<<=1;
return rval;
}
void glError(const char* GLcall, const char* file, const int line) {
GLenum errCode = glGetError();
if(errCode!=GL_NO_ERROR) {
// fprintf(stderr, "OPENGL ERROR #%i: in file %s on line %i :: %s\n",errCode,file, line, GLcall);
}
}
void sdltogl(SDL_Surface *txt, glfont_t& f, int ch) {
#if CAP_TABFONT
int otwidth, otheight, tpix[3000], tpixindex = 0;
loadCompressedChar(otwidth, otheight, tpix);
#else
int otwidth = txt->w;
int otheight = txt->h;
#endif
int twidth = next_p2( otwidth );
int theight = next_p2( otheight );
#if CAP_TABFONT
int expanded_data[twidth * theight];
#else
Uint16 expanded_data[twidth * theight];
#endif
for(int j=0; j <theight;j++) for(int i=0; i < twidth; i++) {
#if CAP_TABFONT
expanded_data[(i+j*twidth)] = (i>=otwidth || j>=otheight) ? 0 : tpix[tpixindex++];
#else
expanded_data[(i+j*twidth)] =
((i>=txt->w || j>=txt->h) ? 0 : ((qpixel(txt, i, j)>>24)&0xFF) * 0x100) | 0x00FF;
#endif
}
f.widths[ch] = otwidth;
f.heights[ch] = otheight;
glBindTexture( GL_TEXTURE_2D, f.textures[ch]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, twidth, theight, 0,
#if CAP_TABFONT
GL_RGBA, GL_UNSIGNED_BYTE,
#else
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
#endif
expanded_data );
float x=(float)otwidth / (float)twidth;
float y=(float)otheight / (float)theight;
f.tx[ch] = x;
f.ty[ch] = y;
}
void init_glfont(int size) {
if(glfont[size]) return;
DEBB(DF_INIT, (debugfile,"init GL font: %d\n", size));
#if !CAP_TABFONT
loadfont(size);
if(!font[size]) return;
#endif
glfont[size] = new glfont_t;
glfont_t& f(*(glfont[size]));
f.textures = new GLuint[128+NUMEXTRA];
//f.list_base = glGenLists(128);
glGenTextures( 128+NUMEXTRA, f.textures );
if(0) for(int i=0; i<128+NUMEXTRA; i++)
printf("texture %d = %d\n", i, f.textures[i]);
#if !CAP_TABFONT
char str[2]; str[1] = 0;
SDL_Color white;
white.r = white.g = white.b = 255;
#endif
// glListBase(0);
#if CAP_TABFONT
resetTabFont();
#endif
for(int ch=1;ch<128+NUMEXTRA;ch++) {
if(ch<32) continue;
#if CAP_TABFONT
sdltogl(NULL, f, ch);
#else
SDL_Surface *txt;
if(ch < 128) {
str[0] = ch;
txt = TTF_RenderText_Blended(font[size], str, white);
}
else {
txt = TTF_RenderUTF8_Blended(font[size], natchars[ch-128], white);
}
if(txt == NULL) continue;
#if CAP_CREATEFONT
generateFont(ch, txt);
#endif
sdltogl(txt, f, ch);
SDL_FreeSurface(txt);
#endif
}
#if CAP_CREATEFONT
printf("#define NUMEXTRA %d\n", NUMEXTRA);
#define DEMACRO(x) #x
printf("#define NATCHARS " DEMACRO(NATCHARS) "\n");
#endif
//printf("init size=%d ok\n", size);
GLERR("initfont");
}
GLfloat tver[24];
int gl_width(int size, const char *s) {
int gsiz = size;
if(size > vid.fsize || size > 72) gsiz = 72;
#if CAP_FIXEDSIZE
gsiz = CAP_FIXEDSIZE;
#endif
init_glfont(gsiz);
if(!glfont[gsiz]) return 0;
glfont_t& f(*glfont[gsiz]);
int x = 0;
for(int i=0; s[i];) {
int tabid = getnext(s,i);
x += f.widths[tabid] * size/gsiz;
}
return x;
}
bool gl_print(int x, int y, int shift, int size, const char *s, int color, int align) {
int gsiz = size;
if(size > vid.fsize || size > 72) gsiz = 72;
#if CAP_FIXEDSIZE
gsiz = CAP_FIXEDSIZE;
#endif
init_glfont(gsiz);
if(!glfont[gsiz]) return false;
glfont_t& f(*glfont[gsiz]);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glcolor2((color << 8) | 0xFF);
int tsize = 0;
for(int i=0; s[i];) {
tsize += f.widths[getnext(s,i)] * size/gsiz;
}
x -= tsize * align / 16;
y += f.heights[32] * size / (gsiz*2);
int ysiz = f.heights[32] * size / gsiz;
bool clicked = (mousex >= x && mousey <= y && mousex <= x+tsize && mousey >= y-ysiz);
/* extern bool markcorner;
if(clicked && markcorner) {
markcorner = false;
int w = tsize, h = -ysiz;
displaystr(x, y, 1, 10, "X", 0xFFFFFF, 8);
displaystr(x+w, y, 1, 10, "X", 0xFFFFFF, 8);
displaystr(x, y+h, 1, 10, "X", 0xFFFFFF, 8);
displaystr(x+w, y+h, 1, 10, "X", 0xFFFFFF, 8);
markcorner = true;
} */
for(int i=0; s[i];) {
// glListBase(f.list_base);
// glCallList(s[i]); // s[i]);
int tabid = getnext(s,i);
float fx=f.tx[tabid];
float fy=f.ty[tabid];
int wi = f.widths[tabid] * size/gsiz;
int hi = f.heights[tabid] * size/gsiz;
GLERR("pre-print");
for(int ed = (vid.goteyes && shift)?-1:0; ed<2; ed+=2) {
glPushMatrix();
glTranslatef(x-ed*shift-vid.xcenter,y-vid.ycenter, vid.scrdist);
selectEyeMask(ed);
glBindTexture(GL_TEXTURE_2D, f.textures[tabid]);
#if 1
tver[1] = tver[10] = -hi;
tver[6] = tver[9] = wi;
tver[12+4] = tver[12+7] = fy;
tver[12+6] = tver[12+9] = fx;
activateVertexArray(tver, 8);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3, GL_FLOAT, 0, &tver[12]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#else
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2f(0, -hi);
glTexCoord2d(0,fy); glVertex2f(0, 0);
glTexCoord2d(fx,fy); glVertex2f(wi, 0);
glTexCoord2d(fx,0); glVertex2f(wi, -hi);
glEnd();
#endif
glPopMatrix();
}
if(vid.goteyes) selectEyeMask(0);
GLERR("print");
// int tabid = s[i];
x += wi;
/*
printf("point %d,%d\n", x, y);
glBegin(GL_POINTS);
glVertex3f(rand() % 100 - rand() % 100, rand() % 100 - rand() % 100, 100);
glEnd(); */
}
glDisable(GL_TEXTURE_2D);
return clicked;
}
#endif
purehookset hooks_resetGL;
void resetGL() {
DEBB(DF_INIT, (debugfile,"reset GL\n"));
callhooks(hooks_resetGL);
#if CAP_GLFONT
for(int i=0; i<128; i++) if(glfont[i]) {
delete glfont[i];
glfont[i] = NULL;
}
#endif
buildpolys();
}
#endif
#if CAP_XGD
vector<int> graphdata;
void gdpush(int t) {
graphdata.push_back(t);
}
bool displaychr(int x, int y, int shift, int size, char chr, int col) {
gdpush(2); gdpush(x); gdpush(y); gdpush(8);
gdpush(col); gdpush(size); gdpush(0);
gdpush(1); gdpush(chr);
return false;
}
void gdpush_utf8(const string& s) {
int g = (int) graphdata.size(), q = 0;
gdpush((int) s.size()); for(int i=0; i<size(s); i++) {
#if ISANDROID
unsigned char uch = (unsigned char) s[i];
if(uch >= 192 && uch < 224) {
int u = ((s[i] - 192)&31) << 6;
i++;
u += (s[i] - 128) & 63;
gdpush(u); q++;
}
else
#endif
{
gdpush(s[i]); q++;
}
}
graphdata[g] = q;
}
bool displayfr(int x, int y, int b, int size, const string &s, int color, int align) {
gdpush(2); gdpush(x); gdpush(y); gdpush(align);
gdpush(color); gdpush(size); gdpush(b);
gdpush_utf8(s);
int mx = mousex - x;
int my = mousey - y;
int len = textwidth(size, s);
return
mx >= -len*align/32 && mx <= +len*(16-align)/32 &&
my >= -size*3/4 && my <= +size*3/4;
}
bool displaystr(int x, int y, int shift, int size, const string &s, int color, int align) {
return displayfr(x,y,0,size,s,color,align);
}
bool displaystr(int x, int y, int shift, int size, char const *s, int color, int align) {
return displayfr(x,y,0,size,s,color,align);
}
#else
bool displaystr(int x, int y, int shift, int size, const char *str, int color, int align) {
if(strlen(str) == 0) return false;
if(size < 4 || size > 255) {
return false;
}
#if CAP_GLFONT
if(vid.usingGL) return gl_print(x, y, shift, size, str, color, align);
#endif
#if !CAP_SDLTTF
static bool towarn = true;
if(towarn) towarn = false, printf("WARNING: NOTTF works only with OpenGL!\n");
return false;
#else
SDL_Color col;
col.r = (color >> 16) & 255;
col.g = (color >> 8 ) & 255;
col.b = (color >> 0 ) & 255;
col.r >>= darken; col.g >>= darken; col.b >>= darken;
loadfont(size);
SDL_Surface *txt = ((vid.antialias & AA_FONT)?TTF_RenderUTF8_Blended:TTF_RenderUTF8_Solid)(font[size], str, col);
if(txt == NULL) return false;
SDL_Rect rect;
rect.w = txt->w;
rect.h = txt->h;
rect.x = x - rect.w * align / 16;
rect.y = y - rect.h/2;
bool clicked = (mousex >= rect.x && mousey >= rect.y && mousex <= rect.x+rect.w && mousey <= rect.y+rect.h);
if(shift) {
SDL_Surface* txt2 = SDL_DisplayFormat(txt);
SDL_LockSurface(txt2);
SDL_LockSurface(s);
int c0 = qpixel(txt2, 0, 0);
for(int yy=0; yy<rect.h; yy++)
for(int xx=0; xx<rect.w; xx++) if(qpixel(txt2, xx, yy) != c0)
qpixel(s, rect.x+xx-shift, rect.y+yy) |= color & 0xFF0000,
qpixel(s, rect.x+xx+shift, rect.y+yy) |= color & 0x00FFFF;
SDL_UnlockSurface(s);
SDL_UnlockSurface(txt2);
SDL_FreeSurface(txt2);
}
else {
SDL_BlitSurface(txt, NULL, s,&rect);
}
SDL_FreeSurface(txt);
return clicked;
#endif
}
bool displaystr(int x, int y, int shift, int size, const string &s, int color, int align) {
return displaystr(x, y, shift, size, s.c_str(), color, align);
}
bool displayfrSP(int x, int y, int sh, int b, int size, const string &s, int color, int align, int p) {
if(b) {
displaystr(x-b, y, 0, size, s, p, align);
displaystr(x+b, y, 0, size, s, p, align);
displaystr(x, y-b, 0, size, s, p, align);
displaystr(x, y+b, 0, size, s, p, align);
}
if(b >= 2) {
int b1 = b-1;
displaystr(x-b1, y-b1, 0, size, s, p, align);
displaystr(x-b1, y+b1, 0, size, s, p, align);
displaystr(x+b1, y-b1, 0, size, s, p, align);
displaystr(x+b1, y+b1, 0, size, s, p, align);
}
return displaystr(x, y, 0, size, s, color, align);
}
bool displayfr(int x, int y, int b, int size, const string &s, int color, int align) {
return displayfrSP(x, y, 0, b, size, s, color, align, poly_outline>>8);
}
bool displaychr(int x, int y, int shift, int size, char chr, int col) {
char buf[2];
buf[0] = chr; buf[1] = 0;
return displaystr(x, y, shift, size, buf, col, 8);
}
#endif
bool displaynum(int x, int y, int shift, int size, int col, int val, string title) {
char buf[64];
sprintf(buf, "%d", val);
bool b1 = displayfr(x-8, y, 1, size, buf, col, 16);
bool b2 = displayfr(x, y, 1, size, title, col, 0);
if((b1 || b2) && gtouched) {
col ^= 0x00FFFF;
displayfr(x-8, y, 1, size, buf, col, 16);
displayfr(x, y, 1, size, title, col, 0);
}
return b1 || b2;
}
vector<msginfo> msgs;
vector<msginfo> gamelog;
void flashMessages() {
for(int i=0; i<size(msgs); i++)
if(msgs[i].stamp < ticks - 1000 && !msgs[i].flashout) {
msgs[i].flashout = true;
msgs[i].stamp = ticks;
}
}
void addMessageToLog(msginfo& m, vector<msginfo>& log) {
if(size(log) != 0) {
msginfo& last = log[size(log)-1];
if(last.msg == m.msg) {
int q = m.quantity + last.quantity;
last = m; last.quantity = q;
return;
}
}
if(size(log) < 200)
log.push_back(m);
else {
for(int i=0; i<size(log)-1; i++) swap(log[i], log[i+1]);
log[size(log)-1] = m;
}
}
void clearMessages() { msgs.clear(); }
void addMessage(string s, char spamtype) {
DEBB(DF_MSG, (debugfile,"addMessage: %s\n", s.c_str()));
msginfo m;
m.msg = s; m.spamtype = spamtype; m.flashout = false; m.stamp = ticks;
m.quantity = 1;
addMessageToLog(m, gamelog);
addMessageToLog(m, msgs);
}
int colormix(int a, int b, int c) {
for(int p=0; p<3; p++)
part(a, p) = part(a,p) + (part(b,p) - part(a,p)) * part(c,p) / 255;
return a;
}
void drawmessages() {
DEBB(DF_GRAPH, (debugfile,"draw messages\n"));
int i = 0;
int t = ticks;
for(int j=0; j<size(msgs) && j<5; j++) {
int age = msgs[j].flashout * (t - msgs[j].stamp);
if(msgs[j].spamtype) {
for(int i=j+1; i<size(msgs); i++) if(msgs[i].spamtype == msgs[j].spamtype)
msgs[j].flashout = 2;
}
if(age < 256*vid.flashtime) {
int x = vid.xres / 2;
int y = vid.yres - vid.fsize * (size(msgs) - j) - (ISIOS ? 4 : 0);
string s = msgs[j].msg;
if(msgs[j].quantity > 1) s += " (x" + its(msgs[j].quantity) + ")";
poly_outline = gradient(bordcolor, backcolor, 0, age, 256*vid.flashtime) << 8;
displayfr(x, y, 1, vid.fsize, s, gradient(forecolor, backcolor, 0, age, 256*vid.flashtime), 8);
msgs[i++] = msgs[j];
}
}
msgs.resize(i);
}
int gradient(int c0, int c1, ld v0, ld v, ld v1) {
int vv = int(256 * ((v-v0) / (v1-v0)));
int c = 0;
for(int a=0; a<3; a++) {
int p0 = (c0 >> (8*a)) & 255;
int p1 = (c1 >> (8*a)) & 255;
int p = (p0*(256-vv) + p1*vv + 127) >> 8;
c |= p << (8*a);
}
return c;
}
void drawCircle(int x, int y, int size, int color) {
if(size < 0) size = -size;
#if CAP_GL
if(vid.usingGL) {
qglcoords = 0;
glcolor2(color);
x -= vid.xcenter; y -= vid.ycenter;
int pts = size * 4;
if(pts > 1500) pts = 1500;
if(ISMOBILE && pts > 72) pts = 72;
for(int r=0; r<pts; r++) {
float rr = (M_PI * 2 * r) / pts;
glcoords[r][0] = x + size * sin(rr);
glcoords[r][1] = y + size * cos(rr);
glcoords[r][2] = vid.scrdist;
}
qglcoords = pts;
activateGlcoords();
glDrawArrays(GL_LINE_LOOP, 0, pts);
return;
}
#endif
#if CAP_XGD
gdpush(4); gdpush(color); gdpush(x); gdpush(y); gdpush(size);
#elif CAP_SDLGFX
((vid.antialias && AA_NOGL)?aacircleColor:circleColor) (s, x, y, size, color);
#elif CAP_SDL
int pts = size * 4;
if(pts > 1500) pts = 1500;
for(int r=0; r<pts; r++)
qpixel(s, x + int(size * sin(r)), y + int(size * cos(r))) = color;
#endif
}
void displayButton(int x, int y, const string& name, int key, int align, int rad) {
if(displayfr(x, y, rad, vid.fsize, name, 0x808080, align)) {
displayfr(x, y, rad, vid.fsize, name, 0xFFFF00, align);
getcstat = key;
}
}
char mousekey = 'n';
char newmousekey;
void displaymm(char c, int x, int y, int rad, int size, const string& title, int align) {
if(displayfr(x, y, rad, size, title, c == mousekey ? 0xFF8000 : 0xC0C0C0, align)) {
displayfr(x, y, rad, size, title, 0xFFFF00, align);
getcstat = SETMOUSEKEY, newmousekey = c;
}
}
bool displayButtonS(int x, int y, const string& name, int col, int align, int size) {
if(displaystr(x, y, 0, size, name, col, align)) {
displaystr(x, y, 0, size, name, 0xFFFF00, align);
return true;
}
else return false;
}
void displayColorButton(int x, int y, const string& name, int key, int align, int rad, int color, int color2) {
if(displayfr(x, y, rad, vid.fsize, name, color, align)) {
if(color2) displayfr(x, y, rad, vid.fsize, name, color2, align);
getcstat = key;
}
}
ld textscale() {
return vid.fsize / (vid.radius * crossf) * (1+vid.alphax) * 2;
}
// bool notgl = false;
int pngres = 2000;
int pngformat = 0;
#if CAP_PNG
void IMAGESAVE(SDL_Surface *s, const char *fname) {
SDL_Surface *s2 = SDL_PNGFormatAlpha(s);
SDL_SavePNG(s2, fname);
SDL_FreeSurface(s2);
}
#endif
#if CAP_SDL
void saveHighQualityShot(const char *fname, const char *caption, int fade) {
#if !CAP_SDLGFX
addMessage(XLAT("High quality shots not available on this platform"));
return;
#endif
dynamicval<int> v3(sightrange, (cheater && sightrange < 10) ? 10 : sightrange);
if(cheater) doOvergenerate();
time_t timer;
timer = time(NULL);
dynamicval<videopar> v(vid, vid);
dynamicval<bool> v2(inHighQual, true);
dynamicval<int> v4(cheater, 0);
dynamicval<bool> v6(auraNOGL, fname ? true : false);
vid.xres = vid.yres = pngres;
if(pngformat == 1) vid.xres = vid.yres * 4/3;
if(pngformat == 2) vid.xres = vid.yres * 16/9;
if(pngformat == 3) {
vid.xres = vid.yres * 22/16;
while(vid.xres & 15) vid.xres++;
}
printf("format = %d, %d x %d\n", pngformat, vid.xres, vid.yres);
vid.usingGL = false;
// if(vid.pmodel == 0) vid.scale = 0.99;
calcparam();
#if CAP_ROGUEVIZ
rogueviz::fixparam();
#endif
printf("format = %d, %d x %d\n", pngformat, vid.xres, vid.yres);
dynamicval<SDL_Surface*> v5(s, SDL_CreateRGBSurface(SDL_SWSURFACE,vid.xres,vid.yres,32,0,0,0,0));
darken = 0;
int numi = (fname?1:2);
for(int i=0; i<numi; i++) {
SDL_FillRect(s, NULL, numi==1 ? backcolor : i ? 0xFFFFFF : 0);
drawfullmap();
if(fade < 255)
for(int y=0; y<vid.yres; y++)
for(int x=0; x<vid.xres; x++) {
int& p = qpixel(s, x, y);
for(int i=0; i<3; i++) {
part(p,i) = (part(p,i) * fade + 127) / 255;
}
}
if(caption)
displayfr(vid.xres/2, vid.fsize+vid.fsize/4, 3, vid.fsize*2, caption, forecolor, 8);
char buf[128]; strftime(buf, 128, "bigshota-%y%m%d-%H%M%S" IMAGEEXT, localtime(&timer));
buf[7] += i;
if(!fname) fname = buf;
IMAGESAVE(s, fname);
if(i == 0) addMessage(XLAT("Saved the high quality shot to %1", fname));
}
SDL_FreeSurface(s);
}
#endif
#if CAP_SDL
bool setfsize = true;
void setvideomode() {
DEBB(DF_INIT, (debugfile,"setvideomode\n"));
if(!vid.full) {
if(vid.xres > vid.xscr) vid.xres = vid.xscr * 9/10, setfsize = true;
if(vid.yres > vid.yscr) vid.yres = vid.yscr * 9/10, setfsize = true;
}
if(setfsize) vid.fsize = min(vid.yres / 32, vid.xres / 48), setfsize = false;
int flags = 0;
#if CAP_GL
if(vid.usingGL) {
flags = SDL_OPENGL | SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER;
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
if(vid.antialias & AA_MULTI) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, (vid.antialias & AA_MULTI16) ? 16 : 4);
}
}
#endif
int sizeflag = (vid.full ? SDL_FULLSCREEN : SDL_RESIZABLE);
s= SDL_SetVideoMode(vid.xres, vid.yres, 32, flags | sizeflag);
if(vid.full && !s) {
vid.xres = vid.xscr;
vid.yres = vid.yscr;
vid.fsize = min(vid.yres / 32, vid.xres / 48);
s = SDL_SetVideoMode(vid.xres, vid.yres, 32, flags | SDL_FULLSCREEN);
}
if(!s) {
addMessage("Failed to set the graphical mode: "+its(vid.xres)+"x"+its(vid.yres)+(vid.full ? " fullscreen" : " windowed"));
vid.xres = 640;
vid.yres = 480;