Skip to content

Commit

Permalink
Cleaner FFI API
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeicor committed Aug 24, 2022
1 parent 9e984c5 commit 30322d6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ SDF), and you should keep the source code or the wasm file in order to export hi

## Integrations

| Repo | Language | Library | Features | Notes |
|:----------------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------|-----------------------------------------------------------------------------------------------------------|
| [sdf-viewer](https://github.com/Yeicor/sdf-viewer/) | Rust | [Pure Rust](src/sdf/mod.rs) | Core<br/>Hierarchy<br/>Parameters | Used for the demo / feature showcase<br/>Also available as a library, see `sdf-viewer-rs-py` |
| [sdf-viewer-go](https://github.com/Yeicor/sdf-viewer-go/) | Go | [Pure Go](https://github.com/Yeicor/sdf-viewer-go/tree/main/sdf-viewer-go)<br/>[SDFX](https://github.com/deadsy/sdfx) | Core<br/>Hierarchy<br/>Parameters | May be used as a guide for implementing your own<br/>integration due to the simplicity of the Go language |
| Repo | Language | Library | Features | Notes |
|:----------------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------|------------------------------------------------------------------------------------------------------------------|
| [sdf-viewer](https://github.com/Yeicor/sdf-viewer/) | Rust | [Pure Rust](src/sdf/mod.rs) | Core<br/>Hierarchy<br/>Parameters | Used for the demo / feature showcase<br/>Available as a library (`sdfffi` feature, [usage](src/sdf/demo/ffi.rs)) |
| [sdf-viewer-go](https://github.com/Yeicor/sdf-viewer-go/) | Go | [Pure Go](https://github.com/Yeicor/sdf-viewer-go/tree/main/sdf-viewer-go)<br/>[SDFX](https://github.com/deadsy/sdfx) | Core<br/>Hierarchy<br/>Parameters | May be used as a guide for implementing your own<br/>integration due to the simplicity of the Go language |

**Feel free to create integrations for other languages and frameworks and add them to this list!**

Expand Down
8 changes: 8 additions & 0 deletions src/sdf/demo/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::sdf::demo::SDFDemo;
use crate::sdf::ffi::set_root_sdf;

/// Entrypoint: only needs to set the root SDFSurface implementation.
#[no_mangle]
pub extern "C" fn init() {
set_root_sdf(Box::new(SDFDemo::default()));
}
2 changes: 2 additions & 0 deletions src/sdf/demo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::sdf::demo::sphere::SDFDemoSphere;

pub mod cube;
pub mod sphere;
#[cfg(feature = "sdfdemoffi")]
pub mod ffi;

/// An embedded demo `Sdf` implementation to showcase/test most features. Subtracts a cube and a sphere.
#[derive(clap::Parser, Debug, Clone, PartialEq, Eq)]
Expand Down
10 changes: 2 additions & 8 deletions src/sdf/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This provides a external API for the SDF library. It matches the WebAssembly specification
//! defined at [crate::sdf::wasm].
//!
//! See [crate::sdf::demo::ffi::init] for a usage example.

use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -38,14 +40,6 @@ fn sdf_registry<R>(f: impl FnOnce(&HashMap<u32, Box<dyn SDFSurface>>) -> R) -> R
})
}

/// Start entrypoint for building the demo SDF wasm.
/// This contains all the boilerplate that users of the library would need to include in their code.
#[cfg(feature = "sdfdemoffi")]
#[no_mangle]
pub extern "C" fn init() {
set_root_sdf(Box::new(SDFDemo::default()));
}

#[no_mangle]
pub extern "C" fn bounding_box(sdf_id: u32) -> Box<[Vector3<f32>; 2]> {
Box::new(sdf_registry(|r| r.get(&sdf_id)
Expand Down

0 comments on commit 30322d6

Please sign in to comment.