Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions examples/core/core_window_resize.c
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 14 additions & 1 deletion src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down