Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,73 @@ cargo run --example userdata
cargo run --example vertexpull
```

>NOTE: imgui support has been removed for now, the required cimgui submodule dependency caused trouble with
> Github Actions. We'll need to solve this some other way in the future, and in a way that works for all
> language bindings.
## Dear ImGui integration

> _The section below is LLM-generated._

sokol-rust ships bindings for `sokol_imgui.h`, `sokol_gfx_imgui.h` and
`sokol_app_imgui.h` as the crate modules `sokol::imgui`, `sokol::gfximgui`
and `sokol::appimgui`. All three are gated behind the `imgui` cargo
feature and `build.rs` does **not** compile the corresponding C stubs —
Dear ImGui (C++) is not part of the crate and must be supplied by your
project.

To use them:

1. Add `sokol` with the `imgui` feature and clone
[dcimgui](https://github.com/floooh/dcimgui) (an all-in-one Dear ImGui
+ `cimgui.h` C-API drop) into your project.

```toml
[dependencies]
sokol = { path = "path/to/sokol-rust", features = ["imgui"] }

[build-dependencies]
cc = "1"
```

2. Compile dcimgui + the sokol stub in your own `build.rs`. Use `src/`
for the regular flavour or `src-docking/` for the docking flavour:

```rust
// build.rs
fn main() {
let dcimgui = std::path::Path::new("path/to/dcimgui/src");
let sokol_c = std::path::Path::new("path/to/sokol-rust/src/sokol/c");

cc::Build::new()
.cpp(true)
.std("c++17")
.include(dcimgui)
.files([
dcimgui.join("cimgui.cpp"),
dcimgui.join("cimgui_internal.cpp"),
dcimgui.join("imgui.cpp"),
dcimgui.join("imgui_draw.cpp"),
dcimgui.join("imgui_tables.cpp"),
dcimgui.join("imgui_widgets.cpp"),
dcimgui.join("imgui_demo.cpp"),
])
.compile("imgui");

cc::Build::new()
.std("c11")
.define("IMPL", None)
.define("SOKOL_METAL", None) // match the backend sokol-gfx was built with
.include(dcimgui)
.file(sokol_c.join("sokol_imgui.c"))
.compile("sokol_imgui");
}
```

The backend define (`SOKOL_METAL`, `SOKOL_D3D11`, `SOKOL_GLCORE`,
`SOKOL_GLES3`, ...) must match the one sokol-gfx itself was built
with — otherwise the imgui renderer picks a different backend than
sokol-gfx.

3. `use sokol::imgui;` and call `imgui::setup(...)` as normal.

The same flow applies to `sokol_gfx_imgui.h` and `sokol_app_imgui.h`.

## Wasm/Emscripten
To compile for wasm, you will need the emcc compiler which you can get at https://github.com/emscripten-core/emsdk
Expand Down
103 changes: 103 additions & 0 deletions src/appimgui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! To use this module, enable the feature "imgui"
// machine generated, do not edit

#![allow(dead_code)]
#![allow(unused_imports)]

use crate::app as sapp;

/// Helper function to convert a C string to a Rust string slice
#[inline]
fn c_char_ptr_to_rust_str(c_char_ptr: *const core::ffi::c_char) -> &'static str {
let c_str = unsafe { core::ffi::CStr::from_ptr(c_char_ptr) };
c_str.to_str().expect("c_char_ptr contained invalid Utf8 Data")
}

pub mod ffi {
#![allow(unused_imports)]
use super::*;
extern "C" {
pub fn sappimgui_setup();
pub fn sappimgui_shutdown();
pub fn sappimgui_track_frame();
pub fn sappimgui_track_event(ev: *const sapp::Event);
pub fn sappimgui_draw();
pub fn sappimgui_draw_menu(title: *const core::ffi::c_char);
pub fn sappimgui_draw_hud_window_content();
pub fn sappimgui_draw_publicstate_window_content();
pub fn sappimgui_draw_event_window_content();
pub fn sappimgui_draw_hud_window(title: *const core::ffi::c_char);
pub fn sappimgui_draw_publicstate_window(title: *const core::ffi::c_char);
pub fn sappimgui_draw_event_window(title: *const core::ffi::c_char);
pub fn sappimgui_draw_hud_menu_item(label: *const core::ffi::c_char);
pub fn sappimgui_draw_publicstate_menu_item(label: *const core::ffi::c_char);
pub fn sappimgui_draw_event_menu_item(label: *const core::ffi::c_char);
}
}
#[inline]
pub fn setup() {
unsafe { ffi::sappimgui_setup() }
}
#[inline]
pub fn shutdown() {
unsafe { ffi::sappimgui_shutdown() }
}
#[inline]
pub fn track_frame() {
unsafe { ffi::sappimgui_track_frame() }
}
#[inline]
pub fn track_event(ev: &sapp::Event) {
unsafe { ffi::sappimgui_track_event(ev) }
}
#[inline]
pub fn draw() {
unsafe { ffi::sappimgui_draw() }
}
#[inline]
pub fn draw_menu(title: &str) {
let tmp_0 = std::ffi::CString::new(title).unwrap();
unsafe { ffi::sappimgui_draw_menu(tmp_0.as_ptr()) }
}
#[inline]
pub fn draw_hud_window_content() {
unsafe { ffi::sappimgui_draw_hud_window_content() }
}
#[inline]
pub fn draw_publicstate_window_content() {
unsafe { ffi::sappimgui_draw_publicstate_window_content() }
}
#[inline]
pub fn draw_event_window_content() {
unsafe { ffi::sappimgui_draw_event_window_content() }
}
#[inline]
pub fn draw_hud_window(title: &str) {
let tmp_0 = std::ffi::CString::new(title).unwrap();
unsafe { ffi::sappimgui_draw_hud_window(tmp_0.as_ptr()) }
}
#[inline]
pub fn draw_publicstate_window(title: &str) {
let tmp_0 = std::ffi::CString::new(title).unwrap();
unsafe { ffi::sappimgui_draw_publicstate_window(tmp_0.as_ptr()) }
}
#[inline]
pub fn draw_event_window(title: &str) {
let tmp_0 = std::ffi::CString::new(title).unwrap();
unsafe { ffi::sappimgui_draw_event_window(tmp_0.as_ptr()) }
}
#[inline]
pub fn draw_hud_menu_item(label: &str) {
let tmp_0 = std::ffi::CString::new(label).unwrap();
unsafe { ffi::sappimgui_draw_hud_menu_item(tmp_0.as_ptr()) }
}
#[inline]
pub fn draw_publicstate_menu_item(label: &str) {
let tmp_0 = std::ffi::CString::new(label).unwrap();
unsafe { ffi::sappimgui_draw_publicstate_menu_item(tmp_0.as_ptr()) }
}
#[inline]
pub fn draw_event_menu_item(label: &str) {
let tmp_0 = std::ffi::CString::new(label).unwrap();
unsafe { ffi::sappimgui_draw_event_menu_item(tmp_0.as_ptr()) }
}
Loading