kassane / sokol-d

D bindings for the sokol headers (https://github.com/floooh/sokol)
http://sokol-d.dub.pm/
zlib License
11 stars 5 forks source link

add IMGUI support #23

Closed kassane closed 5 months ago

kassane commented 5 months ago

fix #10

old branch, need long rebase... I'll delete (for testing only) https://github.com/kassane/sokol-d/tree/imgui-support

inspired by:

kassane commented 5 months ago

Need cimgui binding for run example. Old module amalgamation cimgui + sokol_imgui: https://github.com/kassane/sokol-d/blob/1d1fc586969aba82fc6b854aeb2d16f7ec656976/src/sokol/imgui.d

now, only sokol_imgui: https://github.com/kassane/sokol-d/blob/1110006dd280fca4be0a16402fc6455f1c82f494/src/examples/imgui.d#L103-L115

kassane commented 5 months ago

Some questions (for zig-dev)

What would you add cimgui to the example?

Unlike zig translate-c, importC doesn't read header, only C source. However, this still falls short (translate-c also has some issues). The only difference between the two projects is the level of support (zig project wins here).

More info - commands Zig `translate-c` has the ability to convert all the code (declaration + implementation), `importC` just the declaration (equivalent to rust-bindgen or sokol_gen). ```bash # zig $ zig translate-c -cflags -- -lc # D $ ldmd2/ldc2 -Hf= (*.di) # zig have inside clang, # ldc2 need call cc (system compiler) - like rustc. # But -gcc=(custom CC) is used only C source and -linker=(custom LD) or -link-internally (inside lld, like zig) any object (w/o C source) # and rustc equivalence is -linker= flag. ```

And not have current D module about cimgui, only old imgui (C++, no extern(C)) version.

kassane commented 5 months ago

Please, @floooh.

Test it!

Preview - Linux (native): ![image](https://github.com/kassane/sokol-d/assets/6756180/25d2f1d9-ae53-4215-a306-7881fec75093) - Wasm32/Firefox: ![image](https://github.com/kassane/sokol-d/assets/6756180/522e6f0c-00bc-429b-bf67-5da3d30de1aa)

Note: For wasm32 build change... https://github.com/kassane/sokol-d/blob/8d589cb82b9a509f561147e3feec853cc1663157/build.zig#L245

Why block? CI error. Why imgui-cpp need assert.h, but rebuild (no-clean global cache) works!

And for old ldc2 versions (1.36...1.38 [latest]) change... .freestanding to .emscripten or try ldc2-master https://github.com/kassane/sokol-d/blob/8d589cb82b9a509f561147e3feec853cc1663157/build.zig#L512-L515

floooh commented 5 months ago

Note that there was an bug in the translateC step in the cimgui sample which caused this above error about missing assert.h in imgui.h when trying to build for wasm32-emscripten. I fixed this by always running the translateC step with the host target (instead of the build target):

https://github.com/floooh/sokol-zig-imgui-sample/blob/b2fd57bc0ddc406fc3a66160fa37ca27842ad127/deps/cimgui/build.zig#L51

...otherwise I would somehow need to inject the Emscripten include path into the translateC step. Using the host triplet is the better solution though, since there no platform-specific stuff happening in the translateC step.

kassane commented 5 months ago

Note that there was an bug in the translateC step in the cimgui sample which caused this above error about missing assert.h in imgui.h when trying to build for wasm32-emscripten. I fixed this by always running the translateC step with the host target [...]

According to this solution, perhaps it would have been possible to use importC to bypass the error, just like translate-c. However, the assert.h error occurs during the libsokol build, a step before ldc2. I know there exists a step wait for emsdk_setup in libsokol and this will run imgui in a direct dependency.

https://github.com/kassane/sokol-d/blob/20fe32c375502d2d4bc709086a0fdd2027e31f0c/build.zig#L79-L82 https://github.com/kassane/sokol-d/blob/20fe32c375502d2d4bc709086a0fdd2027e31f0c/build.zig#L184-L194


Another question that puzzles me. Would it be useful to use the build.zig dependency to make sokol-zig a dependency of sokol-d? That way we could reduce the complexity redundancy of build.zig.

kassane commented 5 months ago

@floooh , my assert.h fix - if with_imgui enabled, move lib.step.dependOn(&emsdk_setup.step); to libimgui.step.dependOn(&emsdk_setup.step); https://github.com/kassane/sokol-d/commit/bca71beb160425aeecf7a76f7256f4e2ca356744

floooh commented 5 months ago

Ah btw, I fixed this a bit differently, by letting the cimgui library depend on the sokol C library (up in the top level project):

https://github.com/floooh/sokol-zig-imgui-sample/blob/87d703e2f1f43135a5a5e9bfc2a56910d48b7e7b/build.zig#L69-L73

Since the sokol C library depends on the Emscripten SDK setup this also guarantees that the SDK is setup when cimgui is compiled (e.g. the idea is that all C libraries need to depend on the sokol C library... but maybe this could indeed be moved up into sokol-zig since we're iterating over the C libraries anyway in the linker step - also the injection of the Emscripten include path... hmm...)

Another question that puzzles me. Would it be useful to use the build.zig dependency to make sokol-zig a dependency of sokol-d?

Not a fan tbh, it would probably make more sense to move the emsdk stuff into a separate project (and Zig package) which could then be used both by sokol-zig and sokol-d.

floooh commented 5 months ago

Wrote a ticket for that idea (to handle the C library stuff in sokol-zig build.zig): https://github.com/floooh/sokol-zig/issues/71

kassane commented 5 months ago

it would probably make more sense to move the emsdk stuff into a separate project (and Zig package) which could then be used both by sokol-zig and sokol-d.

Make sense. Like, https://github.com/kassane/anotherBuildStep (a.k.a abs) Also, I have not yet completed emsdk support. But in pacman.d, besides sokol-d (passing libsokol + D modules), using abs to set ldc2. Maybe ldc2/ldmd2 could be ported outside of sokol-d using this idea.