-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.cpp
177 lines (138 loc) · 4.77 KB
/
text.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
/* ************************************************************************* *
OldSkoolGravityGame (OSGG) Lunar Lander-like game for linux.
Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
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 3 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, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "text.hpp"
GLfloat glTextClass::getHeight(int font)
{
return(fontInfo[font].height*2.0);
}
glTextClass::glTextClass()
{
TTF_Init();
//Parse font-description file
string line,set,val,tempName;
genFontTex(DATADIR "Bandal.ttf", 60, FONT_DEFAULT);
TTF_Quit();
}
glTextClass::~glTextClass()
{
for(int i=0; i < FONT_NUM; i++)
{
glDeleteTextures(1, &fontInfo[i].tex);
}
}
void glTextClass::genFontTex(string TTFfontName, int fontSize, int font)
{
TTF_Font *ttfFont = NULL;
SDL_Surface *c, *t=NULL;
Uint32 rmask, gmask, bmask, amask;
char tempChar[2] = { 0,0 };
int sX=0,sY=0; //Size of the rendered character
SDL_Rect src={0,0,0,0},dst={0,0,0,0};
SDL_Color white = { 255,255,255 };
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
ttfFont = TTF_OpenFont( TTFfontName.data(), fontSize );
if(!ttfFont)
{
cout << "Could not load font" << endl;
}
t = SDL_CreateRGBSurface(0, 512, 512, 32, rmask,gmask,bmask,amask);
dst.x=1;
dst.y=1;
fontInfo[font].height=0.0;
for(int i=32; i < 128; i++)
{
tempChar[0] = (char)i;
//Render to surface
c = TTF_RenderText_Blended(ttfFont, tempChar, white);
SDL_SetAlpha(c, 0, 0);
TTF_SizeUTF8(ttfFont, tempChar, &sX, &sY);
src.x=0;
src.y=0;
src.w=sX;
src.h=sY;
if(dst.x + sX > 512)
{
dst.x=1;
dst.y += sY+2;
}
fontInfo[font].ch[i].Xa = ( 1.0 / ( 512.0 / (float)dst.x ) );
fontInfo[font].ch[i].Xb = ( 1.0 / ( 512.0 / ((float)dst.x+sX) ) );
fontInfo[font].ch[i].Ya = ( 1.0 / ( 512.0 / (float)dst.y ) );
fontInfo[font].ch[i].Yb = ( 1.0 / ( 512.0 / ((float)dst.y+sY) ) );
fontInfo[font].ch[i].width = sX/800.0;
if(sY/800.0 > fontInfo[font].height)
{
fontInfo[font].height = sY/800.0;
}
//blit
dst.w=sX;
dst.h=sY;
SDL_BlitSurface(c,&src,t,&dst);
dst.x += sX+2; // Waste some space 1 px padding around each char
SDL_FreeSurface(c); //Free character-surface
}
glGenTextures(1, &fontInfo[font].tex); //Generate a gltexture for this font
glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, fontInfo[font].tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t->w, t->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, t->pixels);
TTF_CloseFont(ttfFont); //Free the font
SDL_FreeSurface(t); //Free text-surface
}
void glTextClass::write(string text, int font, GLfloat scale, GLfloat x, GLfloat y)
{
int c;
GLfloat sX,sY,ssY,posX=0;
posX = x;
glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, fontInfo[font].tex);
sY = fontInfo[font].height*scale;
ssY = sY+sY;
//Draw the quads
for(unsigned int i=0; i < text.length(); i++)
{
c = (unsigned int)text[i];
if( c != '\n' ) {
sX = fontInfo[font].ch[c].width*scale;
posX += sX;
glBegin(GL_QUADS);
glTexCoord2f( fontInfo[font].ch[c].Xa,fontInfo[font].ch[c].Ya ); glVertex3f(-sX+posX, sY+y,0);
glTexCoord2f( fontInfo[font].ch[c].Xb,fontInfo[font].ch[c].Ya ); glVertex3f( sX+posX, sY+y,0);
glTexCoord2f( fontInfo[font].ch[c].Xb,fontInfo[font].ch[c].Yb ); glVertex3f( sX+posX,-sY+y,0);
glTexCoord2f( fontInfo[font].ch[c].Xa,fontInfo[font].ch[c].Yb ); glVertex3f(-sX+posX,-sY+y,0);
glEnd( );
posX += sX;
} else {
posX=x;
y -= ssY;
}
}
glDisable( GL_TEXTURE_2D );
}