Skip to content

Commit ff4b7d5

Browse files
committed
refactor to simplify porting to Android
1 parent 8c81df9 commit ff4b7d5

11 files changed

+52
-41
lines changed

src/main/native/glue/cu/CustomBodyActivationListener.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -56,7 +56,7 @@ class CustomBodyActivationListener : BodyActivationListener {
5656

5757
void OnBodyActivated(const BodyID &inBodyID, uint64 inBodyUserData) {
5858
JNIEnv *pAttachEnv;
59-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
59+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6060
JPH_ASSERT(retCode == JNI_OK);
6161

6262
const jlong idVa = reinterpret_cast<jlong> (&inBodyID);
@@ -68,7 +68,7 @@ class CustomBodyActivationListener : BodyActivationListener {
6868

6969
void OnBodyDeactivated(const BodyID &inBodyID, uint64 inBodyUserData) {
7070
JNIEnv *pAttachEnv;
71-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
71+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
7272
JPH_ASSERT(retCode == JNI_OK);
7373

7474
const jlong idVa = reinterpret_cast<jlong> (&inBodyID);
@@ -80,7 +80,7 @@ class CustomBodyActivationListener : BodyActivationListener {
8080

8181
~CustomBodyActivationListener() {
8282
JNIEnv *pAttachEnv;
83-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
83+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
8484
JPH_ASSERT(retCode == JNI_OK);
8585

8686
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomCastRayCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomCastRayCollector : CastRayCollector {
5252

5353
void AddHit(const RayCastResult &inResult) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong resultVa = reinterpret_cast<jlong> (&inResult);
@@ -63,7 +63,7 @@ class CustomCastRayCollector : CastRayCollector {
6363

6464
~CustomCastRayCollector() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomCastShapeCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomCastShapeCollector : CastShapeCollector {
5252

5353
void AddHit(const ShapeCastResult &inResult) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong resultVa = reinterpret_cast<jlong> (&inResult);
@@ -63,7 +63,7 @@ class CustomCastShapeCollector : CastShapeCollector {
6363

6464
~CustomCastShapeCollector() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomCharacterContactListener.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -85,7 +85,7 @@ class CustomCharacterContactListener : CharacterContactListener {
8585
const Body &inBody2, Vec3 &ioLinearVelocity,
8686
Vec3 &ioAngularVelocity) {
8787
JNIEnv *pAttachEnv;
88-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
88+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
8989
JPH_ASSERT(retCode == JNI_OK);
9090

9191
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -121,7 +121,7 @@ class CustomCharacterContactListener : CharacterContactListener {
121121
const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition,
122122
Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) {
123123
JNIEnv *pAttachEnv;
124-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
124+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
125125
JPH_ASSERT(retCode == JNI_OK);
126126

127127
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -152,7 +152,7 @@ class CustomCharacterContactListener : CharacterContactListener {
152152
const PhysicsMaterial *inContactMaterial,
153153
Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity) {
154154
JNIEnv *pAttachEnv;
155-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
155+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
156156
JPH_ASSERT(retCode == JNI_OK);
157157

158158
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -205,7 +205,7 @@ class CustomCharacterContactListener : CharacterContactListener {
205205
const CharacterVirtual *inOtherCharacter,
206206
const SubShapeID &inSubShapeID2) {
207207
JNIEnv *pAttachEnv;
208-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
208+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
209209
JPH_ASSERT(retCode == JNI_OK);
210210

211211
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -225,7 +225,7 @@ class CustomCharacterContactListener : CharacterContactListener {
225225
RVec3Arg inContactPosition, Vec3Arg inContactNormal,
226226
CharacterContactSettings &ioSettings) {
227227
JNIEnv *pAttachEnv;
228-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
228+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
229229
JPH_ASSERT(retCode == JNI_OK);
230230

231231
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -254,7 +254,7 @@ class CustomCharacterContactListener : CharacterContactListener {
254254
Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial,
255255
Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity) {
256256
JNIEnv *pAttachEnv;
257-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
257+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
258258
JPH_ASSERT(retCode == JNI_OK);
259259

260260
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -304,7 +304,7 @@ class CustomCharacterContactListener : CharacterContactListener {
304304
bool OnContactValidate(const CharacterVirtual *inCharacter,
305305
const BodyID &inBodyID2, const SubShapeID &inSubShapeID2) {
306306
JNIEnv *pAttachEnv;
307-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
307+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
308308
JPH_ASSERT(retCode == JNI_OK);
309309

310310
const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
@@ -319,7 +319,7 @@ class CustomCharacterContactListener : CharacterContactListener {
319319

320320
~CustomCharacterContactListener() {
321321
JNIEnv *pAttachEnv;
322-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
322+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
323323
JPH_ASSERT(retCode == JNI_OK);
324324

325325
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomCollidePointCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomCollidePointCollector : CollidePointCollector {
5252

5353
void AddHit(const CollidePointResult &inResult) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong resultVa = reinterpret_cast<jlong> (&inResult);
@@ -63,7 +63,7 @@ class CustomCollidePointCollector : CollidePointCollector {
6363

6464
~CustomCollidePointCollector() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomCollideShapeBodyCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomCollideShapeBodyCollector : CollideShapeBodyCollector {
5252

5353
void AddHit(const BodyID &inResult) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong resultVa = reinterpret_cast<jlong> (&inResult);
@@ -63,7 +63,7 @@ class CustomCollideShapeBodyCollector : CollideShapeBodyCollector {
6363

6464
~CustomCollideShapeBodyCollector() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomCollideShapeCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomCollideShapeCollector : CollideShapeCollector {
5252

5353
void AddHit(const CollideShapeResult &inResult) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong resultVa = reinterpret_cast<jlong> (&inResult);
@@ -63,7 +63,7 @@ class CustomCollideShapeCollector : CollideShapeCollector {
6363

6464
~CustomCollideShapeCollector() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomContactListener.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -65,7 +65,7 @@ class CustomContactListener : ContactListener {
6565
void OnContactAdded(const Body &inBody1, const Body &inBody2,
6666
const ContactManifold &inManifold, ContactSettings &ioSettings) {
6767
JNIEnv *pAttachEnv;
68-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
68+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6969
JPH_ASSERT(retCode == JNI_OK);
7070

7171
const jlong body1Va = reinterpret_cast<jlong> (&inBody1);
@@ -80,7 +80,7 @@ class CustomContactListener : ContactListener {
8080
void OnContactPersisted(const Body &inBody1, const Body &inBody2,
8181
const ContactManifold &inManifold, ContactSettings &ioSettings) {
8282
JNIEnv *pAttachEnv;
83-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
83+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
8484
JPH_ASSERT(retCode == JNI_OK);
8585

8686
const jlong body1Va = reinterpret_cast<jlong> (&inBody1);
@@ -95,7 +95,7 @@ class CustomContactListener : ContactListener {
9595

9696
void OnContactRemoved(const SubShapeIDPair &pair) {
9797
JNIEnv *pAttachEnv;
98-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
98+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
9999
JPH_ASSERT(retCode == JNI_OK);
100100

101101
const jlong pairVa = reinterpret_cast<jlong> (&pair);
@@ -107,7 +107,7 @@ class CustomContactListener : ContactListener {
107107
ValidateResult OnContactValidate(const Body &inBody1, const Body &inBody2,
108108
RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult) {
109109
JNIEnv *pAttachEnv;
110-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
110+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
111111
JPH_ASSERT(retCode == JNI_OK);
112112

113113
const jlong body1Va = reinterpret_cast<jlong> (&inBody1);
@@ -125,7 +125,7 @@ class CustomContactListener : ContactListener {
125125

126126
~CustomContactListener() {
127127
JNIEnv *pAttachEnv;
128-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
128+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
129129
JPH_ASSERT(retCode == JNI_OK);
130130

131131
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomPhysicsStepListener.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomPhysicsStepListener : PhysicsStepListener {
5252

5353
void OnStep(const PhysicsStepListenerContext &inContext) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong contextVa = reinterpret_cast<jlong> (&inContext);
@@ -63,7 +63,7 @@ class CustomPhysicsStepListener : PhysicsStepListener {
6363

6464
~CustomPhysicsStepListener() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/cu/CustomRayCastBodyCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2024 Stephen Gold
2+
Copyright (c) 2024-2025 Stephen Gold
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
@@ -52,7 +52,7 @@ class CustomRayCastBodyCollector : RayCastBodyCollector {
5252

5353
void AddHit(const BroadPhaseCastResult &inResult) {
5454
JNIEnv *pAttachEnv;
55-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
55+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
5656
JPH_ASSERT(retCode == JNI_OK);
5757

5858
const jlong resultVa = reinterpret_cast<jlong> (&inResult);
@@ -63,7 +63,7 @@ class CustomRayCastBodyCollector : RayCastBodyCollector {
6363

6464
~CustomRayCastBodyCollector() {
6565
JNIEnv *pAttachEnv;
66-
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
66+
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
6767
JPH_ASSERT(retCode == JNI_OK);
6868

6969
pAttachEnv->DeleteGlobalRef(mJavaObject);

src/main/native/glue/glue.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _Included_glue
22
#define _Included_glue
33
/*
4-
Copyright (c) 2024 Stephen Gold
4+
Copyright (c) 2024-2025 Stephen Gold
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -66,4 +66,15 @@ extern bool gTraceAllocations;
6666
return reinterpret_cast<jlong> (pResult); \
6767
}
6868

69+
#ifdef ANDROID
70+
// doesn't match the Invocation API spec
71+
#define ATTACH_CURRENT_THREAD(mpVM, ppAttachEnv) \
72+
(mpVM)->AttachCurrentThread(ppAttachEnv, NULL)
73+
74+
#else
75+
#define ATTACH_CURRENT_THREAD(mpVM, ppAttachEnv) \
76+
(mpVM)->AttachCurrentThread((void **)(ppAttachEnv), NULL)
77+
78+
#endif
79+
6980
#endif

0 commit comments

Comments
 (0)