From 886c0996d2e3fd23b49ef7764ba94225143e07a1 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Mon, 2 Nov 2009 22:03:32 +0000 Subject: [PATCH] Allow drop shadows in fonts again --- include/gui2/Label.hpp | 6 +++--- include/gui2/Theme.hpp | 4 +++- layouts/game.xml | 12 +++++------ schemas/layout.xsd | 1 + src/Game.cpp | 2 +- src/ft/Font.cpp | 46 +++++++++++++++++++++++++++++++----------- src/ft/IFont.hpp | 4 ++-- src/gui2/Label.cpp | 13 +++++++----- src/gui2/Theme.cpp | 19 ++++++++++++++++- 9 files changed, 75 insertions(+), 32 deletions(-) diff --git a/include/gui2/Label.hpp b/include/gui2/Label.hpp index c3c1b14..0a8f552 100644 --- a/include/gui2/Label.hpp +++ b/include/gui2/Label.hpp @@ -29,15 +29,15 @@ namespace gui { public: Label(const AttributeSet& attrs); - const string& text() const { return _text; } - void text(const string& t) { _text = t; } + const string& text() const { return text_; } + void text(const string& t) { text_ = t; } void format(const char* fmt, ...); void render(RenderContext& rc) const; void adjustForTheme(Theme& theme); private: - string _text; + string text_, fontName; }; } diff --git a/include/gui2/Theme.hpp b/include/gui2/Theme.hpp index f5b0ba8..3bee32a 100644 --- a/include/gui2/Theme.hpp +++ b/include/gui2/Theme.hpp @@ -38,9 +38,11 @@ namespace gui { // Fonts IFontPtr normal_font() const { return normal_font_; } + IFontPtr font(const string& fontName) const; private: - IFontPtr normal_font_; + IFontPtr normal_font_; + IFontPtr dropShadowFont; }; } diff --git a/layouts/game.xml b/layouts/game.xml index 071ef0f..c71d8c2 100644 --- a/layouts/game.xml +++ b/layouts/game.xml @@ -1,10 +1,8 @@ - - - + + diff --git a/schemas/layout.xsd b/schemas/layout.xsd index 0f3f70b..43787cb 100644 --- a/schemas/layout.xsd +++ b/schemas/layout.xsd @@ -41,6 +41,7 @@ + diff --git a/src/Game.cpp b/src/Game.cpp index 6e739b1..d7d1bcc 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -190,7 +190,7 @@ void Game::update(IPickBufferPtr aPickBuffer, int aDelta) myThrottleMeter->setValue(train->controller()->throttle()); myBrakeLabel->setVisible(train->controller()->brakeOn()); - layout->cast("/status_wnd/speed_label").format( + layout->cast("/speed_label").format( "Speed: %.1lfmph", train->speed() * msToMPH); const double pressure = train->controller()->pressure(); diff --git a/src/ft/Font.cpp b/src/ft/Font.cpp index a81d9c4..29a941d 100644 --- a/src/ft/Font.cpp +++ b/src/ft/Font.cpp @@ -35,7 +35,7 @@ using namespace ft; class Glyph { public: - Glyph(FT_Face& face, FT_ULong uch, FontType type); + Glyph(FT_Face& face, FT_ULong uch, FontType type, bool dropShadow); ~Glyph(); void render() const; @@ -47,15 +47,19 @@ public: float advance_y() const { return advance_y_; } private: static int next_power_of_2(int a); + + void renderGlyph() const; GLuint tex; float width_, height_; float tex_xmax, tex_ymax; float top, left; float advance_x_, advance_y_; + bool dropShadow; }; -Glyph::Glyph(FT_Face& face, FT_ULong uch, FontType type) +Glyph::Glyph(FT_Face& face, FT_ULong uch, FontType type, bool dropShadow) + : dropShadow(dropShadow) { int index = FT_Get_Char_Index(face, uch); @@ -149,10 +153,8 @@ Glyph::~Glyph() glDeleteTextures(1, &tex); } -void Glyph::render() const +void Glyph::renderGlyph() const { - glBindTexture(GL_TEXTURE_2D, tex); - glBegin(GL_QUADS); glTexCoord2f(0, tex_ymax); @@ -168,6 +170,25 @@ void Glyph::render() const glVertex2f(left + width_, height_ - top); glEnd(); +} + +void Glyph::render() const +{ + glBindTexture(GL_TEXTURE_2D, tex); + + if (dropShadow) { + glPushMatrix(); + glPushAttrib(GL_CURRENT_BIT); + + glTranslatef(1.5f, 1.5f, 0.0f); + glColor3f(0.0f, 0.0f, 0.0f); + renderGlyph(); + + glPopAttrib(); + glPopMatrix(); + } + + renderGlyph(); glTranslatef(advance_x_, advance_y_, 0.0f); } @@ -185,7 +206,7 @@ namespace {// REMOVE class Font : public IFont { public: - Font(const string& file, int h, FontType type); + Font(const string& file, int h, FontType type, bool dropShadow); ~Font(); void print(int x, int y, Colour c, const string& s) const; @@ -205,7 +226,7 @@ private: FT_Library Font::library; int Font::library_ref_count = 0; -Font::Font(const string& file, int h, FontType type) + Font::Font(const string& file, int h, FontType type, bool dropShadow) { if (++library_ref_count == 1) { if (FT_Init_FreeType(&library)) @@ -224,7 +245,7 @@ Font::Font(const string& file, int h, FontType type) 0, 0); // Default DPI for (char ch = 0; ch < 127; ch++) - glyphs.push_back(new Glyph(face, ch, type)); + glyphs.push_back(new Glyph(face, ch, type, dropShadow)); int ascent = static_cast(face->size->metrics.ascender / 64.0); int descent = static_cast(face->size->metrics.descender / 64.0); @@ -278,18 +299,19 @@ int Font::text_width(const string& s) const } } -IFontPtr ft::load_font(const string& file, int h, FontType type) +IFontPtr ft::loadFont(const string& file, int h, FontType type, + bool dropShadow) { - typedef tuple FontToken; + typedef tuple FontToken; static map cache; - FontToken t = make_tuple(file, h); + FontToken t = make_tuple(file, h, dropShadow); map::iterator it = cache.find(t); if (it != cache.end()) return (*it).second; else { - IFontPtr p(new Font(file, h, type)); + IFontPtr p(new Font(file, h, type, dropShadow)); cache[t] = p; return p; } diff --git a/src/ft/IFont.hpp b/src/ft/IFont.hpp index b6a9b84..534d4be 100644 --- a/src/ft/IFont.hpp +++ b/src/ft/IFont.hpp @@ -39,8 +39,8 @@ namespace ft { FONT_NORMAL, FONT_MONO }; - IFontPtr load_font(const string& file, int h, - FontType type=FONT_NORMAL); + IFontPtr loadFont(const string& file, int h, + FontType type=FONT_NORMAL, bool dropShadow=false); } diff --git a/src/gui2/Label.cpp b/src/gui2/Label.cpp index d02d4a8..f57ce0c 100644 --- a/src/gui2/Label.cpp +++ b/src/gui2/Label.cpp @@ -25,20 +25,23 @@ using namespace gui; Label::Label(const AttributeSet& attrs) : Widget(attrs), - _text(attrs.get("text")) + text_(attrs.get("text")), + fontName(attrs.get("font", "")) { - _text = "foo"; + text_ = "foo"; } void Label::render(RenderContext& rc) const { - rc.print(rc.theme().normal_font(), x(), y(), _text); + rc.print(rc.theme().font(fontName), x(), y(), text_); } void Label::adjustForTheme(Theme& theme) { - width(theme.normal_font()->text_width(_text)); - height(theme.normal_font()->height()); + IFontPtr font = theme.font(fontName); + + width(font->text_width(text_)); + height(font->height()); } void Label::format(const char* fmt, ...) diff --git a/src/gui2/Theme.cpp b/src/gui2/Theme.cpp index 7564c45..3cb209d 100644 --- a/src/gui2/Theme.cpp +++ b/src/gui2/Theme.cpp @@ -16,15 +16,20 @@ // #include "gui2/Theme.hpp" +#include "ILogger.hpp" #include +#include using namespace gui; Theme::Theme() { - normal_font_ = ft::load_font("data/fonts/Vera.ttf", + normal_font_ = ft::loadFont("data/fonts/Vera.ttf", 18, ft::FONT_NORMAL); + + dropShadowFont = ft::loadFont("data/fonts/Vera.ttf", + 20, ft::FONT_NORMAL, true); } Colour Theme::background() const @@ -36,3 +41,15 @@ Colour Theme::border() const { return makeColour(0.0f, 0.0f, 1.0f, 1.0f); } + +IFontPtr Theme::font(const string& fontName) const +{ + // TODO: Layouts should have a method for specifying fonts! + if (fontName == "") + return normal_font_; + else if (fontName == "status-font") + return dropShadowFont; + else + throw runtime_error("Unknown font: " + fontName); +} + -- 2.39.2