diff --git a/source/graphics/FontManager.cpp b/source/graphics/FontManager.cpp index 4bffa209b8..846ac6850f 100644 --- a/source/graphics/FontManager.cpp +++ b/source/graphics/FontManager.cpp @@ -26,7 +26,7 @@ #include "ps/Filesystem.h" #include "renderer/Renderer.h" -shared_ptr CFontManager::LoadFont(const CStrW& fontName) +shared_ptr CFontManager::LoadFont(CStrIntern fontName) { FontsMap::iterator it = m_Fonts.find(fontName); if (it != m_Fonts.end()) @@ -37,24 +37,24 @@ shared_ptr CFontManager::LoadFont(const CStrW& fontName) if (!ReadFont(font.get(), fontName)) { // Fall back to default font (unless this is the default font) - if (fontName == L"sans-10") + if (fontName == str_sans_10) font.reset(); else - font = LoadFont(L"sans-10"); + font = LoadFont(str_sans_10); } m_Fonts[fontName] = font; return font; } -bool CFontManager::ReadFont(CFont* font, const CStrW& fontName) +bool CFontManager::ReadFont(CFont* font, CStrIntern fontName) { const VfsPath path(L"fonts/"); // Read font definition file into a stringstream shared_ptr buf; size_t size; - const VfsPath fntName(fontName + L".fnt"); + const VfsPath fntName(fontName.string() + ".fnt"); if (g_VFS->LoadFile(path / fntName, buf, size) < 0) { LOGERROR(L"Failed to open font file %ls", (path / fntName).string().c_str()); @@ -66,7 +66,7 @@ bool CFontManager::ReadFont(CFont* font, const CStrW& fontName) FNTStream >> Version; if (Version != 101) // Make sure this is from a recent version of the font builder { - LOGERROR(L"Font %ls has invalid version", fontName.c_str()); + LOGERROR(L"Font %hs has invalid version", fontName.c_str()); return 0; } @@ -100,7 +100,7 @@ bool CFontManager::ReadFont(CFont* font, const CStrW& fontName) if (Codepoint < 0 || Codepoint > 0xFFFF) { - LOGWARNING(L"Font %ls has invalid codepoint 0x%x", fontName.c_str(), Codepoint); + LOGWARNING(L"Font %hs has invalid codepoint 0x%x", fontName.c_str(), Codepoint); continue; } @@ -121,7 +121,7 @@ bool CFontManager::ReadFont(CFont* font, const CStrW& fontName) ENSURE(font->m_Height); // Ensure the height has been found (which should always happen if the font includes an 'I') // Load glyph texture - const VfsPath imgName(fontName + L".png"); + const VfsPath imgName(fontName.string() + ".png"); CTextureProperties textureProps(path / imgName); textureProps.SetFilter(GL_NEAREST); if (!font->m_HasRGB) diff --git a/source/graphics/FontManager.h b/source/graphics/FontManager.h index 9275c10755..c769bb379b 100644 --- a/source/graphics/FontManager.h +++ b/source/graphics/FontManager.h @@ -21,7 +21,7 @@ #include class CFont; -class CStrW; +class CStrIntern; /** * Font manager: loads and caches bitmap fonts. @@ -29,12 +29,12 @@ class CStrW; class CFontManager { public: - shared_ptr LoadFont(const CStrW& fontName); + shared_ptr LoadFont(CStrIntern fontName); private: - bool ReadFont(CFont* font, const CStrW& fontName); + bool ReadFont(CFont* font, CStrIntern fontName); - typedef boost::unordered_map > FontsMap; + typedef boost::unordered_map > FontsMap; FontsMap m_Fonts; }; diff --git a/source/graphics/FontMetrics.cpp b/source/graphics/FontMetrics.cpp index 49f84dee0b..1026b90efb 100644 --- a/source/graphics/FontMetrics.cpp +++ b/source/graphics/FontMetrics.cpp @@ -24,7 +24,7 @@ #include "ps/CLogger.h" #include "renderer/Renderer.h" -CFontMetrics::CFontMetrics(const CStrW& font) +CFontMetrics::CFontMetrics(CStrIntern font) { m_Font = g_Renderer.GetFontManager().LoadFont(font); } diff --git a/source/graphics/FontMetrics.h b/source/graphics/FontMetrics.h index 1024a0cf01..d62d590604 100644 --- a/source/graphics/FontMetrics.h +++ b/source/graphics/FontMetrics.h @@ -19,7 +19,7 @@ #define INCLUDED_FONTMETRICS class CFont; -class CStrW; +class CStrIntern; /** * Helper class for measuring sizes of text. @@ -29,7 +29,7 @@ class CStrW; class CFontMetrics { public: - CFontMetrics(const CStrW& font); + CFontMetrics(CStrIntern font); int GetLineSpacing() const; int GetHeight() const; diff --git a/source/graphics/ShaderDefines.cpp b/source/graphics/ShaderDefines.cpp index b07d9b9079..6449aef13f 100644 --- a/source/graphics/ShaderDefines.cpp +++ b/source/graphics/ShaderDefines.cpp @@ -24,11 +24,6 @@ #include -size_t hash_value(const CStrIntern& v) -{ - return v.GetHash(); -} - size_t hash_value(const CVector4D& v) { size_t hash = 0; diff --git a/source/graphics/TextRenderer.cpp b/source/graphics/TextRenderer.cpp index c150cf81ae..a594def218 100644 --- a/source/graphics/TextRenderer.cpp +++ b/source/graphics/TextRenderer.cpp @@ -32,7 +32,7 @@ CTextRenderer::CTextRenderer(const CShaderProgramPtr& shader) : { ResetTransform(); Color(CColor(1.0f, 1.0f, 1.0f, 1.0f)); - Font(L"sans-10"); + Font(str_sans_10); } void CTextRenderer::ResetTransform() @@ -85,7 +85,7 @@ void CTextRenderer::Color(float r, float g, float b, float a) Color(CColor(r, g, b, a)); } -void CTextRenderer::Font(const CStrW& font) +void CTextRenderer::Font(CStrIntern font) { if (font != m_FontName) { diff --git a/source/graphics/TextRenderer.h b/source/graphics/TextRenderer.h index 199fe206a0..f2a80ac423 100644 --- a/source/graphics/TextRenderer.h +++ b/source/graphics/TextRenderer.h @@ -20,7 +20,7 @@ #include "graphics/ShaderProgram.h" #include "maths/Matrix3D.h" -#include "ps/CStr.h" +#include "ps/CStrIntern.h" #include "ps/Overlay.h" class CFont; @@ -62,7 +62,7 @@ public: /** * Set the font for subsequent print calls. */ - void Font(const CStrW& font); + void Font(CStrIntern font); /** * Print formatted text at (0,0) under the current transform, @@ -160,7 +160,7 @@ private: CRect m_Clipping; CColor m_Color; - CStrW m_FontName; + CStrIntern m_FontName; shared_ptr m_Font; bool m_Dirty; diff --git a/source/gui/CCheckBox.cpp b/source/gui/CCheckBox.cpp index c5d7486333..9f3fc8e004 100644 --- a/source/gui/CCheckBox.cpp +++ b/source/gui/CCheckBox.cpp @@ -24,6 +24,7 @@ CCheckBox #include "CCheckBox.h" #include "ps/CLogger.h" +#include "ps/CStrIntern.h" #include "graphics/FontMetrics.h" @@ -130,7 +131,7 @@ void CCheckBox::Draw() GUI::GetSetting(this, "cell_id", cell_id); // Get line height - CFontMetrics font (font_name); + CFontMetrics font (CStrIntern(font_name.ToUTF8())); float line_height = (float)font.GetHeight(); float bz = GetBufferedZ(); diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 0781873735..6ad0c60f76 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -642,10 +642,12 @@ struct SGenerateTextImage }; SGUIText CGUI::GenerateText(const CGUIString &string, - const CStrW& Font, const float &Width, const float &BufferZone, + const CStrW& FontW, const float &Width, const float &BufferZone, const IGUIObject *pObject) { SGUIText Text; // object we're generating + + CStrIntern Font(FontW.ToUTF8()); if (string.m_Words.size() == 0) return Text; diff --git a/source/gui/CInput.cpp b/source/gui/CInput.cpp index e679f98fce..888e37e734 100644 --- a/source/gui/CInput.cpp +++ b/source/gui/CInput.cpp @@ -1030,12 +1030,13 @@ void CInput::Draw() if (GetGUI()) { - CStrW font_name; + CStrW font_name_w; CColor color, color_selected; //CStrW caption; - GUI::GetSetting(this, "font", font_name); + GUI::GetSetting(this, "font", font_name_w); GUI::GetSetting(this, "textcolor", color); GUI::GetSetting(this, "textcolor_selected", color_selected); + CStrIntern font_name(font_name_w.ToUTF8()); // Get pointer of caption, it might be very large, and we don't // want to copy it continuously. @@ -1378,13 +1379,14 @@ void CInput::Draw() void CInput::UpdateText(int from, int to_before, int to_after) { CStrW caption; - CStrW font_name; + CStrW font_name_w; float buffer_zone; bool multiline; - GUI::GetSetting(this, "font", font_name); + GUI::GetSetting(this, "font", font_name_w); GUI::GetSetting(this, "caption", caption); GUI::GetSetting(this, "buffer_zone", buffer_zone); GUI::GetSetting(this, "multiline", multiline); + CStrIntern font_name(font_name_w.ToUTF8()); // Ensure positions are valid after caption changes m_iBufferPos = std::min(m_iBufferPos, (int)caption.size()); @@ -1765,10 +1767,11 @@ int CInput::GetMouseHoveringTextPosition() if (multiline) { - CStrW font_name; + CStrW font_name_w; bool scrollbar; - GUI::GetSetting(this, "font", font_name); + GUI::GetSetting(this, "font", font_name_w); GUI::GetSetting(this, "scrollbar", scrollbar); + CStrIntern font_name(font_name_w.ToUTF8()); float scroll=0.f; if (scrollbar) @@ -1917,10 +1920,11 @@ void CInput::UpdateAutoScroll() // Autoscrolling up and down if (multiline) { - CStrW font_name; + CStrW font_name_w; bool scrollbar; - GUI::GetSetting(this, "font", font_name); + GUI::GetSetting(this, "font", font_name_w); GUI::GetSetting(this, "scrollbar", scrollbar); + CStrIntern font_name(font_name_w.ToUTF8()); float scroll=0.f; if (!scrollbar) diff --git a/source/gui/GUItext.cpp b/source/gui/GUItext.cpp index d1b7d958a5..770c8ea003 100644 --- a/source/gui/GUItext.cpp +++ b/source/gui/GUItext.cpp @@ -43,7 +43,7 @@ void CGUIString::SFeedback::Reset() } void CGUIString::GenerateTextCall(SFeedback &Feedback, - const CStrW& DefaultFont, + CStrIntern DefaultFont, const int &from, const int &to, const bool FirstLine, const IGUIObject *pObject) const @@ -224,7 +224,7 @@ void CGUIString::GenerateTextCall(SFeedback &Feedback, if (it2->m_TagType == CGUIString::TextChunk::Tag::TAG_FONT) { // TODO Gee: (2004-08-15) Check if Font exists? - TextCall.m_Font = CStr(it2->m_TagValue).FromUTF8(); + TextCall.m_Font = CStrIntern(it2->m_TagValue); } } diff --git a/source/gui/GUItext.h b/source/gui/GUItext.h index fe1abf11b5..2e12df891d 100644 --- a/source/gui/GUItext.h +++ b/source/gui/GUItext.h @@ -39,6 +39,7 @@ GUI text, handles text stuff #include #include "CGUISprite.h" +#include "ps/CStrIntern.h" //-------------------------------------------------------- // Declarations @@ -130,7 +131,7 @@ struct SGUIText /** * Font name */ - CStrW m_Font; + CStrIntern m_Font; /** * Settings @@ -318,7 +319,7 @@ public: * it avoids duplicates. */ void GenerateTextCall(SFeedback &Feedback, - const CStrW& DefaultFont, + CStrIntern DefaultFont, const int &from, const int &to, const bool FirstLine, const IGUIObject *pObject=NULL) const; diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index fae7961c40..ccdb61a750 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -214,7 +214,7 @@ void CConsole::Render() CShaderTechniquePtr textTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); textTech->BeginPass(); CTextRenderer textRenderer(textTech->GetShader()); - textRenderer.Font(CONSOLE_FONT); + textRenderer.Font(CStrIntern(CONSOLE_FONT)); textRenderer.SetTransform(transform); DrawHistory(textRenderer); diff --git a/source/ps/CConsole.h b/source/ps/CConsole.h index 7e17dc2ef2..545a7411a0 100644 --- a/source/ps/CConsole.h +++ b/source/ps/CConsole.h @@ -40,7 +40,7 @@ class CTextRenderer; #define CONSOLE_BUFFER_SIZE 1024 // for text being typed into the console #define CONSOLE_MESSAGE_SIZE 1024 // for messages being printed into the console -#define CONSOLE_FONT L"mono-10" +#define CONSOLE_FONT "mono-10" /** * In-game console. diff --git a/source/ps/CLogger.cpp b/source/ps/CLogger.cpp index 8c85159dcf..1a68943e6d 100644 --- a/source/ps/CLogger.cpp +++ b/source/ps/CLogger.cpp @@ -280,7 +280,7 @@ void CLogger::Render() CleanupRenderQueue(); - CStrW font_name(L"mono-stroke-10"); + CStrIntern font_name("mono-stroke-10"); CFontMetrics font(font_name); int lineSpacing = font.GetLineSpacing(); diff --git a/source/ps/CStrIntern.cpp b/source/ps/CStrIntern.cpp index ea396de7de..1254c71014 100644 --- a/source/ps/CStrIntern.cpp +++ b/source/ps/CStrIntern.cpp @@ -147,6 +147,11 @@ size_t CStrIntern::length() const return m->data.length(); } +bool CStrIntern::empty() const +{ + return m->data.empty(); +} + const std::string& CStrIntern::string() const { return m->data; diff --git a/source/ps/CStrIntern.h b/source/ps/CStrIntern.h index 6ee5342241..c31f0d4cbc 100644 --- a/source/ps/CStrIntern.h +++ b/source/ps/CStrIntern.h @@ -56,6 +56,8 @@ public: */ size_t length() const; + bool empty() const; + /** * Returns as std::string. */ @@ -69,6 +71,11 @@ public: return m == b.m; } + bool operator!=(const CStrIntern& b) const + { + return m != b.m; + } + /** * Compare with some arbitrary total order. * (In particular, this is not alphabetic order, @@ -83,6 +90,11 @@ private: CStrInternInternals* m; }; +static inline size_t hash_value(const CStrIntern& str) +{ + return str.GetHash(); +} + #define X(id) extern CStrIntern str_##id; #define X2(id, str) extern CStrIntern str_##id; #include "CStrInternStatic.h" diff --git a/source/ps/CStrInternStatic.h b/source/ps/CStrInternStatic.h index 500b7c03ab..0e85771002 100644 --- a/source/ps/CStrInternStatic.h +++ b/source/ps/CStrInternStatic.h @@ -119,6 +119,7 @@ X(refractionMap) X(refractionMatrix) X(renderedTex) X(repeatScale) +X2(sans_10, "sans-10"); X(saturation) X(screenSize) X(shadingColor) diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index 05c66b578c..ba0fc8df3e 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -507,7 +507,7 @@ static void InitPs(bool setup_gui, const CStrW& gui_page, CScriptVal initData) g_Console->UpdateScreenSize(g_xres, g_yres); // Calculate and store the line spacing - CFontMetrics font(CONSOLE_FONT); + CFontMetrics font(CStrIntern(CONSOLE_FONT)); g_Console->m_iFontHeight = font.GetLineSpacing(); g_Console->m_iFontWidth = font.GetCharacterWidth(L'C'); g_Console->m_charsPerPage = (size_t)(g_xres / g_Console->m_iFontWidth); diff --git a/source/ps/ProfileViewer.cpp b/source/ps/ProfileViewer.cpp index 8641739c29..ec18b8736f 100644 --- a/source/ps/ProfileViewer.cpp +++ b/source/ps/ProfileViewer.cpp @@ -170,7 +170,7 @@ void CProfileViewer::RenderProfile() const std::vector& columns = table->GetColumns(); size_t numrows = table->GetNumberRows(); - CStrW font_name = L"mono-stroke-10"; + CStrIntern font_name("mono-stroke-10"); CFontMetrics font(font_name); int lineSpacing = font.GetLineSpacing(); diff --git a/source/renderer/TerrainRenderer.cpp b/source/renderer/TerrainRenderer.cpp index a1c599ff92..f5592ed404 100644 --- a/source/renderer/TerrainRenderer.cpp +++ b/source/renderer/TerrainRenderer.cpp @@ -999,7 +999,7 @@ void TerrainRenderer::RenderPriorities() tech->BeginPass(); CTextRenderer textRenderer(tech->GetShader()); - textRenderer.Font(L"mono-stroke-10"); + textRenderer.Font(CStrIntern("mono-stroke-10")); textRenderer.Color(1.0f, 1.0f, 0.0f); for (size_t i = 0; i < m->visiblePatches.size(); ++i)