From fd50426ddae49de2b61f35b8ac67bc41a5f3626b Mon Sep 17 00:00:00 2001 From: Quantum Date: Sun, 21 Feb 2021 01:25:54 -0500 Subject: [PATCH] [client] egl: scale UI elements on high DPI displays This is done by actually rendering the text at high DPI. --- client/renderers/EGL/alert.c | 7 ++++ client/renderers/EGL/alert.h | 1 + client/renderers/EGL/egl.c | 73 ++++++++++++++++++++++++++++++------ client/renderers/EGL/fps.c | 5 +++ client/renderers/EGL/fps.h | 1 + client/renderers/EGL/help.c | 5 +++ client/renderers/EGL/help.h | 1 + 7 files changed, 82 insertions(+), 11 deletions(-) diff --git a/client/renderers/EGL/alert.c b/client/renderers/EGL/alert.c index d3afa308..5558ab90 100644 --- a/client/renderers/EGL/alert.c +++ b/client/renderers/EGL/alert.c @@ -164,6 +164,13 @@ void egl_alert_set_text (EGL_Alert * alert, const char * str) LG_UNLOCK(alert->lock); } +void egl_alert_set_font(EGL_Alert * alert, LG_Font * fontObj) +{ + LG_LOCK(alert->lock); + alert->fontObj = fontObj; + LG_UNLOCK(alert->lock); +} + void egl_alert_render(EGL_Alert * alert, const float scaleX, const float scaleY) { if (alert->update) diff --git a/client/renderers/EGL/alert.h b/client/renderers/EGL/alert.h index 9c54f667..dc61902d 100644 --- a/client/renderers/EGL/alert.h +++ b/client/renderers/EGL/alert.h @@ -30,4 +30,5 @@ void egl_alert_free(EGL_Alert ** alert); void egl_alert_set_color(EGL_Alert * alert, const uint32_t color); void egl_alert_set_text (EGL_Alert * alert, const char * str); +void egl_alert_set_font (EGL_Alert * alert, LG_Font * fontObj); void egl_alert_render (EGL_Alert * alert, const float scaleX, const float scaleY); \ No newline at end of file diff --git a/client/renderers/EGL/egl.c b/client/renderers/EGL/egl.c index 895f0209..e9efa346 100644 --- a/client/renderers/EGL/egl.c +++ b/client/renderers/EGL/egl.c @@ -30,6 +30,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include +#include #include #include "app.h" @@ -83,6 +84,7 @@ struct Inst bool closeFlag; int width, height; + float uiScale; LG_RendererRect destRect; LG_RendererRotate rotate; //client side rotation @@ -101,7 +103,9 @@ struct Inst const LG_Font * font; LG_FontObj fontObj; + unsigned fontSize; LG_FontObj helpFontObj; + unsigned helpFontSize; }; static struct Option egl_options[] = @@ -163,6 +167,55 @@ void egl_setup(void) option_register(egl_options); } +static bool egl_update_font(struct Inst * this) +{ + unsigned size = round(16.0f * this->uiScale); + if (size == this->fontSize) + return true; + + LG_FontObj fontObj; + if (!this->font->create(&fontObj, NULL, size)) + { + DEBUG_ERROR("Failed to create a font instance"); + return false; + } + + if (this->alert) + egl_alert_set_font(this->alert, fontObj); + + if (this->fps) + egl_fps_set_font(this->fps, fontObj); + + if (this->fontObj) + this->font->destroy(this->fontObj); + this->fontObj = fontObj; + + return true; +} + +static bool egl_update_help_font(struct Inst * this) +{ + unsigned size = round(14.0f * this->uiScale); + if (size == this->helpFontSize) + return true; + + LG_FontObj fontObj; + if (!this->font->create(&fontObj, NULL, size)) + { + DEBUG_ERROR("Failed to create a font instance"); + return false; + } + + if (this->help) + egl_help_set_font(this->help, fontObj); + + if (this->helpFontObj) + this->font->destroy(this->helpFontObj); + this->helpFontObj = fontObj; + + return true; +} + bool egl_create(void ** opaque, const LG_RendererParams params, bool * needsOpenGL) { // check if EGL is even available @@ -191,19 +244,14 @@ bool egl_create(void ** opaque, const LG_RendererParams params, bool * needsOpen this->scaleY = 1.0f; this->screenScaleX = 1.0f; this->screenScaleY = 1.0f; + this->uiScale = 1.0; this->font = LG_Fonts[0]; - if (!this->font->create(&this->fontObj, NULL, 16)) - { - DEBUG_ERROR("Failed to create a font instance"); + if (!egl_update_font(this)) return false; - } - if (!this->font->create(&this->helpFontObj, NULL, 14)) - { - DEBUG_ERROR("Failed to create a font instance"); + if (!egl_update_help_font(this)) return false; - } *needsOpenGL = false; return true; @@ -384,9 +432,10 @@ void egl_on_resize(void * opaque, const int width, const int height, const doubl { struct Inst * this = (struct Inst *)opaque; - this->width = width * scale; - this->height = height * scale; - this->rotate = rotate; + this->width = width * scale; + this->height = height * scale; + this->uiScale = (float) scale; + this->rotate = rotate; this->destRect.x = destRect.x * scale; this->destRect.y = destRect.y * scale; @@ -413,6 +462,8 @@ void egl_on_resize(void * opaque, const int width, const int height, const doubl this->screenScaleY = 1.0f / this->height; egl_calc_mouse_state(this); + egl_update_font(this); + egl_update_help_font(this); } bool egl_on_mouse_shape(void * opaque, const LG_RendererCursor cursor, diff --git a/client/renderers/EGL/fps.c b/client/renderers/EGL/fps.c index a5c87d67..e74a06a8 100644 --- a/client/renderers/EGL/fps.c +++ b/client/renderers/EGL/fps.c @@ -138,6 +138,11 @@ void egl_fps_set_display(EGL_FPS * fps, bool display) fps->display = display; } +void egl_fps_set_font(EGL_FPS * fps, LG_Font * fontObj) +{ + fps->fontObj = fontObj; +} + void egl_fps_update(EGL_FPS * fps, const float avgFPS, const float renderFPS) { if (!fps->display) diff --git a/client/renderers/EGL/fps.h b/client/renderers/EGL/fps.h index 8ab2781f..28192aca 100644 --- a/client/renderers/EGL/fps.h +++ b/client/renderers/EGL/fps.h @@ -29,5 +29,6 @@ bool egl_fps_init(EGL_FPS ** fps, const LG_Font * font, LG_FontObj fontObj); void egl_fps_free(EGL_FPS ** fps); void egl_fps_set_display(EGL_FPS * fps, bool display); +void egl_fps_set_font (EGL_FPS * fps, LG_Font * fontObj); void egl_fps_update(EGL_FPS * fps, const float avgUPS, const float avgFPS); void egl_fps_render(EGL_FPS * fps, const float scaleX, const float scaleY); \ No newline at end of file diff --git a/client/renderers/EGL/help.c b/client/renderers/EGL/help.c index 764f57d2..fcc8d32e 100644 --- a/client/renderers/EGL/help.c +++ b/client/renderers/EGL/help.c @@ -154,6 +154,11 @@ void egl_help_set_text(EGL_Help * help, const char * help_text) } } +void egl_help_set_font(EGL_Help * help, LG_FontObj fontObj) +{ + help->fontObj = fontObj; +} + void egl_help_render(EGL_Help * help, const float scaleX, const float scaleY) { LG_FontBitmap * bmp = atomic_exchange(&help->bmp, NULL); diff --git a/client/renderers/EGL/help.h b/client/renderers/EGL/help.h index b975c4e9..69b6c493 100644 --- a/client/renderers/EGL/help.h +++ b/client/renderers/EGL/help.h @@ -29,4 +29,5 @@ bool egl_help_init(EGL_Help ** help, const LG_Font * font, LG_FontObj fontObj); void egl_help_free(EGL_Help ** help); void egl_help_set_text(EGL_Help * help, const char * help_text); +void egl_help_set_font(EGL_Help * help, LG_FontObj fontObj); void egl_help_render(EGL_Help * help, const float scaleX, const float scaleY);