-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathgl_draw.c
2105 lines (1744 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.
$Id: gl_draw.c,v 1.104 2007-10-18 05:28:23 dkure Exp $
*/
#include "quakedef.h"
#include "gl_model.h"
#include "gl_local.h"
#include "wad.h"
#include "stats_grid.h"
#include "utils.h"
#include "sbar.h"
#include "common_draw.h"
#ifndef __APPLE__
#include "tr_types.h"
#endif
extern cvar_t crosshair, cl_crossx, cl_crossy, crosshaircolor, crosshairsize;
extern cvar_t scr_coloredText, con_shift, hud_faderankings;
cvar_t scr_conalpha = {"scr_conalpha", "0.8"};
cvar_t scr_conback = {"scr_conback", "1"};
void OnChange_scr_conpicture(cvar_t *, char *, qbool *);
cvar_t scr_conpicture = {"scr_conpicture", "conback", 0, OnChange_scr_conpicture};
cvar_t scr_menualpha = {"scr_menualpha", "0.7"};
cvar_t scr_menudrawhud = {"scr_menudrawhud", "0"};
void OnChange_gl_crosshairimage(cvar_t *, char *, qbool *);
cvar_t gl_crosshairimage = {"crosshairimage", "", 0, OnChange_gl_crosshairimage};
void OnChange_gl_consolefont (cvar_t *, char *, qbool *);
cvar_t gl_consolefont = {"gl_consolefont", "povo5", 0, OnChange_gl_consolefont};
cvar_t gl_alphafont = {"gl_alphafont", "1"};
cvar_t gl_crosshairalpha = {"crosshairalpha", "1"};
void OnChange_gl_smoothfont (cvar_t *var, char *string, qbool *cancel);
cvar_t gl_smoothfont = {"gl_smoothfont", "1", 0, OnChange_gl_smoothfont};
byte *draw_chars; // 8*8 graphic characters
mpic_t *draw_disc;
static mpic_t *draw_backtile;
int translate_texture;
int char_textures[MAX_CHARSETS];
int char_range[MAX_CHARSETS];
static mpic_t conback;
#define NUMCROSSHAIRS 6
int crosshairtextures[NUMCROSSHAIRS];
int crosshairtexture_txt;
mpic_t crosshairpic;
static byte customcrosshairdata[64];
#define CROSSHAIR_NONE 0
#define CROSSHAIR_TXT 1
#define CROSSHAIR_IMAGE 2
static int customcrosshair_loaded = CROSSHAIR_NONE;
static byte crosshairdata[NUMCROSSHAIRS][64] = {
{
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff,
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
},
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
},
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
},
{
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
0xff, 0xff, 0xfe, 0xff, 0xff, 0xfe, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0xff, 0xff, 0xfe, 0xff, 0xff,
0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe
},
{
0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xff,
0xfe, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xfe, 0xff,
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xff,
0xfe, 0xff, 0xfe, 0xfe, 0xfe, 0xff, 0xfe, 0xff,
0xff, 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff,
0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
},
{
0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
0xfe, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfe, 0xff,
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
}
};
// call it when gl_smoothfont changed or after we load charset
static void Apply_OnChange_gl_smoothfont(int value)
{
int i;
if (!char_textures[0])
return;
for (i = 0; i < MAX_CHARSETS; i++)
{
if (!char_textures[i])
break;
GL_Bind(char_textures[i]);
if (value)
{
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else
{
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
}
}
void OnChange_gl_smoothfont (cvar_t *var, char *string, qbool *cancel)
{
Apply_OnChange_gl_smoothfont( Q_atoi(string) );
}
/*
* Draw_CopyMPICKeepSize
* Copy data from src to dst but keep unchanged dst->width and dst->height
*/
static void Draw_CopyMPICKeepSize(mpic_t *dst, mpic_t *src)
{
byte width[sizeof(dst->width)];
byte height[sizeof(dst->height)];
// remember particular fields
memcpy(width, (byte*)&dst->width, sizeof(width));
memcpy(height, (byte*)&dst->height, sizeof(height));
// bit by bit copy
*dst = *src;
// restore fields
memcpy((byte*)&dst->width, width, sizeof(width));
memcpy((byte*)&dst->height, height, sizeof(height));
}
void OnChange_scr_conpicture(cvar_t *v, char *s, qbool *cancel)
{
mpic_t *pic_24bit;
if (!s[0])
return;
if (!(pic_24bit = GL_LoadPicImage(va("gfx/%s", s), "conback", 0, 0, 0)))
{
Com_Printf("Couldn't load image gfx/%s\n", s);
return;
}
Draw_CopyMPICKeepSize(&conback, pic_24bit);
Draw_AdjustConback();
}
void OnChange_gl_crosshairimage(cvar_t *v, char *s, qbool *cancel)
{
mpic_t *pic;
customcrosshair_loaded &= ~CROSSHAIR_IMAGE;
if (!s[0])
return;
if (!(pic = Draw_CachePicSafe(va("crosshairs/%s", s), false, true)))
{
Com_Printf("Couldn't load image %s\n", s);
return;
}
crosshairpic = *pic;
customcrosshair_loaded |= CROSSHAIR_IMAGE;
}
void customCrosshair_Init(void)
{
char ch;
vfsfile_t *f;
vfserrno_t err;
int i = 0, c;
customcrosshair_loaded = CROSSHAIR_NONE;
crosshairtexture_txt = 0;
if (!(f = FS_OpenVFS("crosshairs/crosshair.txt", "rb", FS_ANY)))
return;
while (i < 64)
{
VFS_READ(f, &ch, sizeof(char), &err);
if (err == VFSERR_EOF)
{
Com_Printf("Invalid format in crosshair.txt (Need 64 X's and O's)\n");
VFS_CLOSE(f);
return;
}
c = ch;
if (isspace(c))
continue;
if (tolower(c) != 'x' && tolower(c) != 'o')
{
Com_Printf("Invalid format in crosshair.txt (Only X's and O's and whitespace permitted)\n");
VFS_CLOSE(f);
return;
}
customcrosshairdata[i++] = (c == 'x' || c == 'X') ? 0xfe : 0xff;
}
VFS_CLOSE(f);
crosshairtexture_txt = GL_LoadTexture ("cross:custom", 8, 8, customcrosshairdata, TEX_ALPHA, 1);
customcrosshair_loaded |= CROSSHAIR_TXT;
}
void Draw_InitCrosshairs(void)
{
int i;
char str[256] = {0};
memset(&crosshairpic, 0, sizeof(crosshairpic));
for (i = 0; i < NUMCROSSHAIRS; i++)
{
snprintf(str, sizeof(str), "cross:hardcoded%d", i);
crosshairtextures[i] = GL_LoadTexture (str, 8, 8, crosshairdata[i], TEX_ALPHA, 1);
}
customCrosshair_Init(); // safe re-init
snprintf(str, sizeof(str), "%s", gl_crosshairimage.string);
Cvar_Set(&gl_crosshairimage, str);
}
float overall_alpha = 1.0;
void Draw_SetOverallAlpha(float alpha)
{
clamp(alpha, 0.0, 1.0);
overall_alpha = alpha;
}
void Draw_EnableScissorRectangle(int x, int y, int width, int height)
{
float resdif_w = (glwidth / (float)vid.conwidth);
float resdif_h = (glheight / (float)vid.conheight);
glEnable(GL_SCISSOR_TEST);
glScissor(
Q_rint(x * resdif_w),
Q_rint((vid.conheight - (y + height)) * resdif_h),
Q_rint(width * resdif_w),
Q_rint(height * resdif_h));
}
void Draw_EnableScissor(int left, int right, int top, int bottom)
{
Draw_EnableScissorRectangle(left, top, (right - left), (bottom - top));
}
void Draw_DisableScissor(void)
{
glDisable(GL_SCISSOR_TEST);
}
//
// =============================================================================
// Scrap allocation
//
// Allocate all the little status bar objects into a single texture
// to crutch up stupid hardware / drivers
// =============================================================================
// Some cards have low quality of alpha pics, so load the pics
// without transparent pixels into a different scrap block.
// scrap 0 is solid pics, 1 is transparent.
#define MAX_SCRAPS 2 // funny, but you can't change this size unless you rewrote code,
// you can looks in FTE how they done it.
// there really no point to split transparent and solid images.
#define BLOCK_WIDTH 256
#define BLOCK_HEIGHT 256
static int scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
static byte scrap_texels[MAX_SCRAPS][BLOCK_WIDTH*BLOCK_HEIGHT];
static int scrap_dirty; // Bit mask.
static int scrap_texnum[MAX_SCRAPS];
// Returns false if allocation failed.
static qbool Scrap_AllocBlock (int scrapnum, int w, int h, int *x, int *y)
{
int i, j, best, best2;
best = BLOCK_HEIGHT;
for (i = 0; i < BLOCK_WIDTH - w; i++)
{
best2 = 0;
for (j = 0; j < w; j++)
{
if (scrap_allocated[scrapnum][i + j] >= best)
break;
if (scrap_allocated[scrapnum][i + j] > best2)
best2 = scrap_allocated[scrapnum][i + j];
}
if (j == w)
{
// This is a valid spot.
*x = i;
*y = best = best2;
}
}
if (best + h > BLOCK_HEIGHT)
return false;
for (i = 0; i < w; i++)
scrap_allocated[scrapnum][*x + i] = best + h;
scrap_dirty |= (1 << scrapnum);
return true;
}
static void Scrap_Upload (void)
{
int i;
for (i = 0; i < MAX_SCRAPS; i++)
{
// is dirty?
if (scrap_dirty & (1 << i))
{
char id[64];
// generate id
snprintf(id, sizeof(id), "scrap:%d", i);
// upload it
scrap_texnum[i] = GL_LoadTexture(id, BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[i], TEX_ALPHA | TEX_NOSCALE, 1);
}
}
// no we are clear!
scrap_dirty = 0;
}
static void Scap_Init(void)
{
memset (scrap_allocated, 0, sizeof(scrap_allocated));
memset (scrap_texels, 0, sizeof(scrap_texels));
memset (scrap_texnum, 0, sizeof(scrap_texnum));
scrap_dirty = ~0; // Bit mask - make all bits dirty!
// upload at least first time, so we set scrap_texnum[], its required
Scrap_Upload();
}
//=============================================================================
// Support Routines
mpic_t *Draw_CacheWadPic (char *name)
{
qpic_t *p;
mpic_t *pic, *pic_24bit;
p = W_GetLumpName (name);
pic = (mpic_t *)p;
if ((pic_24bit = GL_LoadPicImage(va("textures/wad/%s", name), name, 0, 0, TEX_ALPHA)) ||
(pic_24bit = GL_LoadPicImage(va("gfx/%s", name), name, 0, 0, TEX_ALPHA)))
{
// Only keep the size info from the lump. The other stuff is copied from the 24 bit image.
pic->sh = pic_24bit->sh;
pic->sl = pic_24bit->sl;
pic->texnum = pic_24bit->texnum;
pic->th = pic_24bit->th;
pic->tl = pic_24bit->tl;
return pic;
}
// Load little ones into the scrap.
if (p->width < 64 && p->height < 64)
{
int x = 0, y = 0, i, j, k;
// is this pic contain alpha bytes (255)
qbool alpha = memchr(p->data, 255, p->width * p->height) != NULL;
// FIXME: as you can see, MAX_SCRAPS hardcoded to 2 and define is just a fiction!
int texnum = alpha ? 1 : 0;
if (!Scrap_AllocBlock (texnum, p->width, p->height, &x, &y))
{
GL_LoadPicTexture (name, pic, p->data);
return pic;
}
k = 0;
for (i = 0; i < p->height; i++)
{
for (j = 0; j < p->width; j++, k++)
{
scrap_texels[texnum][(y + i) * BLOCK_WIDTH + x + j] = p->data[k];
}
}
pic->sl = (x + 0.25) / (float) BLOCK_WIDTH;
pic->sh = (x + p->width - 0.25) / (float) BLOCK_WIDTH;
pic->tl = (y + 0.25) / (float) BLOCK_WIDTH;
pic->th = (y + p->height - 0.25) / (float) BLOCK_WIDTH;
if (!scrap_texnum[texnum])
{
Com_Printf("Scrap_Upload: texture[%d] still not set, HUD will be broken\n", texnum);
}
pic->texnum = scrap_texnum[texnum];
}
else
{
GL_LoadPicTexture (name, pic, p->data);
}
return pic;
}
//
// Loads an image into the cache.
// Variables:
// crash - Crash the client if loading fails.
// only24bit - Don't fall back to loading the normal 8-bit texture if
// loading the 24-bit version fails.
//
mpic_t *Draw_CachePicSafe (const char *path, qbool crash, qbool only24bit)
{
char stripped_path[MAX_PATH];
char lmp_path[MAX_PATH];
char png_path[MAX_PATH];
mpic_t *pic, *fpic, *pic_24bit;
qbool lmp_found = false;
qpic_t *dat = NULL;
vfsfile_t *v = NULL;
// Check if the picture was already cached.
if ((fpic = CachePic_Find(path)))
return fpic;
// Get the filename without extension.
COM_StripExtension(path, stripped_path);
snprintf(lmp_path, MAX_PATH, "%s.lmp", stripped_path);
snprintf(png_path, MAX_PATH, "%s.png", stripped_path);
// Try loading the pic from disk.
// Only load the 24-bit version of the picture.
if (only24bit)
{
if (!(pic_24bit = GL_LoadPicImage(path, NULL, 0, 0, TEX_ALPHA)))
{
if(crash)
Sys_Error ("Draw_CachePicSafe: failed to load %s", path);
return NULL;
}
// Make a new copy of the returned pic, since it's static
// in GL_LoadPicImage and will be overwritten.
fpic = (mpic_t *)Q_malloc(sizeof(mpic_t));
memcpy(fpic, pic_24bit, sizeof(mpic_t));
return CachePic_Add(path, fpic);
}
// Load the ".lmp" file.
if ((v = FS_OpenVFS(lmp_path, "rb", FS_ANY)))
{
VFS_CLOSE(v);
if (!(dat = (qpic_t *)FS_LoadTempFile(lmp_path, NULL)))
{
if(crash)
Sys_Error ("Draw_CachePicSafe: failed to load %s", lmp_path);
return NULL;
}
lmp_found = true;
// Make sure the width and height are correct.
SwapPic (dat);
}
pic = (mpic_t *)Q_malloc(sizeof(mpic_t));
// Try loading the 24-bit picture.
// If that fails load the data for the lmp instead.
if ((pic_24bit = GL_LoadPicImage(path, NULL, 0, 0, TEX_ALPHA)))
{
memcpy(pic, pic_24bit, sizeof(mpic_t));
// Only use the lmp-data if there was one.
if (lmp_found)
{
pic->width = dat->width;
pic->height = dat->height;
}
}
else if (!dat)
{
Q_free(pic);
if(crash)
Sys_Error ("Draw_CachePicSafe: failed to load %s", path);
return NULL;
}
else
{
pic->width = dat->width;
pic->height = dat->height;
GL_LoadPicTexture (path, pic, dat->data);
}
// Add the picture to the cache.
return CachePic_Add(path, pic);
}
mpic_t *Draw_CachePic (char *path)
{
return Draw_CachePicSafe (path, true, false);
}
/*
* Load_LMP_Charset
*/
static int Load_LMP_Charset (char *name, int flags)
{
int i;
byte buf[128*256];
byte *data;
byte *src, *dest;
int texnum;
int filesize;
// We expect an .lmp to be in QPIC format, but it's ok if it's just raw data.
if (!strcasecmp(name, "charset"))
{
// work around for original charset
data = draw_chars;
filesize = 128*128;
}
else
{
data = FS_LoadTempFile (va("gfx/%s.lmp", name), &filesize);
}
if (!data)
return 0;
if (filesize == 128*128)
{
// Raw data.
}
else if (filesize == 128*128 + 8)
{
qpic_t *p = (qpic_t *)data;
SwapPic (p);
if (p->width != 128 || p->height != 128)
return 0;
data += 8;
}
else
{
return 0;
}
for (i=0 ; i < (256 * 64) ; i++)
{
if (data[i] == 0)
data[i] = 255; // Proper transparent color.
}
// Convert the 128*128 conchars texture to 128*256 leaving
// empty space between rows so that chars don't stumble on
// each other because of texture smoothing.
// This hack costs us 64K of GL texture memory
memset (buf, 255, sizeof(buf));
src = data;
dest = buf;
for (i = 0 ; i < 16 ; i++)
{
memcpy (dest, src, 128*8);
src += 128*8;
dest += 128*8*2;
}
texnum = GL_LoadTexture (va("pic:%s", name), 128, 256, buf, flags, 1);
return texnum;
}
/*
* Load_Locale_Charset
*/
static int Load_Locale_Charset (const char *name, const char *locale, unsigned int num, int range, int flags)
{
char texture[1024], id[256], lmp[256], basename[MAX_QPATH];
if (num >= MAX_CHARSETS)
return 0;
char_range[num] = 0;
COM_StripExtension(name, basename);
snprintf(texture, sizeof(texture), "textures/charsets/%s-%s", basename, locale);
snprintf(id, sizeof(id), "pic:charset-%s", locale);
snprintf(lmp, sizeof(lmp), "conchars-%s", locale);
// try first 24 bit
char_textures[num] = GL_LoadCharsetImage (texture, id, flags);
// then 8 bit
if (!char_textures[num])
char_textures[num] = Load_LMP_Charset (lmp, flags);
char_range[num] = char_textures[num] ? range : 0;
return char_textures[num];
}
static int Draw_LoadCharset(const char *name)
{
int flags = TEX_ALPHA | TEX_NOCOMPRESS | TEX_NOSCALE | TEX_NO_TEXTUREMODE;
int texnum;
int i = 0;
qbool loaded = false;
//
// NOTE: we trying to not change char_textures[0] if we can't load charset.
// This way user still have some charset and can fix issue.
//
if (!strcasecmp(name, "original"))
{
if ((texnum = Load_LMP_Charset("charset", flags)))
{
char_textures[0] = texnum;
loaded = true;
}
}
else if ((texnum = GL_LoadCharsetImage(va("textures/charsets/%s", name), "pic:charset", flags)))
{
char_textures[0] = texnum;
loaded = true;
}
if (!loaded)
{
Com_Printf ("Couldn't load charset \"%s\"\n", name);
return 1;
}
// Load alternate charsets if available
for (i = 1; i < MAX_CHARSETS; ++i)
{
char charsetName[10];
int textureNumber;
snprintf(charsetName, sizeof(charsetName), "%03d", i);
textureNumber = Load_Locale_Charset(name, charsetName, i, i << 8, flags);
// 2.2 only supported -cyr suffix
if (i == 4 && !textureNumber)
Load_Locale_Charset(name, "cyr", 4, 0x0400, flags);
}
// apply gl_smoothfont
Apply_OnChange_gl_smoothfont(gl_smoothfont.integer);
return 0;
}
void OnChange_gl_consolefont(cvar_t *var, char *string, qbool *cancel)
{
*cancel = Draw_LoadCharset(string);
}
void Draw_LoadCharset_f (void)
{
switch (Cmd_Argc())
{
case 1:
Com_Printf("Current charset is \"%s\"\n", gl_consolefont.string);
break;
case 2:
Cvar_Set(&gl_consolefont, Cmd_Argv(1));
break;
default:
Com_Printf("Usage: %s <charset>\n", Cmd_Argv(0));
}
}
void Draw_InitCharset(void)
{
int i;
memset(char_textures, 0, sizeof(char_textures));
memset(char_range, 0, sizeof(char_range));
draw_chars = W_GetLumpName ("conchars");
for (i = 0; i < 256 * 64; i++)
{
if (draw_chars[i] == 0)
draw_chars[i] = 255;
}
Draw_LoadCharset(gl_consolefont.string);
if (!char_textures[0])
Cvar_Set(&gl_consolefont, "original");
if (!char_textures[0])
Sys_Error("Draw_InitCharset: Couldn't load charset");
}
void Draw_InitConback (void);
void Draw_Init (void)
{
extern void HUD_Common_Reset_Group_Pics(void);
Cmd_AddCommand("loadcharset", Draw_LoadCharset_f);
Cvar_SetCurrentGroup(CVAR_GROUP_CONSOLE);
Cvar_Register (&scr_conalpha);
Cvar_Register (&scr_conback);
Cvar_Register (&scr_conpicture);
Cvar_Register (&gl_smoothfont);
Cvar_Register (&gl_consolefont);
Cvar_Register (&gl_alphafont);
Cvar_SetCurrentGroup(CVAR_GROUP_SCREEN);
Cvar_Register (&scr_menualpha);
Cvar_Register (&scr_menudrawhud);
Cvar_SetCurrentGroup(CVAR_GROUP_CROSSHAIR);
Cvar_Register (&gl_crosshairimage);
Cvar_Register (&gl_crosshairalpha);
Cvar_ResetCurrentGroup();
draw_chars = NULL;
draw_disc = draw_backtile = NULL;
W_LoadWadFile("gfx.wad"); // Safe re-init.
CachePics_DeInit();
HUD_Common_Reset_Group_Pics();
GL_Texture_Init(); // Probably safe to re-init now.
// Clear the scrap, should be called ASAP after textures initialization
Scap_Init();
// Load the console background and the charset by hand, because we need to write the version
// string into the background before turning it into a texture.
Draw_InitCharset(); // Safe re-init.
Draw_InitConback(); // Safe re-init.
// Load the crosshair pics
Draw_InitCrosshairs();
// Get the other pics we need.
draw_disc = Draw_CacheWadPic("disc");
draw_backtile = Draw_CacheWadPic("backtile");
}
qbool R_CharAvailable (wchar num)
{
int i;
if (num == (num & 0xff))
return true;
for (i = 1; i < MAX_CHARSETS; i++)
if (char_range[i] == (num & 0xFF00))
return true;
return false;
}
#define CHARSET_CHARS_PER_ROW 16
#define CHARSET_WIDTH 1.0
#define CHARSET_HEIGHT 1.0
#define CHARSET_CHAR_WIDTH (CHARSET_WIDTH / CHARSET_CHARS_PER_ROW)
#define CHARSET_CHAR_HEIGHT (CHARSET_HEIGHT / CHARSET_CHARS_PER_ROW)
// x, y = Pixel position of char.
// num = The character to draw.
// scale = The scale of the character.
// apply_overall_alpha = Should the overall alpha for all drawing apply to this char?
// color = Color!
// bigchar = Draw this char using the big character charset.
// gl_statechange = Change the gl state before drawing?
static void Draw_CharacterBase (int x, int y, wchar num, float scale, qbool apply_overall_alpha, byte color[4], qbool bigchar, qbool gl_statechange)
{
float frow, fcol;
int i;
int slot;
int char_size = (bigchar ? 64 : 8);
// Totally off screen.
if (y <= (-char_size * scale))
return;
// Space.
if (num == 32)
return;
// Only apply overall opacity if it's not fully opague.
apply_overall_alpha = (apply_overall_alpha && (overall_alpha < 1.0));
// Only change the GL state if we're told to. We keep track of the need for GL state changes
// in the string drawing function instead. For character drawing functions we do this every time.
// (For instance, only change color in a string when the actual color changes, instead of doing
// it on each character always).
if (gl_statechange)
{
// Turn on alpha transparency.
if ((gl_alphafont.value || apply_overall_alpha))
{
glDisable(GL_ALPHA_TEST);
}
glEnable(GL_BLEND);
if (scr_coloredText.integer)
{
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
else
{
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
// Set the overall alpha.
glColor4ub(color[0], color[1], color[2], color[3] * overall_alpha);
}
if (bigchar)
{
mpic_t *p = Draw_CachePicSafe(MCHARSET_PATH, false, true);
if (p)
{
int sx = 0;
int sy = 0;
int char_width = (p->width / 8);
int char_height = (p->height / 8);
char c = (char)(num & 0xFF);
Draw_GetBigfontSourceCoords(c, char_width, char_height, &sx, &sy);
if (sx >= 0)
{
// Don't apply alpha here, since we already applied it above.
Draw_SAlphaSubPic(x, y, p, sx, sy, char_width, char_height, (((float)char_size / char_width) * scale), 1);
}
return;
}
// TODO : Force players to have mcharset.png or fallback to overscaling normal font? :s
}
slot = 0;
// Is this is a wchar, find a charset that has the char in it.
if ((num & 0xFF00) != 0)
{
for (i = 1; i < MAX_CHARSETS; i++)
{
if (char_range[i] == (num & 0xFF00))
{
slot = i;
break;
}
}
if (i == MAX_CHARSETS)
num = '?';
}
num &= 0xFF; // Only use the first byte.
// Find the texture coordinates for the character.
frow = (num >> 4) * CHARSET_CHAR_HEIGHT; // row = num * (16 chars per row)
fcol = (num & 0x0F) * CHARSET_CHAR_WIDTH;
GL_Bind(char_textures[slot]);
// Draw the character polygon.
glBegin(GL_QUADS);
{
float scale8 = scale * 8;
float scale8_2 = scale8 * 2;
// Top left.
glTexCoord2f (fcol, frow);
glVertex2f (x, y);
// Top right.
glTexCoord2f(fcol + CHARSET_CHAR_WIDTH, frow);
glVertex2f(x + scale8, y);
// Bottom right.
glTexCoord2f(fcol + CHARSET_CHAR_WIDTH, frow + CHARSET_CHAR_WIDTH);
glVertex2f(x + scale8, y + scale8_2);
// Bottom left.
glTexCoord2f(fcol, frow + CHARSET_CHAR_WIDTH);
glVertex2f(x, y + scale8_2);
}
glEnd();
}
static void Draw_ResetCharGLState(void)
{
glEnable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor4ubv(color_white);
}
void Draw_BigCharacter(int x, int y, char c, color_t color, float scale, float alpha)
{
byte rgba[4];
COLOR_TO_RGBA(color, rgba);
Draw_CharacterBase(x, y, char2wc(c), scale, true, rgba, true, true);
Draw_ResetCharGLState();
}
void Draw_SColoredCharacterW (int x, int y, wchar num, color_t color, float scale)
{