Add Zig project example
This commit is contained in:
parent
face5c596a
commit
657499221f
82
projects/Zig/README.md
Normal file
82
projects/Zig/README.md
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
# Starting your raylib project with Zig (0.16.0)
|
||||
|
||||
## How to compile and run it
|
||||
|
||||
To compile the project:
|
||||
|
||||
```sh
|
||||
zig build
|
||||
```
|
||||
|
||||
To run the project:
|
||||
|
||||
```sh
|
||||
zig build run
|
||||
```
|
||||
|
||||
## Compile with different optimization
|
||||
|
||||
To change from debug to release build you can do it with the `-Doptimze=` flag.
|
||||
|
||||
```
|
||||
Debug
|
||||
ReleaseSafe
|
||||
ReleaseFast
|
||||
ReleaseSmall
|
||||
```
|
||||
|
||||
## Choose a different platform
|
||||
|
||||
To compile with a different platform you can use the `-Dplatform=` flag.
|
||||
Here all the options:
|
||||
|
||||
```
|
||||
glfw
|
||||
rgfw
|
||||
sdl
|
||||
sdl2
|
||||
sdl3
|
||||
memory
|
||||
win32
|
||||
drm
|
||||
android
|
||||
```
|
||||
|
||||
In this example the platform `sdl` and `sdl2` are not supported
|
||||
|
||||
Important for the android platform you also have to compile for the right target
|
||||
|
||||
## Compile for a different target
|
||||
|
||||
To compile for a different [target](https://ziglang.org/download/0.16.0/release-notes.html#Support-Table) you can use the `-Dtarget=` flag.
|
||||
Not all targets are supported
|
||||
|
||||
## Example: Compile for web and run it
|
||||
|
||||
To compile for the web we use emscripten and you run it like that:
|
||||
|
||||
```sh
|
||||
zig build -Dtarget=wasm32-emscripten
|
||||
```
|
||||
|
||||
To run it we do:
|
||||
|
||||
```sh
|
||||
zig build run -Dtarget=wasm32-emscripten
|
||||
```
|
||||
|
||||
And to make a relase build we do:
|
||||
|
||||
```sh
|
||||
zig build -Dtarget=wasm32-emscripten -Doptimize=ReleaseFast
|
||||
```
|
||||
|
||||
If we want to use rgfw for the web build we could do:
|
||||
|
||||
```sh
|
||||
zig build -Dplatform=rgfw -Dtarget=wasm32-emscripten -Doptimize=ReleaseFast
|
||||
```
|
||||
|
||||
## More Resources
|
||||
|
||||
See [Zig Build System](https://ziglang.org/learn/build-system/)
|
||||
77
projects/Zig/build.zig
Normal file
77
projects/Zig/build.zig
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
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 raylib_dep = b.dependency("raylib", .{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.platform = platform,
|
||||
});
|
||||
const raylib_artifact = raylib_dep.artifact("raylib");
|
||||
|
||||
if (platform == .sdl3) {
|
||||
if (b.lazyDependency("sdl3", .{ .optimize = optimize, .target = target })) |dep| {
|
||||
raylib_artifact.root_module.linkLibrary(dep.artifact("SDL3"));
|
||||
}
|
||||
}
|
||||
|
||||
const options = b.addOptions();
|
||||
options.addOption(rl.PlatformBackend, "platform", platform);
|
||||
|
||||
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);
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
|
||||
// web exports are completely separate
|
||||
if (target.query.os_tag == .emscripten) {
|
||||
const emsdk = rl.emsdk;
|
||||
const wasm = b.addLibrary(.{
|
||||
.name = "raylib_testing",
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
|
||||
const install_dir: std.Build.InstallDir = .{ .custom = "web" };
|
||||
const emcc_flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize });
|
||||
const emcc_settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize });
|
||||
|
||||
const emcc_step = emsdk.emccStep(b, raylib_artifact, wasm, .{
|
||||
.optimize = optimize,
|
||||
.flags = emcc_flags,
|
||||
.settings = emcc_settings,
|
||||
.shell_file_path = emsdk.shell(raylib_dep),
|
||||
.install_dir = install_dir,
|
||||
});
|
||||
b.getInstallStep().dependOn(emcc_step);
|
||||
|
||||
const html_filename = try std.fmt.allocPrint(b.allocator, "{s}.html", .{wasm.name});
|
||||
const emrun_step = emsdk.emrunStep(
|
||||
b,
|
||||
b.getInstallPath(install_dir, html_filename),
|
||||
&.{},
|
||||
);
|
||||
|
||||
emrun_step.dependOn(emcc_step);
|
||||
run_step.dependOn(emrun_step);
|
||||
} else {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "core_basic_window",
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
||||
}
|
||||
23
projects/Zig/build.zig.zon
Normal file
23
projects/Zig/build.zig.zon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
.{
|
||||
.name = .example,
|
||||
.version = "0.0.1",
|
||||
.minimum_zig_version = "0.16.0",
|
||||
.paths = .{""},
|
||||
|
||||
.dependencies = .{
|
||||
.raylib = .{
|
||||
.path = "../../",
|
||||
},
|
||||
.emsdk = .{
|
||||
.url = "git+https://github.com/emscripten-core/emsdk?ref=4.0.9#3bcf1dcd01f040f370e10fe673a092d9ed79ebb5",
|
||||
.hash = "N-V-__8AAJl1DwBezhYo_VE6f53mPVm00R-Fk28NPW7P14EQ",
|
||||
},
|
||||
.sdl3 = .{
|
||||
.url = "git+https://codeberg.org/7Games/zig-sdl3?ref=master#6d418ef3ddae99098414a96a88bf5e5fdb41785e",
|
||||
.hash = "sdl3-0.1.9-NmT1QwiEJwByePqkmArtppCHQn8Y7kiSWcncT_Mop8ie",
|
||||
.lazy = true,
|
||||
},
|
||||
},
|
||||
|
||||
.fingerprint = 0x6eec9b9f1a9d7aca,
|
||||
}
|
||||
83
projects/Zig/src/core_basic_window.c
Normal file
83
projects/Zig/src/core_basic_window.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* 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 1.3 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
#include <emscripten/emscripten.h>
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
void UpdateDrawFrame(void); // Update and Draw one frame
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//----------------------------------------------------------------------------------
|
||||
int main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||
#else
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
UpdateDrawFrame();
|
||||
}
|
||||
#endif
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
void UpdateDrawFrame(void)
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user