Debian Codegraph @

text.cpp

raw symbols SCIP
1 /* ************************************************************************* *
2 SDL-Ball - DX-Ball/Breakout remake with openGL and SDL 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 #define FONT_MENU 0
20 #define FONT_ANNOUNCE_GOOD 1
21 #define FONT_ANNOUNCE_BAD 2
22 #define FONT_HIGHSCORE 3
23 #define FONT_MENUHIGHSCORE 4
24 #define FONT_INTROHIGHSCORE 5
25 #define FONT_INTRODESCRIPTION 6
26 #define FONT_NUM 7
27
28 struct glCharInfo_struct {
29 GLfloat Xa,Ya,Xb,Yb, width;
30 };
31
32 struct glFontInfo_struct {
33 GLuint tex;
34 GLfloat height;
35 struct glCharInfo_struct ch[255];
36 };
37
38
39 class glTextClass {
40 private:
41 void genFontTex(string TTFfontName, int fontSize, int font);
42 struct glFontInfo_struct fontInfo[FONT_NUM];
43
44 public:
45 GLfloat getHeight(int font);
46 void write(string text, int font, bool center, GLfloat scale, GLfloat x, GLfloat y);
47 glTextClass();
48 };
49
50 GLfloat glTextClass::getHeight(int font)
51 {
52 return(fontInfo[font].height*2.0);
53 }
54
55 glTextClass::glTextClass()
56 {
57 TTF_Init();
58 //Parse font-description file
59 ifstream f;
60 string line,set,val,tempName;
61
62 f.open( useTheme("/font/fonts.txt",setting.gfxTheme).data() );
63 if(f.is_open())
64 {
65 while(!f.eof())
66 {
67 getline(f, line);
68 if(line.find('=') != string::npos)
69 {
70 set=line.substr(0,line.find('='));
71 val=line.substr(line.find('=')+1);
72 if(set=="menufile")
73 {
74 tempName=val;
75 } else if(set=="menusize")
76 {
77 genFontTex(tempName, atoi(val.data()), FONT_MENU);
78 } else if(set=="announcegoodfile")
79 {
80 tempName=val;
81 } else if(set=="announcegoodsize")
82 {
83 genFontTex(tempName, atoi(val.data()), FONT_ANNOUNCE_GOOD);
84
85 } else if(set=="announcebadfile")
86 {
87 tempName=val;
88 } else if(set=="announcebadsize")
89 {
90 genFontTex(tempName, atoi(val.data()), FONT_ANNOUNCE_BAD);
91 } else if(set=="highscorefile")
92 {
93 tempName=val;
94 } else if(set=="highscoresize")
95 {
96 genFontTex(tempName, atoi(val.data()), FONT_HIGHSCORE);
97 } else if(set=="menuhighscorefile")
98 {
99 tempName=val;
100 } else if(set=="menuhighscoresize")
101 {
102 genFontTex(tempName, atoi(val.data()), FONT_MENUHIGHSCORE);
103 } else if(set=="introhighscorefile")
104 {
105 tempName=val;
106 } else if(set=="introhighscoresize")
107 {
108 genFontTex(tempName, atoi(val.data()), FONT_INTROHIGHSCORE);
109 } else if(set=="introdescriptionfile")
110 {
111 tempName=val;
112 } else if(set=="introdescriptionsize")
113 {
114 genFontTex(tempName, atoi(val.data()), FONT_INTRODESCRIPTION);
115 }
116 }
117 }
118 f.close();
119 } else {
120 cout << "Error: could not open font-description file.";
121 }
122 TTF_Quit();
123 }
124
125 void glTextClass::genFontTex(string TTFfontName, int fontSize, int font)
126 {
127 TTF_Font *ttfFont = NULL;
128 SDL_Surface *c, *t;
129 Uint32 rmask, gmask, bmask, amask;
130 char tempChar[2] = { 0,0 };
131 int sX=0,sY=0; //Size of the rendered character
132 SDL_Rect src={0,0,0,0},dst={0,0,0,0};
133 SDL_Color white = { 255,255,255 };
134
135 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
136 rmask = 0xff000000;
137 gmask = 0x00ff0000;
138 bmask = 0x0000ff00;
139 amask = 0x000000ff;
140 #else
141 rmask = 0x000000ff;
142 gmask = 0x0000ff00;
143 bmask = 0x00ff0000;
144 amask = 0xff000000;
145 #endif
146
147 ttfFont = TTF_OpenFont( useTheme(TTFfontName,setting.gfxTheme).data(), fontSize );
148 t = SDL_CreateRGBSurface(0, 512, 512, 32, rmask,gmask,bmask,amask);
149
150 dst.x=1;
151 dst.y=1;
152
153 fontInfo[font].height=0.0;
154 for(int i=32; i < 128; i++)
155 {
156 tempChar[0] = (char)i;
157
158 //Render to surface
159 c = TTF_RenderText_Blended(ttfFont, tempChar, white);
160 SDL_SetAlpha(c, 0, 0);
161 TTF_SizeUTF8(ttfFont, tempChar, &sX, &sY);
162
163 src.x=0;
164 src.y=0;
165 src.w=sX;
166 src.h=sY;
167
168
169 if(dst.x + sX > 512)
170 {
171 dst.x=1;
172 dst.y += sY+2;
173 if(dst.y > 512 && font != FONT_HIGHSCORE) //FONT_HIGHSCORE is always rendered too big for texture
174 {
175 cout << "Too many chars for tex ("<<i<<")"<<endl;
176 }
177 }
178
179 fontInfo[font].ch[i].Xa = ( 1.0 / ( 512.0 / (float)dst.x ) );
180 fontInfo[font].ch[i].Xb = ( 1.0 / ( 512.0 / ((float)dst.x+sX) ) );
181 fontInfo[font].ch[i].Ya = ( 1.0 / ( 512.0 / (float)dst.y ) );
182 fontInfo[font].ch[i].Yb = ( 1.0 / ( 512.0 / ((float)dst.y+sY) ) );
183 fontInfo[font].ch[i].width = sX/800.0;
184
185 if(sY/800.0 > fontInfo[font].height)
186 {
187 fontInfo[font].height = sY/800.0;
188 }
189
190 //blit
191 dst.w=sX;
192 dst.h=sY;
193 SDL_BlitSurface(c,&src,t,&dst);
194
195 dst.x += sX+2; // Waste some space 1 px padding around each char
196
197 SDL_FreeSurface(c); //Free character-surface
198 }
199
200 glGenTextures(1, &fontInfo[font].tex); //Generate a gltexture for this font
201
202 glBindTexture(GL_TEXTURE_2D, fontInfo[font].tex);
203 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, t->w, t->h, GL_RGBA, GL_UNSIGNED_BYTE, t->pixels);
204
205 TTF_CloseFont(ttfFont); //Free the font
206 SDL_FreeSurface(t); //Free text-surface
207 }
208
209 void glTextClass::write(string text, int font,bool center, GLfloat scale, GLfloat x, GLfloat y)
210 {
211 int c;
212 GLfloat sX,sY,posX=0;
213
214 //We need to find out half of the string width in order to center
215 if(center)
216 {
217 for(unsigned int i=0; i < text.length(); i++)
218 {
219 c = (unsigned int)text[i];
220 sX = fontInfo[font].ch[c].width*scale;
221 posX += sX;
222 }
223 posX *= -1;
224 }
225 posX += x;
226
227 glEnable( GL_TEXTURE_2D );
228 glBindTexture(GL_TEXTURE_2D, fontInfo[font].tex);
229
230
231 //Draw the quads
232 for(unsigned int i=0; i < text.length(); i++)
233 {
234 c = (unsigned int)text[i];
235 sX = fontInfo[font].ch[c].width*scale;
236 sY = fontInfo[font].height*scale;
237 posX += sX;
238
239 glBegin(GL_QUADS);
240 glTexCoord2f( fontInfo[font].ch[c].Xa,fontInfo[font].ch[c].Ya ); glVertex3f(-sX+posX, sY+y,0);
241 glTexCoord2f( fontInfo[font].ch[c].Xb,fontInfo[font].ch[c].Ya ); glVertex3f( sX+posX, sY+y,0);
242 glTexCoord2f( fontInfo[font].ch[c].Xb,fontInfo[font].ch[c].Yb ); glVertex3f( sX+posX,-sY+y,0);
243 glTexCoord2f( fontInfo[font].ch[c].Xa,fontInfo[font].ch[c].Yb ); glVertex3f(-sX+posX,-sY+y,0);
244 glEnd( );
245 posX += sX;
246 }
247 }