[Fix] Font scaling.

This commit is contained in:
Allanis 2018-08-21 19:15:47 +01:00
parent 406646f5f0
commit c2ec30a048
2 changed files with 17 additions and 16 deletions

View File

@ -11,7 +11,7 @@ int Screen::realWidth;
int Screen::realHeight; int Screen::realHeight;
float Screen::invRealWidth; float Screen::invRealWidth;
float Screen::invRealHeight; float Screen::invRealHeight;
float Screen::fontScale; float Screen::fontScale[2];
std::list<Widget*> Screen::kbshortcut_widgets; std::list<Widget*> Screen::kbshortcut_widgets;
std::vector<Screen::LabelPos> Screen::labelPositions; std::vector<Screen::LabelPos> Screen::labelPositions;
Gui::Fixed* Screen::baseContainer; 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::invRealWidth = 1.0f/real_width;
Screen::invRealHeight = 1.0f/real_height; Screen::invRealHeight = 1.0f/real_height;
Screen::init = true; 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, * Why? Because although our font textures get bigger with screen resolution,
* out GUI Ortho projections is still 800x600 so vertex * out GUI Ortho projections is still 800x600 so vertex
* coords must be scaled. * 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::fontScale = 1.0/fontscale;
Screen::baseContainer = new Gui::Fixed(Screen::width, Screen::height); Screen::baseContainer = new Gui::Fixed(Screen::width, Screen::height);
Screen::baseContainer->SetPosition(0,0); Screen::baseContainer->SetPosition(0,0);
@ -131,38 +132,38 @@ void Screen::OnKeyDown(const SDL_keysym* sym) {
} }
float Screen::GetFontHeight(void) { float Screen::GetFontHeight(void) {
return font->GetHeight()*fontScale; return font->GetHeight()*fontScale[1];
} }
void Screen::MeasureString(const std::string& s, float& w, float& h) { void Screen::MeasureString(const std::string& s, float& w, float& h) {
font->MeasureString(s.c_str(), w, h); font->MeasureString(s.c_str(), w, h);
w *= fontScale; w *= fontScale[0];
h *= fontScale; h *= fontScale[1];
} }
void Screen::MeasureLayout(const std::string& s, const float width, float outSize[2]) { void Screen::MeasureLayout(const std::string& s, const float width, float outSize[2]) {
font->MeasureLayout(s.c_str(), width, outSize); font->MeasureLayout(s.c_str(), width / fontScale[0], outSize);
outSize[0] *= Screen::fontScale; outSize[0] *= fontScale[0];
outSize[1] *= Screen::fontScale; outSize[1] *= fontScale[1];
} }
void Screen::LayoutString(const std::string& s, const float width) { void Screen::LayoutString(const std::string& s, const float width) {
glPushMatrix(); glPushMatrix();
glScalef(Screen::fontScale, Screen::fontScale, 1); glScalef(Screen::fontScale[0], Screen::fontScale[1], 1);
font->LayoutString(s.c_str(), width); font->LayoutString(s.c_str(), width / fontScale[0]);
glPopMatrix(); glPopMatrix();
} }
void Screen::RenderString(const std::string& s) { void Screen::RenderString(const std::string& s) {
glPushMatrix(); glPushMatrix();
glScalef(Screen::fontScale, Screen::fontScale, 1); glScalef(Screen::fontScale[0], Screen::fontScale[0], 1);
font->RenderString(s.c_str()); font->RenderString(s.c_str());
glPopMatrix(); glPopMatrix();
} }
void Screen::RenderMarkup(const std::string& s) { void Screen::RenderMarkup(const std::string& s) {
glPushMatrix(); glPushMatrix();
glScalef(Screen::fontScale, Screen::fontScale, 1); glScalef(Screen::fontScale[0], Screen::fontScale[1], 1);
font->RenderMarkup(s.c_str()); font->RenderMarkup(s.c_str());
glPopMatrix(); glPopMatrix();
} }
@ -180,7 +181,7 @@ void Screen::RenderLabel(const std::string& s, float x, float y) {
labelPositions.push_back(LabelPos(x, y)); labelPositions.push_back(LabelPos(x, y));
glPushMatrix(); glPushMatrix();
glTranslatef(x, y, 0); 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); glTranslatef(0.5*font->GetWidth(), -0.5*font->GetHeight(), 0);
font->RenderString(s.c_str()); font->RenderString(s.c_str());
glPopMatrix(); glPopMatrix();
@ -196,7 +197,7 @@ void Screen::PutClickableLabel(const std::string& s, float x, float y,
labelPositions.push_back(p); labelPositions.push_back(p);
glPushMatrix(); glPushMatrix();
glTranslatef(x, y, 0); 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); glTranslatef(0.5*font->GetWidth(), -0.5*font->GetHeight(), 0);
font->RenderString(s.c_str()); font->RenderString(s.c_str());
glPopMatrix(); glPopMatrix();

View File

@ -51,7 +51,7 @@ namespace Gui {
static std::list<Widget*> kbshortcut_widgets; static std::list<Widget*> kbshortcut_widgets;
static std::list<Widget*> mouseHoveredWidgets; static std::list<Widget*> mouseHoveredWidgets;
static TextureFontFace* font; static TextureFontFace* font;
static float fontScale; static float fontScale[2];
static Gui::Fixed* baseContainer; static Gui::Fixed* baseContainer;
}; };
} }