/
gl_font.c
1474 lines (1303 loc) · 39.5 KB
/
gl_font.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
/**\file gl_font.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2011 Daniel Swanson <danij@dengine.net>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Font Renderer.
*/
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "de_base.h"
#include "de_console.h"
#include "de_graphics.h"
#include "de_system.h"
#include "de_refresh.h"
#include "de_render.h"
#include "bitmapfont.h"
#include "m_args.h"
#include "m_misc.h"
typedef struct fr_state_attributes_s {
int tracking;
float leading;
int shadowOffsetX, shadowOffsetY;
float shadowStrength;
float glitterStrength;
boolean caseScale;
} fr_state_attributes_t;
typedef struct {
int fontIdx;
int attribStackDepth;
fr_state_attributes_t attribStack[FR_MAX_ATTRIB_STACK_DEPTH];
} fr_state_t;
static fr_state_t fr;
fr_state_t* frState = &fr;
typedef struct {
fontid_t font;
float scaleX, scaleY;
float offX, offY;
float angle;
float color[4];
float glitterStrength, shadowStrength;
int shadowOffsetX, shadowOffsetY;
int tracking;
float leading;
boolean typeIn;
boolean caseScale;
struct {
float scale, offset;
} caseMod[2]; // 1=upper, 0=lower
} drawtextstate_t;
static __inline fr_state_attributes_t* currentAttributes(void);
static int topToAscent(bitmapfont_t* font);
static int lineHeight(bitmapfont_t* font, unsigned char ch);
static void drawChar(unsigned char ch, int posX, int posY, bitmapfont_t* font, int alignFlags, short textFlags);
static void drawFlash(int x, int y, int w, int h, int bright);
static int inited = false;
static char smallTextBuffer[FR_SMALL_TEXT_BUFFER_SIZE+1];
static char* largeTextBuffer = NULL;
static size_t largeTextBufferSize = 0;
static int numFonts = 0;
static bitmapfont_t** fonts = 0; // The list of fonts.
static int typeInTime;
static fontid_t getNewFontId(void)
{
assert(inited);
{
int i;
fontid_t max = 0;
for(i = 0; i < numFonts; ++i)
{
const bitmapfont_t* font = fonts[i];
if(BitmapFont_Id(font) > max)
max = BitmapFont_Id(font);
}
return max + 1;
}
}
static fontid_t findFontIdForName(const char* name)
{
assert(inited);
if(name && name[0])
{
int i;
for(i = 0; i < numFonts; ++i)
{
const bitmapfont_t* font = fonts[i];
if(!Str_CompareIgnoreCase(BitmapFont_Name(font), name))
return BitmapFont_Id(font);
}
}
return 0; // Not found.
}
static int findFontIdxForName(const char* name)
{
assert(inited);
if(name && name[0])
{
int i;
for(i = 0; i < numFonts; ++i)
{
const bitmapfont_t* font = fonts[i];
if(!Str_CompareIgnoreCase(BitmapFont_Name(font), name))
return i;
}
}
return -1;
}
static int findFontIdxForId(fontid_t id)
{
assert(inited);
if(id != 0)
{
int i;
for(i = 0; i < numFonts; ++i)
{
const bitmapfont_t* font = fonts[i];
if(BitmapFont_Id(font) == id)
return i;
}
}
return -1;
}
static void destroyFontIdx(int idx)
{
assert(inited);
BitmapFont_Destruct(fonts[idx]);
memmove(&fonts[idx], &fonts[idx + 1], sizeof(*fonts) * (numFonts - idx - 1));
--numFonts;
fonts = realloc(fonts, sizeof(*fonts) * numFonts);
if(fr.fontIdx == idx)
fr.fontIdx = -1;
}
static void destroyFonts(void)
{
assert(inited);
{
while(numFonts)
destroyFontIdx(0);
fonts = 0;
fr.fontIdx = -1;
}
}
static bitmapfont_t* addFont(bitmapfont_t* font)
{
assert(inited && font);
fonts = realloc(fonts, sizeof(*fonts) * ++numFonts);
fonts[numFonts-1] = font;
return font;
}
/**
* Creates a new font record for a file and attempts to prepare it.
*/
static bitmapfont_t* loadFont(const char* name, const char* path)
{
assert(inited);
{
bitmapfont_t* font;
if(0 != (font = addFont(BitmapFont_Construct(getNewFontId(), name, path))))
{
VERBOSE( Con_Message("New font '%s'.\n", Str_Text(BitmapFont_Name(font))) );
}
return font;
}
}
static int topToAscent(bitmapfont_t* font)
{
assert(font);
{
int lineHeight = BitmapFont_Leading(font);
if(lineHeight == 0)
return 0;
return lineHeight - BitmapFont_Ascent(font);
}
}
static int lineHeight(bitmapfont_t* font, unsigned char ch)
{
int ascent = BitmapFont_Ascent(font);
if(ascent != 0)
return ascent;
return BitmapFont_CharHeight(font, ch);
}
static __inline fr_state_attributes_t* currentAttributes(void)
{
return fr.attribStack + fr.attribStackDepth;
}
int FR_Init(void)
{
if(inited)
return -1; // No reinitializations...
inited = true;
numFonts = 0;
fonts = 0; // No fonts!
fr.fontIdx = -1;
FR_LoadDefaultAttrib();
typeInTime = 0;
return 0;
}
void FR_Shutdown(void)
{
if(!inited)
return;
destroyFonts();
inited = false;
}
fontid_t FR_CreateFontFromDef(ded_compositefont_t* def)
{
bitmapfont_t* font;
fontid_t id;
// Valid name?
if(!def->id[0])
Con_Error("FR_CreateFontFromDef: A valid name is required (not NULL or zero-length).");
// Already a font by this name?
if(0 != (id = FR_FindFontForName(def->id)))
{
Con_Error("FR_CreateFontFromDef: Failed to create font '%s', name already in use.");
return 0; // Unreachable.
}
// A new font.
font = addFont(BitmapFont_Construct(getNewFontId(), def->id, 0));
{ int i;
for(i = 0; i < def->charMapCount.num; ++i)
{
ddstring_t* path;
if(!def->charMap[i].path)
continue;
if(0 != (path = Uri_Resolved(def->charMap[i].path)))
{
BitmapFont_CharSetPatch(font, def->charMap[i].ch, Str_Text(path));
Str_Delete(path);
}
}}
return BitmapFont_Id(font);
}
fontid_t FR_LoadSystemFont(const char* name, const char* searchPath)
{
assert(name && searchPath && searchPath[0]);
{
bitmapfont_t* font;
if(!inited)
{
#if _DEBUG
Con_Message("Warning: Font renderer has not yet been initialized.");
#endif
return 0;
}
if(0 != findFontIdForName(name) || 0 == F_Access(searchPath))
{
return 0; // Error.
}
VERBOSE2( Con_Message("Preparing font \"%s\"...\n", F_PrettyPath(searchPath)) );
if(0 == (font = loadFont(name, searchPath)))
{ // Error.
Con_Message("Warning: Unknown format for %s\n", searchPath);
return 0;
}
// Make this the current font.
fr.fontIdx = numFonts-1;
return BitmapFont_Id(font);
}
}
void FR_DestroyFont(fontid_t id)
{
int idx;
if(!inited)
Con_Error("FR_DestroyFont: Font renderer has not yet been initialized.");
if(-1 != (idx = findFontIdxForId(id)))
{
destroyFontIdx(idx);
if(fr.fontIdx == idx)
fr.fontIdx = -1;
}
}
void FR_Update(void)
{
assert(inited);
if(novideo || isDedicated)
return;
{ int i;
for(i = 0; i < numFonts; ++i)
{
BitmapFont_DeleteGLTextures(fonts[i]);
BitmapFont_DeleteGLDisplayLists(fonts[i]);
}}
}
void FR_Ticker(timespan_t ticLength)
{
static trigger_t fixed = { 1 / 35.0 };
if(!inited)
return;
// Restricted to fixed 35 Hz ticks.
if(!M_RunTrigger(&fixed, ticLength))
return; // It's too soon.
++typeInTime;
}
void FR_ResetTypeInTimer(void)
{
if(!inited)
return;
typeInTime = 0;
}
bitmapfont_t* FR_BitmapFontForId(fontid_t id)
{
assert(inited);
{ int idx;
if(-1 != (idx = findFontIdxForId(id)))
return fonts[idx];
}
return 0;
}
fontid_t FR_FindFontForName(const char* name)
{
if(!inited)
{
#if _DEBUG
Con_Message("Warning: Font renderer has not yet been initialized.");
#endif
return 0;
}
return findFontIdForName(name);
}
int FR_GetFontIdx(fontid_t id)
{
int idx;
if(!inited)
Con_Error("FR_GetFontIdx: Font renderer has not yet been initialized.");
if(-1 == (idx = findFontIdxForId(id)))
Con_Message("Warning:FR_GetFontIdx: Unknown ID %u.\n", id);
return idx;
}
void FR_SetFont(fontid_t id)
{
int idx;
if(!inited)
Con_Error("FR_SetFont: Font renderer has not yet been initialized.");
idx = FR_GetFontIdx(id);
if(idx == -1)
return; // No such font.
fr.fontIdx = idx;
}
fontid_t FR_Font(void)
{
if(!inited)
Con_Error("FR_Font: Font renderer has not yet been initialized.");
if(fr.fontIdx != -1)
return BitmapFont_Id(fonts[fr.fontIdx]);
return 0;
}
void FR_LoadDefaultAttrib(void)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_LoadDefaultAttrib: Font renderer has not yet been initialized.");
sat->leading = FR_DEF_ATTRIB_LEADING;
sat->tracking = FR_DEF_ATTRIB_TRACKING;
sat->shadowStrength = FR_DEF_ATTRIB_SHADOW_STRENGTH;
sat->shadowOffsetX = FR_DEF_ATTRIB_SHADOW_XOFFSET;
sat->shadowOffsetY = FR_DEF_ATTRIB_SHADOW_YOFFSET;
sat->glitterStrength = FR_DEF_ATTRIB_GLITTER_STRENGTH;
sat->caseScale = FR_DEF_ATTRIB_CASE_SCALE;
}
void FR_PushAttrib(void)
{
if(!inited)
Con_Error("FR_PushAttrib: Font renderer has not yet been initialized.");
if(fr.attribStackDepth == FR_MAX_ATTRIB_STACK_DEPTH)
{
Con_Error("FR_PushAttrib: STACK_OVERFLOW.");
exit(1); // Unreachable.
}
memcpy(fr.attribStack + fr.attribStackDepth + 1, fr.attribStack + fr.attribStackDepth, sizeof(fr.attribStack[0]));
++fr.attribStackDepth;
}
void FR_PopAttrib(void)
{
if(!inited)
Con_Error("FR_PopAttrib: Font renderer has not yet been initialized.");
if(fr.attribStackDepth == 0)
{
Con_Error("FR_PopAttrib: STACK_UNDERFLOW.");
exit(1); // Unreachable.
}
--fr.attribStackDepth;
}
float FR_Leading(void)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_Leading: Font renderer has not yet been initialized.");
return sat->leading;
}
void FR_SetLeading(float value)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_SetLeading: Font renderer has not yet been initialized.");
sat->leading = value;
}
int FR_Tracking(void)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_Tracking: Font renderer has not yet been initialized.");
return sat->tracking;
}
void FR_SetTracking(int value)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_SetTracking: Font renderer has not yet been initialized.");
sat->tracking = value;
}
void FR_ShadowOffset(int* offsetX, int* offsetY)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_ShadowOffset: Font renderer has not yet been initialized.");
if(NULL != offsetX) *offsetX = sat->shadowOffsetX;
if(NULL != offsetY) *offsetY = sat->shadowOffsetY;
}
void FR_SetShadowOffset(int offsetX, int offsetY)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_SetShadowOffset: Font renderer has not yet been initialized.");
sat->shadowOffsetX = offsetX;
sat->shadowOffsetY = offsetY;
}
float FR_ShadowStrength(void)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_ShadowStrength: Font renderer has not yet been initialized.");
return sat->shadowStrength;
}
void FR_SetShadowStrength(float value)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_SetShadowStrength: Font renderer has not yet been initialized.");
sat->shadowStrength = MINMAX_OF(0, value, 1);
}
float FR_GlitterStrength(void)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_GlitterStrength: Font renderer has not yet been initialized.");
return sat->glitterStrength;
}
void FR_SetGlitterStrength(float value)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_SetGlitterStrength: Font renderer has not yet been initialized.");
sat->glitterStrength = MINMAX_OF(0, value, 1);
}
boolean FR_CaseScale(void)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_CaseScale: Font renderer has not yet been initialized.");
return sat->caseScale;
}
void FR_SetCaseScale(boolean value)
{
fr_state_attributes_t* sat = currentAttributes();
if(!inited)
Con_Error("FR_SetCaseScale: Font renderer has not yet been initialized.");
sat->caseScale = value;
}
void FR_CharDimensions(int* width, int* height, unsigned char ch)
{
if(!inited)
Con_Error("FR_CharDimensions: Font renderer has not yet been initialized.");
BitmapFont_CharDimensions(fonts[fr.fontIdx], width, height, ch);
}
int FR_CharWidth(unsigned char ch)
{
if(!inited)
Con_Error("FR_CharWidth: Font renderer has not yet been initialized.");
if(fr.fontIdx != -1)
return BitmapFont_CharWidth(fonts[fr.fontIdx],ch);
return 0;
}
int FR_CharHeight(unsigned char ch)
{
if(!inited)
Con_Error("FR_CharHeight: Font renderer has not yet been initialized.");
if(fr.fontIdx != -1)
return BitmapFont_CharHeight(fonts[fr.fontIdx], ch);
return 0;
}
int FR_SingleLineHeight(const char* text)
{
if(!inited)
Con_Error("FR_SingleLineHeight: Font renderer has not yet been initialized.");
if(fr.fontIdx == -1 || !text)
return 0;
{ int ascent = BitmapFont_Ascent(fonts[fr.fontIdx]);
if(ascent != 0)
return ascent;
}
return BitmapFont_CharHeight(fonts[fr.fontIdx], (unsigned char)text[0]);
}
int FR_GlyphTopToAscent(const char* text)
{
int lineHeight;
if(!inited)
Con_Error("FR_GlyphTopToAscent: Font renderer has not yet been initialized.");
if(fr.fontIdx == -1 || !text)
return 0;
lineHeight = BitmapFont_Leading(fonts[fr.fontIdx]);
if(lineHeight == 0)
return 0;
return lineHeight - BitmapFont_Ascent(fonts[fr.fontIdx]);
}
static void drawTextFragment(const char* string, int x, int y, int alignFlags,
short textFlags, int initialCount)
{
assert(inited && string && string[0] && fr.fontIdx != -1);
{
bitmapfont_t* cf = fonts[fr.fontIdx];
fr_state_attributes_t* sat = currentAttributes();
boolean noTypein = (textFlags & DTF_NO_TYPEIN) != 0;
boolean noGlitter = (sat->glitterStrength <= 0 || (textFlags & DTF_NO_GLITTER) != 0);
boolean noShadow = (sat->shadowStrength <= 0 || (textFlags & DTF_NO_SHADOW) != 0 ||
(BitmapFont_Flags(cf) & BFF_HAS_EMBEDDEDSHADOW) != 0);
float glitter = (noGlitter? 0 : sat->glitterStrength), glitterMul;
float shadow = (noShadow ? 0 : sat->shadowStrength), shadowMul;
int w, h, cx, cy, count, yoff;
float origColor[4], flashColor[3];
unsigned char c;
const char* ch;
if(alignFlags & ALIGN_RIGHT)
x -= FR_TextWidth(string);
else if(!(alignFlags & ALIGN_LEFT))
x -= FR_TextWidth(string)/2;
if(alignFlags & ALIGN_BOTTOM)
y -= FR_TextHeight(string);
else if(!(alignFlags & ALIGN_TOP))
y -= FR_TextHeight(string)/2;
if(!(noTypein && noShadow && noGlitter))
glGetFloatv(GL_CURRENT_COLOR, origColor);
if(!(noTypein && noGlitter))
{
flashColor[CR] = (1 + 2 * origColor[0]) / 3;
flashColor[CG] = (1 + 2 * origColor[1]) / 3;
flashColor[CB] = (1 + 2 * origColor[2]) / 3;
}
if(0 != BitmapFont_GLTextureName(cf))
{
glBindTexture(GL_TEXTURE_2D, BitmapFont_GLTextureName(cf));
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glScalef(1.f / BitmapFont_TextureWidth(cf),
1.f / BitmapFont_TextureHeight(cf), 1.f);
}
{ int pass;
for(pass = (noShadow? 1 : 0); pass < 2; ++pass)
{
count = initialCount;
ch = string;
cx = x + (pass == 0? sat->shadowOffsetX : 0);
cy = y + (pass == 0? sat->shadowOffsetY : 0);
for(;;)
{
c = *ch++;
yoff = 0;
glitter = (noGlitter? 0 : sat->glitterStrength);
glitterMul = 0;
shadow = (noShadow? 0 : sat->shadowStrength);
shadowMul = (noShadow? 0 : origColor[3]);
if(!(noTypein && noShadow && noGlitter))
glColor4fv(origColor);
// Do the type-in effect?
if(!noTypein && (pass || (!noShadow && !pass)))
{
int maxCount = (typeInTime > 0? typeInTime * 2 : 0);
if(pass)
{
if(!noGlitter)
{
if(count == maxCount)
{
glitterMul = 1;
flashColor[CR] = origColor[0];
flashColor[CG] = origColor[1];
flashColor[CB] = origColor[2];
}
else if(count + 1 == maxCount)
{
glitterMul = 0.88f;
flashColor[CR] = (1 + origColor[0]) / 2;
flashColor[CG] = (1 + origColor[1]) / 2;
flashColor[CB] = (1 + origColor[2]) / 2;
}
else if(count + 2 == maxCount)
{
glitterMul = 0.75f;
flashColor[CR] = origColor[0];
flashColor[CG] = origColor[1];
flashColor[CB] = origColor[2];
}
else if(count + 3 == maxCount)
{
glitterMul = 0.5f;
flashColor[CR] = origColor[0];
flashColor[CG] = origColor[1];
flashColor[CB] = origColor[2];
}
else if(count > maxCount)
{
break;
}
}
else if(count > maxCount)
{
break;
}
}
else
{
if(count == maxCount)
{
shadowMul = 0;
}
else if(count + 1 == maxCount)
{
shadowMul *= .25f;
}
else if(count + 2 == maxCount)
{
shadowMul *= .5f;
}
else if(count + 3 == maxCount)
{
shadowMul *= .75f;
}
else if(count > maxCount)
{
break;
}
}
}
count++;
if(!c || c == '\n')
break;
w = FR_CharWidth(c);
h = FR_CharHeight(c);
if(' ' != c &&
(0 != BitmapFont_GLTextureName(cf) || 0 != BitmapFont_CharPatch(cf, c)))
{
// A non-white-space character we have a glyph for.
if(pass)
{
// The character itself.
drawChar(c, cx, cy + yoff, cf, ALIGN_TOPLEFT, DTF_NO_EFFECTS);
if(!noGlitter && glitter > 0)
{ // Do something flashy.
glColor4f(flashColor[CR], flashColor[CG], flashColor[CB], glitter * glitterMul);
drawFlash(cx, cy + yoff, w, h, true);
}
}
else if(!noShadow)
{
glColor4f(1, 1, 1, shadow * shadowMul);
drawFlash(cx, cy + yoff, w, h, false);
}
}
cx += w + sat->tracking;
}
}}
// Restore previous GL-state.
if(0 != BitmapFont_GLTextureName(cf))
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
if(!(noTypein && noGlitter && noShadow))
glColor4fv(origColor);
}
}
void FR_DrawChar3(unsigned char ch, int x, int y, int alignFlags, short textFlags)
{
bitmapfont_t* cf;
if(fr.fontIdx == -1)
return;
cf = fonts[fr.fontIdx];
if(0 != BitmapFont_GLTextureName(cf))
{
glBindTexture(GL_TEXTURE_2D, BitmapFont_GLTextureName(cf));
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glScalef(1.f / BitmapFont_TextureWidth(cf),
1.f / BitmapFont_TextureHeight(cf), 1.f);
}
drawChar(ch, x, y, cf, alignFlags, textFlags);
if(0 != BitmapFont_GLTextureName(cf))
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
}
void FR_DrawChar2(unsigned char ch, int x, int y, int alignFlags)
{
FR_DrawChar3(ch, x, y, alignFlags, DEFAULT_DRAWFLAGS);
}
void FR_DrawChar(unsigned char ch, int x, int y)
{
FR_DrawChar2(ch, x, y, DEFAULT_ALIGNFLAGS);
}
static void drawChar(unsigned char ch, int posX, int posY, bitmapfont_t* font,
int alignFlags, short textFlags)
{
float x = (float) posX, y = (float) posY;
DGLuint tex;
if(alignFlags & ALIGN_RIGHT)
x -= BitmapFont_CharWidth(font, ch);
else if(!(alignFlags & ALIGN_LEFT))
x -= BitmapFont_CharWidth(font, ch) / 2;
if(alignFlags & ALIGN_BOTTOM)
y -= topToAscent(font) + lineHeight(font, ch);
else if(!(alignFlags & ALIGN_TOP))
y -= (topToAscent(font) + lineHeight(font, ch))/2;
if(renderWireframe)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_TEXTURE_2D);
}
else if(0 != (tex = BitmapFont_GLTextureName(font)))
{
GL_BindTexture(tex, GL_NEAREST);
}
else
{
patchid_t patch = BitmapFont_CharPatch(font, ch);
if(patch == 0)
return;
GL_BindTexture(font->_chars[ch].tex, (filterUI ? GL_LINEAR : GL_NEAREST));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
glMatrixMode(GL_MODELVIEW);
glTranslatef(x, y, 0);
if(font->_chars[ch].dlist)
{
GL_CallList(font->_chars[ch].dlist);
}
else
{
int s[2], t[2], x = 0, y = 0, w, h;
BitmapFont_CharCoords(font, &s[0], &s[1], &t[0], &t[1], ch);
w = s[1] - s[0];
h = t[1] - t[0];
if(!BitmapFont_GLTextureName(font))
{
x = font->_chars[ch].x;
y = font->_chars[ch].y;
w = font->_chars[ch].w;
h = font->_chars[ch].h;
}
x -= font->_marginWidth;
y -= font->_marginHeight;
glBegin(GL_QUADS);
// Upper left.
glTexCoord2i(s[0], t[0]);
glVertex2f(x, y);
// Upper Right.
glTexCoord2i(s[1], t[0]);
glVertex2f(x + w, y);
// Lower right.
glTexCoord2i(s[1], t[1]);
glVertex2f(x + w, y + h);
// Lower left.
glTexCoord2i(s[0], t[1]);
glVertex2f(x, y + h);
glEnd();
}
glMatrixMode(GL_MODELVIEW);
glTranslatef(-x, -y, 0);
if(renderWireframe)
{
/// \fixme do not assume previous state.
glEnable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
static void drawFlash(int x, int y, int w, int h, int bright)
{
float fsize = 4.f + bright, fw = fsize * w / 2.0f, fh = fsize * h / 2.0f;
// Don't draw anything for very small letters.
if(h <= 4)
return;
x += (int) (w / 2.0f - fw / 2);
y += (int) (h / 2.0f - fh / 2);
w = (int) fw;
h = (int) fh;
glBindTexture(GL_TEXTURE_2D, GL_PrepareLSTexture(LST_DYNAMIC));
if(bright)
GL_BlendMode(BM_ADD);
else
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
GL_BlendMode(BM_NORMAL);
}
static int C_DECL compareFontName(const void* a, const void* b)
{
return Str_CompareIgnoreCase(*((const ddstring_t**)a), Str_Text(*((const ddstring_t**)b)));
}
ddstring_t** FR_CollectFontNames(int* count)
{
assert(inited);
if(numFonts != 0)
{
ddstring_t** list = malloc(sizeof(*list) * (numFonts+1));
int i;
for(i = 0; i < numFonts; ++i)
{
list[i] = Str_New();
Str_Copy(list[i], BitmapFont_Name(fonts[i]));
}
list[i] = 0; // Terminate.
qsort(list, numFonts, sizeof(*list), compareFontName);
if(count)
*count = numFonts;
return list;
}
if(count)
*count = 0;
return 0;
}
/**
* Expected: <whitespace> = <whitespace> <float>
*/
static float parseFloat(char** str)
{
float value;
char* end;
*str = M_SkipWhite(*str);
if(**str != '=')
return 0; // Now I'm confused!
*str = M_SkipWhite(*str + 1);
value = (float) strtod(*str, &end);
*str = end;
return value;
}
static void parseParamaterBlock(char** strPtr, drawtextstate_t* state, int* numBreaks)
{