From 6bb805978cf3c8092201584d1c735a16861d2e88 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 10 Jun 2011 04:34:58 +0000 Subject: [PATCH] - better quality font rendering and performance still good. --- source/glest_game/facilities/logger.cpp | 22 +- source/glest_game/main/battle_end.cpp | 8 +- .../sources/graphics/gl/font_textFTGL.cpp | 4 +- .../sources/graphics/gl/text_renderer_gl.cpp | 248 ++++++------------ 4 files changed, 107 insertions(+), 175 deletions(-) diff --git a/source/glest_game/facilities/logger.cpp b/source/glest_game/facilities/logger.cpp index 048bdd03a..8fd173700 100644 --- a/source/glest_game/facilities/logger.cpp +++ b/source/glest_game/facilities/logger.cpp @@ -156,37 +156,41 @@ void Logger::renderLoadingScreen() { } } + int xLocation = metrics.getVirtualW() / 4; if(Renderer::renderText3DEnabled) { - renderer.renderText3D( - state, coreData.getMenuFontBig3D(), Vec3f(1.f), - metrics.getVirtualW()/4, 65*metrics.getVirtualH()/100, false); renderer.renderText3D( - current, coreData.getMenuFontNormal3D(), 1.0f, - metrics.getVirtualW() / 4, + state, coreData.getMenuFontBig3D(), Vec3f(1.f), + xLocation, + 65 * metrics.getVirtualH() / 100, false); + + renderer.renderText3D( + current, coreData.getMenuFontNormal3D(), Vec3f(1.f), + xLocation, 62 * metrics.getVirtualH() / 100, false); if(this->statusText != "") { renderer.renderText3D( this->statusText, coreData.getMenuFontNormal3D(), 1.0f, - metrics.getVirtualW() / 4, + xLocation, 56 * metrics.getVirtualH() / 100, false); } } else { renderer.renderText( state, coreData.getMenuFontBig(), Vec3f(1.f), - metrics.getVirtualW()/4, 65*metrics.getVirtualH()/100, false); + xLocation, + 65 * metrics.getVirtualH() / 100, false); renderer.renderText( current, coreData.getMenuFontNormal(), 1.0f, - metrics.getVirtualW() / 4, + xLocation, 62 * metrics.getVirtualH() / 100, false); if(this->statusText != "") { renderer.renderText( this->statusText, coreData.getMenuFontNormal(), 1.0f, - metrics.getVirtualW() / 4, + xLocation, 56 * metrics.getVirtualH() / 100, false); } } diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 2b978b6b3..d88b7cb35 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -72,9 +72,11 @@ void BattleEnd::update() { void BattleEnd::render(){ Renderer &renderer= Renderer::getInstance(); - TextRenderer2D *textRenderer2D= renderer.getTextRenderer(); - TextRenderer3D *textRenderer3D= renderer.getTextRenderer3D(); - TextRenderer *textRenderer= NULL; + + TextRenderer2D *textRenderer2D = renderer.getTextRenderer(); + TextRenderer3D *textRenderer3D = renderer.getTextRenderer3D(); + TextRenderer *textRenderer = NULL; + if(Renderer::renderText3DEnabled == true) { textRenderer= textRenderer3D; } diff --git a/source/shared_lib/sources/graphics/gl/font_textFTGL.cpp b/source/shared_lib/sources/graphics/gl/font_textFTGL.cpp index cdd189670..78d4b1337 100644 --- a/source/shared_lib/sources/graphics/gl/font_textFTGL.cpp +++ b/source/shared_lib/sources/graphics/gl/font_textFTGL.cpp @@ -60,7 +60,7 @@ TextFTGL::TextFTGL(FontTextHandlerType type) : Text(type) { //printf("2D font [%s]\n",fontFile); } else if(type == ftht_3D) { - ftFont = new FTBufferFont(fontFile); + ftFont = new FTGLTextureFont(fontFile); //printf("3D font [%s]\n",fontFile); } else { @@ -110,7 +110,7 @@ void TextFTGL::init(string fontName, int fontSize) { //printf("2D font [%s]\n",fontFile); } else if(type == ftht_3D) { - ftFont = new FTBufferFont(fontFile); + ftFont = new FTGLTextureFont(fontFile); //printf("3D font [%s]\n",fontFile); } else { diff --git a/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp b/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp index 9b295f7ae..6386906e3 100644 --- a/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp +++ b/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp @@ -19,6 +19,7 @@ #include #include "string_utils.h" #include "utf8.h" +#include "util.h" #include "leak_dumper.h" using namespace Shared::Util; @@ -32,41 +33,20 @@ namespace Shared { namespace Graphics { namespace Gl { TextRenderer2DGl::TextRenderer2DGl() { rendering= false; this->font = NULL; - - //font3D = NULL; - //tester = new TextRenderer3DGl(); } TextRenderer2DGl::~TextRenderer2DGl() { - //delete font3D; - //font3D = NULL; - - //delete tester; - //tester = NULL; } void TextRenderer2DGl::begin(Font2D *font) { this->font = static_cast(font); -// if(font3D == NULL) { -// font3D = new Font3DGl(); -// font3D->setYOffsetFactor(this->font->getYOffsetFactor()); -// font3D->setType("", this->font->getType()); -// font3D->setDepth(this->font->getWidth()); -// } -// tester->begin(font3D); -// return; - assert(!rendering); rendering = true; } void TextRenderer2DGl::render(const string &text, float x, float y, bool centered, Vec3f *color) { - - printf("**** RENDERING 2D text [%s]\n",text.c_str()); - - //tester->render(text, x, y, this->font->getWidth(),centered); - //return; + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("**** RENDERING 2D text [%s]\n",text.c_str()); assert(rendering); @@ -393,7 +373,26 @@ void TextRenderer3DGl::begin(Font3D *font) { void TextRenderer3DGl::render(const string &text, float x, float y, bool centered, Vec3f *color) { assert(rendering); - internalRender(text, x, y, centered, color); + string renderText = text; + if(Font::fontIsMultibyte == true) { + if(font->getTextHandler() != NULL) { + if(Font::fontIsRightToLeft == true) { + //printf("\n\n#A [%s]\n",renderText.c_str()); + //bool isRLM = utf8::starts_with_rlm(text.begin(), text.end() + text.size()); + + //printf("\n\nORIGINAL TEXT [%s] isRLM = %d\n\n",text.c_str(),isRLM); + //for(int i = 0; i < renderText.size(); ++i) { + // printf("i = %d c [%c][%d][%X]\n",i,renderText[i],renderText[i],renderText[i]); + //} + //if(isRLM == true) { + if(is_string_all_ascii(renderText) == false) { + strrev_utf8(renderText); + } + } + } + } + + internalRender(renderText, x, y, centered, color); } void TextRenderer3DGl::internalRender(const string &text, float x, float y, bool centered, Vec3f *color) { @@ -447,91 +446,91 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo } } - //glScalef(scale, scale, scale); float scaleX = 0.65; float scaleY = 0.75; float scaleZ = 1.0; + //float scaleX = 1; + //float scaleY = 1; + //float scaleZ = 1; - //x = translatePos.x; float yScaleFactor = (metrics->getHeight() * (1.0 - scaleY)); translatePos.y += yScaleFactor; - //y = translatePos.y; glTranslatef(translatePos.x, translatePos.y, translatePos.z); glScalef(scaleX, scaleY, scaleZ); //font->getTextHandler()->Render(text.c_str()); - if(Font::fontIsMultibyte == true) { - if(font->getTextHandler() != NULL) { - if(text.find("\n") == text.npos && text.find("\t") == text.npos) { - font->getTextHandler()->Render(text.c_str()); - } - else { - int line=0; - bool lastCharacterWasSpecial = true; - vector parts; - char szBuf[4096]=""; + if(font->getTextHandler() != NULL) { + if(text.find("\n") == text.npos && text.find("\t") == text.npos) { + font->getTextHandler()->Render(text.c_str()); + } + else { + int line=0; + bool lastCharacterWasSpecial = true; + vector parts; + char szBuf[4096]=""; - for (int i=0; text[i] != '\0'; ++i) { - szBuf[0] = '\0'; - sprintf(szBuf,"%c",text[i]); + for (int i=0; text[i] != '\0'; ++i) { + szBuf[0] = '\0'; + sprintf(szBuf,"%c",text[i]); - switch(text[i]) { - case '\t': + switch(text[i]) { + case '\t': + parts.push_back(szBuf); + lastCharacterWasSpecial = true; + //rasterPos= Vec2f((rasterPos.x / size + 3.f) * size, y-(size + 1.f) * line); + //glRasterPos2f(rasterPos.x, rasterPos.y); + break; + case '\n': + parts.push_back(szBuf); + lastCharacterWasSpecial = true; + //line++; + //rasterPos= Vec2f(static_cast(x), y - (fontFTGL->LineHeight(text.c_str()) * 2.f) * line); + //glRasterPos2f(rasterPos.x, rasterPos.y); + break; + default: + //glCallList(font->getHandle()+utext[i]); + if(lastCharacterWasSpecial == true) { parts.push_back(szBuf); - lastCharacterWasSpecial = true; - //rasterPos= Vec2f((rasterPos.x / size + 3.f) * size, y-(size + 1.f) * line); - //glRasterPos2f(rasterPos.x, rasterPos.y); - break; - case '\n': - parts.push_back(szBuf); - lastCharacterWasSpecial = true; - //line++; - //rasterPos= Vec2f(static_cast(x), y - (fontFTGL->LineHeight(text.c_str()) * 2.f) * line); - //glRasterPos2f(rasterPos.x, rasterPos.y); - break; - default: - //glCallList(font->getHandle()+utext[i]); - if(lastCharacterWasSpecial == true) { - parts.push_back(szBuf); - } - else { - parts[parts.size()-1] += szBuf; - } - lastCharacterWasSpecial = false; - } + } + else { + parts[parts.size()-1] += szBuf; + } + lastCharacterWasSpecial = false; } + } - bool needsRecursiveRender = false; - for (unsigned int i=0; i < parts.size(); ++i) { - switch(parts[i][0]) { - case '\t': - //translatePos= Vec3f((translatePos.x / size + 3.f) * size, y-(size + 1.f) * line, translatePos.z); - translatePos= Vec3f((translatePos.x / size + 3.f) * size, translatePos.y, translatePos.z); - needsRecursiveRender = true; - break; - case '\n': - { - line++; - float yLineValue = font->getTextHandler()->LineHeight(parts[i].c_str()); - translatePos= Vec3f(translatePos.x, translatePos.y - yLineValue, translatePos.z); - needsRecursiveRender = true; - } - break; - default: - if(needsRecursiveRender == true) { - internalRender(parts[i], translatePos.x, translatePos.y, false, color); - needsRecursiveRender = false; - } - else { - font->getTextHandler()->Render(parts[i].c_str()); - } - } + bool needsRecursiveRender = false; + for (unsigned int i=0; i < parts.size(); ++i) { + switch(parts[i][0]) { + case '\t': + //translatePos= Vec3f((translatePos.x / size + 3.f) * size, y-(size + 1.f) * line, translatePos.z); + translatePos= Vec3f((translatePos.x / size + 3.f) * size, translatePos.y, translatePos.z); + needsRecursiveRender = true; + break; + case '\n': + { + line++; + float yLineValue = font->getTextHandler()->LineHeight(parts[i].c_str()); + translatePos= Vec3f(translatePos.x, translatePos.y - yLineValue, translatePos.z); + needsRecursiveRender = true; + } + break; + default: + if(needsRecursiveRender == true) { + internalRender(parts[i], translatePos.x, translatePos.y, false, color); + needsRecursiveRender = false; + } + else { + font->getTextHandler()->Render(parts[i].c_str()); + } } } } - else { + } + else { + if(Font::fontIsMultibyte == true) { //setlocale(LC_CTYPE, "en_ca.UTF-8"); //wstring wText = widen(text); @@ -554,77 +553,6 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo //glListBase(font->getHandle()); //glCallLists(utfText.length(), GL_UNSIGNED_SHORT, &utfText[0]); } - } - else { - - if(font->getTextHandler() != NULL) { - if(text.find("\n") == text.npos && text.find("\t") == text.npos) { - font->getTextHandler()->Render(text.c_str()); - } - else { - int line=0; - bool lastCharacterWasSpecial = true; - vector parts; - char szBuf[4096]=""; - - for (int i=0; text[i] != '\0'; ++i) { - szBuf[0] = '\0'; - sprintf(szBuf,"%c",text[i]); - - switch(text[i]) { - case '\t': - parts.push_back(szBuf); - lastCharacterWasSpecial = true; - //rasterPos= Vec2f((rasterPos.x / size + 3.f) * size, y-(size + 1.f) * line); - //glRasterPos2f(rasterPos.x, rasterPos.y); - break; - case '\n': - parts.push_back(szBuf); - lastCharacterWasSpecial = true; - //line++; - //rasterPos= Vec2f(static_cast(x), y - (fontFTGL->LineHeight(text.c_str()) * 2.f) * line); - //glRasterPos2f(rasterPos.x, rasterPos.y); - break; - default: - //glCallList(font->getHandle()+utext[i]); - if(lastCharacterWasSpecial == true) { - parts.push_back(szBuf); - } - else { - parts[parts.size()-1] += szBuf; - } - lastCharacterWasSpecial = false; - } - } - - bool needsRecursiveRender = false; - for (unsigned int i=0; i < parts.size(); ++i) { - switch(parts[i][0]) { - case '\t': - //translatePos= Vec3f((translatePos.x / size + 3.f) * size, y-(size + 1.f) * line, translatePos.z); - translatePos= Vec3f((translatePos.x / size + 3.f) * size, translatePos.y, translatePos.z); - needsRecursiveRender = true; - break; - case '\n': - { - line++; - float yLineValue = font->getTextHandler()->LineHeight(parts[i].c_str()); - translatePos= Vec3f(translatePos.x, translatePos.y - yLineValue, translatePos.z); - needsRecursiveRender = true; - } - break; - default: - if(needsRecursiveRender == true) { - internalRender(parts[i], translatePos.x, translatePos.y, false, color); - needsRecursiveRender = false; - } - else { - font->getTextHandler()->Render(parts[i].c_str()); - } - } - } - } - } else { for (int i=0; utext[i]!='\0'; ++i) { glCallList(font->getHandle()+utext[i]); @@ -633,8 +561,6 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo } glPopMatrix(); - //glPopMatrix(); - //glPopAttrib(); if(color != NULL) { glPopAttrib();