← ziglang.inraylib-zigGitHub

rectangle scaling

★★☆☆by Vlad Adrian (@demizdor)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [shapes] example - rectangle scaling
//!
//! Zig-API translation of examples/shapes/shapes_rectangle_scaling.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2018-2025 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)

const rl = @import("raylib");

const mouse_scale_mark_size = 12;

pub fn main() void {
    // Initialization
    const screen_width = 800;
    const screen_height = 450;

    rl.initWindow(screen_width, screen_height, "raylib [shapes] example - rectangle scaling");
    defer rl.closeWindow(); // Close window and OpenGL context

    var rec: rl.Rectangle = .{ .x = 100, .y = 100, .width = 200, .height = 80 };

    var mouse_scale_ready = false;
    var mouse_scale_mode = false;

    rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second

    // Main game loop
    while (!rl.windowShouldClose()) { // Detect window close button or ESC key
        // Update
        const mouse_position = rl.getMousePosition();

        const scale_mark: rl.Rectangle = .{
            .x = rec.x + rec.width - mouse_scale_mark_size,
            .y = rec.y + rec.height - mouse_scale_mark_size,
            .width = mouse_scale_mark_size,
            .height = mouse_scale_mark_size,
        };
        if (rl.checkCollisionPointRec(mouse_position, scale_mark)) {
            mouse_scale_ready = true;
            if (rl.isMouseButtonPressed(.left)) mouse_scale_mode = true;
        } else mouse_scale_ready = false;

        if (mouse_scale_mode) {
            mouse_scale_ready = true;

            rec.width = mouse_position.x - rec.x;
            rec.height = mouse_position.y - rec.y;

            // Check minimum rec size
            if (rec.width < mouse_scale_mark_size) rec.width = mouse_scale_mark_size;
            if (rec.height < mouse_scale_mark_size) rec.height = mouse_scale_mark_size;

            // Check maximum rec size
            const max_width = @as(f32, @floatFromInt(rl.getScreenWidth())) - rec.x;
            const max_height = @as(f32, @floatFromInt(rl.getScreenHeight())) - rec.y;
            if (rec.width > max_width) rec.width = max_width;
            if (rec.height > max_height) rec.height = max_height;

            if (rl.isMouseButtonReleased(.left)) mouse_scale_mode = false;
        }

        // Draw
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.ray_white);

        rl.drawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, .gray);

        rl.drawRectangleRec(rec, rl.fade(.green, 0.5));

        if (mouse_scale_ready) {
            rl.drawRectangleLinesEx(rec, 1, .red);
            rl.drawTriangle(
                .{ .x = rec.x + rec.width - mouse_scale_mark_size, .y = rec.y + rec.height },
                .{ .x = rec.x + rec.width, .y = rec.y + rec.height },
                .{ .x = rec.x + rec.width, .y = rec.y + rec.height - mouse_scale_mark_size },
                .red,
            );
        }
    }
}
/*******************************************************************************************
*
*   raylib [shapes] example - rectangle scaling
*
*   Example complexity rating: [★★☆☆] 2/4
*
*   Example originally created with raylib 2.5, last time updated with raylib 2.5
*
*   Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
*   BSD-like license that allows static linking with closed source software
*
*   Copyright (c) 2018-2025 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#define MOUSE_SCALE_MARK_SIZE   12

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling");

    Rectangle rec = { 100, 100, 200, 80 };

    Vector2 mousePosition = { 0 };

    bool mouseScaleReady = false;
    bool mouseScaleMode = false;

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        mousePosition = GetMousePosition();

        if (CheckCollisionPointRec(mousePosition, (Rectangle){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE }))
        {
            mouseScaleReady = true;
            if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) mouseScaleMode = true;
        }
        else mouseScaleReady = false;

        if (mouseScaleMode)
        {
            mouseScaleReady = true;

            rec.width = (mousePosition.x - rec.x);
            rec.height = (mousePosition.y - rec.y);

            // Check minimum rec size
            if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
            if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;

            // Check maximum rec size
            if (rec.width > (GetScreenWidth() - rec.x)) rec.width = GetScreenWidth() - rec.x;
            if (rec.height > (GetScreenHeight() - rec.y)) rec.height = GetScreenHeight() - rec.y;

            if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) mouseScaleMode = false;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);

            DrawRectangleRec(rec, Fade(GREEN, 0.5f));

            if (mouseScaleReady)
            {
                DrawRectangleLinesEx(rec, 1, RED);
                DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
                             (Vector2){ rec.x + rec.width, rec.y + rec.height },
                             (Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
            }

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

From raylib 6.2. The original on GitHub.