-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use jolt_sys::*; | ||
|
||
pub struct BodyInterface(*mut JPC_BodyInterface); | ||
|
||
impl BodyInterface { | ||
pub(crate) fn new(inner: *mut JPC_BodyInterface) -> Self { | ||
Self(inner) | ||
} | ||
|
||
pub fn as_raw(&self) -> *mut JPC_BodyInterface { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::ffi::{c_uint, c_void}; | ||
use std::marker::PhantomData; | ||
use std::ptr; | ||
|
||
use jolt_sys::*; | ||
|
||
use crate::{BroadPhaseLayer, ObjectLayer}; | ||
|
||
pub trait IntoBroadPhaseLayerInterface { | ||
fn as_raw(&self) -> *mut JPC_BroadPhaseLayerInterface; | ||
} | ||
|
||
pub trait BroadPhaseLayerInterface: Sized { | ||
fn get_num_broad_phase_layers(&self) -> u32; | ||
fn get_broad_phase_layer(&self, layer: ObjectLayer) -> BroadPhaseLayer; | ||
|
||
fn as_raw(&self) -> *mut JPC_BroadPhaseLayerInterface { | ||
type Bridge<T> = BroadPhaseLayerInterfaceBridge<T>; | ||
|
||
let fns = JPC_BroadPhaseLayerInterfaceFns { | ||
GetNumBroadPhaseLayers: Some(Bridge::<Self>::GetNumBroadPhaseLayers as _), | ||
GetBroadPhaseLayer: Some(Bridge::<Self>::GetBroadPhaseLayer as _), | ||
}; | ||
|
||
unsafe { JPC_BroadPhaseLayerInterface_new(ptr::from_ref(self).cast::<c_void>(), fns) } | ||
} | ||
} | ||
|
||
impl IntoBroadPhaseLayerInterface for *mut JPC_BroadPhaseLayerInterface { | ||
fn as_raw(&self) -> *mut JPC_BroadPhaseLayerInterface { | ||
*self | ||
} | ||
} | ||
|
||
impl<T> IntoBroadPhaseLayerInterface for T | ||
where | ||
T: BroadPhaseLayerInterface, | ||
{ | ||
fn as_raw(&self) -> *mut JPC_BroadPhaseLayerInterface { | ||
<Self as BroadPhaseLayerInterface>::as_raw(self) | ||
} | ||
} | ||
|
||
struct BroadPhaseLayerInterfaceBridge<T> { | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
impl<T: BroadPhaseLayerInterface> BroadPhaseLayerInterfaceBridge<T> { | ||
unsafe extern "C" fn GetNumBroadPhaseLayers(this: *const c_void) -> c_uint { | ||
let this = this.cast::<T>().as_ref().unwrap(); | ||
|
||
this.get_num_broad_phase_layers() | ||
} | ||
|
||
unsafe extern "C" fn GetBroadPhaseLayer( | ||
this: *const c_void, | ||
layer: JPC_ObjectLayer, | ||
) -> JPC_BroadPhaseLayer { | ||
let this = this.cast::<T>().as_ref().unwrap(); | ||
let layer = ObjectLayer(layer); | ||
|
||
this.get_broad_phase_layer(layer).raw() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use jolt_sys::*; | ||
|
||
use crate::{BodyInterface, IntoBroadPhaseLayerInterface}; | ||
|
||
pub struct PhysicsSystem(*mut JPC_PhysicsSystem); | ||
|
||
impl PhysicsSystem { | ||
pub fn new() -> Self { | ||
unsafe { Self(JPC_PhysicsSystem_new()) } | ||
} | ||
|
||
/// # Safety | ||
/// no | ||
pub unsafe fn init( | ||
&self, | ||
max_bodies: u32, | ||
num_body_mutexes: u32, | ||
max_body_pairs: u32, | ||
max_contact_constraints: u32, | ||
broad_phase_layer_interface: impl IntoBroadPhaseLayerInterface, | ||
object_vs_broad_phase_layer_interface: *mut JPC_ObjectVsBroadPhaseLayerFilter, | ||
object_layer_pair_filter: *mut JPC_ObjectLayerPairFilter, | ||
) { | ||
unsafe { | ||
JPC_PhysicsSystem_Init( | ||
self.0, | ||
max_bodies, | ||
num_body_mutexes, | ||
max_body_pairs, | ||
max_contact_constraints, | ||
broad_phase_layer_interface.as_raw(), | ||
object_vs_broad_phase_layer_interface, | ||
object_layer_pair_filter, | ||
); | ||
} | ||
} | ||
|
||
pub fn body_interface(&self) -> BodyInterface { | ||
unsafe { BodyInterface::new(JPC_PhysicsSystem_GetBodyInterface(self.0)) } | ||
} | ||
|
||
pub fn as_raw(&self) -> *mut JPC_PhysicsSystem { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Drop for PhysicsSystem { | ||
fn drop(&mut self) { | ||
unsafe { | ||
JPC_PhysicsSystem_delete(self.0); | ||
} | ||
} | ||
} |