← ziglang.inraylib-zigGitHub

3d camera split screen

★★★☆by Jeffery Myers (@JeffM2501)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [core] example - 3d camera split screen
//!
//! Zig-API translation of examples/core/core_3d_camera_split_screen.c (raylib 6.2).
//! Example contributed by Jeffery Myers (@JeffM2501) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2021-2025 Jeffery Myers (@JeffM2501)

const rl = @import("raylib");

// Grid data
const count = 5;
const spacing: f32 = 4;

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

    rl.initWindow(screen_width, screen_height, "raylib [core] example - 3d camera split screen");
    defer rl.closeWindow(); // Close window and OpenGL context

    // Setup player 1 camera and screen
    var camera_player1: rl.Camera = .{
        .position = .{ .x = 0, .y = 1.0, .z = -3.0 },
        .target = .{ .x = 0, .y = 1.0, .z = 0 },
        .up = .{ .x = 0, .y = 1.0, .z = 0 },
        .fovy = 45.0,
        .projection = .perspective,
    };

    const screen_player1 = try rl.loadRenderTexture(screen_width / 2, screen_height);
    defer rl.unloadRenderTexture(screen_player1); // Unload render texture

    // Setup player two camera and screen
    var camera_player2: rl.Camera = .{
        .position = .{ .x = -3.0, .y = 3.0, .z = 0 },
        .target = .{ .x = 0, .y = 3.0, .z = 0 },
        .up = .{ .x = 0, .y = 1.0, .z = 0 },
        .fovy = 45.0,
        .projection = .perspective,
    };

    const screen_player2 = try rl.loadRenderTexture(screen_width / 2, screen_height);
    defer rl.unloadRenderTexture(screen_player2); // Unload render texture

    // Build a flipped rectangle the size of the split view to use for drawing later
    const split_screen_rect: rl.Rectangle = .{
        .x = 0.0,
        .y = 0.0,
        .width = @floatFromInt(screen_player1.texture.width),
        .height = @floatFromInt(-screen_player1.texture.height),
    };

    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 anyone moves this frame, how far will they move based on the time since the last frame
        // this moves things at 10 world units per second, regardless of the actual FPS
        const offset_this_frame = 10.0 * rl.getFrameTime();

        // Move Player1 forward and backwards (no turning)
        if (rl.isKeyDown(.w)) {
            camera_player1.position.z += offset_this_frame;
            camera_player1.target.z += offset_this_frame;
        } else if (rl.isKeyDown(.s)) {
            camera_player1.position.z -= offset_this_frame;
            camera_player1.target.z -= offset_this_frame;
        }

        // Move Player2 forward and backwards (no turning)
        if (rl.isKeyDown(.up)) {
            camera_player2.position.x += offset_this_frame;
            camera_player2.target.x += offset_this_frame;
        } else if (rl.isKeyDown(.down)) {
            camera_player2.position.x -= offset_this_frame;
            camera_player2.target.x -= offset_this_frame;
        }

        // Draw
        // Draw Player1 view to the render texture
        {
            rl.beginTextureMode(screen_player1);
            defer rl.endTextureMode();

            rl.clearBackground(.sky_blue);

            drawScene(camera_player1, camera_player1.position, camera_player2.position);

            rl.drawRectangle(0, 0, @divTrunc(rl.getScreenWidth(), 2), 40, rl.fade(.ray_white, 0.8));
            rl.drawText("PLAYER1: W/S to move", 10, 10, 20, .maroon);
        }

        // Draw Player2 view to the render texture
        {
            rl.beginTextureMode(screen_player2);
            defer rl.endTextureMode();

            rl.clearBackground(.sky_blue);

            drawScene(camera_player2, camera_player1.position, camera_player2.position);

            rl.drawRectangle(0, 0, @divTrunc(rl.getScreenWidth(), 2), 40, rl.fade(.ray_white, 0.8));
            rl.drawText("PLAYER2: UP/DOWN to move", 10, 10, 20, .dark_blue);
        }

        // Draw both views render textures to the screen side by side
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.black);

        rl.drawTextureRec(screen_player1.texture, split_screen_rect, .{ .x = 0, .y = 0 }, .white);
        rl.drawTextureRec(screen_player2.texture, split_screen_rect, .{ .x = screen_width / 2.0, .y = 0 }, .white);

        rl.drawRectangle(@divTrunc(rl.getScreenWidth(), 2) - 2, 0, 4, rl.getScreenHeight(), .light_gray);
    }
}

/// Draw scene: grid of cube trees on a plane to make a "world", plus a cube at each player's position
fn drawScene(camera: rl.Camera, player1: rl.Vector3, player2: rl.Vector3) void {
    rl.beginMode3D(camera);
    defer rl.endMode3D();

    rl.drawPlane(.{ .x = 0, .y = 0, .z = 0 }, .{ .x = 50, .y = 50 }, .beige); // Simple world plane

    const extent = count * spacing;
    var x: f32 = -extent;
    while (x <= extent) : (x += spacing) {
        var z: f32 = -extent;
        while (z <= extent) : (z += spacing) {
            rl.drawCube(.{ .x = x, .y = 1.5, .z = z }, 1, 1, 1, .lime);
            rl.drawCube(.{ .x = x, .y = 0.5, .z = z }, 0.25, 1, 0.25, .brown);
        }
    }

    // Draw a cube at each player's position
    rl.drawCube(player1, 1, 1, 1, .red);
    rl.drawCube(player2, 1, 1, 1, .blue);
}
/*******************************************************************************************
*
*   raylib [core] example - 3d camera split screen
*
*   Example complexity rating: [★★★☆] 3/4
*
*   Example originally created with raylib 3.7, last time updated with raylib 4.0
*
*   Example contributed by Jeffery Myers (@JeffM2501) 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) 2021-2025 Jeffery Myers (@JeffM2501)
*
********************************************************************************************/

#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 - 3d camera split screen");

    // Setup player 1 camera and screen
    Camera cameraPlayer1 = { 0 };
    cameraPlayer1.fovy = 45.0f;
    cameraPlayer1.up.y = 1.0f;
    cameraPlayer1.target.y = 1.0f;
    cameraPlayer1.position.z = -3.0f;
    cameraPlayer1.position.y = 1.0f;

    RenderTexture screenPlayer1 = LoadRenderTexture(screenWidth/2, screenHeight);

    // Setup player two camera and screen
    Camera cameraPlayer2 = { 0 };
    cameraPlayer2.fovy = 45.0f;
    cameraPlayer2.up.y = 1.0f;
    cameraPlayer2.target.y = 3.0f;
    cameraPlayer2.position.x = -3.0f;
    cameraPlayer2.position.y = 3.0f;

    RenderTexture screenPlayer2 = LoadRenderTexture(screenWidth/2, screenHeight);

    // Build a flipped rectangle the size of the split view to use for drawing later
    Rectangle splitScreenRect = { 0.0f, 0.0f, (float)screenPlayer1.texture.width, (float)-screenPlayer1.texture.height };

    // Grid data
    int count = 5;
    float spacing = 4;

    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 anyone moves this frame, how far will they move based on the time since the last frame
        // this moves things at 10 world units per second, regardless of the actual FPS
        float offsetThisFrame = 10.0f*GetFrameTime();

        // Move Player1 forward and backwards (no turning)
        if (IsKeyDown(KEY_W))
        {
            cameraPlayer1.position.z += offsetThisFrame;
            cameraPlayer1.target.z += offsetThisFrame;
        }
        else if (IsKeyDown(KEY_S))
        {
            cameraPlayer1.position.z -= offsetThisFrame;
            cameraPlayer1.target.z -= offsetThisFrame;
        }

        // Move Player2 forward and backwards (no turning)
        if (IsKeyDown(KEY_UP))
        {
            cameraPlayer2.position.x += offsetThisFrame;
            cameraPlayer2.target.x += offsetThisFrame;
        }
        else if (IsKeyDown(KEY_DOWN))
        {
            cameraPlayer2.position.x -= offsetThisFrame;
            cameraPlayer2.target.x -= offsetThisFrame;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        // Draw Player1 view to the render texture
        BeginTextureMode(screenPlayer1);
            ClearBackground(SKYBLUE);

            BeginMode3D(cameraPlayer1);

                // Draw scene: grid of cube trees on a plane to make a "world"
                DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // Simple world plane

                for (float x = -count*spacing; x <= count*spacing; x += spacing)
                {
                    for (float z = -count*spacing; z <= count*spacing; z += spacing)
                    {
                        DrawCube((Vector3) { x, 1.5f, z }, 1, 1, 1, LIME);
                        DrawCube((Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN);
                    }
                }

                // Draw a cube at each player's position
                DrawCube(cameraPlayer1.position, 1, 1, 1, RED);
                DrawCube(cameraPlayer2.position, 1, 1, 1, BLUE);

            EndMode3D();

            DrawRectangle(0, 0, GetScreenWidth()/2, 40, Fade(RAYWHITE, 0.8f));
            DrawText("PLAYER1: W/S to move", 10, 10, 20, MAROON);

        EndTextureMode();

        // Draw Player2 view to the render texture
        BeginTextureMode(screenPlayer2);
            ClearBackground(SKYBLUE);

            BeginMode3D(cameraPlayer2);

                // Draw scene: grid of cube trees on a plane to make a "world"
                DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // Simple world plane

                for (float x = -count*spacing; x <= count*spacing; x += spacing)
                {
                    for (float z = -count*spacing; z <= count*spacing; z += spacing)
                    {
                        DrawCube((Vector3) { x, 1.5f, z }, 1, 1, 1, LIME);
                        DrawCube((Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN);
                    }
                }

                // Draw a cube at each player's position
                DrawCube(cameraPlayer1.position, 1, 1, 1, RED);
                DrawCube(cameraPlayer2.position, 1, 1, 1, BLUE);

            EndMode3D();

            DrawRectangle(0, 0, GetScreenWidth()/2, 40, Fade(RAYWHITE, 0.8f));
            DrawText("PLAYER2: UP/DOWN to move", 10, 10, 20, DARKBLUE);

        EndTextureMode();

        // Draw both views render textures to the screen side by side
        BeginDrawing();
            ClearBackground(BLACK);

            DrawTextureRec(screenPlayer1.texture, splitScreenRect, (Vector2){ 0, 0 }, WHITE);
            DrawTextureRec(screenPlayer2.texture, splitScreenRect, (Vector2){ screenWidth/2.0f, 0 }, WHITE);

            DrawRectangle(GetScreenWidth()/2 - 2, 0, 4, GetScreenHeight(), LIGHTGRAY);
        EndDrawing();
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadRenderTexture(screenPlayer1); // Unload render texture
    UnloadRenderTexture(screenPlayer2); // Unload render texture

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

    return 0;
}

From raylib 6.2. The original on GitHub.