Jack-Ji/jok
A minimal 2d/3d game framework for @ziglang.
4.0.3
exe.subsystem
with .Windows
in your build script.
How to start?
bash
zig fetch --save git+https://github.com/jack-ji/jok.git
build.zig
, add:
```zig
const std = @import("std");
const jok = @import("jok");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = jok.createDesktopApp(
b,
"mygame",
"src/main.zig",
target,
optimize,
.{},
);
const install_cmd = b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install_cmd.step);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(&install_cmd.step);
const run_step = b.step("run", "Run game");
run_step.dependOn(&run_cmd.step);
}
```
If you want to add emscripten support for your project, the build script needs more care:
```zig
const std = @import("std");
const jok = @import("jok");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
if (!target.result.cpu.arch.isWasm()) {
const exe = jok.createDesktopApp(
b,
"mygame",
"src/main.zig",
target,
optimize,
.{},
);
const install_cmd = b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install_cmd.step);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(&install_cmd.step);
const run_step = b.step("run", "Run game");
run_step.dependOn(&run_cmd.step);
} else {
const webapp = createWeb(
b,
"mygame",
"src/main.zig",
target,
optimize,
.{
.preload_path = "optional/relative/path/to/your/assets",
.shell_file_path = "optional/relative/path/to/your/shell",
},
);
b.getInstallStep().dependOn(&webapp.emlink.step);
const run_step = b.step("run", "Run game");
run_step.dependOn(&webapp.emrun.step);
}
}
```
.build_config/sdl.json
in your project directory:
json
{
"x86_64-windows-gnu": {
"include": "D:/SDL2-2.28.5/x86_64-w64-mingw32/include",
"libs": "D:/SDL2-2.28.5/x86_64-w64-mingw32/lib",
"bin": "D:/SDL2-2.28.5/x86_64-w64-mingw32/bin"
}
}
If you have multiple projects, you can config path to a global sdl.json
using environment variable, defaults to SDL_CONFIG_PATH
.
bash
sudo apt install libsdl2-dev
Fedora/CentOS:
bash
sudo yum install SDL2-devel
bash
brew install sdl2
src/main.zig
:
```zig
const std = @import("std");
const jok = @import("jok");
pub fn init(ctx: jok.Context) !void {
// your init code
}
pub fn event(ctx: jok.Context, e: jok.Event) !void {
// your event processing code
}
pub fn update(ctx: jok.Context) !void {
// your game state updating code
}
pub fn draw(ctx: jok.Context) !void {
// your drawing code
}
pub fn quit(ctx: jok.Context) void {
// your deinit code
}
pub fn getMemory() ?*const anyopaque {
// OPTIONAL: return memory of your game
}
pub fn reloadMemory(mem: ?*const anyopaque) void {
// OPTIONAL: restore memory of your game
}
```
Noticed yet? That's right, you don't need to write main function, jok
got your back.
The game is deemed as a separate package to jok
's runtime as a matter of fact. Your
only responsibility is to provide 5 public functions:
* init - initialize your game, run only once
* event - process events happened between frames (keyboard/mouse/controller etc)
* update - logic update between frames
* draw - render your screen here (60 fps by default)
* quit - do something before game is closed
You can customize some setup settings (window width/height, fps, debug level etc), by
defining some public constants using predefined names (they're all prefixed withjok_
).
Checkout src/config.zig
.
Most of which are still modifiable at runtime.
The other 2 optional public functions are only needed when you'are writing a plugin:
* getMemory - get memory of you plugin, called when plugin is updated
* reloadMemory - reload memory of your plugin, called when plugin is updated
For more detail of plugin's usage, check example hotreload
.
Now, compile and run your game using command zig build run
, have fun!
Please let me know if you have any issue or developed something interesting with this little framework.