-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
basegraph.cpp
1734 lines (1445 loc) · 43.5 KB
/
basegraph.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
// Hyperbolic Rogue -- basic graphics
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
/** \file basegraph.cpp
* \brief This file implements the basic graphical routines
*/
#include "hyper.h"
#ifdef FONTCONFIG
#include <fontconfig/fontconfig.h>
#endif
namespace hr {
#if HDR
struct radarpoint {
hyperpoint h;
char glyph;
color_t color;
color_t line;
};
struct radarline {
hyperpoint h1, h2;
color_t line;
};
/** configuration of the current view */
struct display_data {
/** The cell which is currently in the center. */
cell *precise_center;
/** The current rotation, relative to precise_center. */
transmatrix view_matrix;
/** Camera rotation, used in nonisotropic geometries. */
transmatrix local_perspective;
/** The view relative to the player character. */
shiftmatrix player_matrix;
/** On-screen coordinates for all the visible cells. */
map<cell*, shiftmatrix_or_null> cellmatrices, old_cellmatrices;
/** Position of the current map view, relative to the screen (0 to 1). */
ld xmin, ymin, xmax, ymax;
/** Position of the current map view, in pixels. */
ld xtop, ytop, xsize, ysize;
display_data() { xmin = ymin = 0; xmax = ymax = 1; }
/** Center of the current map view, in pixels. */
int xcenter, ycenter;
ld radius;
int scrsize;
bool sidescreen;
ld tanfov;
flagtype next_shader_flags;
vector<radarpoint> radarpoints;
vector<radarline> radarlines;
transmatrix radar_transform;
transmatrix radar_transform_post;
ld eyewidth();
bool separate_eyes();
bool in_anaglyph();
void set_viewport(int ed);
void set_projection(int ed, ld shift);
void set_mask(int ed);
void set_all(int ed, ld shift);
/** Which copy of the player cell? */
transmatrix which_copy;
/** On-screen coordinates for all the visible cells. */
map<cell*, vector<shiftmatrix>> all_drawn_copies;
};
#define View (::hr::current_display->view_matrix)
#define cwtV (::hr::current_display->player_matrix)
#define centerover (::hr::current_display->precise_center)
#define gmatrix (::hr::current_display->cellmatrices)
#define gmatrix0 (::hr::current_display->old_cellmatrices)
#define NLP (::hr::current_display->local_perspective)
#endif
EX display_data default_display;
EX display_data *current_display = &default_display;
/** Color of the background. */
EX unsigned backcolor = 0;
EX unsigned bordcolor = 0;
EX unsigned forecolor = 0xFFFFFF;
EX 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;
}
EX int utfsize_before(const string& s, int pos) {
if(!pos) return 0;
int npos = pos - 1;
while(npos && ((unsigned char)s[npos]) >= 128 && ((unsigned char)s[npos]) < 192) npos--;
return pos - npos;
}
EX int get_sightrange() { return getDistLimit() + sightrange_bonus; }
EX int get_sightrange_ambush() {
#if CAP_COMPLEX2
return max(get_sightrange(), ambush::distance);
#else
return get_sightrange();
#endif
}
bool display_data::in_anaglyph() { return vid.stereo_mode == sAnaglyph; }
bool display_data::separate_eyes() { return among(vid.stereo_mode, sAnaglyph, sLR); }
ld display_data::eyewidth() {
switch(vid.stereo_mode) {
case sAnaglyph:
return vid.anaglyph_eyewidth;
case sLR:
return vid.lr_eyewidth;
default:
return 0;
}
}
bool eqs(const char* x, const char* y) {
return *y? *x==*y?eqs(x+1,y+1):false:true;
}
EX 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;
}
#ifdef REPLACE_LETTERS
for(int j=0; j<isize(dialog::latin_letters_l); j++)
if(s[i] == dialog::foreign_letters[2*j] && s[i+1] == dialog::foreign_letters[2*j+1]) {
i += 2;
return int(dialog::latin_letters_l[j]);
}
#endif
printf("Unknown character in: '%s' at position %d\n", s, i);
i += siz; return '?';
}
#if CAP_SDLTTF
void fix_font_size(int& size) {
if(size < 1) size = 1;
if(size > max_font_size) size = max_font_size;
if(size > 72) size &=~ 3;
if(size > 144) size &=~ 7;
}
#endif
#if CAP_SDL
#if !CAP_SDL2
#if HDR
typedef SDL_Surface SDL_Renderer;
#define srend s
#endif
#endif
EX SDL_Surface *s;
EX SDL_Surface *s_screen;
#if CAP_SDL2
EX SDL_Renderer *s_renderer, *s_software_renderer;
#if HDR
#define srend s_software_renderer
#endif
EX SDL_Texture *s_texture;
EX SDL_Window *s_window;
EX SDL_GLContext s_context;
EX bool s_have_context;
#endif
EX color_t qpixel_pixel_outside;
EX color_t& qpixel(SDL_Surface *surf, int x, int y) {
if(x<0 || y<0 || x >= surf->w || y >= surf->h) return qpixel_pixel_outside;
char *p = (char*) surf->pixels;
p += y * surf->pitch;
color_t *pi = (color_t*) (p);
return pi[x];
}
EX void present_surface() {
#if CAP_SDL2
SDL_UpdateTexture(s_texture, nullptr, s->pixels, s->w * sizeof (Uint32));
SDL_RenderClear(s_renderer);
SDL_RenderCopy(s_renderer, s_texture, nullptr, nullptr);
SDL_RenderPresent(s_renderer);
#else
SDL_UpdateRect(s, 0, 0, 0, 0);
#endif
}
EX void present_screen() {
#if CAP_GL
if(vid.usingGL) {
#if CAP_SDL2
SDL_GL_SwapWindow(s_window);
#else
SDL_GL_SwapBuffers();
#endif
return;
}
#endif
present_surface();
}
#endif
#if CAP_SDLTTF
EX vector<string> font_filenames = {
"DejaVuSans-Bold.ttf",
"DejaVuSans.ttf",
"cmunss.ttf",
"NotoSans-Regular.ttf",
"OpenDyslexic3-Regular.ttf",
"font.ttf",
"font.otf"
};
EX vector<pair<string, string>> font_names = {
{"DejaVu Sans Bold", ""},
{"DejaVu Sans", ""},
{"Computer Modern Sans", ""},
{"Noto Sans", "note: if using the Chinese translation, Noto Sans will override the options above it"},
{"OpenDyslexic3-Regular", ""},
{"TTF font", ""},
{"OTF font", ""}
};
EX int last_font_id = 0;
EX int font_id = 0;
#ifdef FONTCONFIG
TTF_Font* findfont(int siz) {
auto orig = cfont->filename;
FcPattern *pat;
FcResult result;
if (!FcInit()) return nullptr;
string s =cfont->filename;
auto rep = [&] (string what, string by) {
int pos = s.size() - what.size();
if(pos < 0) return;
if(s.substr(pos) == what) s = s.substr(0, pos) + by;
};
rep(".ttf", "");
rep(".otf", "");
rep(".TTF", "");
rep(".OTF", "");
rep("-Bold", ":weight=bold");
rep("-Regular", ":weight=regular");
pat = FcNameParse((FcChar8 *)s.c_str());
FcConfigSubstitute(0, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
FcPattern *match;
match = FcFontMatch(0, pat, &result);
if (match) {
FcChar8 *file;
if (FcPatternGetString(match, FC_FILE, 0, &file) == FcResultMatch) {
cfont->filename = (const char *)file;
}
FcPatternDestroy(match);
}
FcPatternDestroy(pat);
FcFini();
cfont->use_fontconfig = false;
if(debugflags & DF_INIT) println(hlog, "fontpath is: ", cfont->filename);
return TTF_OpenFont(cfont->filename.c_str(), siz);
}
#endif
void loadfont(int siz) {
fix_font_size(siz);
auto& cf = cfont->font[siz];
if(!cf) {
if(cf == NULL) cf = TTF_OpenFont(find_file(cfont->filename).c_str(), siz);
#ifdef FONTCONFIG
if(cf == NULL && cfont->use_fontconfig)
cf = findfont(siz);
#endif
if(cf == NULL) {
printf("error: Font file not found: %s\n", cfont->filename.c_str());
if(font_id == 0) throw hr_exception("font file not found");
font_id = 0; set_cfont(); loadfont(siz);
}
}
}
#endif
#if !ISFAKEMOBILE && !ISANDROID & !ISIOS
int textwidth(int siz, const string &str) {
if(isize(str) == 0) return 0;
#if CAP_SDLTTF
fix_font_size(siz);
loadfont(siz);
int w, h;
TTF_SizeUTF8(cfont->font[siz], str.c_str(), &w, &h);
// printf("width = %d [%d]\n", w, isize(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
#if !CAP_GL
EX void setcameraangle(bool b) { }
#endif
#if !CAP_GL
EX void reset_projection() { }
EX void glflush() { }
EX bool model_needs_depth() { return false; }
void display_data::set_all(int ed, ld lshift) {}
#endif
#if CAP_GL
EX void eyewidth_translate(int ed) {
glhr::using_eyeshift = false;
if(ed) glhr::projection_multiply(glhr::translate(-ed * current_display->eyewidth(), 0, 0));
}
tuple<int, eModel, display_data*, int> last_projection;
EX bool new_projection_needed;
#if HDR
inline void reset_projection() { new_projection_needed = true; }
#endif
EX ld lband_shift;
void display_data::set_all(int ed, ld shift) {
auto t = this;
auto current_projection = tie(ed, pmodel, t, current_rbuffer);
if(new_projection_needed || !glhr::current_glprogram || (next_shader_flags & GF_which) != (glhr::current_glprogram->shader_flags & GF_which) || current_projection != last_projection || shift != lband_shift) {
last_projection = current_projection;
lband_shift = shift;
set_projection(ed, shift);
set_mask(ed);
set_viewport(ed);
new_projection_needed = false;
}
}
void display_data::set_mask(int ed) {
if(ed == 0 || vid.stereo_mode != sAnaglyph) {
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 display_data::set_viewport(int ed) {
ld xtop = current_display->xtop;
ld ytop = current_display->ytop;
ld xsize = current_display->xsize;
ld ysize = current_display->ysize;
if(ed == 0 || vid.stereo_mode != sLR) ;
else if(ed == 1) xsize /= 2;
else if(ed == -1) xsize /= 2, xtop += xsize;
glViewport(xtop, ytop, xsize, ysize);
}
EX bool model_needs_depth() {
return GDIM == 3 || pmodel == mdBall;
}
EX void setGLProjection(color_t col IS(backcolor)) {
DEBBI(DF_GRAPH, ("setGLProjection"));
GLERR("pre_setGLProjection");
glClearColor(part(col, 2) / 255.0, part(col, 1) / 255.0, part(col, 0) / 255.0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLERR("setGLProjection #1");
glEnable(GL_BLEND);
#ifndef GLES_ONLY
if(vid.antialias & AA_LINES) {
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
}
else glDisable(GL_LINE_SMOOTH);
#endif
glLineWidth(vid.linewidth);
GLERR("setGLProjection #2");
#ifndef GLES_ONLY
if(vid.antialias & AA_POLY) {
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
else glDisable(GL_POLYGON_SMOOTH);
#endif
GLERR("setGLProjection #3");
//glLineWidth(1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#ifdef GL_ES
glClearDepthf(1.0f);
#else
glClearDepth(1.0f);
#endif
glDepthFunc(GL_LEQUAL);
GLERR("setGLProjection");
reset_projection();
glhr::set_depthwrite(true);
glClear(GL_DEPTH_BUFFER_BIT);
}
EX 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;
}
#if HDR
constexpr int max_glfont_size = 72;
constexpr int max_font_size = 288;
struct fontdata {
string filename;
#if FONTCONFIG
bool use_fontconfig;
#endif
struct glfont_t* glfont[max_glfont_size+1];
#if CAP_SDLTTF
TTF_Font* font[max_font_size+1];
#endif
struct basic_textureinfo *finf;
~fontdata();
};
#endif
EX map<string, fontdata> fontdatas;
EX fontdata *cfont, *cfont_chinese;
EX fontdata* font_by_name(string fname) {
auto& fd = fontdatas[fname];
if(fd.filename == "") {
fd.filename = fname;
#if FONTCONFIG
fd.use_fontconfig = true;
#endif
for(int i=0; i<=max_glfont_size; i++) fd.glfont[i] = nullptr;
#if CAP_SDLTTF
for(int i=0; i<=max_font_size; i++) fd.font[i] = nullptr;
#endif
fd.finf = nullptr;
}
return &fd;
}
#if CAP_GLFONT
#define CHARS (128+NUMEXTRA)
#if HDR
struct charinfo_t {
int w, h;
float tx0, ty0, tx1, ty1;
};
struct glfont_t {
GLuint texture; // Holds The Texture Id
//GLuint list_base; // Holds The First Display List ID
vector<charinfo_t> chars;
};
#endif
typedef Uint16 texturepixel;
#define FONTTEXTURESIZE 4096
int curx = 0, cury = 0, theight = 0;
texturepixel fontpixels[FONTTEXTURESIZE][FONTTEXTURESIZE];
void sdltogl(SDL_Surface *txt, glfont_t& f, int ch) {
#if CAP_TABFONT
if(ch < 32) return;
int otwidth, otheight, tpixindex = 0;
unsigned char tpix[3000];
loadCompressedChar(otwidth, otheight, tpix);
#else
if(!txt) return;
int otwidth = txt->w;
int otheight = txt->h;
#endif
if(otwidth+curx+1 > FONTTEXTURESIZE) curx = 0, cury += theight+1, theight = 0;
theight = max(theight, otheight);
for(int j=0; j<otheight;j++) for(int i=0; i<otwidth; i++) {
fontpixels[j+cury][i+curx] =
#if CAP_TABFONT
(i>=otwidth || j>=otheight) ? 0 : (tpix[tpixindex++] * 0x100) | 0xFF;
#else
((i>=txt->w || j>=txt->h) ? 0 : ((qpixel(txt, i, j)>>24)&0xFF) * 0x100) | 0x00FF;
#endif
}
auto& c = f.chars[ch];
c.w = otwidth;
c.h = otheight;
c.tx0 = (float) curx / (float) FONTTEXTURESIZE;
c.tx1 = (float) (curx+otwidth) / (float) FONTTEXTURESIZE;
c.ty0 = (float) cury;
c.ty1 = (float) (cury+otheight);
curx += otwidth+1;
}
EX void init_glfont(int size) {
if(cfont->glfont[size]) return;
DEBBI(DF_GRAPH, ("init GL font: ", size));
#if !CAP_TABFONT
loadfont(size);
if(!cfont->font[size]) return;
#endif
cfont->glfont[size] = new glfont_t;
glfont_t& f(*(cfont->glfont[size]));
f.chars.resize(CHARS);
//f.list_base = glGenLists(128);
glGenTextures(1, &f.texture );
#if !CAP_TABFONT
char str[2]; str[1] = 0;
SDL_Color white;
white.r = white.g = white.b = 255;
#endif
for(int y=0; y<FONTTEXTURESIZE; y++)
for(int x=0; x<FONTTEXTURESIZE; x++)
fontpixels[y][x] = 0;
#if CAP_TABFONT
resetTabFont();
#endif
// glListBase(0);
curx = 0, cury = 0, theight = 0;
for(int ch=1;ch<CHARS;ch++) {
if(ch<32) continue;
#if CAP_TABFONT
sdltogl(NULL, f, ch);
#else
SDL_Surface *txt;
int siz = size;
fix_font_size(siz);
if(ch < 128) {
str[0] = ch;
txt = TTF_RenderText_Blended(cfont->font[siz], str, white);
}
else {
txt = TTF_RenderUTF8_Blended(cfont->font[siz], natchars[ch-128], white);
}
if(txt == NULL) continue;
#if CAP_CREATEFONT
generateFont(ch, txt);
#endif
sdltogl(txt, f, ch);
SDL_FreeSurface(txt);
#endif
}
glBindTexture( GL_TEXTURE_2D, f.texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
theight = next_p2(cury + theight);
glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, FONTTEXTURESIZE, theight, 0,
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
fontpixels);
for(int ch=0; ch<CHARS; ch++) f.chars[ch].ty0 /= theight, f.chars[ch].ty1 /= theight;
#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");
}
int gl_width(int size, const char *s) {
int gsiz = size;
if(size > vid.fsize || size > max_glfont_size) gsiz = max_glfont_size;
#if CAP_FIXEDSIZE
gsiz = CAP_FIXEDSIZE;
#endif
init_glfont(gsiz);
if(!cfont->glfont[gsiz]) return 0;
glfont_t& f(*cfont->glfont[gsiz]);
int x = 0;
for(int i=0; s[i];) {
int tabid = getnext(s,i);
x += f.chars[tabid].w * size/gsiz;
}
return x;
}
glhr::textured_vertex charvertex(int x1, int y1, ld tx, ld ty) {
glhr::textured_vertex res;
res.coords[0] = x1;
res.coords[1] = y1;
res.coords[2] = 0;
res.coords[3] = 1;
res.texture[0] = tx;
res.texture[1] = ty;
return res;
}
bool gl_print(int x, int y, int shift, int size, const char *s, color_t color, int align) {
int gsiz = size;
if(size > vid.fsize || size > max_glfont_size) gsiz = max_glfont_size;
#if CAP_FIXEDSIZE
gsiz = CAP_FIXEDSIZE;
#endif
init_glfont(gsiz);
if(!cfont->glfont[gsiz]) return false;
glfont_t& f(*cfont->glfont[gsiz]);
int tsize = 0;
for(int i=0; s[i];) {
tsize += f.chars[getnext(s,i)].w * size/gsiz;
}
x -= tsize * align / 16;
y += f.chars[32].h * size / (gsiz*2);
int ysiz = f.chars[32].h * size / gsiz;
bool clicked = (mousex >= x && mousey <= y && mousex <= x+tsize && mousey >= y-ysiz);
color_t icolor = (color << 8) | 0xFF;
if(icolor != text_color || f.texture != text_texture || shift != text_shift || shapes_merged) {
glflush();
text_color = icolor;
text_texture = f.texture;
text_shift = shift;
}
texts_merged++;
auto& tver = text_vertices;
glBindTexture(GL_TEXTURE_2D, f.texture);
for(int i=0; s[i];) {
int tabid = getnext(s,i);
auto& c = f.chars[tabid];
int wi = c.w * size/gsiz;
int hi = c.h * size/gsiz;
GLERR("pre-print");
glhr::textured_vertex t00 = charvertex(x, y-hi, c.tx0, c.ty0);
glhr::textured_vertex t01 = charvertex(x, y, c.tx0, c.ty1);
glhr::textured_vertex t11 = charvertex(x+wi, y, c.tx1, c.ty1);
glhr::textured_vertex t10 = charvertex(x+wi, y-hi, c.tx1, c.ty0);
tver.push_back(t00);
tver.push_back(t01);
tver.push_back(t10);
tver.push_back(t10);
tver.push_back(t01);
tver.push_back(t11);
x += wi;
}
return clicked;
}
#endif
EX purehookset hooks_resetGL;
EX void resetGL() {
DEBBI(DF_INIT | DF_GRAPH, ("reset GL"))
callhooks(hooks_resetGL);
#if CAP_GLFONT
for(auto& cf: fontdatas)
for(int i=0; i<=max_glfont_size; i++) if(cf.second.glfont[i]) {
delete cf.second.glfont[i];
cf.second.glfont[i] = NULL;
}
#endif
#if MAXMDIM >= 4
if(floor_textures) {
delete floor_textures;
floor_textures = NULL;
}
#endif
#if MAXMDIM >= 4 && CAP_GL
if(airbuf) {
delete airbuf;
airbuf = nullptr;
}
#endif
check_cgi();
if(currentmap) cgi.require_shapes();
#if MAXMDIM >= 4
if(GDIM == 3 && !floor_textures) make_floor_textures();
#endif
cgi.initPolyForGL();
compiled_programs.clear();
matched_programs.clear();
glhr::current_glprogram = nullptr;
ray::reset_raycaster();
#if CAP_RUG
if(rug::glbuf) rug::close_glbuf();
#endif
}
#endif
#if CAP_XGD
vector<int> graphdata;
EX void gdpush(int t) {
graphdata.push_back(t);
}
EX bool displaychr(int x, int y, int shift, int size, char chr, color_t 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<isize(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 if(uch >= 224 && uch < 240) {
int u = ((s[i] - 224)&15) << 12;
i++;
u += (s[i] & 63) << 6;
i++;
u += (s[i] & 63) << 0;
gdpush(u); q++;
}
else
#endif
{
gdpush(s[i]); q++;
}
}
graphdata[g] = q;
}
EX bool displayfr(int x, int y, int b, int size, const string &s, color_t 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;
}
EX bool displaystr(int x, int y, int shift, int size, const string &s, color_t color, int align) {
return displayfr(x,y,0,size,s,color,align);
}
EX bool displaystr(int x, int y, int shift, int size, char const *s, color_t color, int align) {
return displayfr(x,y,0,size,s,color,align);
}
#endif
#if !CAP_XGD
EX bool displaystr(int x, int y, int shift, int size, const char *str, color_t color, int align) {
if(strlen(str) == 0) return false;
if(size < 4 || size > 2000) {
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;
fix_font_size(size);
loadfont(size);
SDL_Surface *txt = ((vid.antialias & AA_FONT)?TTF_RenderUTF8_Blended:TTF_RenderUTF8_Solid)(cfont->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) {
#if CAP_SDL2
SDL_Surface* txt2 = SDL_ConvertSurfaceFormat(txt, SDL_PIXELFORMAT_RGBA8888, 0);
#else
SDL_Surface* txt2 = SDL_DisplayFormat(txt);
#endif
SDL_LockSurface(txt2);
SDL_LockSurface(s);
color_t 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
}
EX bool displaystr(int x, int y, int shift, int size, const string &s, color_t color, int align) {
return displaystr(x, y, shift, size, s.c_str(), color, align);
}
EX bool displayfrSP(int x, int y, int sh, int b, int size, const string &s, color_t 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);
}
EX bool displayfr(int x, int y, int b, int size, const string &s, color_t color, int align) {
return displayfrSP(x, y, 0, b, size, s, color, align, poly_outline>>8);
}
EX bool displaychr(int x, int y, int shift, int size, char chr, color_t col) {
char buf[2];
buf[0] = chr; buf[1] = 0;
return displaystr(x, y, shift, size, buf, col, 8);
}
#endif
#if HDR
struct msginfo {
int stamp;
time_t rtstamp;
int gtstamp;
int turnstamp;
char flashout;
char spamtype;
int quantity;
string msg;
};
#endif
EX vector<msginfo> msgs;
EX vector<msginfo> gamelog;
EX void flashMessages() {
for(int i=0; i<isize(msgs); i++)
if(msgs[i].stamp < ticks - 1000 && !msgs[i].flashout) {
msgs[i].flashout = true;
msgs[i].stamp = ticks;
}
}
EX string fullmsg(msginfo& m) {
string s = m.msg;
if(m.quantity > 1) s += " (x" + its(m.quantity) + ")";
return s;
}
void addMessageToLog(msginfo& m, vector<msginfo>& log) {
if(isize(log) != 0) {
msginfo& last = log[isize(log)-1];
if(last.msg == m.msg) {
int q = m.quantity + last.quantity;
last = m; last.quantity = q;
return;
}
}
if(isize(log) < 1000)
log.push_back(m);
else {
for(int i=0; i<isize(log)-1; i++) swap(log[i], log[i+1]);