diff --git a/projects/Zig/README.md b/projects/Zig/README.md index b074e53f8..a7c24e093 100644 --- a/projects/Zig/README.md +++ b/projects/Zig/README.md @@ -77,6 +77,8 @@ If we want to use rgfw for the web build we could do: zig build -Dplatform=rgfw -Dtarget=wasm32-emscripten -Doptimize=ReleaseFast ``` +## Compiling the Zig code? Just add `-Dzig` and try out zig ;) + ## More Resources See [Zig Build System](https://ziglang.org/learn/build-system/) diff --git a/projects/Zig/build.zig b/projects/Zig/build.zig index 50d038f24..917e97b71 100644 --- a/projects/Zig/build.zig +++ b/projects/Zig/build.zig @@ -5,6 +5,7 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const platform = b.option(rl.PlatformBackend, "platform", "select the platform") orelse rl.PlatformBackend.glfw; + const zig = b.option(bool, "zig", "compile zig code") orelse false; const raylib_dep = b.dependency("raylib", .{ .target = target, @@ -19,13 +20,24 @@ pub fn build(b: *std.Build) !void { } } - const exe_mod = b.createModule(.{ - .target = target, - .optimize = optimize, - .link_libc = true, - }); - exe_mod.addCSourceFile(.{ .file = b.path("src/core_basic_window.c") }); - exe_mod.linkLibrary(raylib_artifact); + var exe_mod: *std.Build.Module = undefined; + + if (zig) { + exe_mod = b.createModule(.{ + .root_source_file = b.path("src/core_basic_window.zig"), + .target = target, + .optimize = optimize, + }); + exe_mod.addImport("raylib", raylib_dep.module("raylib")); + } else { + exe_mod = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }); + exe_mod.addCSourceFile(.{ .file = b.path("src/core_basic_window.c") }); + exe_mod.linkLibrary(raylib_artifact); + } const run_step = b.step("run", "Run the app"); diff --git a/projects/Zig/src/core_basic_window.zig b/projects/Zig/src/core_basic_window.zig new file mode 100644 index 000000000..bf69e7326 --- /dev/null +++ b/projects/Zig/src/core_basic_window.zig @@ -0,0 +1,73 @@ +//******************************************************************************************* +//* +//* raylib [core] example - Basic window (adapted for HTML5 platform) +//* +//* This example is prepared to compile for PLATFORM_WEB and PLATFORM_DESKTOP +//* As you will notice, code structure is slightly different to the other examples... +//* To compile it for PLATFORM_WEB just uncomment #define PLATFORM_WEB at beginning +//* +//* This example has been created using raylib 6.0 (www.raylib.com) +//* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +//* +//* Copyright (c) 2015 Ramon Santamaria (@raysan5) +//* Rewrite in Zig by HaxSam +//* +//******************************************************************************************* + +const rl = @import("raylib"); +const std = @import("std"); +const builtin = @import("builtin"); + +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +const screenWidth: c_int = 800; +const screenHeight: c_int = 450; + +//---------------------------------------------------------------------------------- +// Program main entry point +//---------------------------------------------------------------------------------- +pub fn main() void { + // Initialization + //-------------------------------------------------------------------------------------- + rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); + + if (builtin.os.tag == .emscripten) { + std.os.emscripten.emscripten_set_main_loop(UpdateDrawFrame, 0, 1); + } else { + 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 + { + UpdateDrawFrame(); + } + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + rl.CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- +} + +//---------------------------------------------------------------------------------- +// Module Functions Definition +//---------------------------------------------------------------------------------- +fn UpdateDrawFrame() callconv(.c) void { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + rl.BeginDrawing(); + + rl.ClearBackground(rl.RAYWHITE); + + rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LIGHTGRAY); + + rl.EndDrawing(); + //---------------------------------------------------------------------------------- +}