From 719ed9a67b206ce1978438c0c39a1456bf3766cc Mon Sep 17 00:00:00 2001 From: NyakoFox Date: Mon, 28 Apr 2025 01:15:31 -0300 Subject: [PATCH] Fix uncommon division-by-zero Every now and then, the game crashes for me because of a division by zero, due to the rect returned by `Graphics::get_stretch_info`. I don't fully know how the width and height get set to 0, but this should protect against it. As I always use integer scaling, my guess is that `Screen::GetScreenSize` (which later calls `SDL_GetRendererOutputSize`) returns 0 sometimes, and the code trusts that -- but I know that windows and things can be finicky, so the clamp is probably a good idea. --- desktop_version/src/Graphics.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index 72b50c01..8841a69e 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -3536,12 +3536,15 @@ void Graphics::get_stretch_info(SDL_Rect* rect) break; default: SDL_assert(0 && "Invalid scaling mode!"); - /* Width and height should be nonzero to avoid division by zero. */ rect->x = 0; rect->y = 0; rect->w = width; rect->h = height; } + + // In case anything accidentally set the width/height to 0, we'll clamp it to avoid crashing from a division by 0 + rect->w = SDL_max(1, rect->w); + rect->h = SDL_max(1, rect->h); } void Graphics::render(void)