Clean up build.zig and examples

This commit affects the build.zig and examples files. It reformats the
code to meet a 120 column limit. It also adjusts comments so that
there is a space after the comment symbol '//' and the grammar in the
comments has been fixed.
This commit is contained in:
vent 2023-09-11 16:04:32 +01:00
parent 4dbbea186e
commit a378960088
9 changed files with 219 additions and 71 deletions

142
build.zig
View File

@ -10,7 +10,12 @@ const Program = struct {
desc: []const u8, desc: []const u8,
}; };
pub fn link(b: *std.Build, exe: *std.Build.Step.Compile, target: std.zig.CrossTarget, optimize: std.builtin.Mode) void { pub fn link(
b: *std.Build,
exe: *std.Build.Step.Compile,
target: std.zig.CrossTarget,
optimize: std.builtin.Mode,
) void {
const raylib = b.dependency("raylib", .{ const raylib = b.dependency("raylib", .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
@ -44,12 +49,10 @@ pub fn link(b: *std.Build, exe: *std.Build.Step.Compile, target: std.zig.CrossTa
exe.linkSystemLibrary("Xcursor"); exe.linkSystemLibrary("Xcursor");
}, },
.emscripten, .wasi => { .emscripten, .wasi => {
//when using emscripten, // When using emscripten, the libries don't need to be linked
// the libries don't need to be linked // because emscripten is going to do that later.
// because emscripten is going
// to do that later.
}, },
else => { // linux and possibly others else => { // Linux and possibly others.
exe.linkSystemLibrary("GL"); exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("rt"); exe.linkSystemLibrary("rt");
exe.linkSystemLibrary("dl"); exe.linkSystemLibrary("dl");
@ -61,7 +64,11 @@ pub fn link(b: *std.Build, exe: *std.Build.Step.Compile, target: std.zig.CrossTa
exe.linkLibrary(art); exe.linkLibrary(art);
} }
pub fn getArtifact(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.Mode) *std.Build.Step.Compile { pub fn getArtifact(
b: *std.Build,
target: std.zig.CrossTarget,
optimize: std.builtin.Mode,
) *std.Build.Step.Compile {
const raylib = b.dependency("raylib", .{ const raylib = b.dependency("raylib", .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
@ -70,7 +77,7 @@ pub fn getArtifact(b: *std.Build, target: std.zig.CrossTarget, optimize: std.bui
return raylib.artifact("raylib"); return raylib.artifact("raylib");
} }
//TODO: make these not comptime // TODO: Make these not comptime.
pub fn getModule(b: *std.Build, comptime rl_path: []const u8) *std.Build.Module { pub fn getModule(b: *std.Build, comptime rl_path: []const u8) *std.Build.Module {
if (b.modules.contains("raylib")) { if (b.modules.contains("raylib")) {
return b.modules.get("raylib").?; return b.modules.get("raylib").?;
@ -88,12 +95,18 @@ pub fn getModuleInternal(b: *std.Build) *std.Build.Module {
pub const math = struct { pub const math = struct {
pub fn getModule(b: *std.Build, comptime rl_path: []const u8) *std.Build.Module { pub fn getModule(b: *std.Build, comptime rl_path: []const u8) *std.Build.Module {
var raylib = rl.getModule(b, rl_path); var raylib = rl.getModule(b, rl_path);
return b.addModule("raylib-math", .{ .source_file = .{ .path = rl_path ++ "/lib/raylib-zig-math.zig" }, .dependencies = &.{.{ .name = "raylib-zig", .module = raylib }} }); return b.addModule("raylib-math", .{
.source_file = .{ .path = rl_path ++ "/lib/raylib-zig-math.zig" },
.dependencies = &.{.{ .name = "raylib-zig", .module = raylib }},
});
} }
fn getModuleInternal(b: *std.Build) *std.Build.Module { fn getModuleInternal(b: *std.Build) *std.Build.Module {
var raylib = rl.getModuleInternal(b); var raylib = rl.getModuleInternal(b);
return b.addModule("raylib-math", .{ .source_file = .{ .path = "lib/raylib-zig-math.zig" }, .dependencies = &.{.{ .name = "raylib-zig", .module = raylib }} }); return b.addModule("raylib-math", .{
.source_file = .{ .path = "lib/raylib-zig-math.zig" },
.dependencies = &.{.{ .name = "raylib-zig", .module = raylib }},
});
} }
}; };
@ -177,7 +190,9 @@ pub fn build(b: *std.Build) !void {
exe_lib.addModule("raylib", raylib); exe_lib.addModule("raylib", raylib);
exe_lib.addModule("raylib-math", raylib_math); exe_lib.addModule("raylib-math", raylib_math);
const raylib_artifact = getArtifact(b, target, optimize); const raylib_artifact = getArtifact(b, target, optimize);
// Note that raylib itself isn't actually added to the exe_lib output file, so it also needs to be linked with emscripten.
// Note that raylib itself isn't actually added to the exe_lib
// output file, so it also needs to be linked with emscripten.
exe_lib.linkLibrary(raylib_artifact); exe_lib.linkLibrary(raylib_artifact);
const link_step = try linkWithEmscripten(b, &[_]*std.Build.Step.Compile{ exe_lib, raylib_artifact }); const link_step = try linkWithEmscripten(b, &[_]*std.Build.Step.Compile{ exe_lib, raylib_artifact });
link_step.addArg("--embed-file"); link_step.addArg("--embed-file");
@ -188,7 +203,12 @@ pub fn build(b: *std.Build) !void {
const run_option = b.step(ex.name, ex.desc); const run_option = b.step(ex.name, ex.desc);
run_option.dependOn(&run_step.step); run_option.dependOn(&run_step.step);
} else { } else {
const exe = b.addExecutable(.{ .name = ex.name, .root_source_file = .{ .path = ex.path }, .optimize = optimize, .target = target }); const exe = b.addExecutable(.{
.name = ex.name,
.root_source_file = .{ .path = ex.path },
.optimize = optimize,
.target = target,
});
rl.link(b, exe, target, optimize); rl.link(b, exe, target, optimize);
exe.addModule("raylib", raylib); exe.addModule("raylib", raylib);
exe.addModule("raylib-math", raylib_math); exe.addModule("raylib-math", raylib_math);
@ -203,11 +223,11 @@ pub fn build(b: *std.Build) !void {
const emccOutputDir = "zig-out" ++ std.fs.path.sep_str ++ "htmlout" ++ std.fs.path.sep_str; const emccOutputDir = "zig-out" ++ std.fs.path.sep_str ++ "htmlout" ++ std.fs.path.sep_str;
const emccOutputFile = "index.html"; const emccOutputFile = "index.html";
pub fn emscriptenRunStep(b: *std.Build) !*std.Build.Step.Run { pub fn emscriptenRunStep(b: *std.Build) !*std.Build.Step.Run {
//find emrun // Find emrun.
if (b.sysroot == null) { if (b.sysroot == null) {
@panic("Pass '--sysroot \"[path to emsdk installation]/upstream/emscripten\"'"); @panic("Pass '--sysroot \"[path to emsdk installation]/upstream/emscripten\"'");
} }
//If compiling on windows , use emrun.bat // If compiling on windows , use emrun.bat.
const emrunExe = switch (builtin.os.tag) { const emrunExe = switch (builtin.os.tag) {
.windows => "emrun.bat", .windows => "emrun.bat",
else => "emrun", else => "emrun",
@ -221,36 +241,56 @@ pub fn emscriptenRunStep(b: *std.Build) !*std.Build.Step.Run {
return run_cmd; return run_cmd;
} }
//Creates the static library to build a project for Emscripten // Creates the static library to build a project for Emscripten.
pub fn compileForEmscripten(b: *std.Build, name: []const u8, root_source_file: []const u8, target: std.zig.CrossTarget, optimize: std.builtin.Mode) *std.Build.Step.Compile { pub fn compileForEmscripten(
// TODO: It might be a good idea to create a custom compile step, b: *std.Build,
// that does both the compile to static library and the link with emcc name: []const u8,
// By overidding the make function of the step, root_source_file: []const u8,
// However it might also be a bad idea since it messes with the build system itself. target: std.zig.CrossTarget,
optimize: std.builtin.Mode,
) *std.Build.Step.Compile {
// TODO: It might be a good idea to create a custom compile step, that does
// both the compile to static library and the link with emcc by overidding
// the make function of the step. However it might also be a bad idea since
// it messes with the build system itself.
const new_target = updateTargetForWeb(target); const new_target = updateTargetForWeb(target);
//the project is built as a library and linked later // The project is built as a library and linked later.
const exe_lib = b.addStaticLibrary(.{ .name = name, .root_source_file = .{ .path = root_source_file }, .target = new_target, .optimize = optimize }); const exe_lib = b.addStaticLibrary(.{
.name = name,
.root_source_file = .{ .path = root_source_file },
.target = new_target,
.optimize = optimize,
});
//There are some symbols that need to be defined in C. // There are some symbols that need to be defined in C.
const webhack_c_file_step = b.addWriteFiles(); const webhack_c_file_step = b.addWriteFiles();
const webhack_c_file = webhack_c_file_step.add("webhack.c", webhack_c); const webhack_c_file = webhack_c_file_step.add("webhack.c", webhack_c);
exe_lib.addCSourceFile(.{ .file = webhack_c_file, .flags = &[_][]u8{} }); exe_lib.addCSourceFile(.{ .file = webhack_c_file, .flags = &[_][]u8{} });
//Since it's creating a static library, the symbols raylib uses to webgl and glfw don't need to be linked by emscripten yet. // Since it's creating a static library, the symbols raylib uses to webgl
// and glfw don't need to be linked by emscripten yet.
exe_lib.step.dependOn(&webhack_c_file_step.step); exe_lib.step.dependOn(&webhack_c_file_step.step);
return exe_lib; return exe_lib;
} }
//links a set of items together using emscripten. // Links a set of items together using emscripten.
// Will accept objects and static libraries as items to link //
// As for files to include, it is recomended to have a single resources directory and just pass the entire directory // Will accept objects and static libraries as items to link. As for files to
// instead of passing every file individually. The entire path given will be the path to read the file within the program. // include, it is recomended to have a single resources directory and just pass
// So, if "resources/image.png" is passed, your program will use "resources/image.png" as the path to load the file. // the entire directory instead of passing every file individually. The entire
// TODO: test if shared libraries are accepted, I don't remember if emcc can link a shared library with a project or not // path given will be the path to read the file within the program. So, if
// TODO: add a parameter that allows a custom output directory // "resources/image.png" is passed, your program will use "resources/image.png"
pub fn linkWithEmscripten(b: *std.Build, itemsToLink: []const *std.Build.Step.Compile) !*std.Build.Step.Run { // as the path to load the file.
//Raylib uses --sysroot in order to find emscripten, so do the same here //
// TODO: Test if shared libraries are accepted, I don't remember if emcc can
// link a shared library with a project or not.
// TODO: Add a parameter that allows a custom output directory.
pub fn linkWithEmscripten(
b: *std.Build,
itemsToLink: []const *std.Build.Step.Compile,
) !*std.Build.Step.Run {
// Raylib uses --sysroot in order to find emscripten, so do the same here.
if (b.sysroot == null) { if (b.sysroot == null) {
@panic("Pass '--sysroot \"[path to emsdk installation]/upstream/emscripten\"'"); @panic("Pass '--sysroot \"[path to emsdk installation]/upstream/emscripten\"'");
} }
@ -261,26 +301,39 @@ pub fn linkWithEmscripten(b: *std.Build, itemsToLink: []const *std.Build.Step.Co
var emcc_run_arg = try b.allocator.alloc(u8, b.sysroot.?.len + emccExe.len + 1); var emcc_run_arg = try b.allocator.alloc(u8, b.sysroot.?.len + emccExe.len + 1);
defer b.allocator.free(emcc_run_arg); defer b.allocator.free(emcc_run_arg);
emcc_run_arg = try std.fmt.bufPrint(emcc_run_arg, "{s}" ++ std.fs.path.sep_str ++ "{s}", .{ b.sysroot.?, emccExe }); emcc_run_arg = try std.fmt.bufPrint(
emcc_run_arg,
"{s}" ++ std.fs.path.sep_str ++ "{s}",
.{ b.sysroot.?, emccExe },
);
//create the output directory because emcc can't do it. // Create the output directory because emcc can't do it.
const mkdir_command = b.addSystemCommand(&[_][]const u8{ "mkdir", "-p", emccOutputDir }); const mkdir_command = b.addSystemCommand(&[_][]const u8{ "mkdir", "-p", emccOutputDir });
//Actually link everything together
// Actually link everything together.
const emcc_command = b.addSystemCommand(&[_][]const u8{emcc_run_arg}); const emcc_command = b.addSystemCommand(&[_][]const u8{emcc_run_arg});
for (itemsToLink) |item| { for (itemsToLink) |item| {
emcc_command.addFileArg(item.getEmittedBin()); emcc_command.addFileArg(item.getEmittedBin());
emcc_command.step.dependOn(&item.step); emcc_command.step.dependOn(&item.step);
} }
//This puts the file in zig-out/htmlout/index.html // This puts the file in zig-out/htmlout/index.html.
emcc_command.step.dependOn(&mkdir_command.step); emcc_command.step.dependOn(&mkdir_command.step);
emcc_command.addArgs(&[_][]const u8{ "-o", emccOutputDir ++ emccOutputFile, "-sFULL-ES3=1", "-sUSE_GLFW=3", "-sASYNCIFY", "-O3", "--emrun" }); emcc_command.addArgs(&[_][]const u8{
"-o",
emccOutputDir ++ emccOutputFile,
"-sFULL-ES3=1",
"-sUSE_GLFW=3",
"-sASYNCIFY",
"-O3",
"--emrun",
});
return emcc_command; return emcc_command;
} }
//TODO: see if zig's standard library already has somehing like this // TODO: See if zig's standard library already has somehing like this.
fn lastIndexOf(string: []const u8, character: u8) usize { fn lastIndexOf(string: []const u8, character: u8) usize {
//interestingly, zig has no nice way of iterating a slice backwards // Interestingly, Zig has no nice way of iterating a slice backwards.
for (0..string.len) |i| { for (0..string.len) |i| {
const index = string.len - i - 1; const index = string.len - i - 1;
if (string[index] == character) return index; if (string[index] == character) return index;
@ -288,11 +341,12 @@ fn lastIndexOf(string: []const u8, character: u8) usize {
return string.len - 1; return string.len - 1;
} }
// TODO: each zig update, remove this and see if everything still works. // TODO: each zig update, remove this and see if everything still works.
// https://github.com/ziglang/zig/issues/16776 is where the issue is submitted // https://github.com/ziglang/zig/issues/16776 is where the issue is submitted.
fn updateTargetForWeb(target: std.zig.CrossTarget) std.zig.CrossTarget { fn updateTargetForWeb(target: std.zig.CrossTarget) std.zig.CrossTarget {
//zig building to emscripten doesn't work, because the zig standard library is missing some things in the C system. // Zig building to emscripten doesn't work, because the Zig standard library
// "std/c.zig" is missing fd_t, which causes compilation to fail. // is missing some things in the C system. "std/c.zig" is missing fd_t,
// So build to wasi instead, until it gets fixed. // which causes compilation to fail. So build to wasi instead, until it gets
// fixed.
return std.zig.CrossTarget{ return std.zig.CrossTarget{
.cpu_arch = target.cpu_arch, .cpu_arch = target.cpu_arch,
.cpu_model = target.cpu_model, .cpu_model = target.cpu_model,

View File

@ -28,7 +28,12 @@ pub fn main() anyerror!void {
spacing += @as(i32, @intFromFloat(buildings[i].width)); spacing += @as(i32, @intFromFloat(buildings[i].width));
buildColors[i] = rl.Color.init(@as(u8, @intCast(rl.getRandomValue(200, 240))), @as(u8, @intCast(rl.getRandomValue(200, 240))), @as(u8, @intCast(rl.getRandomValue(200, 250))), 255); buildColors[i] = rl.Color.init(
@as(u8, @intCast(rl.getRandomValue(200, 240))),
@as(u8, @intCast(rl.getRandomValue(200, 240))),
@as(u8, @intCast(rl.getRandomValue(200, 250))),
255,
);
} }
var camera = rl.Camera2D{ var camera = rl.Camera2D{
@ -97,8 +102,20 @@ pub fn main() anyerror!void {
rl.drawRectangleRec(player, rl.Color.red); rl.drawRectangleRec(player, rl.Color.red);
rl.drawLine(@as(i32, @intFromFloat(camera.target.x)), -screenHeight * 10, @as(i32, @intFromFloat(camera.target.x)), screenHeight * 10, rl.Color.green); rl.drawLine(
rl.drawLine(-screenWidth * 10, @as(i32, @intFromFloat(camera.target.y)), screenWidth * 10, @as(i32, @intFromFloat(camera.target.y)), rl.Color.green); @as(i32, @intFromFloat(camera.target.x)),
-screenHeight * 10,
@as(i32, @intFromFloat(camera.target.x)),
screenHeight * 10,
rl.Color.green,
);
rl.drawLine(
-screenWidth * 10,
@as(i32, @intFromFloat(camera.target.y)),
screenWidth * 10,
@as(i32, @intFromFloat(camera.target.y)),
rl.Color.green,
);
} }
rl.drawText("SCREEN AREA", 640, 10, 20, rl.Color.red); rl.drawText("SCREEN AREA", 640, 10, 20, rl.Color.red);

View File

@ -27,8 +27,17 @@ pub fn main() anyerror!void {
for (0..heights.len) |i| { for (0..heights.len) |i| {
heights[i] = @as(f32, @floatFromInt(rl.getRandomValue(1, 12))); heights[i] = @as(f32, @floatFromInt(rl.getRandomValue(1, 12)));
positions[i] = rl.Vector3.init(@as(f32, @floatFromInt(rl.getRandomValue(-15, 15))), heights[i] / 2.0, @as(f32, @floatFromInt(rl.getRandomValue(-15, 15)))); positions[i] = rl.Vector3.init(
colors[i] = rl.Color.init(@as(u8, @intCast(rl.getRandomValue(20, 255))), @as(u8, @intCast(rl.getRandomValue(10, 55))), 30, 255); @as(f32, @floatFromInt(rl.getRandomValue(-15, 15))),
heights[i] / 2.0,
@as(f32, @floatFromInt(rl.getRandomValue(-15, 15))),
);
colors[i] = rl.Color.init(
@as(u8, @intCast(rl.getRandomValue(20, 255))),
@as(u8, @intCast(rl.getRandomValue(10, 55))),
30,
255,
);
} }
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second

View File

@ -36,7 +36,13 @@ pub fn main() anyerror!void {
rl.drawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, rl.Color.gray); rl.drawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, rl.Color.gray);
rl.drawText(rl.textFormat("Box position Y: %03i", .{@as(i32, @intFromFloat(boxPositionY))}), 10, 40, 20, rl.Color.light_gray); rl.drawText(
rl.textFormat("Box position Y: %03i", .{@as(i32, @intFromFloat(boxPositionY))}),
10,
40,
20,
rl.Color.light_gray,
);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
} }

View File

@ -69,7 +69,13 @@ pub fn main() anyerror!void {
// Draw circle and touch index number // Draw circle and touch index number
rl.drawCircleV(touchPosition, 34, rl.Color.orange); rl.drawCircleV(touchPosition, 34, rl.Color.orange);
rl.drawText(rl.textFormat("%d", .{i}), @as(i32, @intFromFloat(touchPosition.x)) - 10, @as(i32, @intFromFloat(touchPosition.y)) - 70, 40, rl.Color.black); rl.drawText(
rl.textFormat("%d", .{i}),
@as(i32, @intFromFloat(touchPosition.x)) - 10,
@as(i32, @intFromFloat(touchPosition.y)) - 70,
40,
rl.Color.black,
);
} }
} }

View File

@ -28,7 +28,7 @@ pub fn main() anyerror!void {
var model = LoadModel(resourceDir ++ "models/castle.obj"); // Load model var model = LoadModel(resourceDir ++ "models/castle.obj"); // Load model
var texture = LoadTexture(resourceDir ++ "models/castle_diffuse.png"); // Load model texture var texture = LoadTexture(resourceDir ++ "models/castle_diffuse.png"); // Load model texture
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set map diffuse texture model.materials[0].maps[@intFromEnum(MAP_DIFFUSE)].texture = texture; // Set map diffuse texture
var position = Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }; // Set model position var position = Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }; // Set model position
@ -64,7 +64,8 @@ pub fn main() anyerror!void {
{ {
UnloadModel(model); // Unload previous model UnloadModel(model); // Unload previous model
model = LoadModel(droppedFiles[0]); // Load new model model = LoadModel(droppedFiles[0]); // Load new model
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set current map diffuse texture // Set current map diffuse texture
model.materials[0].maps[@intFromEnum(MAP_DIFFUSE)].texture = texture;
bounds = MeshBoundingBox(model.meshes[0]); bounds = MeshBoundingBox(model.meshes[0]);
@ -74,7 +75,7 @@ pub fn main() anyerror!void {
// Unload current model texture and load new one // Unload current model texture and load new one
UnloadTexture(texture); UnloadTexture(texture);
texture = LoadTexture(droppedFiles[0]); texture = LoadTexture(droppedFiles[0]);
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; model.materials[0].maps[@intFromEnum(MAP_DIFFUSE)].texture = texture;
} }
} }

View File

@ -44,9 +44,9 @@ pub fn main() !void {
const texture = LoadTexture(resourceDir ++ "texel_checker.png"); const texture = LoadTexture(resourceDir ++ "texel_checker.png");
// Assign texture to default model material // Assign texture to default model material
modelA.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; modelA.materials[0].maps[@intFromEnum(MAP_DIFFUSE)].texture = texture;
modelB.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; modelB.materials[0].maps[@intFromEnum(MAP_DIFFUSE)].texture = texture;
modelC.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; modelC.materials[0].maps[@intFromEnum(MAP_DIFFUSE)].texture = texture;
var shader = LoadShader( var shader = LoadShader(
resourceDir ++ "/shaders/glsl330/base_lighting.vs", resourceDir ++ "/shaders/glsl330/base_lighting.vs",
@ -54,13 +54,13 @@ pub fn main() !void {
); );
// Get some shader loactions // Get some shader loactions
shader.locs[@enumToInt(ShaderLocationIndex.SHADER_LOC_MATRIX_MODEL)] = GetShaderLocation(shader, "matModel"); shader.locs[@intFromEnum(ShaderLocationIndex.SHADER_LOC_MATRIX_MODEL)] = GetShaderLocation(shader, "matModel");
shader.locs[@enumToInt(ShaderLocationIndex.SHADER_LOC_VECTOR_VIEW)] = GetShaderLocation(shader, "viewPos"); shader.locs[@intFromEnum(ShaderLocationIndex.SHADER_LOC_VECTOR_VIEW)] = GetShaderLocation(shader, "viewPos");
// ambient light level // ambient light level
const ambientLoc = GetShaderLocation(shader, "ambient"); const ambientLoc = GetShaderLocation(shader, "ambient");
const ambientVals = [4]f32{ 0.2, 0.2, 0.2, 1.0 }; const ambientVals = [4]f32{ 0.2, 0.2, 0.2, 1.0 };
SetShaderValue(shader, ambientLoc, &ambientVals, @enumToInt(ShaderUniformDataType.SHADER_UNIFORM_VEC4)); SetShaderValue(shader, ambientLoc, &ambientVals, @intFromEnum(ShaderUniformDataType.SHADER_UNIFORM_VEC4));
var angle: f32 = 6.282; var angle: f32 = 6.282;
@ -126,7 +126,12 @@ pub fn main() !void {
// Update the light shader with the camera view position // Update the light shader with the camera view position
const cameraPos = [3]f32{ camera.position.x, camera.position.y, camera.position.z }; const cameraPos = [3]f32{ camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(shader, shader.locs[@enumToInt(ShaderLocationIndex.SHADER_LOC_VECTOR_VIEW)], &cameraPos, @enumToInt(ShaderUniformDataType.SHADER_UNIFORM_VEC3)); SetShaderValue(
shader,
shader.locs[@intFromEnum(ShaderLocationIndex.SHADER_LOC_VECTOR_VIEW)],
&cameraPos,
@intFromEnum(ShaderUniformDataType.SHADER_UNIFORM_VEC3),
);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw

View File

@ -23,7 +23,10 @@ pub fn main() anyerror!void {
var outlineSize: f32 = 2.0; var outlineSize: f32 = 2.0;
const outlineColor = [4]f32{ 1.0, 0.0, 0.0, 1.0 }; // Normalized RED color const outlineColor = [4]f32{ 1.0, 0.0, 0.0, 1.0 }; // Normalized RED color
const textureSize = rl.Vector2.init(@as(f32, @floatFromInt(texture.width)), @as(f32, @floatFromInt(texture.height))); const textureSize = rl.Vector2.init(
@as(f32, @floatFromInt(texture.width)),
@as(f32, @floatFromInt(texture.height)),
);
// Get shader locations // Get shader locations
const outlineSizeLoc = rl.getShaderLocation(shdrOutline, "outlineSize"); const outlineSizeLoc = rl.getShaderLocation(shdrOutline, "outlineSize");
@ -31,9 +34,24 @@ pub fn main() anyerror!void {
const textureSizeLoc = rl.getShaderLocation(shdrOutline, "textureSize"); const textureSizeLoc = rl.getShaderLocation(shdrOutline, "textureSize");
// Set shader values (they can be changed later) // Set shader values (they can be changed later)
rl.setShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, @intFromEnum(rl.ShaderUniformDataType.shader_uniform_float)); rl.setShaderValue(
rl.setShaderValue(shdrOutline, outlineColorLoc, &outlineColor, @intFromEnum(rl.ShaderUniformDataType.shader_uniform_vec4)); shdrOutline,
rl.setShaderValue(shdrOutline, textureSizeLoc, &textureSize, @intFromEnum(rl.ShaderUniformDataType.shader_uniform_vec2)); outlineSizeLoc,
&outlineSize,
@intFromEnum(rl.ShaderUniformDataType.shader_uniform_float),
);
rl.setShaderValue(
shdrOutline,
outlineColorLoc,
&outlineColor,
@intFromEnum(rl.ShaderUniformDataType.shader_uniform_vec4),
);
rl.setShaderValue(
shdrOutline,
textureSizeLoc,
&textureSize,
@intFromEnum(rl.ShaderUniformDataType.shader_uniform_vec2),
);
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -45,7 +63,12 @@ pub fn main() anyerror!void {
outlineSize += rl.getMouseWheelMove(); outlineSize += rl.getMouseWheelMove();
if (outlineSize < 1.0) outlineSize = 1.0; if (outlineSize < 1.0) outlineSize = 1.0;
rl.setShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, @intFromEnum(rl.ShaderUniformDataType.shader_uniform_float)); rl.setShaderValue(
shdrOutline,
outlineSizeLoc,
&outlineSize,
@intFromEnum(rl.ShaderUniformDataType.shader_uniform_float),
);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -59,12 +82,22 @@ pub fn main() anyerror!void {
rl.beginShaderMode(shdrOutline); rl.beginShaderMode(shdrOutline);
defer rl.endShaderMode(); defer rl.endShaderMode();
texture.draw(@divFloor(rl.getScreenWidth(), 2) - @divFloor(texture.width, 2), -30, rl.Color.white); texture.draw(
@divFloor(rl.getScreenWidth(), 2) - @divFloor(texture.width, 2),
-30,
rl.Color.white,
);
} }
rl.drawText("Shader-based\ntexture\noutline", 10, 10, 20, rl.Color.gray); rl.drawText("Shader-based\ntexture\noutline", 10, 10, 20, rl.Color.gray);
rl.drawText(rl.textFormat("Outline size: %i px", .{@as(i32, @intFromFloat(outlineSize))}), 10, 120, 20, rl.Color.maroon); rl.drawText(
rl.textFormat("Outline size: %i px", .{@as(i32, @intFromFloat(outlineSize))}),
10,
120,
20,
rl.Color.maroon,
);
rl.drawFPS(710, 10); rl.drawFPS(710, 10);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------

View File

@ -21,7 +21,12 @@ pub fn main() anyerror!void {
defer rl.unloadTexture(scarfy); // Texture unloading defer rl.unloadTexture(scarfy); // Texture unloading
const position = rl.Vector2.init(350.0, 280.0); const position = rl.Vector2.init(350.0, 280.0);
var frameRec = rl.Rectangle.init(0, 0, @as(f32, @floatFromInt(@divFloor(scarfy.width, 6))), @as(f32, @floatFromInt(scarfy.height))); var frameRec = rl.Rectangle.init(
0,
0,
@as(f32, @floatFromInt(@divFloor(scarfy.width, 6))),
@as(f32, @floatFromInt(scarfy.height)),
);
var currentFrame: u8 = 0; var currentFrame: u8 = 0;
var framesCounter: u8 = 0; var framesCounter: u8 = 0;
@ -69,7 +74,13 @@ pub fn main() anyerror!void {
rl.drawTexture(scarfy, 15, 40, rl.Color.white); rl.drawTexture(scarfy, 15, 40, rl.Color.white);
rl.drawRectangleLines(15, 40, scarfy.width, scarfy.height, rl.Color.lime); rl.drawRectangleLines(15, 40, scarfy.width, scarfy.height, rl.Color.lime);
rl.drawRectangleLines(15 + @as(i32, @intFromFloat(frameRec.x)), 40 + @as(i32, @intFromFloat(frameRec.y)), @as(i32, @intFromFloat(frameRec.width)), @as(i32, @intFromFloat(frameRec.height)), rl.Color.red); rl.drawRectangleLines(
15 + @as(i32, @intFromFloat(frameRec.x)),
40 + @as(i32, @intFromFloat(frameRec.y)),
@as(i32, @intFromFloat(frameRec.width)),
@as(i32, @intFromFloat(frameRec.height)),
rl.Color.red,
);
rl.drawText("FRAME SPEED: ", 165, 210, 10, rl.Color.dark_gray); rl.drawText("FRAME SPEED: ", 165, 210, 10, rl.Color.dark_gray);
rl.drawText(rl.textFormat("%02i FPS", .{framesSpeed}), 575, 210, 10, rl.Color.dark_gray); rl.drawText(rl.textFormat("%02i FPS", .{framesSpeed}), 575, 210, 10, rl.Color.dark_gray);
@ -84,7 +95,13 @@ pub fn main() anyerror!void {
scarfy.drawRec(frameRec, position, rl.Color.white); // Draw part of the texture scarfy.drawRec(frameRec, position, rl.Color.white); // Draw part of the texture
rl.drawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, rl.Color.gray); rl.drawText(
"(c) Scarfy sprite by Eiden Marsal",
screenWidth - 200,
screenHeight - 20,
10,
rl.Color.gray,
);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
} }