bytedeco / javacpp-presets

The missing Java distribution of native C++ libraries
Other
2.66k stars 741 forks source link

liquidfun how to use SetUserData, GetUserData? #969

Closed dukke closed 3 years ago

dukke commented 3 years ago

Hi,

I'm porting my app from using JBox2d, which is a port of liquidfun totally in Java (i.e. no jni, C++, etc) to "javacpp-presets liquidfun". I was making use of b2Body setUserData and getUserData which in jbox2d served to store and retrieve any arbitrary Java Object. In "javacpp presets liquidfun" the methods are passed in, and return a Pointer. I don't know how I can use SetUserData and GetUserData the same way I was using it, that is, somehow converting a subclass of a Java Object instance to a Pointer and then when retrieving converting the Pointer back to that Object.

Thanks

saudet commented 3 years ago

To reference a Java object, the easiest thing to do is probably to have a HashMap that associates a Pointer, for example, the b2Body instance, to your Object.

saudet commented 3 years ago

Duplicate of https://github.com/bytedeco/javacpp/issues/419

dukke commented 3 years ago

Yeah I know that, been using that (associating objects with hashmaps) for a long time. I was just wondering if there would be any way to store a java object in a javacpp Pointer. Like I said I'm porting the code from using jbox2d to using javacpp presets liquidfun and the best way to do it would be to take advantage of SetUserData, GetUserData as I was when using jbox2d.

Anyways, if that's not recommended/supported then I'll find some other way..

Thx!

dukke commented 3 years ago

Alternatively, perhaps that SetUserData, GetUserData methods would be better of actually returning and receiving a Java Object, would be more inline with the purpose of this API. i.e. in box2d those methods are used to store any arbitrary instance of the language you're using. That way they could store a Pointer as well as any other Java Object.

Though not sure, of the implications as I'm not fully aware of the process involved in generating a Java library using javacpp.. and that might not be recommended..

saudet commented 3 years ago

Sure, ideally we should be able to do that, see bytedeco/javacpp#419. Contributions are welcome! Either in code or in cash: https://xscode.com/bytedeco/javacpp-presets

dukke commented 3 years ago

OK. Thank you very much for the quick reply @saudet ! 👍 🙂