forked from skeeto/pixelcity
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Render.cpp
1084 lines (878 loc) · 29.6 KB
/
Render.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
/*-----------------------------------------------------------------------------
Render.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This is the core of the gl rendering functions. This contains the main
rendering function RenderUpdate (), which initiates the various
other renders in the other modules.
-----------------------------------------------------------------------------*/
#define RENDER_DISTANCE 1280
#define MAX_TEXT 256
#define YOUFAIL(message) {WinPopup (message);return;}
#define HELP_SIZE sizeof(help)
#define COLOR_CYCLE_TIME 10000 //milliseconds
#define COLOR_CYCLE (COLOR_CYCLE_TIME / 4)
#define FONT_COUNT (sizeof (fonts) / sizeof (struct glFont))
#define FONT_SIZE (LOGO_PIXELS - LOGO_PIXELS / 8)
#define BLOOM_SCALING 0.07f
#ifdef WINDOWS
#include <windows.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#ifndef WINDOWS
#include <X11/Xlib.h>
#include <GL/glx.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
#include <errno.h>
#endif
#include "glTypes.h"
#include "Entity.h"
#include "Car.h"
#include "Camera.h"
#include "Ini.h"
#include "Light.h"
#include "Macro.h"
#include "Math.h"
#include "Render.h"
#include "Sky.h"
#include "Texture.h"
#include "World.h"
#include "Win.h"
#include "time_util.h"
#ifdef WINDOWS
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our glRgbaDepth
0, 0, 0, 0, 0, 0, // glRgbaBits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // Accumulation Buffers
0, 0, 0, 0, // Accumulation Bits Ignored
16, // Z-Buffer (Depth Buffer) bits
0, // Stencil Buffers
1, // Auxiliary Buffers
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
#endif
static char help[] =
"ESC - Exit!\n"
"F1 - Show this help screen\n"
"R - Rebuild city\n"
"L - Toggle 'letterbox' mode\n"
"F - Show Framecounter\n"
"W - Toggle Wireframe\n"
"E - Change full-scene effects\n"
"T - Toggle Textures\n"
"G - Toggle Fog\n"
#ifndef WINDOWS
"D - Toggle Frame Delay\n"
#endif
;
struct glFont
{
const char* name;
unsigned base_char;
} fonts[] =
{
{"Courier New", 0},
{"Arial", 0},
{"Times New Roman", 0},
{"Arial Black", 0},
{"Impact", 0},
{"Agency FB", 0},
{"Book Antiqua", 0},
};
#if SCREENSAVER
enum
{
EFFECT_NONE,
EFFECT_BLOOM,
EFFECT_BLOOM_RADIAL,
EFFECT_COLOR_CYCLE,
EFFECT_GLASS_CITY,
EFFECT_COUNT,
EFFECT_DEBUG,
EFFECT_DEBUG_OVERBLOOM
};
#else
enum
{
EFFECT_NONE,
EFFECT_BLOOM,
EFFECT_COUNT,
EFFECT_DEBUG_OVERBLOOM,
EFFECT_DEBUG,
EFFECT_BLOOM_RADIAL,
EFFECT_COLOR_CYCLE,
EFFECT_GLASS_CITY
};
#endif
#ifdef WINDOWS
static HDC hDC;
static HGLRC hRC;
#else
static GLXContext ctx;
#endif
static float render_aspect;
static float fog_distance;
static int render_width;
static int render_height;
static bool letterbox;
static int letterbox_offset;
static int effect;
// static unsigned next_fps; // unused
static unsigned current_fps;
static unsigned frames;
static bool show_wireframe;
static bool flat;
static bool show_fps;
static bool show_fog;
static bool show_help;
/*-----------------------------------------------------------------------------
Draw a clock-ish progress.. widget... thing. It's cute.
-----------------------------------------------------------------------------*/
static void do_progress (float center_x, float center_y, float radius, float opacity, float progress)
{
int i;
int end_angle;
float inner, outer;
float angle;
float s, c;
float gap;
//Outer Ring
gap = radius * 0.05f;
outer = radius;
inner = radius - gap * 2;
glColor4f (1,1,1, opacity);
glBegin (GL_QUAD_STRIP);
for (i = 0; i <= 360; i+= 15) {
angle = (float)i * DEGREES_TO_RADIANS;
s = sinf (angle);
c = -cosf (angle);
glVertex2f (center_x + s * outer, center_y + c * outer);
glVertex2f (center_x + s * inner, center_y + c * inner);
}
glEnd ();
//Progress indicator
glColor4f (1,1,1, opacity);
end_angle = (int)(360 * progress);
outer = radius - gap * 3;
glBegin (GL_TRIANGLE_FAN);
glVertex2f (center_x, center_y);
for (i = 0; i <= end_angle; i+= 3) {
angle = (float)i * DEGREES_TO_RADIANS;
s = sinf (angle);
c = -cosf (angle);
glVertex2f (center_x + s * outer, center_y + c * outer);
}
glEnd ();
//Tic lines
glLineWidth (2.0f);
outer = radius - gap * 1;
inner = radius - gap * 2;
glColor4f (0,0,0, opacity);
glBegin (GL_LINES);
for (i = 0; i <= 360; i+= 15) {
angle = (float)i * DEGREES_TO_RADIANS;
s = sinf (angle);
c = -cosf (angle);
glVertex2f (center_x + s * outer, center_y + c * outer);
glVertex2f (center_x + s * inner, center_y + c * inner);
}
glEnd ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void do_effects (int type)
{
float hue1, hue2, hue3, hue4;
GLrgba color;
float fade;
int radius;
int x, y;
int i;
int bloom_radius;
int bloom_step;
fade = WorldFade ();
bloom_radius = 15;
bloom_step = bloom_radius / 3;
if (!TextureReady ())
return;
//Now change projection modes so we can render full-screen effects
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
glOrtho (0, render_width, render_height, 0, 0.1f, 2048);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity();
glTranslatef(0, 0, -1.0f);
glDisable (GL_CULL_FACE);
glDisable (GL_FOG);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//Render full-screen effects
glBlendFunc (GL_ONE, GL_ONE);
glEnable (GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDepthMask (false);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_BLOOM));
switch (type) {
case EFFECT_DEBUG:
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_LOGOS));
glDisable (GL_BLEND);
glBegin (GL_QUADS);
glColor3f (1, 1, 1);
glTexCoord2f (0, 0); glVertex2i (0, render_height / 4);
glTexCoord2f (0, 1); glVertex2i (0, 0);
glTexCoord2f (1, 1); glVertex2i (render_width / 4, 0);
glTexCoord2f (1, 0); glVertex2i (render_width / 4, render_height / 4);
glTexCoord2f (0, 0); glVertex2i (0, 512);
glTexCoord2f (0, 1); glVertex2i (0, 0);
glTexCoord2f (1, 1); glVertex2i (512, 0);
glTexCoord2f (1, 0); glVertex2i (512, 512);
glEnd ();
break;
case EFFECT_BLOOM_RADIAL:
//Psychedelic bloom
glEnable (GL_BLEND);
glBegin (GL_QUADS);
color = WorldBloomColor () * BLOOM_SCALING * 2;
glColor3fv (&color.red);
for (i = 0; i <= 100; i+=10) {
glTexCoord2f (0, 0); glVertex2i (-i, i + render_height);
glTexCoord2f (0, 1); glVertex2i (-i, -i);
glTexCoord2f (1, 1); glVertex2i (i + render_width, -i);
glTexCoord2f (1, 0); glVertex2i (i + render_width, i + render_height);
}
glEnd ();
break;
case EFFECT_COLOR_CYCLE:
//Oooh. Pretty colors. Tint the scene according to screenspace.
hue1 = (float)(GetTimeInMillis () % COLOR_CYCLE_TIME) / COLOR_CYCLE_TIME;
hue2 = (float)((GetTimeInMillis () + COLOR_CYCLE) % COLOR_CYCLE_TIME) / COLOR_CYCLE_TIME;
hue3 = (float)((GetTimeInMillis () + COLOR_CYCLE * 2) % COLOR_CYCLE_TIME) / COLOR_CYCLE_TIME;
hue4 = (float)((GetTimeInMillis () + COLOR_CYCLE * 3) % COLOR_CYCLE_TIME) / COLOR_CYCLE_TIME;
glBindTexture(GL_TEXTURE_2D, 0);
glEnable (GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE);
glBlendFunc (GL_DST_COLOR, GL_SRC_COLOR);
glBegin (GL_QUADS);
color = glRgbaFromHsl (hue1, 1.0f, 0.6f);
glColor3fv (&color.red);
glTexCoord2f (0, 0); glVertex2i (0, render_height);
color = glRgbaFromHsl (hue2, 1.0f, 0.6f);
glColor3fv (&color.red);
glTexCoord2f (0, 1); glVertex2i (0, 0);
color = glRgbaFromHsl (hue3, 1.0f, 0.6f);
glColor3fv (&color.red);
glTexCoord2f (1, 1); glVertex2i (render_width, 0);
color = glRgbaFromHsl (hue4, 1.0f, 0.6f);
glColor3fv (&color.red);
glTexCoord2f (1, 0); glVertex2i (render_width, render_height);
glEnd ();
break;
case EFFECT_BLOOM:
//Simple bloom effect
glBegin (GL_QUADS);
color = WorldBloomColor () * BLOOM_SCALING;
glColor3fv (&color.red);
for (x = -bloom_radius; x <= bloom_radius; x += bloom_step) {
for (y = -bloom_radius; y <= bloom_radius; y += bloom_step) {
if (abs (x) == abs (y) && x)
continue;
glTexCoord2f (0, 0); glVertex2i (x, y + render_height);
glTexCoord2f (0, 1); glVertex2i (x, y);
glTexCoord2f (1, 1); glVertex2i (x + render_width, y);
glTexCoord2f (1, 0); glVertex2i (x + render_width, y + render_height);
}
}
glEnd ();
break;
case EFFECT_DEBUG_OVERBLOOM:
//This will punish that uppity GPU. Good for testing low frame rate behavior.
glBegin (GL_QUADS);
color = WorldBloomColor () * 0.01f;
glColor3fv (&color.red);
for (x = -50; x <= 50; x+=5) {
for (y = -50; y <= 50; y+=5) {
glTexCoord2f (0, 0); glVertex2i (x, y + render_height);
glTexCoord2f (0, 1); glVertex2i (x, y);
glTexCoord2f (1, 1); glVertex2i (x + render_width, y);
glTexCoord2f (1, 0); glVertex2i (x + render_width, y + render_height);
}
}
glEnd ();
break;
}
//Do the fade to / from darkness used to hide scene transitions
if (LOADING_SCREEN) {
if (fade > 0.0f) {
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_BLEND);
glDisable (GL_TEXTURE_2D);
glColor4f (0, 0, 0, fade);
glBegin (GL_QUADS);
glVertex2i (0, 0);
glVertex2i (0, render_height);
glVertex2i (render_width, render_height);
glVertex2i (render_width, 0);
glEnd ();
}
if (TextureReady () && !EntityReady () && fade != 0.0f) {
radius = render_width / 16;
do_progress ((float)render_width / 2, (float)render_height / 2, (float)radius, fade, EntityProgress ());
RenderPrint (render_width / 2 - LOGO_PIXELS, render_height / 2 + LOGO_PIXELS, 0, glRgba (0.5f), "%1.2f%%", EntityProgress () * 100.0f);
RenderPrint (1, "%s v%d.%d.%03d", APP_TITLE, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
}
}
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int RenderMaxTextureSize ()
{
int mts;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mts);
mts = MIN (mts, render_width);
return MIN (mts, render_height);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderPrint (int x, int y, int font, GLrgba color, const char *fmt, ...)
{
char text[MAX_TEXT];
va_list ap;
text[0] = 0;
if (fmt == NULL)
return;
va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);
glPushAttrib(GL_LIST_BIT);
glListBase(fonts[font % FONT_COUNT].base_char - 32);
glColor3fv (&color.red);
glRasterPos2i (x, y);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderPrint (int line, const char *fmt, ...)
{
char text[MAX_TEXT];
va_list ap;
text[0] = 0;
if (fmt == NULL)
return;
va_start (ap, fmt);
vsprintf (text, fmt, ap);
va_end (ap);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
glOrtho (0, render_width, render_height, 0, 0.1f, 2048);
glDisable(GL_DEPTH_TEST);
glDepthMask (false);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity();
glTranslatef(0, 0, -1.0f);
glDisable (GL_BLEND);
glDisable (GL_FOG);
glDisable (GL_TEXTURE_2D);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
RenderPrint (0, line * FONT_SIZE - 2, 0, glRgba (0.0f), text);
RenderPrint (4, line * FONT_SIZE + 2, 0, glRgba (0.0f), text);
RenderPrint (2, line * FONT_SIZE, 0, glRgba (1.0f), text);
glPopAttrib();
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void static do_help (void)
{
char* text;
int line;
char parse[HELP_SIZE];
int x;
strcpy (parse, help);
line = 0;
text = strtok (parse, "\n");
x = 10;
while (text) {
RenderPrint (line + 2, text);
text = strtok (NULL, "\n");
line++;
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void do_fps ()
{
LIMIT_INTERVAL (1000);
current_fps = frames;
frames = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderResize (void)
{
float fovy;
render_width = WinWidth ();
render_height = WinHeight ();
if (letterbox) {
letterbox_offset = render_height / 6;
render_height = render_height - letterbox_offset * 2;
} else
letterbox_offset = 0;
//render_aspect = (float)render_height / (float)render_width;
glViewport (0, letterbox_offset, render_width, render_height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
render_aspect = (float)render_width / (float)render_height;
fovy = 60.0f;
if (render_aspect > 1.0f)
fovy /= render_aspect;
gluPerspective (fovy, render_aspect, 0.1f, RENDER_DISTANCE);
glMatrixMode (GL_MODELVIEW);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderTerm (void)
{
#ifdef WINDOWS
if (!hRC)
return;
wglDeleteContext (hRC);
hRC = NULL;
#else
Display *dpy = WinGetDisplay();
if (!ctx)
return;
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, ctx);
ctx = NULL;
#endif
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
#ifndef WINDOWS
static bool RenderLoadFonts(Display *dpy, Visual *vis)
{
FT_Error err;
FT_Library lib;
FcPattern *pattern, *font;
FT_Face face;
FcResult res;
bool del_face;
GLubyte *bits;
GLint alignment;
char str[96];
if((err = FT_Init_FreeType(&lib))) {
std::cerr << "FT_Init_Freetype() failed: " << err << ".\n";
return false;
}
if(!FcInit()) {
std::cerr << "FcInit() failed: " << strerror(errno) << "\n";
return false;
}
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for(int j = 0; j < 96; j++)
str[j] = j+32;
for(unsigned int i = 0; i < FONT_COUNT; i++) {
pattern = FcPatternBuild(NULL,
FC_FAMILY, FcTypeString, fonts[i].name,
FC_SIZE, FcTypeDouble, static_cast<double>(FONT_SIZE),
FC_WEIGHT, FcTypeInteger, FC_WEIGHT_BOLD,
(char *)0);
if(!pattern) {
std::cerr << "FcPatternBuild returned null; face = '" << fonts[i].name << "'...\n";
return false;
}
if(!FcConfigSubstitute(NULL, pattern, FcMatchPattern)) {
std::cerr << "FcConfigSubstitute failed; face = '" << fonts[i].name << "'...\n";
return false;
}
FcDefaultSubstitute(pattern);
res = FcResultMatch; // grrr, stupid convention...
font = FcFontMatch(NULL, pattern, &res);
if(res != FcResultMatch) {
std::cerr << "Couldn't match font; face = '" << fonts[i].name << "'. Val: " << res << "\n";
return false;
}
FcPatternDestroy(pattern);
res = FcPatternGetFTFace(font, FC_FT_FACE, 0, &face);
if(res == FcResultNoMatch) {
// sigh; apparently it often (always?) fails to load the FT_Face? do it manually.
FcChar8 *filename;
int face_index;
res = FcPatternGetString(font, FC_FILE, 0, &filename);
if(res != FcResultMatch) {
std::cerr << "Couldn't get font filename; face = '" << fonts[i].name << "'. Val: " << res;
return false;
}
res = FcPatternGetInteger(font, FC_INDEX, 0, &face_index);
if(res != FcResultMatch) {
std::cerr << "Couldn't get font index; face = '" << fonts[i].name << "'. Val: " << res;
return false;
}
// grrr, "char" vs. "unsigned char"
err = FT_New_Face(lib, reinterpret_cast<char*>(filename), face_index, &face);
if(err) {
std::cerr << "Couldn't load font " << filename << "; err = " << err << "\n";
return false;
}
del_face = true;
}
else if(res != FcResultMatch) {
std::cerr << "Couldn't get FT_Face; face = '" << fonts[i].name << "'. Error: ";
if(res == FcResultTypeMismatch)
std::cerr << "type mismatch\n";
else if(res == FcResultNoId)
std::cerr << "no ID\n";
return false;
}
else {
del_face = false;
}
FcPatternDestroy(font);
err = FT_Set_Char_Size(face,
FONT_SIZE*64, 0, // width, height (in points)
72, 72); // x, y dpi
if(err) {
std::cerr << "Can't set char size; face = '" << fonts[i].name << "'. err: " << err << "\n";
return false;
}
fonts[i].base_char = glGenLists(96);
for(unsigned char j = 0; j < 96; j++) {
FT_Load_Char(face, str[j], FT_LOAD_RENDER);
FT_Glyph_Metrics& m = face->glyph->metrics;
FT_Bitmap& bm = face->glyph->bitmap;
FT_Vector& ad = face->glyph->advance;
int met_height = m.height >> 6;
int met_width = m.width >> 6;
bits = new GLubyte[met_height * ((met_width + 7) / 8)]();
for(int y = 0; y < met_height; y++) {
// freetype and gl have opposite bitmap row orders
int yOff = (met_height - y - 1) * ((met_width + 7) / 8);
for(int x = 0; x < met_width; x++) {
if(bm.buffer[y * bm.width + x] > 127) {
bits[yOff + (x / 8)] |= (1 << (7 - (x % 8)));
}
}
}
glNewList(fonts[i].base_char + j, GL_COMPILE);
glBitmap(met_width, met_height,
-(m.horiBearingX / 64.0), (m.height - m.horiBearingY) / 64.0,
ad.x / 64.0, ad.y / 64.0,
bits);
glEndList();
delete[] bits;
}
if(del_face)
FT_Done_Face(face);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
FT_Done_FreeType(lib);
return true;
}
#endif /* !WINDOWS */
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderInit (void)
{
#ifdef WINDOWS
HWND hWnd;
unsigned PixelFormat;
HFONT font;
HFONT oldfont;
hWnd = WinHwnd ();
if (!(hDC = GetDC (hWnd)))
YOUFAIL ("Can't Create A GL Device Context.") ;
if (!(PixelFormat = ChoosePixelFormat(hDC,&pfd)))
YOUFAIL ("Can't Find A Suitable PixelFormat.") ;
if(!SetPixelFormat(hDC,PixelFormat,&pfd))
YOUFAIL ("Can't Set The PixelFormat.");
if (!(hRC = wglCreateContext (hDC)))
YOUFAIL ("Can't Create A GL Rendering Context.");
if(!wglMakeCurrent(hDC,hRC))
YOUFAIL ("Can't Activate The GL Rendering Context.");
//Load the fonts for printing debug info to the window.
for (int i = 0; i < FONT_COUNT; i++) {
fonts[i].base_char = glGenLists(96);
font = CreateFont (FONT_SIZE, 0, 0, 0,
FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE|DEFAULT_PITCH,
fonts[i].name);
oldfont = (HFONT)SelectObject(hDC, font);
wglUseFontBitmaps(hDC, 32, 96, fonts[i].base_char);
SelectObject(hDC, oldfont);
DeleteObject(font);
}
#else
Display *dpy = WinGetDisplay();
XVisualInfo *vis = WinGetVisual();
if(!vis) {
std::cerr << "huh? no visual has been set...\n";
return;
}
if(!(ctx = glXCreateContext(dpy, vis, NULL, True))) {
std::cerr << "Could not create a GLX rendering context: " << strerror(errno) << ".\n";
return;
}
glXMakeCurrent(dpy, WinGetWindow(), ctx);
if(!RenderLoadFonts(dpy, vis->visual)) {
std::cerr << "Couldn't load fonts...\n";
return;
}
#endif
//If the program is running for the first time, set the defaults.
if (!IniInt ("SetDefaults")) {
IniIntSet ("SetDefaults", 1);
IniIntSet ("Effect", EFFECT_BLOOM);
IniIntSet ("ShowFog", 1);
}
//load in our settings
letterbox = IniInt ("Letterbox") != 0;
show_wireframe = IniInt ("Wireframe") != 0;
show_fps = IniInt ("ShowFPS") != 0;
show_fog = IniInt ("ShowFog") != 0;
effect = IniInt ("Effect");
flat = IniInt ("Flat") != 0;
fog_distance = WORLD_HALF;
//clear the viewport so the user isn't looking at trash while the program starts
glViewport (0, 0, WinWidth (), WinHeight ());
glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#ifdef WINDOWS
SwapBuffers (hDC);
#else
glFlush();
glXSwapBuffers(dpy, WinGetWindow());
#endif
RenderResize ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderFPSToggle ()
{
show_fps = !show_fps;
IniIntSet ("ShowFPS", show_fps ? 1 : 0);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool RenderFog ()
{
return show_fog;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderFogToggle ()
{
show_fog = !show_fog;
IniIntSet ("ShowFog", show_fog ? 1 : 0);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderLetterboxToggle ()
{
letterbox = !letterbox;
IniIntSet ("Letterbox", letterbox ? 1 : 0);
RenderResize ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderWireframeToggle ()
{
show_wireframe = !show_wireframe;
IniIntSet ("Wireframe", show_wireframe ? 1 : 0);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool RenderWireframe ()
{
return show_wireframe;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderEffectCycle ()
{
effect = (effect + 1) % EFFECT_COUNT;
IniIntSet ("Effect", effect);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool RenderBloom ()
{
return effect == EFFECT_BLOOM || effect == EFFECT_BLOOM_RADIAL
|| effect == EFFECT_DEBUG_OVERBLOOM || effect == EFFECT_COLOR_CYCLE;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool RenderFlat ()
{
return flat;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderFlatToggle ()
{
flat = !flat;
IniIntSet ("Flat", flat ? 1 : 0);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderHelpToggle ()
{
show_help = !show_help;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float RenderFogDistance ()
{
return fog_distance;
}
/*-----------------------------------------------------------------------------
This is used to set a gradient fog that goes from camera to some portion of
the normal fog distance. This is used for making wireframe outlines and
flat surfaces fade out after rebuild. Looks cool.
-----------------------------------------------------------------------------*/
void RenderFogFX (float scalar)
{
if (scalar >= 1.0f) {
glDisable (GL_FOG);
return;
}
glFogf (GL_FOG_START, 0.0f);
glFogf (GL_FOG_END, fog_distance * 2.0f * scalar);
glEnable (GL_FOG);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderUpdate (void)
{
GLvector pos;
GLvector angle;
GLrgba color;
int elapsed;
frames++;
do_fps ();
glViewport (0, 0, WinWidth (), WinHeight ());
glDepthMask (true);
glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (letterbox)
glViewport (0, letterbox_offset, render_width, render_height);
if (LOADING_SCREEN && TextureReady () && !EntityReady ()) {
do_effects (EFFECT_NONE);
#ifdef WINDOWS
SwapBuffers (hDC);
#else