From 232dc6c17c01d026a7dbc60af542dad8cc1cbe53 Mon Sep 17 00:00:00 2001 From: kub Date: Mon, 9 Sep 2024 23:07:49 +0200 Subject: [PATCH] platform, add integer scaling for sdl windows --- platform/common/menu_pico.c | 26 ++++++++-------- platform/common/plat_sdl.c | 62 +++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/platform/common/menu_pico.c b/platform/common/menu_pico.c index 415e1b5f7..23f0bba7d 100644 --- a/platform/common/menu_pico.c +++ b/platform/common/menu_pico.c @@ -104,24 +104,24 @@ static void make_bg(int no_scale, int from_screen) if (!no_scale && g_menuscreen_w / w >= 2 && g_menuscreen_h / h >= 2) { int xf = g_menuscreen_w / w, yf = g_menuscreen_h / h; - int f = no_scale ? 1 : xf < yf ? xf : yf; - int xs = f * w, ys = f * h; - unsigned short t; - int i, j, k, l; + int f = no_scale ? 1 : xf < yf ? xf : yf, xs = f * w, ys = f * h; + int x = (g_menuscreen_w - xs)/2, y = (g_menuscreen_h - ys)/2; + uint16_t *p = (uint16_t *)g_menubg_ptr; + uint16_t *q = (uint16_t *)src; - x = (g_menuscreen_w - xs)/2, y = (g_menuscreen_h - ys)/2; - dst = (short *)g_menubg_ptr + y * g_menuscreen_w + x; + int i, j, k, l; + p += y * g_menuscreen_pp + x; for (i = 0; i < h; i++) { - for (j = 0; j < w; j++, src++) { - t = (PXMASKH(*src,1)>>1) - (PXMASKH(*src,3)>>3); + for (j = 0; j < w; j++, q++) { + uint16_t t = (PXMASKH(*q,1)>>1) - (PXMASKH(*q,3)>>3); for (l = 0; l < f; l++) - *dst++ = t; + *p++ = t; } - src += pp - w; - dst += g_menuscreen_w - xs; + p += g_menuscreen_pp - xs; + q += pp - w; for (k = 1; k < f; k++) { - memcpy(dst, dst-g_menuscreen_w, g_menuscreen_w*2); - dst += g_menuscreen_w; + memcpy(p, p-g_menuscreen_pp, g_menuscreen_w*2); + p += g_menuscreen_pp; } } return; diff --git a/platform/common/plat_sdl.c b/platform/common/plat_sdl.c index d1e833f35..b70730970 100644 --- a/platform/common/plat_sdl.c +++ b/platform/common/plat_sdl.c @@ -174,6 +174,29 @@ void rgb565_to_uyvy(void *d, const void *s, int w, int h, int pitch, int dpitch, } } +void copy_intscale(void *dst, int w, int h, int pp, void *src, int sw, int sh, int spp) +{ + int xf = w / sw, yf = h / sh; + int f = xf < yf ? xf : yf, xs = f * sw, ys = f * sh; + int x = (w - xs)/2, y = (h - ys)/2; + uint16_t *p = (uint16_t *)dst; + uint16_t *q = (uint16_t *)src; + + int i, j, k, l; + p += y * pp + x; + for (i = 0; i < sh; i++) { + for (j = 0; j < sw; j++, q++) + for (l = 0; l < f; l++) + *p++ = *q; + p += pp - xs; + q += spp - sw; + for (k = 1; k < f; k++) { + memcpy(p, p-pp, w*2); + p += pp; + } + } +} + static int clear_buf_cnt, clear_stat_cnt; static void resize_buffers(void) @@ -190,17 +213,12 @@ void plat_video_set_size(int w, int h) { if (area.w != w || area.h != h) { area = (struct area) { w, h }; - if (plat_sdl_change_video_mode(w, h, 0) < 0) { - // failed, revert to original resolution - area = (struct area) { g_screen_width,g_screen_height }; - plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0); - } - if (!plat_sdl_overlay && !plat_sdl_gl_active) { - g_screen_width = plat_sdl_screen->w; - g_screen_height = plat_sdl_screen->h; - g_screen_ppitch = plat_sdl_screen->pitch/2; - g_screen_ptr = plat_sdl_screen->pixels; - } else { + if (plat_sdl_overlay || plat_sdl_gl_active) { + if (plat_sdl_change_video_mode(w, h, 0) < 0) { + // failed, revert to original resolution + area = (struct area) { g_screen_width,g_screen_height }; + plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0); + } g_screen_width = w; g_screen_height = h; g_screen_ppitch = w; @@ -236,15 +254,13 @@ void plat_video_flip(void) gl_flip(shadow_fb, g_screen_ppitch, g_screen_height); } else { - if (SDL_MUSTLOCK(plat_sdl_screen)) { + copy_intscale(plat_sdl_screen->pixels, plat_sdl_screen->w, plat_sdl_screen->h, plat_sdl_screen->pitch/2, shadow_fb, 320, 240, 320); + if (SDL_MUSTLOCK(plat_sdl_screen)) SDL_UnlockSurface(plat_sdl_screen); - SDL_Flip(plat_sdl_screen); + SDL_Flip(plat_sdl_screen); + if (SDL_MUSTLOCK(plat_sdl_screen)) SDL_LockSurface(plat_sdl_screen); - } else - SDL_Flip(plat_sdl_screen); - g_screen_ppitch = plat_sdl_screen->pitch/2; - g_screen_ptr = plat_sdl_screen->pixels; - plat_video_set_buffer(g_screen_ptr); + if (clear_buf_cnt) { memset(g_screen_ptr, 0, plat_sdl_screen->pitch*plat_sdl_screen->h); clear_buf_cnt--; @@ -356,17 +372,17 @@ void plat_video_loop_prepare(void) } g_screen_ppitch = g_screen_width; g_screen_ptr = shadow_fb; + plat_video_set_size(g_screen_width, g_screen_height); } else { - g_screen_width = plat_sdl_screen->w; - g_screen_height = plat_sdl_screen->h; - g_screen_ppitch = plat_sdl_screen->pitch/2; + g_screen_width = 320; + g_screen_height = 240; + g_screen_ppitch = 320; if (SDL_MUSTLOCK(plat_sdl_screen)) SDL_LockSurface(plat_sdl_screen); - g_screen_ptr = plat_sdl_screen->pixels; + g_screen_ptr = shadow_fb; } - plat_video_set_size(g_screen_width, g_screen_height); plat_video_set_buffer(g_screen_ptr); }