sokol-zig icon indicating copy to clipboard operation
sokol-zig copied to clipboard

Fetching a file from the filesystem at runtime

Open ssteinbach opened this issue 1 month ago • 1 comments

Hi, I have a zig app that uses sokol and builds with -Dtarget=wasm32-emscripten that wants to read a json file with some data at runtime, so that I can plot graphs and what not based on it. I can see that use_filesystem is turned on, but std.fs.* doesn't seem to function. I was wondering, are there any zig examples of what the right pattern is to read a file with a known file name at runtime in a wasm build of a sokol app?

The file can be in the same directory as the html etc., doesn't need to be from the entire filesystem.

Thanks!

ssteinbach avatar Dec 09 '25 05:12 ssteinbach

There's several layers where this can go wrong:

  • I'm not sure if std.fs.* in the Zig stdlib even has support for wasm32-emscripten
  • the Emscripten filesystem emulation doesn't allow direct access to the filesystem from the browser environment, instead files must be bundled into the WASM and 'mapped' into WASM memory

If you want to load files from the web server (inclduing localhost) instead of the local filesystem, have a look at sokol_fetch.h instead:

https://github.com/floooh/sokol/blob/master/sokol_fetch.h

...the sokol-fetch bindings are included in sokol-zig, but there's no Zig example. A simple C example is here: https://github.com/floooh/sokol-samples/blob/master/sapp/loadpng-sapp.c

floooh avatar Dec 09 '25 08:12 floooh