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

Example of how to use `sokol-zig` with `sokol_imgui.h`?

Open samrat opened this issue 3 years ago • 10 comments

Hello, thanks for this library and the many clean and well-commented examples.

Is there an example of how I can use sokol_imgui.h with the sokol-zig bindings. I started to attempt @cImport-ing the header file directly, but ran into some errors("Please include sokol_app.h before sokol_imgui.h") probably because I'm using the Zig bindings.

Do you plan on providing Zig bindings for sokol_imgui.h? Or is there a way of using sokol-zig and sokol_imgui.h?

samrat avatar Oct 15 '22 13:10 samrat

sokol_imgui.h doesn't have zig bindings yet, but I need to get back to the zig bindings anyway before the zig 0.10.0 release. Maybe I can at least get a quick and dirty version into sokol-zig (you'd still need to figure out how to bind with imgui.h though, but maybe there are already existing solutions).

floooh avatar Oct 15 '22 19:10 floooh

👋 I found some related links, I thought I would drop them here. Sorry if this is noise. (Thanks for sokol-zig, it's been a fun playground for me!)

  • https://github.com/Namek/zig-sokol-imgui
  • https://github.com/dumheter/zig-imgui-template
  • https://github.com/JonSnowbd/ZT

cbarrett avatar Nov 09 '22 18:11 cbarrett

Sorry to bump this, any updates on this thread?

llunS avatar Jun 26 '23 15:06 llunS

One more bindings project to add on the above list: https://github.com/nurpax/zig-imgui

I try to keep it up to date with sokol and zig-devel. Zig moves fast so there's been a lot of churn and I think it's once again broken against latest zig master. But should get fixed soonish.

jannehellsten avatar Jun 28 '23 15:06 jannehellsten

Sorry to bump this, any updates on this thread?

For anyone who might be interested, I ended up adding all the sokol files in my zig project. Build them with zig, and @cImport for zig to consume. No extra layer, not like how this binding does.

In short: I did it manually since Zig can use C library with sources relatively easy.

llunS avatar Jun 28 '23 16:06 llunS

One more bindings project to add on the above list: https://github.com/nurpax/zig-imgui

This is great! You might also try adding cimgui and imgui to the zon file if you look at zig-devel (master version). Avoidance of ugly git sub-modules!!! :fearful:

kassane avatar Jan 13 '24 19:01 kassane

You might also try adding cimgui and imgui to the zon file if you look at zig-devel (master version). Avoidance of ugly git sub-modules!!! :fearful:

It doesn't use git submodulea but subtree, so at least most people will know how to clone it. 😀

But it definitely should be updated for the package manager. I think I'll look into updating it when sokol zig is released as a package.

nurpax avatar Jan 15 '24 19:01 nurpax

Btw @nurpax, I was considering dropping support for the "old" way of using sokol-zig via git submodule (etc...) and importing the build.zig directly, because that could get rid of a couple of things like the optional prefix path.

Using sokol-zig would then look like this (if you don't use wasm-emscripten you can ignore the bottom half of the file, and in that case the const sokol = @import("sokol") also isn't necessary.

https://github.com/floooh/pacman.zig/blob/zig-0.12.0/build.zig

I thought that the recent breaking API changes in the zig build system would be a good opportunity to switch over to the package manager completely since people need to touch their build files anyway.

floooh avatar Jan 16 '24 18:01 floooh

I think it makes sense to drop support for the old style! IIRC in my use of sokol I have essentially duplicated the sokol build.zig on my side but I'd prefer to cut over to .zon style usage. My released zig sokol/imgui stuff is probably not used by anyone else than me so as far as I'm concerned, feel free to break the build as much ss you need. (The build breaks will continue until morale improves, as they say 😀)

nurpax avatar Jan 16 '24 19:01 nurpax

Looking forward to adding all the utils head! Like sokol_nuklear too :)

Angluca avatar Jan 30 '24 03:01 Angluca

I'd been working on trying to get a nice way of compiling ImGui + ImPlot for WASM (amongst other targets), and found that using sokol was the easiest way to do it at the moment. In case it's useful, I've made it available here https://github.com/fjebaker/zig-sokol-imgui-wasm

I used this repo's binding along with the sokol_imgui.h header and my own wrapper around imgui and implot. It works but the build is a little clunky; e.g. for WASM things you need to reach into various parts of sokol-zig's build.zig to get libraries linked against emscripten.

fjebaker avatar May 29 '24 00:05 fjebaker

I'll see if I can easily add sokol_imgui.h to the official bindings, and then cobble together a little demo outside the sokol-zig repo (the main reason I hadn't done that so far is that I didn't want to add a Dear ImGui dependency to the sokol-zig repository for a sample, but now with the Zig package manager it's easier to do this in a separate repo.

floooh avatar May 30 '24 16:05 floooh

Bah, I'm running into LazyPath problems in build.zig again.

Need to figure out how to build the sokol C library with a cimgui dependency without actually adding a cimgui dependency to sokol-zig. I thought I could simply do that by calling buildSokolLib() function in the sokol-zig build.zig file directly from a top-level build.zig, but then it doesn't find the sokol C source files since the build system thinks they are in the top-level project instead of the Zig package cache. And so far I'm failing at constructing a valid LazyPath which points into the Zig cache, but also isn't an absolute path (since that's forbidden by the build system).

floooh avatar May 30 '24 18:05 floooh

Ah, nvm, I just need to pass the sokol-dependency builder to that function...

floooh avatar May 30 '24 18:05 floooh

Hmm ok, next problem: cimgui can't be used via build.zig.zon, because it includes imgui as submodule :/

Guess I'll need to copy the cimgui sources into the sample project.

floooh avatar May 31 '24 15:05 floooh

@floooh I have cimgui and cimplot bindings for zig here https://github.com/fjebaker/zig-imgui-implot/blob/main/build.zig

This was originally only for Zig's Mach engine, but it doesn't depend on it, and thanks to Zig's lazy package manager you can just grab the cimgui and cimplot artifacts and includes in downstream projects.

Does that help?

fjebaker avatar May 31 '24 16:05 fjebaker

I have an example of using these with sokol here https://github.com/fjebaker/zig-sokol-imgui-wasm/blob/5cec07ba01df3736fab2b6c7a8da8f3d0c3f40cd/build.zig#L40-L46

The difficulty I was having was getting the emscripten functions that do the linker step for targeting WASM explicitly, but if you don't need that the build.zig script is actually quite well contained. I only needed to add the sokol_imgui.h header with a tiny sokol_imgui.c to build the sokol imgui implementation, but that could be upstreamed?

fjebaker avatar May 31 '24 16:05 fjebaker

I think I have a solution now that's somewhat flexible and decouples the sokol-zig package from the cimgui dependency (just need to write a couple of readmes and changelogs, and merge everything).

The only change to sokol-zig is a flag which instructs the buildLibSokol() to include sokol_imgui.c in the build:

https://github.com/floooh/sokol-zig/blob/c01b2c9062f72e2dc66ded6393595ae13a2dfcd2/build.zig#L276-L283

...this wouldn't compile though because the cimgui.h header cannot be found. It's the toplevel build.zig's responsibility to add a header search path to the lib_sokol compile step, this means that sokol needs to be integrated 'manually' by calling buildLibSokol() manually like this:

https://github.com/floooh/sokol-zig-imgui-sample/blob/2ef705ad811f7da5e7a8baa4ba01d0dc651565fb/build.zig#L18-L30

cimgui itself had to be included directly into the toplevel project, the package manager doesn't work with git submodules.

I haven't looked into Emscripten support yet, the initial version of the sample will only work on native platforms.

Sample code looks like this btw:

https://github.com/floooh/sokol-zig-imgui-sample/blob/main/src/main.zig

PS: will probably wrap up everything tomorrow.

floooh avatar May 31 '24 17:05 floooh

...ok I found a "package manager friendly way" to inject the cimgui header search path into the sokol C library compile step :)

https://github.com/floooh/sokol-zig-imgui-sample/blob/2a7a32b297404deadf64f8f0aea5c3d593b737a8/build.zig#L15-L19

floooh avatar May 31 '24 17:05 floooh

...and more build system goodness curtesy of @geoot: https://github.com/floooh/sokol-zig-imgui-sample/pull/1

floooh avatar Jun 01 '24 13:06 floooh

Ok, all merged now.

Sample project is here: https://github.com/floooh/sokol-zig-imgui-sample

It would be really nice to have an "official" code-generated Zig cimgui bindings package which is automatically updated when a new tag is created in the cimgui repository (don't know if that's even possible with GH Actions), and which would allow to write imgui code like this:

ig.setNextWindowSize(.{ .x = 400, .y = 100 }, .Once);

...instead of this (which is the '@cImport' version):

ig.igSetNextWindowSize(.{ .x = 400, .y = 100 }, ig.ImGuiCond_Once);

...not sure yet if I want to tackle this myself ;)

floooh avatar Jun 01 '24 14:06 floooh

Closing this ticket now since from my pov it's fixed :)

floooh avatar Jun 01 '24 14:06 floooh