Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created January 15, 2021 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/5738c738e1250820b833db9f88b81555 to your computer and use it in GitHub Desktop.
Save peterhellberg/5738c738e1250820b833db9f88b81555 to your computer and use it in GitHub Desktop.
Raylib examples translated from C to Zig https://github.com/raysan5/raylib/tree/master/examples
const std = @import("std");
const ray = @cImport({
@cInclude("raylib.h");
});
pub fn main() void {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");
defer ray.CloseWindow();
var ballPosition = ray.Vector2{
.x = -100.0,
.y = -100.0,
};
var ballColor = ray.DARKBLUE;
// Set our game to run at 60 frames-per-second
ray.SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!ray.WindowShouldClose()) {
// Update
//----------------------------------------------------------------------------------
// For some reason ballPosition = ray.GetMousePosition();
// resulted in diagonal movement of the ball,
// instead I’m using GetMouseX and GetMouseY:
ballPosition.x = @intToFloat(f32, ray.GetMouseX());
ballPosition.y = @intToFloat(f32, ray.GetMouseY());
if (ray.IsMouseButtonPressed(ray.MOUSE_LEFT_BUTTON)) {
ballColor = ray.MAROON;
} else if (ray.IsMouseButtonPressed(ray.MOUSE_MIDDLE_BUTTON)) {
ballColor = ray.LIME;
} else if (ray.IsMouseButtonPressed(ray.MOUSE_RIGHT_BUTTON)) {
ballColor = ray.DARKBLUE;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
ray.BeginDrawing();
defer ray.EndDrawing();
ray.ClearBackground(ray.RAYWHITE);
ray.DrawCircleV(ballPosition, 40, ballColor);
ray.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, ray.DARKGRAY);
//----------------------------------------------------------------------------------
}
}
@peterhellberg
Copy link
Author

build.zig that links to raylib

const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});

    // Standard release options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
    const mode = b.standardReleaseOptions();

    const exe = b.addExecutable("ray", "src/main.zig");

    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.linkSystemLibrary("raylib");
    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

@peterhellberg
Copy link
Author

Related Zig issues

@peterhellberg
Copy link
Author

const std = @import("std");

const ray = @cImport({
    @cInclude("raylib.h");
});

pub fn main() void {
    const screenWidth = 650;
    const screenHeight = 420;

    ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");
    defer ray.CloseWindow();

    var ballPosition = ray.Vector2{
        .x = -100.0,
        .y = -100.0,
    };

    var ballColor = ray.DARKBLUE;

    ray.SetTargetFPS(60);

    while (!ray.WindowShouldClose()) {
        ballPosition = ray.GetMousePosition();

        if (ray.IsMouseButtonPressed(ray.MOUSE_LEFT_BUTTON)) {
            ballColor = ray.MAROON;
        } else if (ray.IsMouseButtonPressed(ray.MOUSE_MIDDLE_BUTTON)) {
            ballColor = ray.LIME;
        } else if (ray.IsMouseButtonPressed(ray.MOUSE_RIGHT_BUTTON)) {
            ballColor = ray.DARKBLUE;
        }

        ray.BeginDrawing();
        defer ray.EndDrawing();

        ray.ClearBackground(ray.RAYWHITE);
        ray.DrawCircleV(ballPosition, 40, ballColor);

        ray.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, ray.DARKGRAY);
    }
}

Now (v0.10.0) does what you expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment