← ziglang.inraylib-zigGitHub

render texture

★☆☆☆by Ramon Santamaria (@raysan5)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [core] example - render texture
//!
//! Zig-API translation of examples/core/core_render_texture.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Ramon Santamaria (@raysan5)

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 - render texture");
    defer rl.closeWindow(); // Close window and OpenGL context

    // Define a render texture to render
    const render_texture_width = 300;
    const render_texture_height = 300;
    const target = try rl.loadRenderTexture(render_texture_width, render_texture_height);
    defer rl.unloadRenderTexture(target);

    var ball_position: rl.Vector2 = .{ .x = render_texture_width / 2.0, .y = render_texture_height / 2.0 };
    var ball_speed: rl.Vector2 = .{ .x = 5.0, .y = 4.0 };
    const ball_radius = 20;

    var rotation: f32 = 0.0;

    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
        // Ball movement logic
        ball_position.x += ball_speed.x;
        ball_position.y += ball_speed.y;

        // Check walls collision for bouncing
        if (ball_position.x >= render_texture_width - ball_radius or ball_position.x <= ball_radius) ball_speed.x *= -1.0;
        if (ball_position.y >= render_texture_height - ball_radius or ball_position.y <= ball_radius) ball_speed.y *= -1.0;

        // Render texture rotation
        rotation += 0.5;

        // Draw
        // Draw our scene to the render texture
        {
            rl.beginTextureMode(target);
            defer rl.endTextureMode();

            rl.clearBackground(.sky_blue);

            rl.drawRectangle(0, 0, 20, 20, .red);
            rl.drawCircleV(ball_position, ball_radius, .maroon);
        }

        // Draw render texture to main framebuffer
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.ray_white);

        // Draw our render texture with rotation applied
        // NOTE 1: We set the origin of the texture to the center of the render texture
        // NOTE 2: We flip vertically the texture setting negative source rectangle height
        const width: f32 = @floatFromInt(target.texture.width);
        const height: f32 = @floatFromInt(target.texture.height);
        rl.drawTexturePro(
            target.texture,
            .{ .x = 0, .y = 0, .width = width, .height = -height },
            .{ .x = screen_width / 2.0, .y = screen_height / 2.0, .width = width, .height = height },
            .{ .x = width / 2.0, .y = height / 2.0 },
            rotation,
            .white,
        );

        rl.drawText("DRAWING BOUNCING BALL INSIDE RENDER TEXTURE!", 10, screen_height - 40, 20, .black);

        rl.drawFPS(10, 10);
    }
}
/*******************************************************************************************
*
*   raylib [core] example - render texture
*
*   Example complexity rating: [★☆☆☆] 1/4
*
*   Example originally created with raylib 6.0, last time updated with raylib 6.0
*
*   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) 2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#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 - render texture");

    // Define a render texture to render
    int renderTextureWidth = 300;
    int renderTextureHeight = 300;
    RenderTexture2D target = LoadRenderTexture(renderTextureWidth, renderTextureHeight);

    Vector2 ballPosition = { renderTextureWidth/2.0f, renderTextureHeight/2.0f };
    Vector2 ballSpeed = { 5.0f, 4.0f };
    int ballRadius = 20;

    float rotation = 0.0f;

    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
        //-----------------------------------------------------
        // Ball movement logic
        ballPosition.x += ballSpeed.x;
        ballPosition.y += ballSpeed.y;

        // Check walls collision for bouncing
        if ((ballPosition.x >= (renderTextureWidth - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
        if ((ballPosition.y >= (renderTextureHeight - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;

        // Render texture rotation
        rotation += 0.5f;
        //-----------------------------------------------------

        // Draw
        //-----------------------------------------------------
        // Draw our scene to the render texture
        BeginTextureMode(target);

            ClearBackground(SKYBLUE);

            DrawRectangle(0, 0, 20, 20, RED);
            DrawCircleV(ballPosition, (float)ballRadius, MAROON);

        EndTextureMode();

        // Draw render texture to main framebuffer
        BeginDrawing();

            ClearBackground(RAYWHITE);

            // Draw our render texture with rotation applied
            // NOTE 1: We set the origin of the texture to the center of the render texture
            // NOTE 2: We flip vertically the texture setting negative source rectangle height
            DrawTexturePro(target.texture,
                (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height },
                (Rectangle){ screenWidth/2.0f, screenHeight/2.0f, (float)target.texture.width, (float)target.texture.height },
                (Vector2){ target.texture.width/2.0f, target.texture.height/2.0f }, rotation, WHITE);

            DrawText("DRAWING BOUNCING BALL INSIDE RENDER TEXTURE!", 10, screenHeight - 40, 20, BLACK);


            DrawFPS(10, 10);

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

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

    return 0;
}

From raylib 6.2. The original on GitHub.