00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <GLEXT/GLFont2d.h>
00023 #include <GLEXT/GLCameraFrustum.h>
00024 #include <GLEXT/GLTextureBase.h>
00025 #include <GLEXT/GLFont2dFreeType.h>
00026 #include <common/Defines.h>
00027
00028 unsigned int GLFont2d::totalCharacters_(0);
00029
00030 GLFont2d::GLFont2d()
00031 {
00032 freetype_ = new GLFont2dFreeType();
00033 }
00034
00035 GLFont2d::~GLFont2d()
00036 {
00037 delete freetype_;
00038 }
00039
00040 float GLFont2d::getWidth(float size, const LangString &text, int len)
00041 {
00042 float width = 0.0f;
00043 if (len == 0) len = (int) text.size();
00044
00045 const unsigned int *a = text.c_str();
00046 for (int i=0; i<len; i++, a++)
00047 {
00048 width += float(getCharacter(*a)->advances) * size / height_;
00049 }
00050 return width;
00051 }
00052
00053 int GLFont2d::getChars(float size, const LangString &text, float len)
00054 {
00055 int l = 0;
00056 float width = 0.0f;
00057 for (const unsigned int *a=text.c_str(); *a; a++)
00058 {
00059 width += float(getCharacter(*a)->advances) * size / height_;
00060 if (width < len) l++;
00061 }
00062 return l;
00063 }
00064
00065 void GLFont2d::draw(Vector &color, float size,
00066 float x, float y, float z,
00067 const LangString &text)
00068 {
00069 drawString((GLsizei) text.size(), color, 1.0f, size, x, y, z, text.c_str(), false);
00070 }
00071
00072 void GLFont2d::drawA(Vector &color, float alpha, float size,
00073 float x, float y, float z,
00074 const LangString &text)
00075 {
00076 drawString((GLsizei) text.size(), color, alpha, size, x, y, z, text.c_str(), false);
00077 }
00078
00079 void GLFont2d::drawA(GLFont2dI *handler, Vector &color, float alpha, float size,
00080 float x, float y, float z,
00081 const LangString &text)
00082 {
00083 drawStringHandler((GLsizei) text.size(), handler, color, alpha, size, x, y, z, text.c_str());
00084 }
00085
00086 void GLFont2d::drawSubStr(int start, int len,
00087 Vector &color, float size,
00088 float x, float y, float z,
00089 const LangString &text)
00090 {
00091 drawSubStrA(start, len, color, 1.0f, size, x, y, z, text);
00092 }
00093
00094 void GLFont2d::drawSubStrA(int start, int len,
00095 Vector &color, float alpha, float size,
00096 float x, float y, float z,
00097 const LangString &text)
00098 {
00099 int s = start;
00100 float width = 0.0f;
00101 for (const unsigned int *a=text.c_str(); *a; a++)
00102 {
00103 if (--s < 0) break;
00104 width += float(getCharacter(*a)->advances) * size / height_;
00105 }
00106
00107 if (len - start > 0)
00108 {
00109 drawString(len - start, color, alpha, size, x + width, y, z, text.c_str() + start, false);
00110 }
00111 }
00112
00113 void GLFont2d::drawWidth(float len, Vector &color, float size,
00114 float x, float y, float z,
00115 const LangString &text)
00116 {
00117 int l = getChars(size, text, len);
00118 drawString(l, color, 1.0f, size, x, y, z, text.c_str(), false);
00119 }
00120
00121 void GLFont2d::drawWidthRhs(float len, Vector &color, float size,
00122 float x, float y, float z,
00123 const LangString &text)
00124 {
00125 int slen = text.size();
00126 if (slen > 0)
00127 {
00128 int l = 0;
00129 float width = 0.0f;
00130 const unsigned int *a=& (text.c_str())[slen-1];
00131 for (; a >= text.c_str(); a--, l++)
00132 {
00133 width += float(getCharacter(*a)->advances) * size / height_;
00134 if (width > len) break;
00135 }
00136 a++;
00137
00138 drawString(l, color, 1.0f, size, x, y, z, a, false);
00139 }
00140 }
00141
00142 void GLFont2d::drawBilboard(Vector &color, float alpha, float size,
00143 float x, float y, float z,
00144 const LangString &text)
00145 {
00146 drawString((GLsizei) text.size(), color, alpha, size, x, y, z, text.c_str(), true);
00147 }
00148
00149 float GLFont2d::getWidth(float size, const std::string &text, int len)
00150 {
00151 LangStringUtil::replaceToLang(langText_, text);
00152 return getWidth(size, langText_, len);
00153 }
00154
00155 int GLFont2d::getChars(float size, const std::string &text, float len)
00156 {
00157 LangStringUtil::replaceToLang(langText_, text);
00158 return getChars(size, langText_, len);
00159 }
00160
00161 void GLFont2d::draw(Vector &color, float size,
00162 float x, float y, float z,
00163 const std::string &text)
00164 {
00165 LangStringUtil::replaceToLang(langText_, text);
00166 draw(color, size, x, y, z, langText_);
00167 }
00168
00169 void GLFont2d::drawA(Vector &color, float alpha, float size,
00170 float x, float y, float z,
00171 const std::string &text)
00172 {
00173 LangStringUtil::replaceToLang(langText_, text);
00174 drawA(color, alpha, size, x, y, z, langText_);
00175 }
00176
00177 void GLFont2d::drawA(GLFont2dI *handler, Vector &color, float alpha, float size,
00178 float x, float y, float z,
00179 const std::string &text)
00180 {
00181 LangStringUtil::replaceToLang(langText_, text);
00182 drawA(handler, color, alpha, size, x, y, z, langText_);
00183 }
00184
00185 void GLFont2d::drawWidth(float width,
00186 Vector &color, float size,
00187 float x, float y, float z,
00188 const std::string &text)
00189 {
00190 LangStringUtil::replaceToLang(langText_, text);
00191 drawWidth(width, color, size, x, y, z, langText_);
00192 }
00193
00194 void GLFont2d::drawWidthRhs(float width,
00195 Vector &color, float size,
00196 float x, float y, float z,
00197 const std::string &text)
00198 {
00199 LangStringUtil::replaceToLang(langText_, text);
00200 drawWidthRhs(width, color, size, x, y, z, langText_);
00201 }
00202
00203 void GLFont2d::drawSubStr(int start, int len,
00204 Vector &color, float size,
00205 float x, float y, float z,
00206 const std::string &text)
00207 {
00208 LangStringUtil::replaceToLang(langText_, text);
00209 drawSubStr(start, len, color, size, x, y, z, langText_);
00210 }
00211
00212 void GLFont2d::drawSubStrA(int start, int len,
00213 Vector &color, float alpha, float size,
00214 float x, float y, float z,
00215 const std::string &text)
00216 {
00217 LangStringUtil::replaceToLang(langText_, text);
00218 drawSubStrA(start, len, color, alpha, size, x, y, z, langText_);
00219 }
00220
00221 void GLFont2d::drawBilboard(Vector &color, float alpha, float size,
00222 float x, float y, float z,
00223 const std::string &text)
00224 {
00225 LangStringUtil::replaceToLang(langText_, text);
00226 drawBilboard(color, alpha, size, x, y, z, langText_);
00227 }
00228
00229 void GLFont2d::drawLetter(GLFont2dStorage::CharEntry *entry)
00230 {
00231 Vector &bilX = GLCameraFrustum::instance()->getBilboardVectorX();
00232 Vector &bilY = GLCameraFrustum::instance()->getBilboardVectorY();
00233
00234 glBindTexture(GL_TEXTURE_2D, entry->texture);
00235 glPushMatrix();
00236
00237 Vector trans = bilX * (float) entry->left +
00238 bilY * (float) entry->rows;
00239 glTranslatef(trans[0], trans[1], trans[2]);
00240
00241 glBegin(GL_QUADS);
00242 glTexCoord2f(0.0f,0.0f);
00243 glVertex3fv(bilY * entry->height);
00244
00245 glTexCoord2f(0.0f, entry->y);
00246 glVertex3f(0.0f,0.0f,0.0f);
00247
00248 glTexCoord2f(entry->x,entry->y);
00249 glVertex3fv(bilX * entry->width);
00250
00251 glTexCoord2f(entry->x,0.0f);
00252 glVertex3fv(bilX * entry->width + bilY * entry->height);
00253 glEnd();
00254 glPopMatrix();
00255
00256 bilX *= (float)(entry->advances);
00257 glTranslatef(bilX[0], bilX[1], bilX[2]);
00258 }
00259
00260 bool GLFont2d::drawStringHandler(unsigned length,
00261 GLFont2dI *handler,
00262 Vector &color, float alpha, float size,
00263 float x, float y, float z,
00264 const unsigned int *string)
00265 {
00266 GLTextureBase::setLastBind(0);
00267
00268 GLState currentState(GLState::BLEND_ON | GLState::TEXTURE_ON);
00269 Vector4 acolor;
00270 Vector position(x, y, z);
00271
00272 glPushMatrix();
00273 glTranslatef(position[0], position[1], position[2]);
00274 glScalef(size / height_, size / height_, size / height_);
00275
00276 int pos = 0;
00277 for (;*string; string++, pos++)
00278 {
00279 unsigned int list = *string;
00280
00281 acolor[0] = color[0];
00282 acolor[1] = color[1];
00283 acolor[2] = color[2];
00284 acolor[3] = alpha;
00285
00286 GLFont2dStorage::CharEntry *charEntry = getCharacter(*string);
00287 handler->drawCharacter(pos, position, *charEntry, acolor);
00288
00289 glColor4fv(acolor);
00290 glCallList(charEntry->displaylist);
00291
00292 position[0] += float(charEntry->advances) * size / height_;
00293 }
00294 glPopMatrix();
00295
00296 return true;
00297 }
00298
00299 bool GLFont2d::drawString(unsigned length, Vector &color, float alpha, float size,
00300 float x, float y, float z,
00301 const unsigned int *string,
00302 bool bilboard)
00303 {
00304 GLTextureBase::setLastBind(0);
00305
00306 GLState currentState(GLState::BLEND_ON | GLState::TEXTURE_ON);
00307 glColor4f(color[0], color[1], color[2], alpha);
00308
00309 if (bilboard)
00310 {
00311 glPushMatrix();
00312 glTranslatef(x,y,z);
00313 glScalef(size / height_, size / height_, size / height_);
00314
00315 for (const unsigned int *current = string; *current; current++)
00316 {
00317 GLFont2dStorage::CharEntry *entry = getCharacter(*current);
00318 drawLetter(entry);
00319 }
00320 glPopMatrix();
00321 }
00322 else
00323 {
00324 static unsigned int lists[1024];
00325 unsigned int *currentList = lists;
00326 for (const unsigned int *current = string; *current; current++, currentList++)
00327 {
00328 GLFont2dStorage::CharEntry *entry = getCharacter(*current);
00329 *currentList = entry->displaylist;
00330 }
00331
00332 glPushMatrix();
00333 glTranslatef(x,y,z);
00334 glScalef(size / height_, size / height_, size / height_);
00335 glCallLists(length, GL_UNSIGNED_INT, lists);
00336 glPopMatrix();
00337 }
00338
00339 return true;
00340 }
00341
00342 GLFont2dStorage::CharEntry *GLFont2d::getCharacter(unsigned int character)
00343 {
00344 GLFont2dStorage::CharEntry *entry = characters_.getEntry(character);
00345 if (!entry->displaylist)
00346 {
00347 totalCharacters_++;
00348 freetype_->createCharacter(character, entry);
00349 }
00350
00351 return entry;
00352 }
00353
00354 bool GLFont2d::createFont(const std::string &typeFace, unsigned int h, bool makeShadow)
00355 {
00356 height_ = float(h);
00357 return freetype_->createFont(typeFace, h, makeShadow);
00358 }