Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarknessFX committed Jan 9, 2025
1 parent 20db813 commit 4b52cd3
Show file tree
Hide file tree
Showing 19 changed files with 2,838 additions and 12 deletions.
31 changes: 19 additions & 12 deletions projects/zig_raylib_examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn build(b: *std.Build) !void {
"audio",
"core",
"text",
"shapes",
};

// Loop each folder to build
Expand Down Expand Up @@ -49,18 +50,24 @@ pub fn build(b: *std.Build) !void {
}

// // Make bin/build/resources folders
const destFolder = fmt("{s}\\{s}\\{s}\\{s}", .{ cwd, "bin", build_dir, "resources\\" } );
std.fs.makeDirAbsolute( fmt("{s}\\{s}", .{ cwd, "bin" } )) catch { };
std.fs.makeDirAbsolute( fmt("{s}\\{s}\\{s}", .{ cwd, "bin", build_dir } )) catch { };
std.fs.makeDirAbsolute( destFolder ) catch { };

const copyStep = b.addSystemCommand(&.{ "CMD", "/C",
b.fmt("COPY /Y {s} {s}>nul", .{
fmt("{s}\\{s}\\resources\\*.*", .{ cwd, folder } ),
destFolder,
}),
});
b.getInstallStep().dependOn(&copyStep.step);
const cwd_resources = try std.fs.path.join(b.allocator, &.{ cwd, folder, "resources" });
var dir_resources = std.fs.cwd().openDir(cwd_resources, .{ .iterate = false }) catch null;
defer if ((comptime builtin.zig_version.minor >= 13) and (dir_resources != null)) dir_resources.?.close();

if (@TypeOf(dir_resources) == std.fs.Dir) {
const destFolder = fmt("{s}\\{s}\\{s}\\{s}", .{ cwd, "bin", build_dir, "resources\\" } );
std.fs.makeDirAbsolute( fmt("{s}\\{s}", .{ cwd, "bin" } )) catch { };
std.fs.makeDirAbsolute( fmt("{s}\\{s}\\{s}", .{ cwd, "bin", build_dir } )) catch { };
std.fs.makeDirAbsolute( destFolder ) catch { };

const copyStep = b.addSystemCommand(&.{ "CMD", "/C",
b.fmt("COPY /Y {s} {s} >nul", .{
fmt("{s}\\{s}\\resources\\*.*", .{ cwd, folder } ),
destFolder,
}),
});
b.getInstallStep().dependOn(&copyStep.step);
}
}
}

Expand Down
116 changes: 116 additions & 0 deletions projects/zig_raylib_examples/shapes/shapes_basic_shapes.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//!zig-autodoc-section: shapes_basic_shapes.Main
//! raylib_examples/shapes_basic_shapes.zig
//! Example - Draw basic shapes 2d.
//!
//! raylib Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
//! Zig port created by DarknessFX | https://dfx.lv | X @DrkFX

// /*******************************************************************************************
// *
// * raylib [shapes] example - Draw basic shapes 2d (rectangle, circle, line...)
// *
// * Example originally created with raylib 1.0, last time updated with raylib 4.2
// *
// * 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) 2014-2024 Ramon Santamaria (@raysan5)
// *
// ********************************************************************************************/

const std = @import("std");

// NOTE ABOUT VSCODE + ZLS:
// Use full path for all cIncludes:
// @cInclude("C:/raylib_examples/lib/raylib.h");
const ray = @cImport({
@cInclude("raylib.h");
});

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
pub fn main() !u8 {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth: f32 = 800.0;
const screenHeight: f32 = 450.0;

ray.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - basic shapes drawing");

var rotation: f32 = 0.0;

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

// Main game loop
while (!ray.WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
rotation += 0.2;
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
ray.BeginDrawing();

ray.ClearBackground(ray.RAYWHITE);

ray.DrawText("some basic shapes available on raylib", 20, 20, 20, ray.DARKGRAY);

// Circle shapes and lines
ray.DrawCircle(screenWidth / 5, 120, 35, ray.DARKBLUE);
ray.DrawCircleGradient(screenWidth / 5, 220, 60, ray.GREEN, ray.SKYBLUE);
ray.DrawCircleLines(screenWidth / 5, 340, 80, ray.DARKBLUE);

// Rectangle shapes and lines
ray.DrawRectangle(screenWidth / 4 * 2 - 60, 100, 120, 60, ray.RED);
ray.DrawRectangleGradientH(screenWidth / 4 * 2 - 90, 170, 180, 130, ray.MAROON, ray.GOLD);
ray.DrawRectangleLines(screenWidth / 4 * 2 - 40, 320, 80, 60, ray.ORANGE); // NOTE: Uses QUADS internally, not lines

// Triangle shapes and lines
ray.DrawTriangle(ray.Vector2{ .x = screenWidth / 4.0 * 3.0, .y = 80.0 },
ray.Vector2{ .x = screenWidth / 4.0 * 3.0 - 60.0, .y = 150.0 },
ray.Vector2{ .x = screenWidth / 4.0 * 3.0 + 60.0, .y = 150.0 }, ray.VIOLET);

ray.DrawTriangleLines(ray.Vector2{ .x = screenWidth / 4.0 * 3.0, .y = 160.0 },
ray.Vector2{ .x = screenWidth / 4.0 * 3.0 - 20.0, .y = 230.0 },
ray.Vector2{ .x = screenWidth / 4.0 * 3.0 + 20.0, .y = 230.0 }, ray.DARKBLUE);

// Polygon shapes and lines
ray.DrawPoly(ray.Vector2{ .x = screenWidth / 4.0 * 3, .y = 330 }, 6, 80, rotation, ray.BROWN);
ray.DrawPolyLines(ray.Vector2{ .x = screenWidth / 4.0 * 3, .y = 330 }, 6, 90, rotation, ray.BROWN);
ray.DrawPolyLinesEx(ray.Vector2{ .x = screenWidth / 4.0 * 3, .y = 330 }, 6, 85, rotation, 6, ray.BEIGE);

// NOTE: We draw all LINES based shapes together to optimize internal drawing,
// this way, all LINES are rendered in a single draw pass
ray.DrawLine(18, 42, screenWidth - 18, 42, ray.BLACK);
ray.EndDrawing();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
ray.CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

//------------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------------
inline fn toFloat(value: i32) f32 { return @as(f32, @floatFromInt(value));}
inline fn toInt(value: f32) i32 { return @as(i32, @intFromFloat(value));}
inline fn toU8(value: c_int) u8 { return @as(u8, @intCast(value));}
inline fn fmt(comptime format: []const u8, args: anytype) []u8 { return std.fmt.allocPrint(std.heap.page_allocator, format, args) catch unreachable; }
inline fn fmtC(comptime format: []const u8, args: anytype) [:0]u8 { return std.fmt.allocPrintZ(std.heap.page_allocator, format, args) catch unreachable; }

var cwd: []u8 = undefined;
inline fn getCwd() []u8 { return std.process.getCwdAlloc(std.heap.page_allocator) catch unreachable; }
inline fn getPath(folder: []const u8, file: []const u8) [*]const u8 {
if (cwd.len == 0) cwd = getCwd();
std.fs.cwd().access(folder, .{ .mode = std.fs.File.OpenMode.read_only }) catch {
return fmt("{s}/{s}", .{ cwd, file} ).ptr;
};
return fmt("{s}/{s}/{s}", .{ cwd, folder, file} ).ptr;
}
114 changes: 114 additions & 0 deletions projects/zig_raylib_examples/shapes/shapes_bouncing_ball.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//!zig-autodoc-section: shapes_bouncing_ball.Main
//! raylib_examples/shapes_bouncing_ball.zig
//! Example - automation events.
//!
//! raylib Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
//! Zig port created by DarknessFX | https://dfx.lv | X @DrkFX

// /*******************************************************************************************
// *
// * raylib [shapes] example - bouncing ball
// *
// * Example originally created with raylib 2.5, last time updated with raylib 2.5
// *
// * 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) 2013-2024 Ramon Santamaria (@raysan5)
// *
// ********************************************************************************************/

const std = @import("std");

// NOTE ABOUT VSCODE + ZLS:
// Use full path for all cIncludes:
// @cInclude("C:/raylib_examples/lib/raylib.h");
const ray = @cImport({
@cInclude("raylib.h");
});

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
pub fn main() !u8 {
// Initialization
//---------------------------------------------------------
const screenWidth: f32 = 800.0;
const screenHeight: f32 = 450.0;

ray.SetConfigFlags(ray.FLAG_MSAA_4X_HINT);
ray.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");

const getScreenWidth: f32 = @floatFromInt(ray.GetScreenWidth());
const getScreenHeight: f32 = @floatFromInt(ray.GetScreenHeight());

var ballPosition = ray.Vector2{ .x = getScreenWidth / 2.0, .y = getScreenHeight / 2.0 };
var ballSpeed = ray.Vector2{ .x = 5.0, .y = 4.0 };
const ballRadius = 20;

var pause = false;
var framesCounter: c_int = 0;

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

// Main game loop
while (!ray.WindowShouldClose()) { // Detect window close button or ESC key
// Update
//-----------------------------------------------------
if (ray.IsKeyPressed(ray.KEY_SPACE)) pause = !pause;

if (!pause) {
ballPosition.x += ballSpeed.x;
ballPosition.y += ballSpeed.y;

// Check walls collision for bouncing
if ((ballPosition.x >= getScreenWidth - ballRadius) or (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0;
if ((ballPosition.y >= getScreenHeight - ballRadius) or (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0;
} else framesCounter += 1;
//-----------------------------------------------------

// Draw
//-----------------------------------------------------
ray.BeginDrawing();

ray.ClearBackground(ray.RAYWHITE);

ray.DrawCircleV(ballPosition, ballRadius, ray.MAROON);
ray.DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, ray.GetScreenHeight() - 25, 20, ray.LIGHTGRAY);

// On pause, we draw a blinking message
if (pause and (@mod(@divExact(@as(f32, @floatFromInt(framesCounter)), 30.0), 2)) != 0) ray.DrawText("PAUSED", 350, 200, 30, ray.GRAY);

ray.DrawFPS(10, 10);

ray.EndDrawing();
//-----------------------------------------------------
}

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

return 0;
}

//------------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------------
inline fn toFloat(value: i32) f32 { return @as(f32, @floatFromInt(value));}
inline fn toInt(value: f32) i32 { return @as(i32, @intFromFloat(value));}
inline fn toU8(value: c_int) u8 { return @as(u8, @intCast(value));}
inline fn fmt(comptime format: []const u8, args: anytype) []u8 { return std.fmt.allocPrint(std.heap.page_allocator, format, args) catch unreachable; }
inline fn fmtC(comptime format: []const u8, args: anytype) [:0]u8 { return std.fmt.allocPrintZ(std.heap.page_allocator, format, args) catch unreachable; }

var cwd: []u8 = undefined;
inline fn getCwd() []u8 { return std.process.getCwdAlloc(std.heap.page_allocator) catch unreachable; }
inline fn getPath(folder: []const u8, file: []const u8) [*]const u8 {
if (cwd.len == 0) cwd = getCwd();
std.fs.cwd().access(folder, .{ .mode = std.fs.File.OpenMode.read_only }) catch {
return fmt("{s}/{s}", .{ cwd, file} ).ptr;
};
return fmt("{s}/{s}/{s}", .{ cwd, folder, file} ).ptr;
}
Loading

0 comments on commit 4b52cd3

Please sign in to comment.