#build.zig for a monorepo of games

1 messages · Page 1 of 1 (latest)

mint cairn
#

I have multiple games in the same repo. This is the current structure: ```
build.zig
src/
game.zig
sdl_platform.zig
web_platform.zig
games/
foo/GameState.zig
bar/GameState.zig
common/
input.zig
canvas.zig
fonts/my_font.png


`build.zig` compiles either `sdl_platform.zig` or `web_platform.zig`; they both import `game.zig`, which acts as a common interface for all games and has some common hot reloading logic. Right now, `game.zig` does this: ```rs
pub const GameState = @import("games/foo/GameState.zig");
// pub const GameState = @import("games/bar/GameState.zig");
``` and I manually change which line is commented to change which game gets built.

This works, but is a bit clunky. I have tried three things:
- having a `build.zig` in each game folder that mostly just imports functions from `common/build_helpers.zig`. This doesn't work due to `import of file outside module path`
- doing `pub const GameState = @import("games/" ++ game_folder ++ "/GameState.zig");` with `game_folder` defined at comptime. This doesn't work since `@import operand must be a string literal`
- doing `pub const GameState = @import("GameState");` with build.zig doing `games/foo/GameState.zig` as a module and adding it as an import to `game.zig`. This does work, but then in `GameState.zig` I can't do `@embedFile(../../fonts/my_font.png)` since now it's its own module

Is there any way to get the first two solutions working? Does anyone have a better way, or an example project?
cinder matrix
#

use the third option, with an assets module that's just a single zig file that @embedFiles everything in your fonts, images, etc dirs

#

you can autogenerate that asset index file as part of the build process, but it's a little involved to get the caching to work properly - i'm actually working on a build-time library to help with that right now :)

mint cairn
cinder matrix
#

sure, I'll prolly make a #1024381264213594242 thread later today :)