1 /* font test -- based on extext */
2 
3 /* Specifically, this tests alpha fonts blending into backgrounds compared
4  * to solid text with transparent background.
5  */
6 
7 
8 #include <time.h>
9 #include <math.h>
10 #include <string.h>
11 
12 #include "allegro.h"
13 #include "alleggl.h"
14 
15 
main()16 int main() {
17 
18 	FONT *demofont;
19 	DATAFILE *dat;
20 
21 	allegro_init();
22 	install_allegro_gl();
23 
24 	allegro_gl_clear_settings();
25 	allegro_gl_set(AGL_COLOR_DEPTH, 32);
26 	allegro_gl_set(AGL_WINDOWED, TRUE);
27 	allegro_gl_set(AGL_DOUBLEBUFFER, 1);
28 	allegro_gl_set(AGL_SUGGEST, AGL_DOUBLEBUFFER | AGL_COLOR_DEPTH
29 	             | AGL_WINDOWED);
30 
31 	if (set_gfx_mode(GFX_OPENGL, 640, 480, 0, 0)) {
32 		allegro_message("Error initializing OpenGL!\n");
33 		return -1;
34 	}
35 
36 	install_keyboard();
37 	install_timer();
38 
39 	/* Load font palette first, then font itself */
40 	dat = load_datafile_object ("demofont.dat", "PAL");
41 	if (!dat) {
42 		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
43 		allegro_message("Error loading demofont.dat#PAL\n");
44 		return -1;
45 	}
46 	set_palette ((RGB *)dat->dat);
47 	unload_datafile_object (dat);
48 
49 	dat = load_datafile_object ("demofont.dat", "FONT");
50 	if (!dat) {
51 		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
52 		allegro_message("Error loading demofont.dat#FONT\n");
53 		return -1;
54 	}
55 	demofont = allegro_gl_convert_allegro_font_ex((FONT*)dat->dat,
56 	                                   AGL_FONT_TYPE_TEXTURED, 16.0, GL_ALPHA8);
57 	unload_datafile_object(dat);
58 
59 	if (!demofont) {
60 		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
61 		allegro_message("Unable to convert font!\n");
62 		return -1;
63 	}
64 
65 
66 	/* Setup OpenGL the way we want */
67 	glEnable(GL_TEXTURE_2D);
68 
69 	glViewport(0, 0, SCREEN_W, SCREEN_H);
70 	glMatrixMode(GL_PROJECTION);
71 	glLoadIdentity();
72 	glOrtho(0, 1, 0, 1, -1, 1);
73 
74 	glMatrixMode(GL_MODELVIEW);
75 
76 	do {
77 		int x, y;
78 		glLoadIdentity();
79 
80 		/* Draw the background */
81 		for (y = 0; y < 4; y++) {
82 			for (x = 0; x < 4; x++) {
83 				glColor3f ((y&1), (x&1), ((y&2)^(x&2))/2);
84 				glBegin (GL_QUADS);
85 					glVertex2f (x*0.25, y*0.25);
86 					glVertex2f (x*0.25, (y+1)*0.25);
87 					glVertex2f ((x+1)*0.25, (y+1)*0.25);
88 					glVertex2f ((x+1)*0.25, y*0.25);
89 				glEnd();
90 			}
91 		}
92 
93 		/* Draw the text on top */
94 		glScalef (0.1, 0.1, 0.1);
95 
96 		/* First, draw the text alpha blended */
97 		glEnable(GL_BLEND);
98 		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 		allegro_gl_printf(demofont, 1, 8, 0, makeacol(255,255,255,255),
100 		                 "Hello World!");
101 		glBlendFunc(GL_ONE, GL_ZERO);
102 		glDisable(GL_BLEND);
103 
104 		/* Now just draw it solid with transparent background */
105 		glEnable(GL_ALPHA_TEST);
106 		glAlphaFunc(GL_GREATER, 0.5f);
107 		allegro_gl_printf(demofont, 1, 5, 0, makeacol(255,255,255,255),
108 		                 "Hello World!");
109 		glDisable(GL_ALPHA_TEST);
110 
111 		allegro_gl_flip();
112 		rest(2);
113 
114 	} while (!keypressed());
115 
116 	/* Clean up and exit */
117 	allegro_gl_destroy_font(demofont);
118 	TRACE("Font Destroyed");
119 
120 	return 0;
121 }
122 END_OF_MAIN()
123 
124