From dcba0645a3b8ce81bd8825a055c9e91f0b45c8e5 Mon Sep 17 00:00:00 2001 From: S M Nazmuz Sakib Date: Mon, 30 Nov 2026 05:05:23 +0600 Subject: [PATCH] Fix IsWindowResized after programmatic window resize --- examples/core/core_window_resize.c | 73 ++++++++++++++++++++++++++++++ src/rcore.c | 15 +++++- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 examples/core/core_window_resize.c diff --git a/examples/core/core_window_resize.c b/examples/core/core_window_resize.c new file mode 100644 index 000000000000..339ce2cd934a --- /dev/null +++ b/examples/core/core_window_resize.c @@ -0,0 +1,73 @@ +/******************************************************************************************* +* +* raylib [core] example - programmatic window resize +* +* This example checks that IsWindowResized() also reports resizes made through +* SetWindowSize(), not only resizes made by the user with the mouse. Every time +* SPACE is pressed the window is resized and the status bar turns red for one +* frame when the resize is detected. +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +**********************************************************************************************/ + +#include "raylib.h" + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + SetConfigFlags(FLAG_WINDOW_RESIZABLE); + InitWindow(screenWidth, screenHeight, "raylib [core] example - programmatic resize"); + + int newWidth = screenWidth; + int newHeight = screenHeight; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_SPACE)) + { + newWidth = (newWidth == screenWidth)? 640 : screenWidth; + newHeight = (newHeight == screenHeight)? 360 : screenHeight; + SetWindowSize(newWidth, newHeight); + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("Press SPACE to resize the window", 190, 180, 20, DARKGRAY); + + // This must also be true for resizes triggered by SetWindowSize() + if (IsWindowResized()) DrawText("WINDOW RESIZED!", 190, 220, 20, RED); + else DrawText("Window size unchanged", 190, 220, 20, LIGHTGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/src/rcore.c b/src/rcore.c index b7b619145beb..33fbf0889131 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -311,6 +311,7 @@ typedef struct CoreData { Size previousScreen; // Screen previous width and height (required on fullscreen/borderless-windowed toggle) Point previousPosition; // Window previous position (required on fullscreen/borderless-windowed toggle) Size render; // Screen framebuffer width and height + Size previousRender; // Screen framebuffer size at the end of the previous frame Point renderOffset; // Screen framebuffer render offset (Not required anymore?) Size currentFbo; // Current framebuffer render width and height (depends on active render texture) Size screenMin; // Screen minimum width and height (for resizable window) @@ -674,6 +675,9 @@ void InitWindow(int width, int height, const char *title) CORE.Window.render.width = CORE.Window.screen.width; CORE.Window.render.height = CORE.Window.screen.height; } + + // Initialize resize tracking so the first frame is not reported as a resize + CORE.Window.previousRender = CORE.Window.render; //-------------------------------------------------------------- // Initialize rlgl default data (buffers and shaders) @@ -918,7 +922,16 @@ void EndDrawing(void) #endif PollInputEvents(); // Poll user events (before next frame update) - + + // A programmatic resize (SetWindowSize(), MaximizeWindow(), ...) can update the + // framebuffer size before PollInputEvents() clears resizedLastFrame on some + // platforms. Detect the change at core level so IsWindowResized() reports a + // resize for exactly one frame on every backend. + if ((CORE.Window.render.width != CORE.Window.previousRender.width) || + (CORE.Window.render.height != CORE.Window.previousRender.height)) CORE.Window.resizedLastFrame = true; + + CORE.Window.previousRender = CORE.Window.render; + #if SUPPORT_GESTURES_SYSTEM // WARNING: Below checks run per-frame basis while ProcessGestureEvent() only runs on event detected (up/down/move) // If a previous swipe gesture was detected, just reset it