diff --git a/source/gui/CButton.cpp b/source/gui/CButton.cpp index 3329f9d986..e6f18fe278 100755 --- a/source/gui/CButton.cpp +++ b/source/gui/CButton.cpp @@ -46,7 +46,7 @@ void CButton::SetupText() CStr font; if (GUI::GetSetting(this, "font", font) != PS_OK || font.Length()==0) // Use the default if none is specified - font = "palatino12"; + font = "default"; CGUIString caption; GUI::GetSetting(this, "caption", caption); diff --git a/source/gui/CCheckBox.cpp b/source/gui/CCheckBox.cpp index 2a04299e9a..eb39a718ba 100755 --- a/source/gui/CCheckBox.cpp +++ b/source/gui/CCheckBox.cpp @@ -69,7 +69,7 @@ void CCheckBox::SetupText() //GUI::GetSetting(this, "square-side", square_side); // TODO Gee: Establish buffer zones - *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, CStr("palatino12"), m_CachedActualSize.GetWidth()-20, 0); + *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, CStr("default"), m_CachedActualSize.GetWidth()-20, 0); // Set position of text // TODO Gee: Big TODO diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index cd801af515..664b917f6f 100755 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -15,6 +15,7 @@ gee@pyro.nu #include "CRadioButton.h" #include "ps/Xeromyces.h" +#include "ps/Font.h" #include "Prometheus.h" #include "input.h" @@ -847,7 +848,7 @@ void CGUI::DrawText(const SGUIText &Text, const CColor &DefaultColor, glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - Handle font = 0; + CFont* font = NULL; CStr LastFontName; for (vector::const_iterator it = Text.m_TextCalls.begin(); @@ -860,10 +861,9 @@ void CGUI::DrawText(const SGUIText &Text, const CColor &DefaultColor, // Switch fonts when necessary, but remember the last one used if (it->m_Font != LastFontName) { - if (font) - unifont_unload(font); - font = unifont_load(it->m_Font); - unifont_bind(font); + delete font; + font = new CFont(it->m_Font); + font->Bind(); LastFontName = it->m_Font; } @@ -879,8 +879,7 @@ void CGUI::DrawText(const SGUIText &Text, const CColor &DefaultColor, } - if (font) - unifont_unload(font); + delete font; for (vector::const_iterator it=Text.m_SpriteCalls.begin(); it!=Text.m_SpriteCalls.end(); diff --git a/source/gui/CText.cpp b/source/gui/CText.cpp index b098aa6c0b..5551299223 100755 --- a/source/gui/CText.cpp +++ b/source/gui/CText.cpp @@ -56,7 +56,7 @@ void CText::SetupText() CStr font; if (GUI::GetSetting(this, "font", font) != PS_OK || font.Length()==0) // Use the default if none is specified - font = "palatino12"; + font = "default"; CGUIString caption; bool scrollbar; diff --git a/source/gui/GUItext.cpp b/source/gui/GUItext.cpp index 8fab84b3ca..31df0c2a47 100755 --- a/source/gui/GUItext.cpp +++ b/source/gui/GUItext.cpp @@ -11,6 +11,8 @@ gee@pyro.nu #include "OverlayText.h" #include +#include "ps/Font.h" + // TODO Gee: Remove, just for temp-output #include @@ -132,16 +134,12 @@ void CGUIString::GenerateTextCall(SFeedback &Feedback, } } + // Calculate the size of the font CSize size; + CFont font (TextCall.m_Font); + font.CalculateStringSize(TextCall.m_String, size.cx, size.cy); - //COverlayText txt(0, 0, 0, TextCall.m_Font, TextCall.m_String, TextCall.m_Color); - // TODO Gee: Ask Rich to change to (size); - //txt.GetOutputStringSize((int&)size.cx, (int&)size.cy); - Handle font = unifont_load(TextCall.m_Font); - unifont_stringsize(font, TextCall.m_String.c_str(), size.cx, size.cy); - unifont_unload(font); - - // append width, and make maximum height the height. + // Append width, and make maximum height the height. Feedback.m_Size.cx += size.cx; Feedback.m_Size.cy = max(Feedback.m_Size.cy, size.cy); diff --git a/source/lib/res/unifont.cpp b/source/lib/res/unifont.cpp index 2d4f03655a..9b3c2fc1ba 100755 --- a/source/lib/res/unifont.cpp +++ b/source/lib/res/unifont.cpp @@ -1,5 +1,5 @@ /* -$Id: unifont.cpp,v 1.13 2004/08/24 21:07:20 philip Exp $ +$Id: unifont.cpp,v 1.14 2004/08/27 22:07:34 philip Exp $ Unicode OpenGL texture font @@ -67,8 +67,14 @@ static int UniFont_reload(UniFont* f, const char* fn, Handle UNUSEDPARAM(h)) void* RawFNT; size_t FNTSize; - std::string FilenameFnt = FilenameBase+".fnt"; - const char* fnt_fn = FilenameFnt.c_str(); + + std::string FilenameFnt = FilenameBase+".fnt"; + const char* fnt_fn = FilenameFnt.c_str(); + + // Fail quietly if the file simply doesn't exist + if (! vfs_exists(fnt_fn)) + return -1; + Handle fh = vfs_load(fnt_fn, RawFNT, FNTSize); CHECK_ERR(fh); diff --git a/source/main.cpp b/source/main.cpp index ec7a966c39..4b46b670e3 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -19,6 +19,8 @@ #endif #include "lib/res/cursor.h" +#include "ps/Font.h" + #include "ps/CConsole.h" #include "ps/Game.h" @@ -104,9 +106,6 @@ static float g_Gamma = 1.0f; CGameAttributes g_GameAttributes; extern int game_view_handler(const SDL_Event* ev); -static Handle g_Font_Console; // for the console -static Handle g_Font_Misc; // random font for miscellaneous things - static CMusicPlayer MusicPlayer; CStr g_CursorName = "test"; @@ -459,9 +458,11 @@ static void Render() // Temp GUI message GeeTODO glLoadIdentity(); glTranslatef(10, 60, 0); - unifont_bind(g_Font_Misc); - glwprintf( L"%hs", g_GUI.TEMPmessage.c_str() ); - + { + CFont font("misc"); + font.Bind(); + glwprintf( L"%hs", g_GUI.TEMPmessage.c_str() ); + } oglCheck(); glLoadIdentity(); @@ -479,8 +480,6 @@ static void Render() oglCheck(); - unifont_bind(g_Font_Misc); - glColor4f(1.0f, 0.8f, 0.0f, 1.0f); glLoadIdentity(); @@ -489,14 +488,22 @@ static void Render() glScalef(1.0, -1.0, 1.0); CStrW fps_display = translate(L"$num FPS") << fps; - glwprintf(fps_display); + { + CFont font("misc"); + font.Bind(); + glwprintf(fps_display); + } oglCheck(); - unifont_bind(g_Font_Console); - glLoadIdentity(); - MICROLOG(L"render console"); - g_Console->Render(); + { + glLoadIdentity(); + + MICROLOG(L"render console"); + CFont font("console"); + font.Bind(); + g_Console->Render(); + } oglCheck(); @@ -663,14 +670,16 @@ static void InitVfs(char* argv0) static void psInit() { - g_Font_Console = unifont_load("console"); - g_Font_Misc = unifont_load("verdana16"); - g_Console->SetSize(0, g_yres-600.f, (float)g_xres, 600.f); - g_Console->m_iFontHeight = unifont_linespacing(g_Font_Console); - g_Console->m_iFontOffset = 9; + { + // Calculate and store the line spacing + CFont font("console"); + g_Console->m_iFontHeight = font.GetLineSpacing(); + // Offset by an arbitrary amount, to make it fit more nicely + g_Console->m_iFontOffset = 9; + } - I18n::LoadLanguage("pseudogreek"); + I18n::LoadLanguage("english"); loadHotkeys(); @@ -691,9 +700,6 @@ static void psShutdown() delete &g_GUI; #endif - unifont_unload(g_Font_Misc); - unifont_unload(g_Font_Console); - delete g_Console; // disable the special Windows cursor, or free textures for OGL cursors diff --git a/source/ps/Font.cpp b/source/ps/Font.cpp new file mode 100755 index 0000000000..dab49b016e --- /dev/null +++ b/source/ps/Font.cpp @@ -0,0 +1,72 @@ +#include "precompiled.h" + +#include "Font.h" + +#include "ps/ConfigDB.h" +#include "lib/res/unifont.h" + +#include "ps/CLogger.h" +#define LOG_CATEGORY "graphics" + +#include +#include + +const char* DefaultFont = "verdana16"; + +CFont::CFont(const char* name) +{ + // TODO perhaps: cache the resultant filename (or Handle) for each + // font name; but it's nice to allow run-time alteration of the fonts + + std::string fontFilename; + + CStr fontName = "font."; fontName += name; + + // See if the config value can be loaded + CConfigValue* fontFilenameVar = g_ConfigDB.GetValue(CFG_SYSTEM, fontName); + if (fontFilenameVar && fontFilenameVar->GetString(fontFilename)) + { + h = unifont_load(fontFilename.c_str()); + } + else + { + // Not found in the config file -- try it as a simple filename + h = unifont_load(name); + + // Found it + if (h > 0) + return; + + // Not found as a font -- give up and use the default. + LOG_ONCE(ERROR, LOG_CATEGORY, "Failed to find font '%s'", name); + h = unifont_load(DefaultFont); + // Assume this worked + } +} + +CFont::~CFont() +{ + unifont_unload(h); +} + +// Copy constructor +CFont::CFont(const CFont& font) +{ + // Reload the font, to maintain the ref count + h = unifont_load( h_filename(font.h) ); +} + +void CFont::Bind() +{ + unifont_bind(h); +} + +int CFont::GetLineSpacing() +{ + return unifont_linespacing(h); +} + +void CFont::CalculateStringSize(const CStr& string, int& width, int& height) +{ + unifont_stringsize(h, string.c_str(), width, height); +} diff --git a/source/ps/Font.h b/source/ps/Font.h new file mode 100755 index 0000000000..76e9570a1f --- /dev/null +++ b/source/ps/Font.h @@ -0,0 +1,39 @@ +#ifndef _FONT_H +#define _FONT_H + +#include "ps/Singleton.h" +#include "ps/CStr.h" + +#include "lib/res/h_mgr.h" + +#include +#include + +/* + +To use CFont: + +CFont font("name"); +font.Bind(); +glwprintf(L"Hello world"); + +*/ + +class CFont +{ +public: + CFont(const char* name); + ~CFont(); + + CFont(const CFont&); + + void Bind(); + int GetLineSpacing(); + void CalculateStringSize(const CStr& string, int& w, int& h); + +private: + Handle h; +}; + + +#endif // _FONT_H \ No newline at end of file