From cc958d266ec6c57ec6c210da166271c20b847199 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 25 May 2011 21:11:12 +0000 Subject: [PATCH] - updates for possible chinese support (but i really doubt this shows the right text) but stil la good start --- source/glest_game/main/main.cpp | 22 +++--- source/shared_lib/include/graphics/font.h | 1 + source/shared_lib/sources/graphics/font.cpp | 1 + .../sources/graphics/gl/text_renderer_gl.cpp | 68 +++++++++++++++---- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 8a922701..d0b99728 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -2440,8 +2440,9 @@ int glestMain(int argc, char** argv) { Texture::useTextureCompression = config.getBool("EnableTextureCompression","false"); // 256 for English // 30000 for Chinese - Font::charCount = config.getInt("FONT_CHARCOUNT",intToStr(Font::charCount).c_str()); - Font::fontTypeName = config.getString("FONT_TYPENAME",Font::fontTypeName.c_str()); + Font::charCount = config.getInt("FONT_CHARCOUNT",intToStr(Font::charCount).c_str()); + Font::fontTypeName = config.getString("FONT_TYPENAME",Font::fontTypeName.c_str()); + Font::fontIsMultibyte = config.getBool("FONT_MULTIBYTE",intToStr(Font::fontIsMultibyte).c_str()); // Example values: // DEFAULT_CHARSET (English) = 1 // GB2312_CHARSET (Chinese) = 134 @@ -2463,7 +2464,7 @@ int glestMain(int argc, char** argv) { // Setup debug logging etc setupLogging(config, haveSpecialOutputCommandLineOption); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::charSet = %d\n",__FILE__,__FUNCTION__,__LINE__,Font::charCount,Font::fontTypeName.c_str(),Shared::Platform::charSet); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::charSet = %d, Font::fontIsMultibyte = %d\n",__FILE__,__FUNCTION__,__LINE__,Font::charCount,Font::fontTypeName.c_str(),Shared::Platform::charSet,Font::fontIsMultibyte); NetworkInterface::setDisplayMessageFunction(ExceptionHandler::DisplayMessage); MenuStateMasterserver::setDisplayMessageFunction(ExceptionHandler::DisplayMessage); @@ -2531,20 +2532,25 @@ int glestMain(int argc, char** argv) { lang.loadStrings(language); - if( lang.hasString("FONT_CHARCOUNT") && - lang.hasString("FONT_TYPENAME") && - lang.hasString("FONT_CHARSET")) { + if( lang.hasString("FONT_CHARCOUNT")) { // 256 for English // 30000 for Chinese Font::charCount = strToInt(lang.get("FONT_CHARCOUNT")); + } + if( lang.hasString("FONT_TYPENAME")) { Font::fontTypeName = lang.get("FONT_TYPENAME"); + } + if( lang.hasString("FONT_CHARSET")) { // Example values: // DEFAULT_CHARSET (English) = 1 // GB2312_CHARSET (Chinese) = 134 Shared::Platform::charSet = strToInt(lang.get("FONT_CHARSET")); } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::charSet = %d\n",__FILE__,__FUNCTION__,__LINE__,Font::charCount,Font::fontTypeName.c_str(),Shared::Platform::charSet); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Using Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::charSet = %d\n",Font::charCount,Font::fontTypeName.c_str(),Shared::Platform::charSet); + if( lang.hasString("FONT_MULTIBYTE")) { + Font::fontIsMultibyte = strToBool(lang.get("FONT_MULTIBYTE")); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::charSet = %d, Font::fontIsMultibyte = %d\n",__FILE__,__FUNCTION__,__LINE__,Font::charCount,Font::fontTypeName.c_str(),Shared::Platform::charSet,Font::fontIsMultibyte); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Using Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::charSet = %d, Font::fontIsMultibyte = %d\n",Font::charCount,Font::fontTypeName.c_str(),Shared::Platform::charSet,Font::fontIsMultibyte); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/shared_lib/include/graphics/font.h b/source/shared_lib/include/graphics/font.h index 8265bddd..e0f8aefe 100644 --- a/source/shared_lib/include/graphics/font.h +++ b/source/shared_lib/include/graphics/font.h @@ -47,6 +47,7 @@ class Font{ public: static int charCount; static std::string fontTypeName; + static bool fontIsMultibyte; public: enum Width{ diff --git a/source/shared_lib/sources/graphics/font.cpp b/source/shared_lib/sources/graphics/font.cpp index 0bb23faf..9c4dcfa7 100644 --- a/source/shared_lib/sources/graphics/font.cpp +++ b/source/shared_lib/sources/graphics/font.cpp @@ -21,6 +21,7 @@ namespace Shared{ namespace Graphics{ int Font::charCount= 256; std::string Font::fontTypeName = "Times New Roman"; +bool Font::fontIsMultibyte = false; // ===================================================== // class FontMetrics 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 9e3105a3..7bde8ab4 100644 --- a/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp +++ b/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp @@ -13,6 +13,7 @@ #include "opengl.h" #include "font_gl.h" +//#include #include "leak_dumper.h" namespace Shared{ namespace Graphics{ namespace Gl{ @@ -32,6 +33,22 @@ void TextRenderer2DGl::begin(const Font2D *font){ this->font= static_cast(font); } +//// Convert a narrow string to a wide string// +//std::wstring widen(const std::string& str) { +// // Make space for wide string +// wchar_t* buffer = new wchar_t[str.size() + 1]; +// // convert ASCII to UNICODE +// mbstowcs( buffer, str.c_str(), str.size() ); +// // NULL terminate it +// buffer[str.size()] = 0; +// // Clean memory and return it +// std::wstring wstr = buffer; +// delete [] buffer; +// return wstr; +// +//} +//// Widen an individual character + void TextRenderer2DGl::render(const string &text, int x, int y, bool centered, Vec3f *color) { assert(rendering); @@ -57,19 +74,44 @@ void TextRenderer2DGl::render(const string &text, int x, int y, bool centered, V } glRasterPos2f(rasterPos.x, rasterPos.y); - for (int i=0; utext[i]!='\0'; ++i) { - switch(utext[i]){ - case '\t': - rasterPos= Vec2f((rasterPos.x/size+3.f)*size, y-(size+1.f)*line); - glRasterPos2f(rasterPos.x, rasterPos.y); - break; - case '\n': - line++; - rasterPos= Vec2f(static_cast(x), y-(metrics->getHeight()*2.f)*line); - glRasterPos2f(rasterPos.x, rasterPos.y); - break; - default: - glCallList(font->getHandle()+utext[i]); + if(Font::fontIsMultibyte == true) { + //setlocale(LC_CTYPE, "en_ca.UTF-8"); + + //wstring wText = widen(text); + //glListBase(font->getHandle()); + //glCallLists(wText.length(), GL_UNSIGNED_SHORT, &wText[0]); + + //string utfText = text; + //glListBase(font->getHandle()); + //glCallLists(utfText.length(), GL_UNSIGNED_SHORT, &utfText[0]); + + string utfText = text; + glListBase(font->getHandle()); + glCallLists(text.length(), GL_UNSIGNED_SHORT, &utext[0]); + + //std::locale loc(""); + //wstring wText = widen(text); + //std::string strBuffer(Text.size() * 4 + 1, 0); + //std::use_facet >(loc).narrow(&Text[0], &Text[0] + Text.size(), '?', &strBuffer[0]); + //string utfText = std::string(&strBuffer[0]); + //glListBase(font->getHandle()); + //glCallLists(utfText.length(), GL_UNSIGNED_SHORT, &utfText[0]); + } + else { + for (int i=0; utext[i]!='\0'; ++i) { + switch(utext[i]){ + case '\t': + rasterPos= Vec2f((rasterPos.x/size+3.f)*size, y-(size+1.f)*line); + glRasterPos2f(rasterPos.x, rasterPos.y); + break; + case '\n': + line++; + rasterPos= Vec2f(static_cast(x), y-(metrics->getHeight()*2.f)*line); + glRasterPos2f(rasterPos.x, rasterPos.y); + break; + default: + glCallList(font->getHandle()+utext[i]); + } } }