sdl, fix a crash when resizing below 320x240
authorkub <derkub@gmail.com>
Thu, 20 Feb 2025 21:18:44 +0000 (22:18 +0100)
committerkub <derkub@gmail.com>
Thu, 20 Feb 2025 21:18:44 +0000 (22:18 +0100)
platform/common/plat_sdl.c
platform/linux/menu.c

index 4e24d7e..a2f8d01 100644 (file)
@@ -212,9 +212,9 @@ static void resize_buffers(void)
 
 void plat_video_set_size(int w, int h)
 {
-       if ((plat_sdl_overlay || plat_sdl_gl_active) &&
-           (w * g_menuscreen_h != h * g_menuscreen_w)) {
-               // scale to the window, but mind aspect ratio (scaled to 4:3)
+       if ((plat_sdl_overlay || plat_sdl_gl_active) && w <= 320 && h <= 240) {
+               // scale to the window, but mind aspect ratio (scaled to 4:3):
+               // w *= win_aspect / 4:3_aspect or h *= 4:3_aspect / win_aspect
                if (g_menuscreen_w * 3/4 >= g_menuscreen_h)
                        w = (w * 3 * g_menuscreen_w/g_menuscreen_h)/4 & ~1;
                else
@@ -316,7 +316,7 @@ void plat_video_flip(void)
                        SDL_LockSurface(plat_sdl_screen);
 
                if (clear_buf_cnt) {
-                       memset(g_screen_ptr, 0, plat_sdl_screen->pitch*plat_sdl_screen->h);
+                       memset(plat_sdl_screen->pixels, 0, plat_sdl_screen->pitch*plat_sdl_screen->h);
                        clear_buf_cnt--;
                }
        }
@@ -347,7 +347,11 @@ void plat_video_clear_status(void)
 
 void plat_video_clear_buffers(void)
 {
-       memset(shadow_fb, 0, g_menuscreen_w * g_menuscreen_h * 2);
+       int count = g_menuscreen_w * g_menuscreen_h;
+       if (count < area.w * area.h) count = area.w * area.h;
+       memset(shadow_fb, 0, count * 2);
+       if (plat_sdl_overlay)
+               plat_sdl_overlay_clear();
        memset(plat_sdl_screen->pixels, 0, plat_sdl_screen->pitch*plat_sdl_screen->h);
        clear_buf_cnt = 3; // do it thrice in case of triple buffering
 }
@@ -371,6 +375,8 @@ void plat_video_menu_update(void)
                g_menuscreen_pp = g_menuscreen_w;
        else
                g_menuscreen_pp = plat_sdl_screen->pitch / 2;
+
+       resize_buffers();
 }
 
 void plat_video_menu_enter(int is_rom_loaded)
@@ -425,30 +431,23 @@ void plat_video_menu_leave(void)
 
 void plat_video_loop_prepare(void)
 {
+       int w = g_menuscreen_w, h = g_menuscreen_h;
+
        // take over any new vout settings
        area = (struct area) { 0, 0 };
        plat_sdl_change_video_mode(0, 0, 0);
        resize_buffers();
 
        // switch over to scaled output if available, but keep the aspect ratio
-       if (plat_sdl_overlay || plat_sdl_gl_active) {
-               if (g_menuscreen_w * 240 >= g_menuscreen_h * 320) {
-                       g_screen_width = (240 * g_menuscreen_w/g_menuscreen_h) & ~1;
-                       g_screen_height= 240;
-               } else {
-                       g_screen_width = 320;
-                       g_screen_height= (320 * g_menuscreen_h/g_menuscreen_w) & ~1;
-               }
-               plat_video_set_size(g_screen_width, g_screen_height);
-       }
-       else {
-               g_screen_width = g_menuscreen_w;
-               g_screen_height = g_menuscreen_h;
-               plat_video_set_size(g_menuscreen_w, g_menuscreen_h);
+       if (plat_sdl_overlay || plat_sdl_gl_active)
+               w = 320, h = 240;
 
+       g_screen_width = w, g_screen_height = h;
+       plat_video_set_size(w, h);
+
+       if (!(plat_sdl_overlay || plat_sdl_gl_active))
                if (SDL_MUSTLOCK(plat_sdl_screen))
                        SDL_LockSurface(plat_sdl_screen);
-       }
 
        plat_video_set_buffer(g_screen_ptr);
 }
@@ -484,7 +483,6 @@ static void plat_sdl_resize(int w, int h)
 #endif
        }
 
-       resize_buffers();
        rendstatus_old = -1;
 }
 
index 11c72db..a039cc8 100644 (file)
@@ -25,6 +25,7 @@ static int mh_windowsize(int id, int keys)
                if (keys & PBTN_RIGHT) scale++;
                if (keys & PBTN_LEFT ) scale--;
                if (scale <= 0) scale = 1;
+               if (scale > 20) scale = 20;
                g_menuscreen_w = scale*320;
                g_menuscreen_h = scale*240;
                return 0;