refactor(mac): adapt `LITE_USE_SDL_RENDERER` scaling to new windowing system

This commit is contained in:
Guldoman 2024-06-23 01:31:04 +02:00 committed by Takase
parent adb64e6784
commit 9dd4b81b39
3 changed files with 27 additions and 6 deletions

View File

@ -3,6 +3,9 @@
#include "api.h" #include "api.h"
#include "../renderer.h" #include "../renderer.h"
#include "../rencache.h" #include "../rencache.h"
#ifdef LITE_USE_SDL_RENDERER
#include "../renwindow.h"
#endif
#include "lua.h" #include "lua.h"
RenWindow *active_window_renderer = NULL; RenWindow *active_window_renderer = NULL;
@ -116,7 +119,7 @@ static bool font_retrieve(lua_State* L, RenFont** fonts, int idx) {
} }
} }
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
update_font_scale(window_renderer, fonts); update_font_scale(active_window_renderer, fonts);
#endif #endif
return is_table; return is_table;
} }
@ -226,7 +229,13 @@ static int f_font_get_size(lua_State *L) {
static int f_font_set_size(lua_State *L) { static int f_font_set_size(lua_State *L) {
RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1);
float size = luaL_checknumber(L, 2); float size = luaL_checknumber(L, 2);
ren_font_group_set_size(fonts, size); int scale = 1;
#ifdef LITE_USE_SDL_RENDERER
if (active_window_renderer != NULL) {
scale = renwin_get_surface(active_window_renderer).scale;
}
#endif
ren_font_group_set_size(fonts, size, scale);
return 0; return 0;
} }

View File

@ -68,10 +68,11 @@ typedef struct RenFont {
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
void update_font_scale(RenWindow *window_renderer, RenFont **fonts) { void update_font_scale(RenWindow *window_renderer, RenFont **fonts) {
if (window_renderer == NULL) return;
const int surface_scale = renwin_get_surface(window_renderer).scale; const int surface_scale = renwin_get_surface(window_renderer).scale;
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
if (fonts[i]->scale != surface_scale) { if (fonts[i]->scale != surface_scale) {
ren_font_group_set_size(window_renderer, fonts, fonts[0]->size); ren_font_group_set_size(fonts, fonts[0]->size, surface_scale);
return; return;
} }
} }
@ -274,6 +275,9 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial
strcpy(font->path, path); strcpy(font->path, path);
font->face = face; font->face = face;
font->size = size; font->size = size;
#ifdef LITE_USE_SDL_RENDERER
font->scale = 1;
#endif
font->height = (short)((face->height / (float)face->units_per_EM) * font->size); font->height = (short)((face->height / (float)face->units_per_EM) * font->size);
font->baseline = (short)((face->ascender / (float)face->units_per_EM) * font->size); font->baseline = (short)((face->ascender / (float)face->units_per_EM) * font->size);
font->antialiasing = antialiasing; font->antialiasing = antialiasing;
@ -344,11 +348,11 @@ float ren_font_group_get_size(RenFont **fonts) {
return fonts[0]->size; return fonts[0]->size;
} }
void ren_font_group_set_size(RenFont **fonts, float size) { void ren_font_group_set_size(RenFont **fonts, float size, int surface_scale) {
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
font_clear_glyph_cache(fonts[i]); font_clear_glyph_cache(fonts[i]);
FT_Face face = fonts[i]->face; FT_Face face = fonts[i]->face;
FT_Set_Pixel_Sizes(face, 0, (int)(size)); FT_Set_Pixel_Sizes(face, 0, (int)(size*surface_scale));
fonts[i]->size = size; fonts[i]->size = size;
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
fonts[i]->scale = surface_scale; fonts[i]->scale = surface_scale;
@ -385,7 +389,11 @@ double ren_font_group_get_width(RenFont **fonts, const char *text, size_t len, i
if (!set_x_offset) { if (!set_x_offset) {
*x_offset = 0; *x_offset = 0;
} }
#ifdef LITE_USE_SDL_RENDERER
return width / fonts[0]->scale;
#else
return width; return width;
#endif
} }
double ren_draw_text(RenSurface *rs, RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) { double ren_draw_text(RenSurface *rs, RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) {
@ -597,6 +605,10 @@ void ren_get_size(RenWindow *window_renderer, int *x, int *y) {
RenSurface rs = renwin_get_surface(window_renderer); RenSurface rs = renwin_get_surface(window_renderer);
*x = rs.surface->w; *x = rs.surface->w;
*y = rs.surface->h; *y = rs.surface->h;
#ifdef LITE_USE_SDL_RENDERER
*x /= rs.scale;
*y /= rs.scale;
#endif
} }
size_t ren_get_window_list(RenWindow ***window_list_dest) { size_t ren_get_window_list(RenWindow ***window_list_dest) {

View File

@ -31,7 +31,7 @@ void ren_font_free(RenFont *font);
int ren_font_group_get_tab_size(RenFont **font); int ren_font_group_get_tab_size(RenFont **font);
int ren_font_group_get_height(RenFont **font); int ren_font_group_get_height(RenFont **font);
float ren_font_group_get_size(RenFont **font); float ren_font_group_get_size(RenFont **font);
void ren_font_group_set_size(RenFont **font, float size); void ren_font_group_set_size(RenFont **font, float size, int surface_scale);
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
void update_font_scale(RenWindow *window_renderer, RenFont **fonts); void update_font_scale(RenWindow *window_renderer, RenFont **fonts);
#endif #endif