-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_draw.c
1868 lines (1689 loc) · 51.6 KB
/
gl_draw.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
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include "image.h"
#include "wad.h"
#include "cl_video.h"
#include "cl_dyntexture.h"
#include "ft2.h"
#include "ft2_fontdefs.h"
dp_font_t dp_fonts[MAX_FONTS] = {{0}};
cvar_t r_textshadow = {CVAR_SAVE, "r_textshadow", "0", "draws a shadow on all text to improve readability (note: value controls offset, 1 = 1 pixel, 1.5 = 1.5 pixels, etc)"};
cvar_t r_textbrightness = {CVAR_SAVE, "r_textbrightness", "0", "additional brightness for text color codes (0 keeps colors as is, 1 makes them all white)"};
cvar_t r_textcontrast = {CVAR_SAVE, "r_textcontrast", "1", "additional contrast for text color codes (1 keeps colors as is, 0 makes them all black)"};
cvar_t r_font_postprocess_blur = {CVAR_SAVE, "r_font_postprocess_blur", "0", "font blur amount"};
cvar_t r_font_postprocess_outline = {CVAR_SAVE, "r_font_postprocess_outline", "0", "font outline amount"};
cvar_t r_font_postprocess_shadow_x = {CVAR_SAVE, "r_font_postprocess_shadow_x", "0", "font shadow X shift amount, applied during outlining"};
cvar_t r_font_postprocess_shadow_y = {CVAR_SAVE, "r_font_postprocess_shadow_y", "0", "font shadow Y shift amount, applied during outlining"};
cvar_t r_font_postprocess_shadow_z = {CVAR_SAVE, "r_font_postprocess_shadow_z", "0", "font shadow Z shift amount, applied during blurring"};
cvar_t r_font_hinting = {CVAR_SAVE, "r_font_hinting", "3", "0 = no hinting, 1 = light autohinting, 2 = full autohinting, 3 = full hinting"};
cvar_t r_font_antialias = {CVAR_SAVE, "r_font_antialias", "1", "0 = monochrome, 1 = grey" /* , 2 = rgb, 3 = bgr" */};
extern cvar_t v_glslgamma;
//=============================================================================
/* Support Routines */
#define FONT_FILESIZE 13468
static cachepic_t *cachepichash[CACHEPICHASHSIZE];
static cachepic_t cachepics[MAX_CACHED_PICS];
static int numcachepics;
static rtexturepool_t *drawtexturepool;
static const unsigned char concharimage[FONT_FILESIZE] =
{
#include "lhfont.h"
};
static rtexture_t *draw_generateconchars(void)
{
int i;
unsigned char *data;
double random;
rtexture_t *tex;
data = LoadTGA_BGRA (concharimage, FONT_FILESIZE);
// Gold numbers
for (i = 0;i < 8192;i++)
{
random = lhrandom (0.0,1.0);
data[i*4+3] = data[i*4+0];
data[i*4+2] = 83 + (unsigned char)(random * 64);
data[i*4+1] = 71 + (unsigned char)(random * 32);
data[i*4+0] = 23 + (unsigned char)(random * 16);
}
// White chars
for (i = 8192;i < 32768;i++)
{
random = lhrandom (0.0,1.0);
data[i*4+3] = data[i*4+0];
data[i*4+2] = 95 + (unsigned char)(random * 64);
data[i*4+1] = 95 + (unsigned char)(random * 64);
data[i*4+0] = 95 + (unsigned char)(random * 64);
}
// Gold numbers
for (i = 32768;i < 40960;i++)
{
random = lhrandom (0.0,1.0);
data[i*4+3] = data[i*4+0];
data[i*4+2] = 83 + (unsigned char)(random * 64);
data[i*4+1] = 71 + (unsigned char)(random * 32);
data[i*4+0] = 23 + (unsigned char)(random * 16);
}
// Red chars
for (i = 40960;i < 65536;i++)
{
random = lhrandom (0.0,1.0);
data[i*4+3] = data[i*4+0];
data[i*4+2] = 96 + (unsigned char)(random * 64);
data[i*4+1] = 43 + (unsigned char)(random * 32);
data[i*4+0] = 27 + (unsigned char)(random * 32);
}
#if 0
Image_WriteTGABGRA ("gfx/generated_conchars.tga", 256, 256, data);
#endif
tex = R_LoadTexture2D(drawtexturepool, "conchars", 256, 256, data, TEXTYPE_BGRA, TEXF_ALPHA, NULL);
Mem_Free(data);
return tex;
}
static rtexture_t *draw_generateditherpattern(void)
{
int x, y;
unsigned char pixels[8][8];
for (y = 0;y < 8;y++)
for (x = 0;x < 8;x++)
pixels[y][x] = ((x^y) & 4) ? 254 : 0;
return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, pixels[0], TEXTYPE_PALETTE, TEXF_FORCENEAREST, palette_bgra_transparent);
}
typedef struct embeddedpic_s
{
const char *name;
int width;
int height;
const char *pixels;
}
embeddedpic_t;
static const embeddedpic_t embeddedpics[] =
{
{
"gfx/prydoncursor001", 16, 16,
"477777774......."
"77.....6........"
"7.....6........."
"7....6.........."
"7.....6........."
"7..6...6........"
"7.6.6...6......."
"76...6...6......"
"4.....6.6......."
".......6........"
"................"
"................"
"................"
"................"
"................"
"................"
},
{
"ui/mousepointer", 16, 16,
"477777774......."
"77.....6........"
"7.....6........."
"7....6.........."
"7.....6........."
"7..6...6........"
"7.6.6...6......."
"76...6...6......"
"4.....6.6......."
".......6........"
"................"
"................"
"................"
"................"
"................"
"................"
},
{
"gfx/crosshair1", 16, 16,
"................"
"................"
"................"
"...33......33..."
"...355....553..."
"....577..775...."
".....77..77....."
"................"
"................"
".....77..77....."
"....577..775...."
"...355....553..."
"...33......33..."
"................"
"................"
"................"
},
{
"gfx/crosshair2", 16, 16,
"................"
"................"
"................"
"...3........3..."
"....5......5...."
".....7....7....."
"......7..7......"
"................"
"................"
"......7..7......"
".....7....7....."
"....5......5...."
"...3........3..."
"................"
"................"
"................"
},
{
"gfx/crosshair3", 16, 16,
"................"
".......77......."
".......77......."
"................"
"................"
".......44......."
".......44......."
".77..44..44..77."
".77..44..44..77."
".......44......."
".......44......."
"................"
"................"
".......77......."
".......77......."
"................"
},
{
"gfx/crosshair4", 16, 16,
"................"
"................"
"................"
"................"
"................"
"................"
"................"
"................"
"........7777777."
"........752....."
"........72......"
"........7......."
"........7......."
"........7......."
"........7......."
"................"
},
{
"gfx/crosshair5", 8, 8,
"........"
"........"
"....7..."
"........"
"..7.7.7."
"........"
"....7..."
"........"
},
{
"gfx/crosshair6", 2, 2,
"77"
"77"
},
{
"gfx/crosshair7", 16, 16,
"................"
".3............3."
"..5...2332...5.."
"...7.3....3.7..."
"....7......7...."
"...3.7....7.3..."
"..2...7..7...2.."
"..3..........3.."
"..3..........3.."
"..2...7..7...2.."
"...3.7....7.3..."
"....7......7...."
"...7.3....3.7..."
"..5...2332...5.."
".3............3."
"................"
},
{NULL, 0, 0, NULL}
};
static rtexture_t *draw_generatepic(const char *name, qboolean quiet)
{
const embeddedpic_t *p;
for (p = embeddedpics;p->name;p++)
if (!strcmp(name, p->name))
return R_LoadTexture2D(drawtexturepool, p->name, p->width, p->height, (const unsigned char *)p->pixels, TEXTYPE_PALETTE, TEXF_ALPHA, palette_bgra_embeddedpic);
if (!strcmp(name, "gfx/conchars"))
return draw_generateconchars();
if (!strcmp(name, "gfx/colorcontrol/ditherpattern"))
return draw_generateditherpattern();
if (!quiet)
Con_DPrintf("Draw_CachePic: failed to load %s\n", name);
return r_texture_notexture;
}
/*
================
Draw_CachePic
================
*/
// FIXME: move this to client somehow
cachepic_t *Draw_CachePic_Flags(const char *path, unsigned int cachepicflags)
{
int crc, hashkey;
unsigned char *pixels;
cachepic_t *pic;
fs_offset_t lmpsize;
unsigned char *lmpdata;
char lmpname[MAX_QPATH];
// check whether the picture has already been cached
crc = CRC_Block((unsigned char *)path, strlen(path));
hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
for (pic = cachepichash[hashkey];pic;pic = pic->chain)
if (!strcmp (path, pic->name))
return pic;
if (numcachepics == MAX_CACHED_PICS)
{
Con_Printf ("Draw_CachePic: numcachepics == MAX_CACHED_PICS\n");
// FIXME: support NULL in callers?
return cachepics; // return the first one
}
pic = cachepics + (numcachepics++);
strlcpy (pic->name, path, sizeof(pic->name));
// link into list
pic->chain = cachepichash[hashkey];
cachepichash[hashkey] = pic;
// check whether it is an dynamic texture (if so, we can directly use its texture handler)
pic->tex = CL_GetDynTexture( path );
// if so, set the width/height, too
if( pic->tex ) {
pic->width = R_TextureWidth(pic->tex);
pic->height = R_TextureHeight(pic->tex);
// we're done now (early-out)
return pic;
}
pic->texflags = TEXF_ALPHA;
if (!(cachepicflags & CACHEPICFLAG_NOCLAMP))
pic->texflags |= TEXF_CLAMP;
if (!(cachepicflags & CACHEPICFLAG_NOCOMPRESSION) && gl_texturecompression_2d.integer)
pic->texflags |= TEXF_COMPRESS;
pic->autoload = (cachepicflags & CACHEPICFLAG_NOTPERSISTENT);
// load a high quality image from disk if possible
pixels = loadimagepixelsbgra(path, false, true, r_texture_convertsRGB_2d.integer);
if (pixels == NULL && !strncmp(path, "gfx/", 4))
pixels = loadimagepixelsbgra(path+4, false, true, r_texture_convertsRGB_2d.integer);
if (pixels)
{
pic->width = image_width;
pic->height = image_height;
if (!pic->autoload)
pic->tex = R_LoadTexture2D(drawtexturepool, path, image_width, image_height, pixels, TEXTYPE_BGRA, pic->texflags, NULL);
}
else
{
pic->autoload = false;
// never compress the fallback images
pic->texflags &= ~TEXF_COMPRESS;
}
// now read the low quality version (wad or lmp file), and take the pic
// size from that even if we don't upload the texture, this way the pics
// show up the right size in the menu even if they were replaced with
// higher or lower resolution versions
dpsnprintf(lmpname, sizeof(lmpname), "%s.lmp", path);
if (!strncmp(path, "gfx/", 4) && (lmpdata = FS_LoadFile(lmpname, tempmempool, false, &lmpsize)))
{
if (developer_loading.integer)
Con_Printf("loading lump \"%s\"\n", path);
if (lmpsize >= 9)
{
pic->width = lmpdata[0] + lmpdata[1] * 256 + lmpdata[2] * 65536 + lmpdata[3] * 16777216;
pic->height = lmpdata[4] + lmpdata[5] * 256 + lmpdata[6] * 65536 + lmpdata[7] * 16777216;
// if no high quality replacement image was found, upload the original low quality texture
if (!pixels)
pic->tex = R_LoadTexture2D(drawtexturepool, path, pic->width, pic->height, lmpdata + 8, TEXTYPE_PALETTE, pic->texflags, palette_bgra_transparent);
}
Mem_Free(lmpdata);
}
else if ((lmpdata = W_GetLumpName (path + 4)))
{
if (developer_loading.integer)
Con_Printf("loading gfx.wad lump \"%s\"\n", path + 4);
if (!strcmp(path, "gfx/conchars"))
{
// conchars is a raw image and with color 0 as transparent instead of 255
pic->width = 128;
pic->height = 128;
// if no high quality replacement image was found, upload the original low quality texture
if (!pixels)
pic->tex = R_LoadTexture2D(drawtexturepool, path, 128, 128, lmpdata, TEXTYPE_PALETTE, pic->texflags, palette_bgra_font);
}
else
{
pic->width = lmpdata[0] + lmpdata[1] * 256 + lmpdata[2] * 65536 + lmpdata[3] * 16777216;
pic->height = lmpdata[4] + lmpdata[5] * 256 + lmpdata[6] * 65536 + lmpdata[7] * 16777216;
// if no high quality replacement image was found, upload the original low quality texture
if (!pixels)
pic->tex = R_LoadTexture2D(drawtexturepool, path, pic->width, pic->height, lmpdata + 8, TEXTYPE_PALETTE, pic->texflags, palette_bgra_transparent);
}
}
if (pixels)
{
Mem_Free(pixels);
pixels = NULL;
}
else if (pic->tex == NULL)
{
// if it's not found on disk, generate an image
pic->tex = draw_generatepic(path, (cachepicflags & CACHEPICFLAG_QUIET) != 0);
pic->width = R_TextureWidth(pic->tex);
pic->height = R_TextureHeight(pic->tex);
}
return pic;
}
cachepic_t *Draw_CachePic (const char *path)
{
return Draw_CachePic_Flags (path, 0);
}
int draw_frame = 1;
rtexture_t *Draw_GetPicTexture(cachepic_t *pic)
{
if (pic->autoload && !pic->tex)
{
pic->tex = loadtextureimage(drawtexturepool, pic->name, false, pic->texflags, true, r_texture_convertsRGB_2d.integer);
if (pic->tex == NULL && !strncmp(pic->name, "gfx/", 4))
pic->tex = loadtextureimage(drawtexturepool, pic->name+4, false, pic->texflags, true, r_texture_convertsRGB_2d.integer);
if (pic->tex == NULL)
pic->tex = draw_generatepic(pic->name, true);
}
pic->lastusedframe = draw_frame;
return pic->tex;
}
void Draw_Frame(void)
{
int i;
cachepic_t *pic;
static double nextpurgetime;
if (nextpurgetime > realtime)
return;
nextpurgetime = realtime + 0.05;
for (i = 0, pic = cachepics;i < numcachepics;i++, pic++)
{
if (pic->autoload && pic->tex && pic->lastusedframe < draw_frame)
{
R_FreeTexture(pic->tex);
pic->tex = NULL;
}
}
draw_frame++;
}
cachepic_t *Draw_NewPic(const char *picname, int width, int height, int alpha, unsigned char *pixels_bgra)
{
int crc, hashkey;
cachepic_t *pic;
crc = CRC_Block((unsigned char *)picname, strlen(picname));
hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
for (pic = cachepichash[hashkey];pic;pic = pic->chain)
if (!strcmp (picname, pic->name))
break;
if (pic)
{
if (pic->tex && pic->width == width && pic->height == height)
{
R_UpdateTexture(pic->tex, pixels_bgra, 0, 0, width, height);
return pic;
}
}
else
{
if (pic == NULL)
{
if (numcachepics == MAX_CACHED_PICS)
{
Con_Printf ("Draw_NewPic: numcachepics == MAX_CACHED_PICS\n");
// FIXME: support NULL in callers?
return cachepics; // return the first one
}
pic = cachepics + (numcachepics++);
strlcpy (pic->name, picname, sizeof(pic->name));
// link into list
pic->chain = cachepichash[hashkey];
cachepichash[hashkey] = pic;
}
}
pic->width = width;
pic->height = height;
if (pic->tex)
R_FreeTexture(pic->tex);
pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels_bgra, TEXTYPE_BGRA, (alpha ? TEXF_ALPHA : 0) | TEXF_ALLOWUPDATES, NULL);
return pic;
}
void Draw_FreePic(const char *picname)
{
int crc;
int hashkey;
cachepic_t *pic;
// this doesn't really free the pic, but does free it's texture
crc = CRC_Block((unsigned char *)picname, strlen(picname));
hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
for (pic = cachepichash[hashkey];pic;pic = pic->chain)
{
if (!strcmp (picname, pic->name) && pic->tex)
{
R_FreeTexture(pic->tex);
pic->tex = NULL;
pic->width = 0;
pic->height = 0;
return;
}
}
}
static float snap_to_pixel_x(float x, float roundUpAt);
extern int con_linewidth; // to force rewrapping
static void LoadFont(qboolean override, const char *name, dp_font_t *fnt)
{
int i;
float maxwidth, scale;
char widthfile[MAX_QPATH];
char *widthbuf;
fs_offset_t widthbufsize;
if(override || !fnt->texpath[0])
{
strlcpy(fnt->texpath, name, sizeof(fnt->texpath));
// load the cvars when the font is FIRST loaded
fnt->settings.antialias = r_font_antialias.integer;
fnt->settings.hinting = r_font_hinting.integer;
fnt->settings.outline = r_font_postprocess_outline.value;
fnt->settings.blur = r_font_postprocess_blur.value;
fnt->settings.shadowx = r_font_postprocess_shadow_x.value;
fnt->settings.shadowy = r_font_postprocess_shadow_y.value;
fnt->settings.shadowz = r_font_postprocess_shadow_z.value;
}
if(drawtexturepool == NULL)
return; // before gl_draw_start, so will be loaded later
if(fnt->ft2)
{
// clear freetype font
Font_UnloadFont(fnt->ft2);
Mem_Free(fnt->ft2);
fnt->ft2 = NULL;
}
if(fnt->req_face != -1)
{
if(!Font_LoadFont(fnt->texpath, fnt))
Con_DPrintf("Failed to load font-file for '%s', it will not support as many characters.\n", fnt->texpath);
}
fnt->tex = Draw_CachePic_Flags(fnt->texpath, CACHEPICFLAG_QUIET | CACHEPICFLAG_NOCOMPRESSION)->tex;
if(fnt->tex == r_texture_notexture)
{
for (i = 0; i < MAX_FONT_FALLBACKS; ++i)
{
if (!fnt->fallbacks[i][0])
break;
fnt->tex = Draw_CachePic_Flags(fnt->fallbacks[i], CACHEPICFLAG_QUIET | CACHEPICFLAG_NOCOMPRESSION)->tex;
if(fnt->tex != r_texture_notexture)
break;
}
if(fnt->tex == r_texture_notexture)
{
fnt->tex = Draw_CachePic_Flags("gfx/conchars", CACHEPICFLAG_NOCOMPRESSION)->tex;
strlcpy(widthfile, "gfx/conchars.width", sizeof(widthfile));
}
else
dpsnprintf(widthfile, sizeof(widthfile), "%s.width", fnt->fallbacks[i]);
}
else
dpsnprintf(widthfile, sizeof(widthfile), "%s.width", fnt->texpath);
// unspecified width == 1 (base width)
for(i = 1; i < 256; ++i)
fnt->width_of[i] = 1;
scale = 1;
// FIXME load "name.width", if it fails, fill all with 1
if((widthbuf = (char *) FS_LoadFile(widthfile, tempmempool, true, &widthbufsize)))
{
float extraspacing = 0;
const char *p = widthbuf;
int ch = 0;
while(ch < 256)
{
if(!COM_ParseToken_Simple(&p, false, false))
return;
switch(*com_token)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
case '-':
case '.':
fnt->width_of[ch] = atof(com_token) + extraspacing;
if (fnt->ft2)
{
for (i = 0; i < MAX_FONT_SIZES; ++i)
{
//Font_MapForIndex(fnt->ft2, i)->width_of[ch] = snap_to_pixel_x(fnt->width_of[ch] * fnt->req_sizes[i], 0.4);
ft2_font_map_t *map = Font_MapForIndex(fnt->ft2, i);
if (!map)
break;
map->width_of[ch] = Font_SnapTo(fnt->width_of[ch], 1/map->size);
}
}
ch++;
break;
default:
if(!strcmp(com_token, "extraspacing"))
{
if(!COM_ParseToken_Simple(&p, false, false))
return;
extraspacing = atof(com_token);
}
else if(!strcmp(com_token, "scale"))
{
if(!COM_ParseToken_Simple(&p, false, false))
return;
scale = atof(com_token);
}
else
{
Con_Printf("Warning: skipped unknown font property %s\n", com_token);
if(!COM_ParseToken_Simple(&p, false, false))
return;
}
break;
}
}
Mem_Free(widthbuf);
}
maxwidth = fnt->width_of[1];
for(i = 2; i < 256; ++i)
maxwidth = max(maxwidth, fnt->width_of[i]);
fnt->maxwidth = maxwidth;
// fix up maxwidth for overlap
fnt->maxwidth *= scale;
fnt->scale = scale;
if(fnt == FONT_CONSOLE)
con_linewidth = -1; // rewrap console in next frame
}
static dp_font_t *FindFont(const char *title)
{
int i;
for(i = 0; i < MAX_FONTS; ++i)
if(!strcmp(dp_fonts[i].title, title))
return &dp_fonts[i];
return NULL;
}
static float snap_to_pixel_x(float x, float roundUpAt)
{
float pixelpos = x * vid.width / vid_conwidth.value;
int snap = (int) pixelpos;
if (pixelpos - snap >= roundUpAt) ++snap;
return ((float)snap * vid_conwidth.value / vid.width);
/*
x = (int)(x * vid.width / vid_conwidth.value);
x = (x * vid_conwidth.value / vid.width);
return x;
*/
}
static float snap_to_pixel_y(float y, float roundUpAt)
{
float pixelpos = y * vid.height / vid_conheight.value;
int snap = (int) pixelpos;
if (pixelpos - snap > roundUpAt) ++snap;
return ((float)snap * vid_conheight.value / vid.height);
/*
y = (int)(y * vid.height / vid_conheight.value);
y = (y * vid_conheight.value / vid.height);
return y;
*/
}
static void LoadFont_f(void)
{
dp_font_t *f;
int i;
const char *filelist, *c, *cm;
float sz;
char mainfont[MAX_QPATH];
if(Cmd_Argc() < 2)
{
Con_Printf("Available font commands:\n");
for(i = 0; i < MAX_FONTS; ++i)
Con_Printf(" loadfont %s gfx/tgafile[...] [sizes...]\n", dp_fonts[i].title);
Con_Printf("A font can simply be gfx/tgafile, or alternatively you\n"
"can specify multiple fonts and faces\n"
"Like this: gfx/vera-sans:2,gfx/fallback:1\n"
"to load face 2 of the font gfx/vera-sans and use face 1\n"
"of gfx/fallback as fallback font.\n"
"You can also specify a list of font sizes to load, like this:\n"
"loadfont console gfx/conchars,gfx/fallback 8 12 16 24 32\n"
"In many cases, 8 12 16 24 32 should be a good choice.\n"
);
return;
}
f = FindFont(Cmd_Argv(1));
if(f == NULL)
{
Con_Printf("font function not found\n");
return;
}
if(Cmd_Argc() < 3)
filelist = "gfx/conchars";
else
filelist = Cmd_Argv(2);
memset(f->fallbacks, 0, sizeof(f->fallbacks));
memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
// first font is handled "normally"
c = strchr(filelist, ':');
cm = strchr(filelist, ',');
if(c && (!cm || c < cm))
f->req_face = atoi(c+1);
else
{
f->req_face = 0;
c = cm;
}
if(!c || (c - filelist) > MAX_QPATH)
strlcpy(mainfont, filelist, sizeof(mainfont));
else
{
memcpy(mainfont, filelist, c - filelist);
mainfont[c - filelist] = 0;
}
for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
{
c = strchr(filelist, ',');
if(!c)
break;
filelist = c + 1;
if(!*filelist)
break;
c = strchr(filelist, ':');
cm = strchr(filelist, ',');
if(c && (!cm || c < cm))
f->fallback_faces[i] = atoi(c+1);
else
{
f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
c = cm;
}
if(!c || (c-filelist) > MAX_QPATH)
{
strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
}
else
{
memcpy(f->fallbacks[i], filelist, c - filelist);
f->fallbacks[i][c - filelist] = 0;
}
}
// for now: by default load only one size: the default size
f->req_sizes[0] = 0;
for(i = 1; i < MAX_FONT_SIZES; ++i)
f->req_sizes[i] = -1;
if(Cmd_Argc() >= 3)
{
for(i = 0; i < Cmd_Argc()-3; ++i)
{
sz = atof(Cmd_Argv(i+3));
if (sz > 0.001f && sz < 1000.0f) // do not use crap sizes
f->req_sizes[i] = sz;
}
}
LoadFont(true, mainfont, f);
}
/*
===============
Draw_Init
===============
*/
static void gl_draw_start(void)
{
int i;
drawtexturepool = R_AllocTexturePool();
numcachepics = 0;
memset(cachepichash, 0, sizeof(cachepichash));
font_start();
for(i = 0; i < MAX_FONTS; ++i)
LoadFont(false, va("gfx/font_%s", dp_fonts[i].title), &dp_fonts[i]);
// draw the loading screen so people have something to see in the newly opened window
SCR_UpdateLoadingScreen(true);
}
static void gl_draw_shutdown(void)
{
font_shutdown();
R_FreeTexturePool(&drawtexturepool);
numcachepics = 0;
memset(cachepichash, 0, sizeof(cachepichash));
}
static void gl_draw_newmap(void)
{
font_newmap();
}
void GL_Draw_Init (void)
{
int i, j;
Cvar_RegisterVariable(&r_font_postprocess_blur);
Cvar_RegisterVariable(&r_font_postprocess_outline);
Cvar_RegisterVariable(&r_font_postprocess_shadow_x);
Cvar_RegisterVariable(&r_font_postprocess_shadow_y);
Cvar_RegisterVariable(&r_font_postprocess_shadow_z);
Cvar_RegisterVariable(&r_font_hinting);
Cvar_RegisterVariable(&r_font_antialias);
Cvar_RegisterVariable(&r_textshadow);
Cvar_RegisterVariable(&r_textbrightness);
Cvar_RegisterVariable(&r_textcontrast);
Cmd_AddCommand ("loadfont",LoadFont_f, "loadfont function tganame loads a font; example: loadfont console gfx/veramono; loadfont without arguments lists the available functions");
R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
strlcpy(FONT_DEFAULT->title, "default", sizeof(FONT_DEFAULT->title));
strlcpy(FONT_DEFAULT->texpath, "gfx/conchars", sizeof(FONT_DEFAULT->texpath));
strlcpy(FONT_CONSOLE->title, "console", sizeof(FONT_CONSOLE->title));
strlcpy(FONT_SBAR->title, "sbar", sizeof(FONT_SBAR->title));
strlcpy(FONT_NOTIFY->title, "notify", sizeof(FONT_NOTIFY->title));
strlcpy(FONT_CHAT->title, "chat", sizeof(FONT_CHAT->title));
strlcpy(FONT_CENTERPRINT->title, "centerprint", sizeof(FONT_CENTERPRINT->title));
strlcpy(FONT_INFOBAR->title, "infobar", sizeof(FONT_INFOBAR->title));
strlcpy(FONT_MENU->title, "menu", sizeof(FONT_MENU->title));
for(i = 0, j = 0; i < MAX_USERFONTS; ++i)
if(!FONT_USER[i].title[0])
dpsnprintf(FONT_USER[i].title, sizeof(FONT_USER[i].title), "user%d", j++);
}
void _DrawQ_Setup(void)
{
r_viewport_t viewport;
if (r_refdef.draw2dstage)
return;
r_refdef.draw2dstage = true;
CHECKGLERROR
R_Viewport_InitOrtho(&viewport, &identitymatrix, r_refdef.view.x, vid.height - r_refdef.view.y - r_refdef.view.height, r_refdef.view.width, r_refdef.view.height, 0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100, NULL);
R_SetViewport(&viewport);
GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
qglDepthFunc(GL_LEQUAL);CHECKGLERROR
qglDisable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
R_EntityMatrix(&identitymatrix);
GL_DepthMask(true);
GL_DepthRange(0, 1);
GL_PolygonOffset(0, 0);
GL_DepthTest(false);
GL_Color(1,1,1,1);
GL_AlphaTest(false);
GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
static void _DrawQ_ProcessDrawFlag(int flags)
{
_DrawQ_Setup();
CHECKGLERROR
if(flags == DRAWFLAG_ADDITIVE)
GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
else if(flags == DRAWFLAG_MODULATE)
GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
else if(flags == DRAWFLAG_2XMODULATE)
GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
else if(flags == DRAWFLAG_SCREEN)
GL_BlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ONE);
else
GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void DrawQ_Pic(float x, float y, cachepic_t *pic, float width, float height, float red, float green, float blue, float alpha, int flags)
{
float floats[20];
_DrawQ_ProcessDrawFlag(flags);
GL_Color(red, green, blue, alpha);
R_Mesh_VertexPointer(floats, 0, 0);
R_Mesh_ColorPointer(NULL, 0, 0);
R_Mesh_ResetTextureState();
if (pic)
{
if (width == 0)
width = pic->width;
if (height == 0)
height = pic->height;
R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1);
R_Mesh_TexCoordPointer(0, 2, floats + 12, 0, 0);
#if 1
floats[12] = 0.0f;floats[13] = 0.0f;
floats[14] = 1.0f;floats[15] = 0.0f;
floats[16] = 1.0f;floats[17] = 1.0f;
floats[18] = 0.0f;floats[19] = 1.0f;
#else
// AK07: lets be texel correct on the corners
{
float horz_offset = 0.5f / pic->width;
float vert_offset = 0.5f / pic->height;
floats[12] = 0.0f + horz_offset;floats[13] = 0.0f + vert_offset;
floats[14] = 1.0f - horz_offset;floats[15] = 0.0f + vert_offset;
floats[16] = 1.0f - horz_offset;floats[17] = 1.0f - vert_offset;
floats[18] = 0.0f + horz_offset;floats[19] = 1.0f - vert_offset;
}
#endif
}
else
R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1);
floats[2] = floats[5] = floats[8] = floats[11] = 0;
floats[0] = floats[9] = x;
floats[1] = floats[4] = y;
floats[3] = floats[6] = x + width;
floats[7] = floats[10] = y + height;
R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, polygonelement3s, 0, 0);
}
void DrawQ_RotPic(float x, float y, cachepic_t *pic, float width, float height, float org_x, float org_y, float angle, float red, float green, float blue, float alpha, int flags)
{
float floats[20];
float af = DEG2RAD(-angle); // forward
float ar = DEG2RAD(-angle + 90); // right
float sinaf = sin(af);
float cosaf = cos(af);
float sinar = sin(ar);
float cosar = cos(ar);
_DrawQ_ProcessDrawFlag(flags);
GL_Color(red, green, blue, alpha);
R_Mesh_VertexPointer(floats, 0, 0);
R_Mesh_ColorPointer(NULL, 0, 0);
R_Mesh_ResetTextureState();
if (pic)