Skip to content

Commit d1a3b3e

Browse files
committed
- Remove usingnamespace usage
- Introduce a new system for interfaces: - They are defined as structs that are composed as the first member of the implementation - `initInterface` is used to build VTable default values at comptime automatically - Enhance the DebugRenderer test to verify that the render functions are called - Change the signature of DestroyTriangleBatch to pass non-const user pointer - Make all vtable functions non-optional, to improve error messages when `pub` is missing - Fix up test output when PRINT_OUTPUT is enabled - Move defines to a common build function - Add -Dverbose to set PRINT_OUTPUT
1 parent 3b1f716 commit d1a3b3e

File tree

6 files changed

+719
-1063
lines changed

6 files changed

+719
-1063
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);

build.zig.zon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.name = .zphysics,
33
.fingerprint = 0x1def6aac00c4909d,
44
.version = "0.2.0-dev",
5+
.minimum_zig_version = "0.14.1",
56
.paths = .{
67
"build.zig",
78
"build.zig.zon",

libs/JoltC/JoltPhysicsC.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,9 @@ class DebugRendererImpl final : public JPH::DebugRenderer
739739
BatchImpl(const JPC_DebugRenderer_Primitive *c_primitive) : RenderPrimitive(c_primitive) {}
740740
~BatchImpl()
741741
{
742-
if (sInstance && sInstance->c_renderer->vtbl->DestroyTriangleBatch)
742+
if (sInstance)
743743
{
744-
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, c_primitive);
744+
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, (void*)c_primitive);
745745
}
746746
}
747747

@@ -790,6 +790,7 @@ class DebugRendererImpl final : public JPH::DebugRenderer
790790
valid &= (c_renderer->vtbl->DrawTriangle != nullptr);
791791
valid &= (c_renderer->vtbl->CreateTriangleBatch != nullptr);
792792
valid &= (c_renderer->vtbl->CreateTriangleBatchIndexed != nullptr);
793+
valid &= (c_renderer->vtbl->DestroyTriangleBatch != nullptr);
793794
valid &= (c_renderer->vtbl->DrawGeometry != nullptr);
794795
valid &= (c_renderer->vtbl->DrawText3D != nullptr);
795796
return valid ? JPC_DEBUGRENDERER_SUCCESS : JPC_DEBUGRENDERER_INCOMPLETE_IMPL;

libs/JoltC/JoltPhysicsC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ typedef struct JPC_DebugRendererVTable
10601060

10611061
// Optional
10621062
void
1063-
(*DestroyTriangleBatch)(void *in_self, const void *in_primitive);
1063+
(*DestroyTriangleBatch)(void *in_self, void *in_primitive);
10641064

10651065
// Required, *cannot* be NULL.
10661066
void

libs/JoltC/JoltPhysicsC_Tests.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <stdio.h>
66
#include <string.h>
77

8-
//#define PRINT_OUTPUT
9-
108
// Object layers
119
#define NUM_OBJ_LAYERS 2
1210
#define OBJ_LAYER_NON_MOVING 0
@@ -97,9 +95,9 @@ MyContactListener_OnContactValidate(void *in_self,
9795
"\tOnContactValidate(): in_base_offset (%f, %f, %f)\n",
9896
in_base_offset[0], in_base_offset[1], in_base_offset[2]);
9997
fprintf(stderr, "\tOnContactValidate(): penetration_depth (%f)\n", in_collision_result->penetration_depth);
100-
fprintf(stderr, "\tOnContactValidate(): shape1_sub_shape_id (%d)\n", in_collision_result->shape1_sub_shape_id);
101-
fprintf(stderr, "\tOnContactValidate(): shape2_sub_shape_id (%d)\n", in_collision_result->shape2_sub_shape_id);
102-
fprintf(stderr, "\tOnContactValidate(): body2_id (%d)\n", in_collision_result->body2_id);
98+
fprintf(stderr, "\tOnContactValidate(): shape1_sub_shape_id (%d)\n", in_collision_result->shape1_sub_shape_id.id);
99+
fprintf(stderr, "\tOnContactValidate(): shape2_sub_shape_id (%d)\n", in_collision_result->shape2_sub_shape_id.id);
100+
fprintf(stderr, "\tOnContactValidate(): body2_id (%d)\n", in_collision_result->body2_id.id);
103101
#endif
104102
return JPC_VALIDATE_RESULT_ACCEPT_ALL_CONTACTS;
105103
}
@@ -214,7 +212,7 @@ MyDebugRenderer_CreateTriangleBatch(void *in_self,
214212
(JPC_DebugRenderer_Primitive *)prim
215213
);
216214
#ifdef PRINT_OUTPUT
217-
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatch called. Created primitive %x\n", prim);
215+
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatch called. Created primitive %llx\n", (uint64_t)prim);
218216
#endif
219217
return batch;
220218
}
@@ -232,11 +230,21 @@ MyDebugRenderer_CreateTriangleBatchIndexed(void *in_self,
232230
(JPC_DebugRenderer_Primitive *)prim
233231
);
234232
#ifdef PRINT_OUTPUT
235-
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatchIndexed called. Created primitive %x\n", prim);
233+
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatchIndexed called. Created primitive %llx\n", (uint64_t)prim);
236234
#endif
237235
return batch;
238236
}
239237

238+
static void
239+
MyDebugRenderer_DestroyTriangleBatch(void *in_self, void *in_batch)
240+
{
241+
struct MyDebugRenderer *self = (struct MyDebugRenderer*) in_self;
242+
struct MyRenderPrimitive *prim = (MyRenderPrimitive*)in_batch;
243+
#ifdef PRINT_OUTPUT
244+
fprintf(stderr, "\tDebugRenderer: DestroyTriangleBatch called. Destroyed primitive %llx\n", (uint64_t)prim);
245+
#endif
246+
}
247+
240248
static void
241249
MyDebugRenderer_DrawGeometry(void *in_self,
242250
const JPC_RMatrix *inMatrix,
@@ -252,7 +260,7 @@ MyDebugRenderer_DrawGeometry(void *in_self,
252260
JPC_DebugRenderer_TriangleBatch *batch = in_geometry->LODs[0].batch;
253261
const JPC_DebugRenderer_Primitive *prim = JPC_DebugRenderer_TriangleBatch_GetPrimitive(batch);
254262
#ifdef PRINT_OUTPUT
255-
fprintf(stderr, "\tDebugRenderer: DrawGeometry called for %x\n", prim);
263+
fprintf(stderr, "\tDebugRenderer: DrawGeometry called for %llx\n", (uint64_t)prim);
256264
#endif
257265
}
258266

@@ -277,6 +285,7 @@ MyDebugRenderer_Init(void)
277285
.DrawTriangle = MyDebugRenderer_DrawTriangle,
278286
.CreateTriangleBatch = MyDebugRenderer_CreateTriangleBatch,
279287
.CreateTriangleBatchIndexed = MyDebugRenderer_CreateTriangleBatchIndexed,
288+
.DestroyTriangleBatch = MyDebugRenderer_DestroyTriangleBatch,
280289
.DrawGeometry = MyDebugRenderer_DrawGeometry,
281290
.DrawText3D = MyDebugRenderer_DrawText3D,
282291
};

0 commit comments

Comments
 (0)