You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48Lines changed: 48 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,54 @@ If a box2d method returns a pointer, it will not be registered for GC.
36
36
> [!NOTE]
37
37
> For more information on the bindings see the [documentation](https://github.com/libgdx/gdx-jnigen/blob/master/RUNTIME.MD#the-runtime).
38
38
39
+
### Working with VoidPointer context or user data
40
+
Box2d supports passing a `void*` context to callbacks or attaching user data to a type.
41
+
In java interop, we usually want this to be a java object. In a thread-safe manner, this can be achieved like this:
42
+
```java
43
+
publicclassBox2DIDFactory<T> {
44
+
45
+
privatelong id =0;
46
+
privateLongMap<T> map =newLongMap<>();
47
+
48
+
publicVoidPointerputData(Tobj) {
49
+
synchronized (this) {
50
+
id +=1;
51
+
map.put(id, obj);
52
+
returnnewVoidPointer(id, false);
53
+
}
54
+
}
55
+
56
+
publicvoidputData(Tobj, VoidPointerpointer) {
57
+
synchronized (this) {
58
+
id +=1;
59
+
map.put(id, obj);
60
+
pointer.setPointer(id);
61
+
}
62
+
}
63
+
64
+
publiclongputDataRaw(Tobj) {
65
+
synchronized (this) {
66
+
id +=1;
67
+
map.put(id, obj);
68
+
return id;
69
+
}
70
+
}
71
+
72
+
publicTobtainData(VoidPointerpointer) {
73
+
synchronized (this) {
74
+
return map.get(pointer.getPointer());
75
+
}
76
+
}
77
+
78
+
publicTobtainDataRaw(longpointer) {
79
+
synchronized (this) {
80
+
return map.get(pointer);
81
+
}
82
+
}
83
+
}
84
+
```
85
+
86
+
This is a very barebones example. It assumes you would want a factory per type. It is also designed thread-safe, which is not necessary.
39
87
40
88
## Java 8
41
89
The project needs java 8 language features to build. However, it doesn't use any java 8 APIs and is therefor still safe to use with mobiVM.
0 commit comments