Skip to content

Commit

Permalink
refactor to simplify porting to Android
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jan 14, 2025
1 parent 8c81df9 commit ff4b7d5
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/main/native/glue/cu/CustomBodyActivationListener.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -56,7 +56,7 @@ class CustomBodyActivationListener : BodyActivationListener {

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

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

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

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

~CustomBodyActivationListener() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomCastRayCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomCastRayCollector : CastRayCollector {

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

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

~CustomCastRayCollector() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomCastShapeCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomCastShapeCollector : CastShapeCollector {

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

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

~CustomCastShapeCollector() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
18 changes: 9 additions & 9 deletions src/main/native/glue/cu/CustomCharacterContactListener.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -85,7 +85,7 @@ class CustomCharacterContactListener : CharacterContactListener {
const Body &inBody2, Vec3 &ioLinearVelocity,
Vec3 &ioAngularVelocity) {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

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

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

const jlong characterVa = reinterpret_cast<jlong> (inCharacter);
Expand Down Expand Up @@ -205,7 +205,7 @@ class CustomCharacterContactListener : CharacterContactListener {
const CharacterVirtual *inOtherCharacter,
const SubShapeID &inSubShapeID2) {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

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

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

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

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

~CustomCharacterContactListener() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomCollidePointCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomCollidePointCollector : CollidePointCollector {

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

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

~CustomCollidePointCollector() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomCollideShapeBodyCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomCollideShapeBodyCollector : CollideShapeBodyCollector {

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

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

~CustomCollideShapeBodyCollector() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomCollideShapeCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomCollideShapeCollector : CollideShapeCollector {

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

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

~CustomCollideShapeCollector() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
12 changes: 6 additions & 6 deletions src/main/native/glue/cu/CustomContactListener.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -65,7 +65,7 @@ class CustomContactListener : ContactListener {
void OnContactAdded(const Body &inBody1, const Body &inBody2,
const ContactManifold &inManifold, ContactSettings &ioSettings) {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

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

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

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

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

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

~CustomContactListener() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomPhysicsStepListener.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomPhysicsStepListener : PhysicsStepListener {

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

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

~CustomPhysicsStepListener() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/glue/cu/CustomRayCastBodyCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,7 @@ class CustomRayCastBodyCollector : RayCastBodyCollector {

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

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

~CustomRayCastBodyCollector() {
JNIEnv *pAttachEnv;
jint retCode = mpVM->AttachCurrentThread((void **)&pAttachEnv, NULL);
jint retCode = ATTACH_CURRENT_THREAD(mpVM, &pAttachEnv);
JPH_ASSERT(retCode == JNI_OK);

pAttachEnv->DeleteGlobalRef(mJavaObject);
Expand Down
13 changes: 12 additions & 1 deletion src/main/native/glue/glue.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _Included_glue
#define _Included_glue
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -66,4 +66,15 @@ extern bool gTraceAllocations;
return reinterpret_cast<jlong> (pResult); \
}

#ifdef ANDROID
// doesn't match the Invocation API spec
#define ATTACH_CURRENT_THREAD(mpVM, ppAttachEnv) \
(mpVM)->AttachCurrentThread(ppAttachEnv, NULL)

#else
#define ATTACH_CURRENT_THREAD(mpVM, ppAttachEnv) \
(mpVM)->AttachCurrentThread((void **)(ppAttachEnv), NULL)

#endif

#endif

0 comments on commit ff4b7d5

Please sign in to comment.