[client] egl: scale UI elements on high DPI displays

This is done by actually rendering the text at high DPI.
This commit is contained in:
Quantum 2021-02-21 01:25:54 -05:00 committed by Geoffrey McRae
parent 89bdaec95a
commit fd50426dda
7 changed files with 82 additions and 11 deletions

View File

@ -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)

View File

@ -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);

View File

@ -30,6 +30,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <EGL/egl.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#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;
@ -386,6 +434,7 @@ void egl_on_resize(void * opaque, const int width, const int height, const doubl
this->width = width * scale;
this->height = height * scale;
this->uiScale = (float) scale;
this->rotate = rotate;
this->destRect.x = destRect.x * 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,

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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);