← ziglang.inraylib-zigGitHub

scissor test

★☆☆☆by Chris Dill (@MysteriousSpace)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [core] example - scissor test
//!
//! Zig-API translation of examples/core/core_scissor_test.c (raylib 6.2).
//! Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2019-2025 Chris Dill (@MysteriousSpace)

const rl = @import("raylib");

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

    rl.initWindow(screen_width, screen_height, "raylib [core] example - scissor test");
    defer rl.closeWindow(); // Close window and OpenGL context

    var scissor_area: rl.Rectangle = .{ .x = 0, .y = 0, .width = 300, .height = 300 };
    var scissor_mode = true;

    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
        if (rl.isKeyPressed(.s)) scissor_mode = !scissor_mode;

        // Centre the scissor area around the mouse position
        scissor_area.x = @as(f32, @floatFromInt(rl.getMouseX())) - scissor_area.width / 2;
        scissor_area.y = @as(f32, @floatFromInt(rl.getMouseY())) - scissor_area.height / 2;

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

        rl.clearBackground(.ray_white);

        if (scissor_mode) rl.beginScissorMode(
            @intFromFloat(scissor_area.x),
            @intFromFloat(scissor_area.y),
            @intFromFloat(scissor_area.width),
            @intFromFloat(scissor_area.height),
        );

        // Draw full screen rectangle and some text
        // NOTE: Only part defined by scissor area will be rendered
        rl.drawRectangle(0, 0, rl.getScreenWidth(), rl.getScreenHeight(), .red);
        rl.drawText("Move the mouse around to reveal this text!", 190, 200, 20, .light_gray);

        if (scissor_mode) rl.endScissorMode();

        rl.drawRectangleLinesEx(scissor_area, 1, .black);
        rl.drawText("Press S to toggle scissor test", 10, 10, 20, .black);
    }
}
/*******************************************************************************************
*
*   raylib [core] example - scissor test
*
*   Example complexity rating: [★☆☆☆] 1/4
*
*   Example originally created with raylib 2.5, last time updated with raylib 3.0
*
*   Example contributed by Chris Dill (@MysteriousSpace) 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) 2019-2025 Chris Dill (@MysteriousSpace)
*
********************************************************************************************/

#include "raylib.h"

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

    InitWindow(screenWidth, screenHeight, "raylib [core] example - scissor test");

    Rectangle scissorArea = { 0, 0, 300, 300 };
    bool scissorMode = true;

    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
        //----------------------------------------------------------------------------------
        if (IsKeyPressed(KEY_S)) scissorMode = !scissorMode;

        // Centre the scissor area around the mouse position
        scissorArea.x = GetMouseX() - scissorArea.width/2;
        scissorArea.y = GetMouseY() - scissorArea.height/2;
        //----------------------------------------------------------------------------------

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

            ClearBackground(RAYWHITE);

            if (scissorMode) BeginScissorMode((int)scissorArea.x, (int)scissorArea.y, (int)scissorArea.width, (int)scissorArea.height);

            // Draw full screen rectangle and some text
            // NOTE: Only part defined by scissor area will be rendered
            DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), RED);
            DrawText("Move the mouse around to reveal this text!", 190, 200, 20, LIGHTGRAY);

            if (scissorMode) EndScissorMode();

            DrawRectangleLinesEx(scissorArea, 1, BLACK);
            DrawText("Press S to toggle scissor test", 10, 10, 20, BLACK);

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

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

    return 0;
}

From raylib 6.2. The original on GitHub.