Jelajahi Sumber

progress, and also a new problem: use after free

oops I misunderstood the lifetime of some of these types
Lucien Greathouse 1 tahun lalu
induk
melakukan
ac352bbb66
3 mengubah file dengan 106 tambahan dan 8 penghapusan
  1. 11 3
      HelloWorld/main.cpp
  2. 38 1
      JoltC/Functions.h
  3. 57 4
      JoltC/JoltC.cpp

+ 11 - 3
HelloWorld/main.cpp

@@ -103,6 +103,8 @@ int main() {
 		.fns = Hello_OVO,
 	};
 
+	// FIXME: These types get freed on accident
+
 	JPC_PhysicsSystem* physics_system = JPC_PhysicsSystem_new();
 
 	const unsigned int cMaxBodies = 1024;
@@ -123,7 +125,7 @@ int main() {
 	// TODO: register body activation listener
 	// TODO: register contact listener
 
-	const JPC_BodyInterface* body_interface = JPC_PhysicsSystem_GetBodyInterface(physics_system);
+	JPC_BodyInterface* body_interface = JPC_PhysicsSystem_GetBodyInterface(physics_system);
 
 	JPC_BoxShapeSettings* floor_shape_settings = JPC_BoxShapeSettings_new(JPC_Vec3{100.0f, 100.0f, 100.0f});
 	JPC_ConvexShapeSettings_SetDensity((JPC_ConvexShapeSettings*)floor_shape_settings, 1000.0f);
@@ -139,9 +141,15 @@ int main() {
 		exit(1);
 	}
 
-	// BodyCreationSettings floor_settings(floor_shape, RVec3(0.0_r, -1.0_r, 0.0_r), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
+	JPC_BodyCreationSettings floor_settings;
+	JPC_BodyCreationSettings_default(&floor_settings);
+	floor_settings.Position = JPC_RVec3{0.0, -1.0, 0.0};
+	floor_settings.MotionType = JPC_MOTION_TYPE_STATIC;
+	floor_settings.ObjectLayer = HELLO_OL_NON_MOVING;
+	floor_settings.Shape = shape;
+
+	JPC_Body* floor = JPC_BodyInterface_CreateBody(body_interface, &floor_settings);
 
-	// Body *floor = body_interface.CreateBody(floor_settings);
 	// body_interface.AddBody(floor->GetID(), EActivation::DontActivate);
 
 	// TODO: creating bodies

+ 38 - 1
JoltC/Functions.h

@@ -61,6 +61,9 @@ ENSURE_SIZE_ALIGN(JPC_Quat, JPH::Quat);
 
 ENSURE_SIZE_ALIGN(JPC_RVec3, JPH::RVec3);
 
+typedef uint32_t JPC_BodyID;
+ENSURE_SIZE_ALIGN(JPC_BodyID, JPH::BodyID);
+
 typedef uint8_t JPC_BroadPhaseLayer;
 ENSURE_SIZE_ALIGN(JPC_BroadPhaseLayer, JPH::BroadPhaseLayer)
 
@@ -177,11 +180,45 @@ typedef struct JPC_BoxShapeSettings JPC_BoxShapeSettings;
 JPC_API JPC_BoxShapeSettings* JPC_BoxShapeSettings_new(JPC_Vec3 inHalfExtent);
 JPC_API void JPC_BoxShapeSettings_delete(JPC_BoxShapeSettings* object);
 
+////////////////////////////////////////////////////////////////////////////////
+// BodyCreationSettings
+
+typedef struct JPC_BodyCreationSettings {
+	JPC_RVec3 Position;
+	JPC_Quat Rotation;
+	JPC_Vec3 LinearVelocity;
+	JPC_Vec3 AngularVelocity;
+
+	uint64_t UserData;
+
+	JPC_ObjectLayer ObjectLayer;
+	// TODO: CollisionGroup;
+
+	JPC_MotionType MotionType;
+	JPC_AllowedDOFs AllowedDOFs;
+
+	// TODO: More
+
+	JPC_Shape* Shape;
+} JPC_BodyCreationSettings;
+
+JPC_API void JPC_BodyCreationSettings_default(JPC_BodyCreationSettings* settings);
+
+typedef struct JPC_BodyCreationSettings JPC_BodyCreationSettings;
+
+JPC_API JPC_BodyCreationSettings* JPC_BodyCreationSettings_new();
+
 ////////////////////////////////////////////////////////////////////////////////
 // BodyInterface
 
 typedef struct JPC_BodyInterface JPC_BodyInterface;
 
+typedef struct JPC_Body JPC_Body;
+
+JPC_API JPC_Body* JPC_BodyInterface_CreateBody(JPC_BodyInterface* self, JPC_BodyCreationSettings* inSettingsC);
+JPC_API void JPC_BodyInterface_AddBody(JPC_BodyInterface* self, JPC_BodyID inBodyID, JPC_Activation inActivationMode);
+JPC_API void JPC_BodyInterface_RemoveBody(JPC_BodyInterface* self, JPC_BodyID inBodyID);
+
 ////////////////////////////////////////////////////////////////////////////////
 // PhysicsSystem
 
@@ -206,7 +243,7 @@ JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
 	JPC_TempAllocatorImpl *inTempAllocator, // FIXME: un-specialize
 	JPC_JobSystemThreadPool *inJobSystem); // FIXME: un-specialize
 
-JPC_API const JPC_BodyInterface* JPC_PhysicsSystem_GetBodyInterface(JPC_PhysicsSystem* self);
+JPC_API JPC_BodyInterface* JPC_PhysicsSystem_GetBodyInterface(JPC_PhysicsSystem* self);
 
 #ifdef __cplusplus
 }

+ 57 - 4
JoltC/JoltC.cpp

@@ -24,12 +24,19 @@
 		delete to_jph(object); \
 	}
 
+#define ENUM_CONVERSION(c_type, cpp_type) \
+	static c_type to_jpc(cpp_type in) { return static_cast<c_type>(in); } \
+	static cpp_type to_jph(c_type in) { return static_cast<cpp_type>(in); }
+
 template<typename E>
 constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type 
 {
 	return static_cast<typename std::underlying_type<E>::type>(e);
 }
 
+ENUM_CONVERSION(JPC_MotionType, JPH::EMotionType)
+ENUM_CONVERSION(JPC_AllowedDOFs, JPH::EAllowedDOFs)
+
 OPAQUE_WRAPPER(JPC_PhysicsSystem, JPH::PhysicsSystem)
 DESTRUCTOR(JPC_PhysicsSystem)
 
@@ -44,6 +51,7 @@ DESTRUCTOR(JPC_JobSystemThreadPool)
 OPAQUE_WRAPPER(JPC_ConvexShapeSettings, JPH::ConvexShapeSettings)
 OPAQUE_WRAPPER(JPC_ShapeSettings, JPH::ShapeSettings)
 OPAQUE_WRAPPER(JPC_Shape, JPH::Shape)
+OPAQUE_WRAPPER(JPC_Body, JPH::Body)
 
 OPAQUE_WRAPPER(JPC_String, JPH::String)
 DESTRUCTOR(JPC_String)
@@ -61,6 +69,14 @@ static JPH::Vec3 to_jph(JPC_Vec3 in) {
 	return JPH::Vec3(in.x, in.y, in.z);
 }
 
+static JPC_Quat to_jpc(JPH::Quat in) {
+	return JPC_Quat{in.GetX(), in.GetY(), in.GetZ(), in.GetW()};
+}
+static JPH::Quat to_jph(JPC_Quat in) {
+	return JPH::Quat(in.x, in.y, in.z, in.w);
+}
+
+
 JPC_API void JPC_RegisterDefaultAllocator() {
 	JPH::RegisterDefaultAllocator();
 }
@@ -195,8 +211,10 @@ JPC_API bool JPC_ShapeSettings_Create(
 	auto res = to_jph(self)->Create();
 
 	if (res.HasError()) {
-		JPH::String* created = new JPH::String(std::move(res.GetError()));
-		*outError = to_jpc(created);
+		if (outError != nullptr) {
+			JPH::String* created = new JPH::String(std::move(res.GetError()));
+			*outError = to_jpc(created);
+		}
 
 		return false;
 	} else {
@@ -224,11 +242,42 @@ JPC_API JPC_BoxShapeSettings* JPC_BoxShapeSettings_new(JPC_Vec3 inHalfExtent) {
 	return to_jpc(new JPH::BoxShapeSettings(to_jph(inHalfExtent)));
 }
 
+////////////////////////////////////////////////////////////////////////////////
+// BodyCreationSettings
+
+JPC_API void JPC_BodyCreationSettings_default(JPC_BodyCreationSettings* settings) {
+	settings->Position = JPC_RVec3{0, 0, 0};
+	settings->Rotation = JPC_Quat{0, 0, 0, 1};
+	settings->LinearVelocity = JPC_Vec3{0, 0, 0};
+	settings->AngularVelocity = JPC_Vec3{0, 0, 0};
+
+	settings->UserData = 0;
+	settings->ObjectLayer = 0;
+
+	settings->MotionType = JPC_MOTION_TYPE_DYNAMIC;
+	settings->AllowedDOFs = JPC_ALLOWED_DOFS_ALL;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // BodyInterface
 
-JPC_API const JPC_BodyInterface* JPC_PhysicsSystem_GetBodyInterface(JPC_PhysicsSystem* self) {
-	return to_jpc(&to_jph(self)->GetBodyInterface());
+JPC_API JPC_Body* JPC_BodyInterface_CreateBody(JPC_BodyInterface* self, JPC_BodyCreationSettings* inSettingsC) {
+	JPH::BodyCreationSettings inSettings{};
+	inSettings.mPosition = to_jph(inSettingsC->Position);
+	inSettings.mRotation = to_jph(inSettingsC->Rotation);
+	inSettings.mLinearVelocity = to_jph(inSettingsC->LinearVelocity);
+	inSettings.mAngularVelocity = to_jph(inSettingsC->AngularVelocity);
+
+	inSettings.mUserData = inSettingsC->UserData;
+	inSettings.mObjectLayer = inSettingsC->ObjectLayer;
+
+	inSettings.mMotionType = to_jph(inSettingsC->MotionType);
+	inSettings.mAllowedDOFs = to_jph(inSettingsC->AllowedDOFs);
+
+	inSettings.SetShape(to_jph(inSettingsC->Shape));
+
+	JPH::Body* body = to_jph(self)->CreateBody(inSettings);
+	return to_jpc(body);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -262,6 +311,10 @@ JPC_API void JPC_PhysicsSystem_Init(
 		impl_inObjectLayerPairFilter);
 }
 
+JPC_API JPC_BodyInterface* JPC_PhysicsSystem_GetBodyInterface(JPC_PhysicsSystem* self) {
+	return to_jpc(&to_jph(self)->GetBodyInterface());
+}
+
 JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
 	JPC_PhysicsSystem* self,
 	float inDeltaTime,