1 /* ************************************************************************* *
2 OldSkoolGravityGame (OSGG) Lunar Lander-like game for linux.
3 Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * ************************************************************************* */
18
19
20 #include "text.hpp"
21
getHeight(int font)22 GLfloat glTextClass::getHeight(int font)
23 {
24 return(fontInfo[font].height*2.0);
25 }
26
glTextClass()27 glTextClass::glTextClass()
28 {
29 TTF_Init();
30 //Parse font-description file
31 string line,set,val,tempName;
32
33 genFontTex(DATADIR "Bandal.ttf", 60, FONT_DEFAULT);
34
35 TTF_Quit();
36 }
37
~glTextClass()38 glTextClass::~glTextClass()
39 {
40 for(int i=0; i < FONT_NUM; i++)
41 {
42 glDeleteTextures(1, &fontInfo[i].tex);
43 }
44 }
45
genFontTex(string TTFfontName,int fontSize,int font)46 void glTextClass::genFontTex(string TTFfontName, int fontSize, int font)
47 {
48 TTF_Font *ttfFont = NULL;
49 SDL_Surface *c, *t=NULL;
50 Uint32 rmask, gmask, bmask, amask;
51 char tempChar[2] = { 0,0 };
52 int sX=0,sY=0; //Size of the rendered character
53 SDL_Rect src={0,0,0,0},dst={0,0,0,0};
54 SDL_Color white = { 255,255,255 };
55
56 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
57 rmask = 0xff000000;
58 gmask = 0x00ff0000;
59 bmask = 0x0000ff00;
60 amask = 0x000000ff;
61 #else
62 rmask = 0x000000ff;
63 gmask = 0x0000ff00;
64 bmask = 0x00ff0000;
65 amask = 0xff000000;
66 #endif
67
68 ttfFont = TTF_OpenFont( TTFfontName.data(), fontSize );
69
70 if(!ttfFont)
71 {
72 cout << "Could not load font" << endl;
73 }
74
75 t = SDL_CreateRGBSurface(0, 512, 512, 32, rmask,gmask,bmask,amask);
76
77 dst.x=1;
78 dst.y=1;
79
80 fontInfo[font].height=0.0;
81 for(int i=32; i < 128; i++)
82 {
83 tempChar[0] = (char)i;
84
85 //Render to surface
86 c = TTF_RenderText_Blended(ttfFont, tempChar, white);
87 SDL_SetAlpha(c, 0, 0);
88 TTF_SizeUTF8(ttfFont, tempChar, &sX, &sY);
89
90 src.x=0;
91 src.y=0;
92 src.w=sX;
93 src.h=sY;
94
95
96 if(dst.x + sX > 512)
97 {
98 dst.x=1;
99 dst.y += sY+2;
100 }
101
102 fontInfo[font].ch[i].Xa = ( 1.0 / ( 512.0 / (float)dst.x ) );
103 fontInfo[font].ch[i].Xb = ( 1.0 / ( 512.0 / ((float)dst.x+sX) ) );
104 fontInfo[font].ch[i].Ya = ( 1.0 / ( 512.0 / (float)dst.y ) );
105 fontInfo[font].ch[i].Yb = ( 1.0 / ( 512.0 / ((float)dst.y+sY) ) );
106 fontInfo[font].ch[i].width = sX/800.0;
107
108 if(sY/800.0 > fontInfo[font].height)
109 {
110 fontInfo[font].height = sY/800.0;
111 }
112
113 //blit
114 dst.w=sX;
115 dst.h=sY;
116 SDL_BlitSurface(c,&src,t,&dst);
117
118 dst.x += sX+2; // Waste some space 1 px padding around each char
119
120 SDL_FreeSurface(c); //Free character-surface
121 }
122
123 glGenTextures(1, &fontInfo[font].tex); //Generate a gltexture for this font
124
125 glEnable( GL_TEXTURE_2D );
126 glBindTexture(GL_TEXTURE_2D, fontInfo[font].tex);
127
128
129 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
131 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
132
133 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
134 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
135
136 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
137 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t->w, t->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, t->pixels);
138
139
140 TTF_CloseFont(ttfFont); //Free the font
141 SDL_FreeSurface(t); //Free text-surface
142 }
143
write(string text,int font,GLfloat scale,GLfloat x,GLfloat y)144 void glTextClass::write(string text, int font, GLfloat scale, GLfloat x, GLfloat y)
145 {
146 int c;
147 GLfloat sX,sY,ssY,posX=0;
148
149 posX = x;
150
151 glEnable( GL_TEXTURE_2D );
152 glBindTexture(GL_TEXTURE_2D, fontInfo[font].tex);
153
154 sY = fontInfo[font].height*scale;
155 ssY = sY+sY;
156 //Draw the quads
157 for(unsigned int i=0; i < text.length(); i++)
158 {
159 c = (unsigned int)text[i];
160 if( c != '\n' ) {
161 sX = fontInfo[font].ch[c].width*scale;
162 posX += sX;
163
164 glBegin(GL_QUADS);
165 glTexCoord2f( fontInfo[font].ch[c].Xa,fontInfo[font].ch[c].Ya ); glVertex3f(-sX+posX, sY+y,0);
166 glTexCoord2f( fontInfo[font].ch[c].Xb,fontInfo[font].ch[c].Ya ); glVertex3f( sX+posX, sY+y,0);
167 glTexCoord2f( fontInfo[font].ch[c].Xb,fontInfo[font].ch[c].Yb ); glVertex3f( sX+posX,-sY+y,0);
168 glTexCoord2f( fontInfo[font].ch[c].Xa,fontInfo[font].ch[c].Yb ); glVertex3f(-sX+posX,-sY+y,0);
169 glEnd( );
170 posX += sX;
171 } else {
172 posX=x;
173 y -= ssY;
174 }
175 }
176 glDisable( GL_TEXTURE_2D );
177 }
178