Example resize fixes using IsWindowResized()

This commit is contained in:
Jon Daniel 2025-08-02 00:07:46 +02:00
commit b16313a2d3
13 changed files with 325 additions and 177 deletions

View file

@ -12,19 +12,15 @@ uniform vec4 colDiffuse;
// NOTE: Add your custom variables here
// NOTE: Render size values should be passed from code
const float renderWidth = 800.0;
const float renderHeight = 450.0;
float radius = 250.0;
float angle = 0.8;
uniform vec2 size = { 800.0, 450.0 };
uniform vec2 center;
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
vec2 tc = fragTexCoord*size;
tc -= center;
float dist = length(tc);
@ -40,7 +36,7 @@ void main()
}
tc += center;
vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;;
vec4 color = texture2D(texture0, tc/size)*colDiffuse*fragColor;;
gl_FragColor = vec4(color.rgb, 1.0);
}

View file

@ -10,19 +10,15 @@ uniform vec4 colDiffuse;
// NOTE: Add your custom variables here
// NOTE: Render size values should be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float radius = 250.0;
float angle = 0.8;
uniform vec2 size = vec2(800.0, 450.0);
uniform vec2 center;
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
vec2 tc = fragTexCoord*size;
tc -= center;
float dist = length(tc);
@ -38,7 +34,7 @@ void main()
}
tc += center;
vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;;
vec4 color = texture2D(texture0, tc/size)*colDiffuse*fragColor;;
gl_FragColor = vec4(color.rgb, 1.0);;
}
}

View file

@ -13,19 +13,15 @@ out vec4 finalColor;
// NOTE: Add your custom variables here
// NOTE: Render size values should be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float radius = 250.0;
float angle = 0.8;
uniform vec2 size = vec2(800.0, 450.0);
uniform vec2 center = vec2(200.0, 200.0);
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
vec2 tc = fragTexCoord*size;
tc -= center;
float dist = length(tc);
@ -41,7 +37,7 @@ void main()
}
tc += center;
vec4 color = texture(texture0, tc/texSize)*colDiffuse*fragColor;;
vec4 color = texture(texture0, tc/size)*colDiffuse*fragColor;;
finalColor = vec4(color.rgb, 1.0);;
}
}

View file

@ -82,8 +82,8 @@ int main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic pbr");
@ -202,6 +202,12 @@ int main()
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
UpdateCamera(&camera, CAMERA_ORBITAL);
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })

View file

@ -35,8 +35,10 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
float swirlSize[2] = { (float)screenWidth, (float)screenHeight };
float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
@ -62,12 +64,11 @@ int main(void)
// Get variable (uniform) location on the shader to connect with the program
// NOTE: If uniform variable could not be found in the shader, function returns -1
int swirlSizeLoc = GetShaderLocation(shader, "size");
int swirlCenterLoc = GetShaderLocation(shader, "center");
float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
// Create a RenderTexture2D to be used for render to texture
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
RenderTexture2D target;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -75,15 +76,27 @@ int main(void)
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
// Create a RenderTexture2D to be used for render to texture
UnloadRenderTexture(target);
target = LoadRenderTexture(screenWidth, screenHeight);
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
Vector2 mousePosition = GetMousePosition();
swirlSize[0] = screenWidth;
swirlSize[1] = screenHeight;
SetShaderValue(shader, swirlSizeLoc, swirlSize, SHADER_UNIFORM_VEC2);
swirlCenter[0] = mousePosition.x;
swirlCenter[1] = screenHeight - mousePosition.y;
// Send new value to the shader to be used on drawing
SetShaderValue(shader, swirlCenterLoc, swirlCenter, SHADER_UNIFORM_VEC2);
//----------------------------------------------------------------------------------
@ -98,7 +111,7 @@ int main(void)
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode
DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED);
DrawText("TEXT DRAWN IN RENDER TEXTURE", screenWidth - 600, 10, 30, RED);
EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
BeginDrawing();
@ -107,7 +120,7 @@ int main(void)
// Enable shader using the custom uniform
BeginShaderMode(shader);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2){ 0, 0 }, WHITE);
EndShaderMode();
// Draw some 2d text over drawn texture
@ -128,4 +141,4 @@ int main(void)
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -34,8 +34,8 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - hot reloading");
@ -51,9 +51,6 @@ int main(void)
int mouseLoc = GetShaderLocation(shader, "mouse");
int timeLoc = GetShaderLocation(shader, "time");
float resolution[2] = { (float)screenWidth, (float)screenHeight };
SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
float totalTime = 0.0f;
bool shaderAutoReloading = false;
@ -65,6 +62,14 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
float resolution[2] = { (float)screenWidth, (float)screenHeight };
SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
totalTime += GetFrameTime();
Vector2 mouse = GetMousePosition();
float mousePos[2] = { mouse.x, mouse.y };
@ -120,7 +125,7 @@ int main(void)
shaderAutoReloading? "AUTO" : "MANUAL"), 10, 10, 10, shaderAutoReloading? RED : BLACK);
if (!shaderAutoReloading) DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK);
DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, 430, 10, BLACK);
DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, screenHeight - 20, 10, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------

View file

@ -49,8 +49,9 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
Vector2 screenCenter = {.x = screenWidth/2.0f, .y = screenHeight/2.0f};
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer");
@ -69,12 +70,6 @@ int main(void)
marchLocs.camDir = GetShaderLocation(shdrRaymarch, "camDir");
marchLocs.screenCenter = GetShaderLocation(shdrRaymarch, "screenCenter");
// Transfer screenCenter position to shader. Which is used to calculate ray direction.
Vector2 screenCenter = {.x = screenWidth/2.0f, .y = screenHeight/2.0f};
SetShaderValue(shdrRaymarch, marchLocs.screenCenter , &screenCenter , SHADER_UNIFORM_VEC2);
// Use Customized function to create writable depth texture buffer
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
// Define the camera to look into our 3d world
Camera camera = {
@ -87,6 +82,7 @@ int main(void)
// Camera FOV is pre-calculated in the camera Distance.
float camDist = 1.0f/(tanf(camera.fovy*0.5f*DEG2RAD));
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -96,6 +92,18 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
// Use Customized function to create writable depth texture buffer
// Transfer screenCenter position to shader. Which is used to calculate ray direction.
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
screenCenter.x = screenWidth/2.0f;
screenCenter.y = screenHeight/2.0f;
UnloadRenderTexture(target);
target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
}
SetShaderValue(shdrRaymarch, marchLocs.screenCenter , &screenCenter , SHADER_UNIFORM_VEC2);
UpdateCamera(&camera, CAMERA_ORBITAL);
// Update Camera Postion in the ray march shader.
@ -133,7 +141,6 @@ int main(void)
// Draw into screen our custom render texture
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
DrawFPS(10, 10);
EndDrawing();

View file

@ -35,8 +35,8 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
@ -71,6 +71,11 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
UpdateCamera(&camera, CAMERA_FREE);
//----------------------------------------------------------------------------------
@ -106,4 +111,4 @@ int main(void)
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -69,8 +69,8 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
@ -133,6 +133,13 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
if(IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
UnloadRenderTexture(target);
target = LoadRenderTexture(screenWidth, screenHeight);
}
BeginTextureMode(target); // Enable drawing to texture
ClearBackground(RAYWHITE); // Clear texture background
@ -148,7 +155,7 @@ int main(void)
// Render generated texture using selected postprocessing shader
BeginShaderMode(shaders[currentShader]);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2){0, 0}, WHITE);
EndShaderMode();
// Draw 2d shapes and text over drawn texture
@ -158,7 +165,7 @@ int main(void)
DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK);
DrawText(postproShaderText[currentShader], 330, 15, 20, RED);
DrawText("< >", 540, 10, 30, DARKBLUE);
DrawFPS(700, 15);
DrawFPS(screenWidth - 100, 15);
EndDrawing();
//----------------------------------------------------------------------------------
}

View file

@ -68,8 +68,8 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
const Color rectangleColor = BLUE;
const Color shadowColor = DARKBLUE;
@ -100,6 +100,13 @@ int main(void)
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
@ -162,12 +169,10 @@ int main(void)
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
EndShaderMode();
// Draw one more rectangle with all three colors
rec = (Rectangle){ 240, 80, 500, 300 };
DrawRectangleLines((int)rec.x - 30, (int)rec.y - 30, (int)rec.width + 60, (int)rec.height + 60, DARKGRAY);
DrawText("Rectangle with all three combined", (int)rec.x - 30, (int)rec.y - 45, 10, DARKGRAY);
rec = (Rectangle){ 240, 80, screenWidth - 300, screenHeight - 150 };
DrawRectangleLines(rec.x - 30, rec.y - 30, rec.width + 60, rec.height + 60, DARKGRAY);
DrawText("Rectangle with all three combined", rec.x - 30, rec.y - 45, 10, DARKGRAY);
rec.y = screenHeight - rec.y - rec.height;
SetShaderValue(shader, roundedRectangle.rectangleLoc, (float[]){ rec.x, rec.y, rec.width, rec.height }, SHADER_UNIFORM_VEC4);

View file

@ -38,8 +38,8 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT);
// Shadows are a HUGE topic, and this example shows an extremely simple implementation of the shadowmapping algorithm,
@ -101,6 +101,11 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
float dt = GetFrameTime();
Vector3 cameraPos = cam.position;

View file

@ -62,6 +62,9 @@ typedef struct Star {
Vector2 speed;
} Star;
int screenWidth = 800;
int screenHeight = 450;
static void UpdateStar(Star *s);
static void ResetStar(Star *s);
@ -72,9 +75,6 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shader spotlight");
HideCursor();
@ -85,10 +85,8 @@ int main(void)
for (int n = 0; n < MAX_STARS; n++) ResetStar(&stars[n]);
// Progress all the stars on, so they don't all start in the centre
for (int m = 0; m < screenWidth/2.0; m++)
{
for (int m = 0; m < screenWidth * 0.5; m++)
for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
}
int frameCounter = 0;
@ -117,7 +115,7 @@ int main(void)
// Tell the shader how wide the screen is so we can have
// a pitch black half and a dimly lit half.
unsigned int wLoc = GetShaderLocation(shdrSpot, "screenWidth");
float sw = (float)GetScreenWidth();
float sw = (float)screenWidth;
SetShaderValue(shdrSpot, wLoc, &sw, SHADER_UNIFORM_FLOAT);
// Randomize the locations and velocities of the spotlights
@ -150,8 +148,16 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
frameCounter++;
sw = (float)screenWidth;
SetShaderValue(shdrSpot, wLoc, &sw, SHADER_UNIFORM_FLOAT);
// Move the stars, resetting them if the go offscreen
for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
@ -194,8 +200,8 @@ int main(void)
for (int i = 0; i < 16; i++)
{
DrawTexture(texRay,
(int)((screenWidth/2.0f) + cos((frameCounter + i*8)/51.45f)*(screenWidth/2.2f) - 32),
(int)((screenHeight/2.0f) + sin((frameCounter + i*8)/17.87f)*(screenHeight/4.2f)), WHITE);
(int)((screenWidth * 0.5f) + cos((frameCounter + i*8)/51.45f)*(screenWidth/2.2f) - 32),
(int)((screenHeight * 0.5f) + sin((frameCounter + i*8)/17.87f)*(screenHeight/4.2f)), WHITE);
}
// Draw spot lights
@ -210,8 +216,8 @@ int main(void)
DrawFPS(10, 10);
DrawText("Move the mouse!", 10, 30, 20, GREEN);
DrawText("Pitch Black", (int)(screenWidth*0.2f), screenHeight/2, 20, GREEN);
DrawText("Dark", (int)(screenWidth*.66f), screenHeight/2, 20, GREEN);
DrawText("Pitch Black", (int)(screenWidth*0.2f), screenHeight * 0.5f, 20, GREEN);
DrawText("Dark", (int)(screenWidth*.66f), screenHeight * 0.5f, 20, GREEN);
EndDrawing();
//----------------------------------------------------------------------------------
@ -231,7 +237,7 @@ int main(void)
static void ResetStar(Star *s)
{
s->position = (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
s->position = (Vector2){ screenWidth * 0.5f, screenHeight * 0.5f };
do
{
@ -247,8 +253,8 @@ static void UpdateStar(Star *s)
{
s->position = Vector2Add(s->position, s->speed);
if ((s->position.x < 0) || (s->position.x > GetScreenWidth()) ||
(s->position.y < 0) || (s->position.y > GetScreenHeight()))
if ((s->position.x < 0) || (s->position.x > screenWidth) ||
(s->position.y < 0) || (s->position.y > screenHeight))
{
ResetStar(s);
}

View file

@ -41,8 +41,8 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer");
@ -69,11 +69,18 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsWindowResized())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
}
UpdateCamera(&camera, CAMERA_ORBITAL);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
UnloadRenderTexture(target);
target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
// Draw into our custom render texture (framebuffer)
BeginTextureMode(target);
ClearBackground(WHITE);
@ -91,7 +98,7 @@ int main(void)
// Draw into screen our custom render texture
BeginDrawing();
ClearBackground(RAYWHITE);
ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
DrawFPS(10, 10);
EndDrawing();
@ -164,4 +171,4 @@ void UnloadRenderTextureDepthTex(RenderTexture2D target)
// queried and deleted before deleting framebuffer
rlUnloadFramebuffer(target.id);
}
}
}