From 30322d639f3a36200e3481eb65ddefc9558e443c Mon Sep 17 00:00:00 2001
From: Yeicor <4929005+Yeicor@users.noreply.github.com>
Date: Wed, 24 Aug 2022 20:31:23 +0200
Subject: [PATCH] Cleaner FFI API
---
README.md | 8 ++++----
src/sdf/demo/ffi.rs | 8 ++++++++
src/sdf/demo/mod.rs | 2 ++
src/sdf/ffi.rs | 10 ++--------
4 files changed, 16 insertions(+), 12 deletions(-)
create mode 100644 src/sdf/demo/ffi.rs
diff --git a/README.md b/README.md
index 80fdd5d..b5ba402 100644
--- a/README.md
+++ b/README.md
@@ -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
Hierarchy
Parameters | Used for the demo / feature showcase
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)
[SDFX](https://github.com/deadsy/sdfx) | Core
Hierarchy
Parameters | May be used as a guide for implementing your own
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
Hierarchy
Parameters | Used for the demo / feature showcase
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)
[SDFX](https://github.com/deadsy/sdfx) | Core
Hierarchy
Parameters | May be used as a guide for implementing your own
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!**
diff --git a/src/sdf/demo/ffi.rs b/src/sdf/demo/ffi.rs
new file mode 100644
index 0000000..ebcb0f8
--- /dev/null
+++ b/src/sdf/demo/ffi.rs
@@ -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()));
+}
\ No newline at end of file
diff --git a/src/sdf/demo/mod.rs b/src/sdf/demo/mod.rs
index 56c6c8d..00bee49 100644
--- a/src/sdf/demo/mod.rs
+++ b/src/sdf/demo/mod.rs
@@ -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)]
diff --git a/src/sdf/ffi.rs b/src/sdf/ffi.rs
index 73fb74b..bb19b21 100644
--- a/src/sdf/ffi.rs
+++ b/src/sdf/ffi.rs
@@ -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;
@@ -38,14 +40,6 @@ fn sdf_registry(f: impl FnOnce(&HashMap>) -> 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; 2]> {
Box::new(sdf_registry(|r| r.get(&sdf_id)