From c2ec30a048a2c953e33ec5e06b990e5fab274c94 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 21 Aug 2018 19:15:47 +0100 Subject: [PATCH] [Fix] Font scaling. --- src/gui_screen.cpp | 31 ++++++++++++++++--------------- src/gui_screen.h | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/gui_screen.cpp b/src/gui_screen.cpp index 1e6f4d8..1bec217 100644 --- a/src/gui_screen.cpp +++ b/src/gui_screen.cpp @@ -11,7 +11,7 @@ int Screen::realWidth; int Screen::realHeight; float Screen::invRealWidth; float Screen::invRealHeight; -float Screen::fontScale; +float Screen::fontScale[2]; std::list Screen::kbshortcut_widgets; std::vector Screen::labelPositions; Gui::Fixed* Screen::baseContainer; @@ -24,13 +24,14 @@ void Screen::Init(int real_width, int real_height, int ui_width, int ui_height) Screen::invRealWidth = 1.0f/real_width; Screen::invRealHeight = 1.0f/real_height; Screen::init = true; - const float fontscale = real_height / (float)ui_height; - Screen::font = new TextureFontFace("guifont.ttf", 15*fontscale, 15*fontscale); /* * Why? Because although our font textures get bigger with screen resolution, * out GUI Ortho projections is still 800x600 so vertex * coords must be scaled. */ + Screen::fontScale[0] = ui_width / (float)real_width; + Screen::fontScale[1] = ui_height / (float)real_height; + Screen::font = new TextureFontFace("guifont.ttf", 15/fontScale[0], 15/fontScale[1]); Screen::fontScale = 1.0/fontscale; Screen::baseContainer = new Gui::Fixed(Screen::width, Screen::height); Screen::baseContainer->SetPosition(0,0); @@ -131,38 +132,38 @@ void Screen::OnKeyDown(const SDL_keysym* sym) { } float Screen::GetFontHeight(void) { - return font->GetHeight()*fontScale; + return font->GetHeight()*fontScale[1]; } void Screen::MeasureString(const std::string& s, float& w, float& h) { font->MeasureString(s.c_str(), w, h); - w *= fontScale; - h *= fontScale; + w *= fontScale[0]; + h *= fontScale[1]; } void Screen::MeasureLayout(const std::string& s, const float width, float outSize[2]) { - font->MeasureLayout(s.c_str(), width, outSize); - outSize[0] *= Screen::fontScale; - outSize[1] *= Screen::fontScale; + font->MeasureLayout(s.c_str(), width / fontScale[0], outSize); + outSize[0] *= fontScale[0]; + outSize[1] *= fontScale[1]; } void Screen::LayoutString(const std::string& s, const float width) { glPushMatrix(); - glScalef(Screen::fontScale, Screen::fontScale, 1); - font->LayoutString(s.c_str(), width); + glScalef(Screen::fontScale[0], Screen::fontScale[1], 1); + font->LayoutString(s.c_str(), width / fontScale[0]); glPopMatrix(); } void Screen::RenderString(const std::string& s) { glPushMatrix(); - glScalef(Screen::fontScale, Screen::fontScale, 1); + glScalef(Screen::fontScale[0], Screen::fontScale[0], 1); font->RenderString(s.c_str()); glPopMatrix(); } void Screen::RenderMarkup(const std::string& s) { glPushMatrix(); - glScalef(Screen::fontScale, Screen::fontScale, 1); + glScalef(Screen::fontScale[0], Screen::fontScale[1], 1); font->RenderMarkup(s.c_str()); glPopMatrix(); } @@ -180,7 +181,7 @@ void Screen::RenderLabel(const std::string& s, float x, float y) { labelPositions.push_back(LabelPos(x, y)); glPushMatrix(); glTranslatef(x, y, 0); - glScalef(Screen::fontScale, Screen::fontScale, 1); + glScalef(Screen::fontScale[0], Screen::fontScale[1], 1); glTranslatef(0.5*font->GetWidth(), -0.5*font->GetHeight(), 0); font->RenderString(s.c_str()); glPopMatrix(); @@ -196,7 +197,7 @@ void Screen::PutClickableLabel(const std::string& s, float x, float y, labelPositions.push_back(p); glPushMatrix(); glTranslatef(x, y, 0); - glScalef(Screen::fontScale, Screen::fontScale, 1); + glScalef(Screen::fontScale[0], Screen::fontScale[1], 1); glTranslatef(0.5*font->GetWidth(), -0.5*font->GetHeight(), 0); font->RenderString(s.c_str()); glPopMatrix(); diff --git a/src/gui_screen.h b/src/gui_screen.h index 59beed0..3a8a26a 100644 --- a/src/gui_screen.h +++ b/src/gui_screen.h @@ -51,7 +51,7 @@ namespace Gui { static std::list kbshortcut_widgets; static std::list mouseHoveredWidgets; static TextureFontFace* font; - static float fontScale; + static float fontScale[2]; static Gui::Fixed* baseContainer; }; }