Skip to content

Commit cfa9464

Browse files
authored
Merge pull request #12 from kcbanner/0.15.x
Remove usingnamespace, add new interface system, rework IDs, and misc fixes
2 parents df5fda3 + 50e8a98 commit cfa9464

File tree

6 files changed

+900
-1160
lines changed

6 files changed

+900
-1160
lines changed

build.zig

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
const std = @import("std");
22

3+
fn addMacros(module: *std.Build.Module, options: anytype) void {
4+
if (options.enable_cross_platform_determinism)
5+
module.addCMacro("JPH_CROSS_PLATFORM_DETERMINISTIC", "");
6+
if (options.enable_debug_renderer)
7+
module.addCMacro("JPH_DEBUG_RENDERER", "");
8+
if (options.use_double_precision)
9+
module.addCMacro("JPH_DOUBLE_PRECISION", "");
10+
if (options.enable_asserts)
11+
module.addCMacro("JPH_ENABLE_ASSERTS", "");
12+
}
13+
314
pub fn build(b: *std.Build) void {
415
const optimize = b.standardOptimizeOption(.{});
516
const target = b.standardTargetOptions(.{});
@@ -58,21 +69,18 @@ pub fn build(b: *std.Build) void {
5869
});
5970
zjolt.addIncludePath(b.path("libs/JoltC"));
6071

61-
const joltc = if (options.shared) blk: {
62-
const lib = b.addSharedLibrary(.{
63-
.name = "joltc",
72+
const joltc = b.addLibrary(.{
73+
.name = "joltc",
74+
.linkage = if (options.shared) .dynamic else .static,
75+
.root_module = b.createModule(.{
6476
.target = target,
6577
.optimize = optimize,
66-
});
67-
if (target.result.os.tag == .windows) {
68-
lib.root_module.addCMacro("JPC_API", "extern __declspec(dllexport)");
69-
}
70-
break :blk lib;
71-
} else b.addStaticLibrary(.{
72-
.name = "joltc",
73-
.target = target,
74-
.optimize = optimize,
78+
}),
7579
});
80+
81+
if (options.shared and target.result.os.tag == .windows)
82+
joltc.root_module.addCMacro("JPC_API", "extern __declspec(dllexport)");
83+
7684
b.installArtifact(joltc);
7785

7886
joltc.addIncludePath(b.path("libs"));
@@ -87,15 +95,12 @@ pub fn build(b: *std.Build) void {
8795
const src_dir = "libs/Jolt";
8896
const c_flags = &.{
8997
"-std=c++17",
90-
if (options.enable_cross_platform_determinism) "-DJPH_CROSS_PLATFORM_DETERMINISTIC" else "",
91-
if (options.enable_debug_renderer) "-DJPH_DEBUG_RENDERER" else "",
92-
if (options.use_double_precision) "-DJPH_DOUBLE_PRECISION" else "",
93-
if (options.enable_asserts) "-DJPH_ENABLE_ASSERTS" else "",
9498
if (options.no_exceptions) "-fno-exceptions" else "",
9599
"-fno-access-control",
96100
"-fno-sanitize=undefined",
97101
};
98102

103+
addMacros(joltc.root_module, options);
99104
joltc.addCSourceFiles(.{
100105
.files = &.{
101106
"libs/JoltC/JoltPhysicsC.cpp",
@@ -248,9 +253,11 @@ pub fn build(b: *std.Build) void {
248253

249254
const tests = b.addTest(.{
250255
.name = "zphysics-tests",
251-
.root_source_file = b.path("src/zphysics.zig"),
252-
.target = target,
253-
.optimize = optimize,
256+
.root_module = b.createModule(.{
257+
.root_source_file = b.path("src/zphysics.zig"),
258+
.target = target,
259+
.optimize = optimize,
260+
}),
254261
});
255262
b.installArtifact(tests);
256263

@@ -259,17 +266,17 @@ pub fn build(b: *std.Build) void {
259266
tests.want_lto = false;
260267
}
261268

269+
addMacros(tests.root_module, options);
262270
tests.addCSourceFile(.{
263271
.file = b.path("libs/JoltC/JoltPhysicsC_Tests.c"),
264272
.flags = &.{
265-
if (options.enable_cross_platform_determinism) "-DJPH_CROSS_PLATFORM_DETERMINISTIC" else "",
266-
if (options.enable_debug_renderer) "-DJPH_DEBUG_RENDERER" else "",
267-
if (options.use_double_precision) "-DJPH_DOUBLE_PRECISION" else "",
268-
if (options.enable_asserts) "-DJPH_ENABLE_ASSERTS" else "",
269273
"-fno-sanitize=undefined",
270274
},
271275
});
272276

277+
if (b.option(bool, "verbose", "Print verbose test debug output to stderr") orelse false)
278+
tests.root_module.addCMacro("PRINT_OUTPUT", "");
279+
273280
tests.root_module.addImport("zphysics_options", options_module);
274281
tests.addIncludePath(b.path("libs/JoltC"));
275282
tests.linkLibrary(joltc);

libs/JoltC/JoltPhysicsC.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ JPH_SUPPRESS_WARNINGS
6060

6161
#define FN(name) static auto name
6262

63-
FN(toJph)(JPC_BodyID in) { return JPH::BodyID(in); }
64-
FN(toJpc)(JPH::BodyID in) { return in.GetIndexAndSequenceNumber(); }
63+
FN(toJph)(JPC_BodyID in) { return JPH::BodyID(in.id); }
64+
FN(toJpc)(JPH::BodyID in) { return (JPC_BodyID){ in.GetIndexAndSequenceNumber() }; }
6565

6666
FN(toJpc)(const JPH::Body *in) { assert(in); return reinterpret_cast<const JPC_Body *>(in); }
6767
FN(toJph)(const JPC_Body *in) { assert(in); return reinterpret_cast<const JPH::Body *>(in); }
@@ -301,6 +301,7 @@ FN(toJpc)(const JPH::CollisionGroup *in) { assert(in); return reinterpret_cast<c
301301
FN(toJpc)(JPH::CollisionGroup *in) { assert(in); return reinterpret_cast<JPC_CollisionGroup *>(in); }
302302

303303
FN(toJph)(const JPC_SubShapeID *in) { assert(in); return reinterpret_cast<const JPH::SubShapeID *>(in); }
304+
FN(toJph)(const JPC_BodyID *in) { assert(in); return reinterpret_cast<const JPH::BodyID *>(in); }
304305

305306
FN(toJpc)(const JPH::SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPC_SubShapeIDCreator *>(in); }
306307
FN(toJph)(const JPC_SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPH::SubShapeIDCreator *>(in); }
@@ -738,9 +739,9 @@ class DebugRendererImpl final : public JPH::DebugRenderer
738739
BatchImpl(const JPC_DebugRenderer_Primitive *c_primitive) : RenderPrimitive(c_primitive) {}
739740
~BatchImpl()
740741
{
741-
if (sInstance && sInstance->c_renderer->vtbl->DestroyTriangleBatch)
742+
if (sInstance)
742743
{
743-
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, c_primitive);
744+
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, (void*)c_primitive);
744745
}
745746
}
746747

@@ -789,6 +790,7 @@ class DebugRendererImpl final : public JPH::DebugRenderer
789790
valid &= (c_renderer->vtbl->DrawTriangle != nullptr);
790791
valid &= (c_renderer->vtbl->CreateTriangleBatch != nullptr);
791792
valid &= (c_renderer->vtbl->CreateTriangleBatchIndexed != nullptr);
793+
valid &= (c_renderer->vtbl->DestroyTriangleBatch != nullptr);
792794
valid &= (c_renderer->vtbl->DrawGeometry != nullptr);
793795
valid &= (c_renderer->vtbl->DrawText3D != nullptr);
794796
return valid ? JPC_DEBUGRENDERER_SUCCESS : JPC_DEBUGRENDERER_INCOMPLETE_IMPL;
@@ -2509,12 +2511,24 @@ JPC_BodyInterface_ActivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_i
25092511
toJph(in_iface)->ActivateBody(toJph(in_body_id));
25102512
}
25112513

2514+
JPC_API void
2515+
JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID *in_body_ids, int in_num_bodies)
2516+
{
2517+
toJph(in_iface)->ActivateBodies(toJph(in_body_ids), in_num_bodies);
2518+
}
2519+
25122520
JPC_API void
25132521
JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id)
25142522
{
25152523
toJph(in_iface)->DeactivateBody(toJph(in_body_id));
25162524
}
25172525

2526+
JPC_API void
2527+
JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID *in_body_ids, int in_num_bodies)
2528+
{
2529+
toJph(in_iface)->DeactivateBodies(toJph(in_body_ids), in_num_bodies);
2530+
}
2531+
25182532
JPC_API bool
25192533
JPC_BodyInterface_IsActive(const JPC_BodyInterface *in_iface, JPC_BodyID in_body_id)
25202534
{
@@ -2619,7 +2633,7 @@ JPC_BodyInterface_SetObjectLayer(JPC_BodyInterface *in_iface, JPC_BodyID in_body
26192633
JPC_API JPC_BodyID
26202634
JPC_Body_GetID(const JPC_Body *in_body)
26212635
{
2622-
return toJph(in_body)->GetID().GetIndexAndSequenceNumber();
2636+
return (JPC_BodyID){ toJph(in_body)->GetID().GetIndexAndSequenceNumber() };
26232637
}
26242638
//--------------------------------------------------------------------------------------------------
26252639
JPC_API bool
@@ -3198,19 +3212,19 @@ JPC_MotionProperties_SetMaxAngularVelocity(JPC_MotionProperties *in_properties,
31983212
JPC_API uint32_t
31993213
JPC_BodyID_GetIndex(JPC_BodyID in_body_id)
32003214
{
3201-
return JPH::BodyID(in_body_id).GetIndex();
3215+
return JPH::BodyID(in_body_id.id).GetIndex();
32023216
}
32033217
//--------------------------------------------------------------------------------------------------
32043218
JPC_API uint8_t
32053219
JPC_BodyID_GetSequenceNumber(JPC_BodyID in_body_id)
32063220
{
3207-
return JPH::BodyID(in_body_id).GetSequenceNumber();
3221+
return JPH::BodyID(in_body_id.id).GetSequenceNumber();
32083222
}
32093223
//--------------------------------------------------------------------------------------------------
32103224
JPC_API bool
32113225
JPC_BodyID_IsInvalid(JPC_BodyID in_body_id)
32123226
{
3213-
return JPH::BodyID(in_body_id).IsInvalid();
3227+
return JPH::BodyID(in_body_id.id).IsInvalid();
32143228
}
32153229
//--------------------------------------------------------------------------------------------------
32163230
//

libs/JoltC/JoltPhysicsC.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,19 @@ typedef enum JPC_ShapeColor {
292292
typedef uint16_t JPC_ObjectLayer;
293293
typedef uint8_t JPC_BroadPhaseLayer;
294294

295+
typedef struct JPC_BodyID
296+
{
297+
uint32_t id;
298+
} JPC_BodyID;
299+
300+
typedef struct JPC_SubShapeID
301+
{
302+
uint32_t id;
303+
} JPC_SubShapeID;
304+
305+
#define JPC_ID_EQ(a, b) (a.id == b.id)
306+
295307
// TODO: Consider using structures for IDs
296-
typedef uint32_t JPC_BodyID;
297-
typedef uint32_t JPC_SubShapeID;
298308
typedef uint32_t JPC_CollisionGroupID;
299309
typedef uint32_t JPC_CollisionSubGroupID;
300310

@@ -985,8 +995,6 @@ typedef struct JPC_CharacterContactListenerVTable
985995

986996
typedef struct JPC_ContactListenerVTable
987997
{
988-
_JPC_VTABLE_HEADER;
989-
990998
// Optional, can be NULL.
991999
JPC_ValidateResult
9921000
(*OnContactValidate)(void *in_self,
@@ -1050,7 +1058,7 @@ typedef struct JPC_DebugRendererVTable
10501058

10511059
// Optional
10521060
void
1053-
(*DestroyTriangleBatch)(void *in_self, const void *in_primitive);
1061+
(*DestroyTriangleBatch)(void *in_self, void *in_primitive);
10541062

10551063
// Required, *cannot* be NULL.
10561064
void
@@ -1384,9 +1392,9 @@ JPC_PhysicsSystem_GetActiveBodyIDs(const JPC_PhysicsSystem *in_physics_system,
13841392
/// Access a body, will return NULL if the body ID is no longer valid.
13851393
/// Use `JPC_PhysicsSystem_GetBodiesUnsafe()` to get an array of all body pointers.
13861394
#define JPC_TRY_GET_BODY(all_body_ptrs, body_id) \
1387-
JPC_IS_VALID_BODY_POINTER(all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS]) && \
1388-
all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS]->id == body_id ? \
1389-
all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS] : NULL
1395+
JPC_IS_VALID_BODY_POINTER(all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS]) && \
1396+
all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS]->id.id == body_id.id ? \
1397+
all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS] : NULL
13901398

13911399
/// Get direct access to all bodies. Not protected by a lock. Use with great care!
13921400
JPC_API JPC_Body **
@@ -2062,9 +2070,15 @@ JPC_BodyInterface_SetRotation(JPC_BodyInterface *in_iface,
20622070
JPC_API void
20632071
JPC_BodyInterface_ActivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20642072

2073+
JPC_API void
2074+
JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID* in_body_ids, int in_num_bodies);
2075+
20652076
JPC_API void
20662077
JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20672078

2079+
JPC_API void
2080+
JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID* in_body_ids, int in_num_bodies);
2081+
20682082
JPC_API bool
20692083
JPC_BodyInterface_IsActive(const JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20702084

libs/JoltC/JoltPhysicsC_Extensions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ JPC_PhysicsSystem_GetBodyIDs(const JPC_PhysicsSystem *in_physics_system,
6262
for (const JPH::Body *b : physics_system->mBodyManager.mBodies)
6363
if (JPH::BodyManager::sIsValidBodyPointer(b))
6464
{
65-
*out_body_ids = b->GetID().GetIndexAndSequenceNumber();
65+
out_body_ids->id = b->GetID().GetIndexAndSequenceNumber();
6666
out_body_ids += 1;
6767
if (out_num_body_ids) *out_num_body_ids += 1;
6868
in_max_body_ids -= 1;
@@ -91,7 +91,7 @@ JPC_PhysicsSystem_GetActiveBodyIDs(const JPC_PhysicsSystem *in_physics_system,
9191
for (uint32_t i = 0; i < physics_system->mBodyManager.mNumActiveBodies[0]; ++i)
9292
{
9393
const JPH::BodyID body_id = physics_system->mBodyManager.mActiveBodies[0][i];
94-
*out_body_ids = body_id.GetIndexAndSequenceNumber();
94+
out_body_ids->id = body_id.GetIndexAndSequenceNumber();
9595
out_body_ids += 1;
9696
if (out_num_body_ids) *out_num_body_ids += 1;
9797
in_max_body_ids -= 1;

0 commit comments

Comments
 (0)