From ad0e50a5f70e6071a3e82863a345b6cbef460698 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sat, 20 Apr 2024 02:20:27 -0400 Subject: [PATCH] Clean up Rust example --- crates/hello-world-sys/src/main.rs | 117 +++++++++++++---------------- 1 file changed, 53 insertions(+), 64 deletions(-) diff --git a/crates/hello-world-sys/src/main.rs b/crates/hello-world-sys/src/main.rs index 7c0044e..9eec69c 100644 --- a/crates/hello-world-sys/src/main.rs +++ b/crates/hello-world-sys/src/main.rs @@ -2,12 +2,8 @@ use std::ffi::{c_uint, c_void, CStr}; use std::mem::MaybeUninit; use std::ptr; -use jolt_sys::{ - JPC_BodyInterface_AddBody, JPC_BodyInterface_CreateBody, - JPC_BodyInterface_GetCenterOfMassPosition, JPC_BodyInterface_GetLinearVelocity, JPC_Body_GetID, - JPC_BroadPhaseLayer, JPC_ObjectLayer, Real, JPC_ACTIVATION_ACTIVATE, - JPC_ACTIVATION_DONT_ACTIVATE, JPC_MOTION_TYPE_DYNAMIC, JPC_MOTION_TYPE_STATIC, -}; +// Everything prefixed with `JPC_` comes from the jolt_sys crate. +use jolt_sys::*; const OL_NON_MOVING: JPC_ObjectLayer = 0; const OL_MOVING: JPC_ObjectLayer = 1; @@ -31,7 +27,7 @@ unsafe extern "C" fn bpl_get_broad_phase_layer( } } -const BPL: jolt_sys::JPC_BroadPhaseLayerInterfaceFns = jolt_sys::JPC_BroadPhaseLayerInterfaceFns { +const BPL: JPC_BroadPhaseLayerInterfaceFns = JPC_BroadPhaseLayerInterfaceFns { GetNumBroadPhaseLayers: Some(bpl_get_num_broad_phase_layers as _), GetBroadPhaseLayer: Some(bpl_get_broad_phase_layer as _), }; @@ -48,10 +44,9 @@ unsafe extern "C" fn ovb_should_collide( } } -const OVB: jolt_sys::JPC_ObjectVsBroadPhaseLayerFilterFns = - jolt_sys::JPC_ObjectVsBroadPhaseLayerFilterFns { - ShouldCollide: Some(ovb_should_collide as _), - }; +const OVB: JPC_ObjectVsBroadPhaseLayerFilterFns = JPC_ObjectVsBroadPhaseLayerFilterFns { + ShouldCollide: Some(ovb_should_collide as _), +}; unsafe extern "C" fn ovo_should_collide( _this: *const c_void, @@ -65,48 +60,44 @@ unsafe extern "C" fn ovo_should_collide( } } -const OVO: jolt_sys::JPC_ObjectLayerPairFilterFns = jolt_sys::JPC_ObjectLayerPairFilterFns { +const OVO: JPC_ObjectLayerPairFilterFns = JPC_ObjectLayerPairFilterFns { ShouldCollide: Some(ovo_should_collide as _), }; -fn vec3(x: f32, y: f32, z: f32) -> jolt_sys::JPC_Vec3 { - jolt_sys::JPC_Vec3 { x, y, z, _w: z } +fn vec3(x: f32, y: f32, z: f32) -> JPC_Vec3 { + JPC_Vec3 { x, y, z, _w: z } } -fn rvec3(x: Real, y: Real, z: Real) -> jolt_sys::JPC_RVec3 { - jolt_sys::JPC_RVec3 { x, y, z, _w: z } +fn rvec3(x: Real, y: Real, z: Real) -> JPC_RVec3 { + JPC_RVec3 { x, y, z, _w: z } } fn main() { unsafe { - jolt_sys::JPC_RegisterDefaultAllocator(); - jolt_sys::JPC_FactoryInit(); - jolt_sys::JPC_RegisterTypes(); + JPC_RegisterDefaultAllocator(); + JPC_FactoryInit(); + JPC_RegisterTypes(); - let temp_allocator = jolt_sys::JPC_TempAllocatorImpl_new(10 * 1024 * 1024); + let temp_allocator = JPC_TempAllocatorImpl_new(10 * 1024 * 1024); - let job_system = jolt_sys::JPC_JobSystemThreadPool_new2( - jolt_sys::JPC_MAX_PHYSICS_JOBS as _, - jolt_sys::JPC_MAX_PHYSICS_BARRIERS as _, - ); + let job_system = + JPC_JobSystemThreadPool_new2(JPC_MAX_PHYSICS_JOBS as _, JPC_MAX_PHYSICS_BARRIERS as _); - let broad_phase_layer_interface = - jolt_sys::JPC_BroadPhaseLayerInterface_new(ptr::null(), BPL); + let broad_phase_layer_interface = JPC_BroadPhaseLayerInterface_new(ptr::null(), BPL); let object_vs_broad_phase_layer_filter = - jolt_sys::JPC_ObjectVsBroadPhaseLayerFilter_new(ptr::null_mut(), OVB); + JPC_ObjectVsBroadPhaseLayerFilter_new(ptr::null_mut(), OVB); - let object_vs_object_layer_filter = - jolt_sys::JPC_ObjectLayerPairFilter_new(ptr::null_mut(), OVO); + let object_vs_object_layer_filter = JPC_ObjectLayerPairFilter_new(ptr::null_mut(), OVO); - let physics_system = jolt_sys::JPC_PhysicsSystem_new(); + let physics_system = JPC_PhysicsSystem_new(); let max_bodies = 1024; let num_body_mutexes = 0; let max_body_pairs = 1024; let max_contact_constraints = 1024; - jolt_sys::JPC_PhysicsSystem_Init( + JPC_PhysicsSystem_Init( physics_system, max_bodies, num_body_mutexes, @@ -120,25 +111,22 @@ fn main() { // TODO: register body activation listener // TODO: register contact listener - let body_interface = jolt_sys::JPC_PhysicsSystem_GetBodyInterface(physics_system); + let body_interface = JPC_PhysicsSystem_GetBodyInterface(physics_system); - let floor_shape_settings = jolt_sys::JPC_BoxShapeSettings_new(vec3(100.0, 1.0, 100.0)); + let floor_shape_settings = JPC_BoxShapeSettings_new(vec3(100.0, 1.0, 100.0)); - let mut floor_shape: *mut jolt_sys::JPC_Shape = ptr::null_mut(); - let mut err: *mut jolt_sys::JPC_String = ptr::null_mut(); - if !jolt_sys::JPC_ShapeSettings_Create( - floor_shape_settings.cast::(), + let mut floor_shape: *mut JPC_Shape = ptr::null_mut(); + let mut err: *mut JPC_String = ptr::null_mut(); + if !JPC_ShapeSettings_Create( + floor_shape_settings.cast::(), &mut floor_shape, &mut err, ) { - panic!( - "Fatal error: {:?}", - CStr::from_ptr(jolt_sys::JPC_String_c_str(err)) - ); + panic!("Fatal error: {:?}", CStr::from_ptr(JPC_String_c_str(err))); } - let mut floor_settings = MaybeUninit::::zeroed(); - jolt_sys::JPC_BodyCreationSettings_default(floor_settings.as_mut_ptr()); + let mut floor_settings = MaybeUninit::::zeroed(); + JPC_BodyCreationSettings_default(floor_settings.as_mut_ptr()); let mut floor_settings = floor_settings.assume_init(); floor_settings.Position = rvec3(0.0, -1.0, 0.0); floor_settings.MotionType = JPC_MOTION_TYPE_STATIC; @@ -152,23 +140,20 @@ fn main() { JPC_ACTIVATION_DONT_ACTIVATE, ); - let sphere_shape_settings = jolt_sys::JPC_SphereShapeSettings_new(0.5); + let sphere_shape_settings = JPC_SphereShapeSettings_new(0.5); - let mut sphere_shape: *mut jolt_sys::JPC_Shape = ptr::null_mut(); - let mut err: *mut jolt_sys::JPC_String = ptr::null_mut(); - if !jolt_sys::JPC_ShapeSettings_Create( - sphere_shape_settings.cast::(), + let mut sphere_shape: *mut JPC_Shape = ptr::null_mut(); + let mut err: *mut JPC_String = ptr::null_mut(); + if !JPC_ShapeSettings_Create( + sphere_shape_settings.cast::(), &mut sphere_shape, &mut err, ) { - panic!( - "Fatal error: {:?}", - CStr::from_ptr(jolt_sys::JPC_String_c_str(err)) - ); + panic!("Fatal error: {:?}", CStr::from_ptr(JPC_String_c_str(err))); } - let mut sphere_settings = MaybeUninit::::zeroed(); - jolt_sys::JPC_BodyCreationSettings_default(sphere_settings.as_mut_ptr()); + let mut sphere_settings = MaybeUninit::::zeroed(); + JPC_BodyCreationSettings_default(sphere_settings.as_mut_ptr()); let mut sphere_settings = sphere_settings.assume_init(); sphere_settings.Position = rvec3(0.0, 2.0, 0.0); sphere_settings.MotionType = JPC_MOTION_TYPE_DYNAMIC; @@ -193,7 +178,7 @@ fn main() { position.x, position.y, position.z, velocity.x, velocity.y, velocity.z ); - jolt_sys::JPC_PhysicsSystem_Update( + JPC_PhysicsSystem_Update( physics_system, delta_time, collision_steps, @@ -202,18 +187,22 @@ fn main() { ); } - // TODO: RemoveBody and DestroyBody + JPC_BodyInterface_RemoveBody(body_interface, JPC_Body_GetID(floor)); + JPC_BodyInterface_DestroyBody(body_interface, JPC_Body_GetID(floor)); + + JPC_BodyInterface_RemoveBody(body_interface, sphere_id); + JPC_BodyInterface_DestroyBody(body_interface, sphere_id); - jolt_sys::JPC_PhysicsSystem_delete(physics_system); - jolt_sys::JPC_BroadPhaseLayerInterface_delete(broad_phase_layer_interface); - jolt_sys::JPC_ObjectVsBroadPhaseLayerFilter_delete(object_vs_broad_phase_layer_filter); - jolt_sys::JPC_ObjectLayerPairFilter_delete(object_vs_object_layer_filter); + JPC_PhysicsSystem_delete(physics_system); + JPC_BroadPhaseLayerInterface_delete(broad_phase_layer_interface); + JPC_ObjectVsBroadPhaseLayerFilter_delete(object_vs_broad_phase_layer_filter); + JPC_ObjectLayerPairFilter_delete(object_vs_object_layer_filter); - jolt_sys::JPC_JobSystemThreadPool_delete(job_system); - jolt_sys::JPC_TempAllocatorImpl_delete(temp_allocator); + JPC_JobSystemThreadPool_delete(job_system); + JPC_TempAllocatorImpl_delete(temp_allocator); - jolt_sys::JPC_UnregisterTypes(); - jolt_sys::JPC_FactoryDelete(); + JPC_UnregisterTypes(); + JPC_FactoryDelete(); } println!("Hello, world!");