Merge pull request #427 from kbolino/fix-sdl-hidpi-rendering

Fix high-DPI scaling in sdl_renderer
This commit is contained in:
Rob Loach 2022-04-20 13:51:59 -04:00 committed by GitHub
commit a51d9eec96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 12 deletions

View File

@ -76,6 +76,7 @@ main(int argc, char *argv[])
SDL_Renderer *renderer;
int running = 1;
int flags = 0;
float font_scale = 1;
/* GUI */
struct nk_context *ctx;
@ -112,22 +113,46 @@ main(int argc, char *argv[])
exit(-1);
}
/* scale the renderer output for High-DPI displays */
{
int render_w, render_h;
int window_w, window_h;
float scale_x, scale_y;
SDL_GetRendererOutputSize(renderer, &render_w, &render_h);
SDL_GetWindowSize(win, &window_w, &window_h);
scale_x = (float)(render_w) / (float)(window_w);
scale_y = (float)(render_h) / (float)(window_h);
SDL_RenderSetScale(renderer, scale_x, scale_y);
font_scale = scale_y;
}
/* GUI */
ctx = nk_sdl_init(win, renderer);
/* Load Fonts: if none of these are loaded a default font will be used */
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
{struct nk_font_atlas *atlas;
nk_sdl_font_stash_begin(&atlas);
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
nk_sdl_font_stash_end();
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
/*nk_style_set_font(ctx, &roboto->handle)*/;}
{
struct nk_font_atlas *atlas;
struct nk_font_config config = nk_font_config(0);
struct nk_font *font;
/* set up the font atlas and add desired font; note that font sizes are
* multiplied by font_scale to produce better results at higher DPIs */
nk_sdl_font_stash_begin(&atlas);
font = nk_font_atlas_add_default(atlas, 13 * font_scale, &config);
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14 * font_scale, &config);*/
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16 * font_scale, &config);*/
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13 * font_scale, &config);*/
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12 * font_scale, &config);*/
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10 * font_scale, &config);*/
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13 * font_scale, &config);*/
nk_sdl_font_stash_end();
/* this hack makes the font appear to be scaled down to the desired
* size and is only necessary when font_scale > 1 */
font->handle.height /= font_scale;
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
nk_style_set_font(ctx, &font->handle);
}
#ifdef INCLUDE_STYLE
/*set_style(ctx, THEME_WHITE);*/
@ -212,4 +237,3 @@ cleanup:
SDL_Quit();
return 0;
}