Skip to content

Commit 1dcf232

Browse files
committed
Refactor mapping to the native structs
1 parent 1209caa commit 1dcf232

17 files changed

+194
-251
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ Dear ImGui by default uses a stb_strutype library to render a fonts atlas. It's
241241
* To get an input/output to/from Dear ImGui - use primitive wrappers: `ImBool`, `ImInt` etc.
242242
* Due to the Java and JNI restrictions we can't provide a fully fledged callbacks to the `ImGui::InputText` methods.
243243
To replace some features use an ImGuiInputTextData class.
244+
* All classes which are represent Dear ImGui structs have a field with a pointer to those structs.
245+
Field is public and could be modified without restrictions. So by changing the pointer it's possible to use the same java class to modify different
246+
native structs. Commonly you don't need to mind about that.
247+
Just keep in mind that you are able to do advanced stuff with it like: save pointers to the structs to modify them later.
244248
* Read [javadoc](https://javadoc.io/doc/io.imgui.java/binding) and sources comments to get more info.
245249

246250
## How to Build

imgui-binding/src/main/java/imgui/ImDrawData.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package imgui;
22

3+
import imgui.binding.ImGuiStruct;
4+
35
import java.nio.ByteBuffer;
46
import java.nio.ByteOrder;
57

@@ -11,36 +13,28 @@
1113
* BINDING NOTICE: Since it's impossible to do a 1:1 mapping with JNI, current class provides "getCmdList*()" methods.
1214
* Those are used to get the data needed to do a rendering.
1315
*/
14-
public final class ImDrawData {
16+
public final class ImDrawData extends ImGuiStruct {
1517
public static final int SIZEOF_IM_DRAW_IDX = 2;
1618
public static final int SIZEOF_IM_DRAW_VERT = (4 + 1) * 4;
1719

1820
private static final int FACTOR = 5_000;
1921

20-
final long ptr;
21-
2222
private ByteBuffer idxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_IDX * FACTOR).order(ByteOrder.nativeOrder());
2323
private ByteBuffer vtxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_VERT * FACTOR).order(ByteOrder.nativeOrder());
2424

25-
ImDrawData(final long ptr) {
26-
this.ptr = ptr;
25+
public ImDrawData(final long ptr) {
26+
super(ptr);
2727
}
2828

2929
/*JNI
3030
#include <stdint.h>
3131
#include <imgui.h>
3232
#include "jni_common.h"
33+
#include "jni_binding_struct.h"
3334
34-
jfieldID imDrawDataPtrID;
35-
36-
#define IM_DRAW_DATA ((ImDrawData*)env->GetLongField(object, imDrawDataPtrID))
35+
#define IM_DRAW_DATA ((ImDrawData*)STRUCT_PTR)
3736
*/
3837

39-
static native void nInit(); /*
40-
jclass jImDrawDataClass = env->FindClass("imgui/ImDrawData");
41-
imDrawDataPtrID = env->GetFieldID(jImDrawDataClass, "ptr", "J");
42-
*/
43-
4438
///////// Start of Render Methods | Binding
4539

4640
/**

imgui-binding/src/main/java/imgui/ImDrawList.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package imgui;
22

3+
import imgui.binding.ImGuiStruct;
4+
35
/**
46
* Draw command list
57
* This is the low-level list of polygons that ImGui:: functions are filling. At the end of the frame,
@@ -11,28 +13,20 @@
1113
* but you are totally free to apply whatever transformation matrix to want to the data (if you apply such transformation you'll want to apply it to ClipRect as well)
1214
* Important: Primitives are always added to the list and not culled (culling is done at higher-level by ImGui:: functions), if you use this API a lot consider coarse culling your drawn objects.
1315
*/
14-
public final class ImDrawList {
15-
long ptr;
16-
17-
ImDrawList(final long ptr) {
18-
this.ptr = ptr;
16+
public final class ImDrawList extends ImGuiStruct {
17+
public ImDrawList(final long ptr) {
18+
super(ptr);
1919
}
2020

2121
/*JNI
2222
#include <stdint.h>
2323
#include <imgui.h>
2424
#include "jni_common.h"
25+
#include "jni_binding_struct.h"
2526
26-
jfieldID imDrawListPtrID;
27-
28-
#define IM_DRAW_LIST ((ImDrawList*)env->GetLongField(object, imDrawListPtrID))
27+
#define IM_DRAW_LIST ((ImDrawList*)STRUCT_PTR)
2928
*/
3029

31-
static native void nInit(); /*
32-
jclass jImDrawListClass = env->FindClass("imgui/ImDrawList");
33-
imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J");
34-
*/
35-
3630
/**
3731
* Flags, you may poke into these to adjust anti-aliasing settings per-primitive.
3832
*/

imgui-binding/src/main/java/imgui/ImFont.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,37 @@
11
package imgui;
22

3+
import imgui.binding.ImGuiStructDestroyable;
4+
35
/**
46
* Font runtime data and rendering
57
* ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().
68
*/
7-
public final class ImFont implements ImGuiDestroyableStruct {
8-
final long ptr;
9-
9+
public final class ImFont extends ImGuiStructDestroyable {
1010
private ImFontGlyph fallbackGlyph = null;
1111

12-
/**
13-
* This class will create a native structure.
14-
* Call {@link #destroy()} method to manually free used memory.
15-
*/
1612
public ImFont() {
17-
ImGui.touch();
18-
ptr = nCreate();
1913
}
2014

21-
ImFont(final long ptr) {
22-
this.ptr = ptr;
23-
}
24-
25-
@Override
26-
public void destroy() {
27-
nDestroy(ptr);
15+
public ImFont(final long ptr) {
16+
super(ptr);
2817
}
2918

3019
/*JNI
3120
#include <stdint.h>
3221
#include <imgui.h>
3322
#include "jni_common.h"
23+
#include "jni_binding_struct.h"
3424
35-
jfieldID imFontPtrID;
36-
37-
#define IM_FONT ((ImFont*)env->GetLongField(object, imFontPtrID))
25+
#define IM_FONT ((ImFont*)STRUCT_PTR)
3826
*/
3927

40-
static native void nInit(); /*
41-
jclass jImFontClass = env->FindClass("imgui/ImFont");
42-
imFontPtrID = env->GetFieldID(jImFontClass, "ptr", "J");
43-
*/
28+
@Override
29+
protected long create() {
30+
return nCreate();
31+
}
4432

4533
private native long nCreate(); /*
46-
ImFont* imFont = new ImFont();
47-
return (intptr_t)imFont;
48-
*/
49-
50-
private native void nDestroy(long ptr); /*
51-
delete (ImFont*)ptr;
34+
return (intptr_t)(new ImFont());
5235
*/
5336

5437
// TODO IndexAdvanceX

imgui-binding/src/main/java/imgui/ImFontAtlas.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package imgui;
22

3+
import imgui.binding.ImGuiStructDestroyable;
34
import imgui.type.ImInt;
45

56
import java.nio.ByteBuffer;
@@ -24,57 +25,42 @@
2425
* - Even though many functions are suffixed with "TTF", OTF data is supported just as well.
2526
* - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future!
2627
*/
27-
public final class ImFontAtlas implements ImGuiDestroyableStruct {
28-
final long ptr;
29-
28+
public final class ImFontAtlas extends ImGuiStructDestroyable {
3029
private ByteBuffer alpha8pixels = null;
3130
private ByteBuffer rgba32pixels = null;
3231

33-
/**
34-
* This class will create a native structure.
35-
* Call {@link #destroy()} method to manually free used memory.
36-
*/
3732
public ImFontAtlas() {
38-
ImGui.touch();
39-
ptr = nCreate();
4033
}
4134

42-
ImFontAtlas(final long ptr) {
43-
this.ptr = ptr;
44-
}
45-
46-
@Override
47-
public void destroy() {
48-
nDestroy(ptr);
35+
public ImFontAtlas(final long ptr) {
36+
super(ptr);
4937
}
5038

5139
/*JNI
5240
#include <stdint.h>
5341
#include <imgui.h>
42+
#include "jni_binding_struct.h"
5443
55-
jfieldID imFontAtlasPtrID;
56-
57-
#define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID))
44+
#define IM_FONT_ATLAS ((ImFontAtlas*)STRUCT_PTR)
5845
5946
jmethodID jImFontAtlasCreateAlpha8PixelsMID;
6047
jmethodID jImFontAtlasCreateRgba32PixelsMID;
6148
*/
6249

6350
static native void nInit(); /*
6451
jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas");
65-
imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J");
6652
6753
jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;");
6854
jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;");
6955
*/
7056

71-
private native long nCreate(); /*
72-
ImFontAtlas* imFontAtlas = new ImFontAtlas();
73-
return (intptr_t)imFontAtlas;
74-
*/
57+
@Override
58+
protected long create() {
59+
return nCreate();
60+
}
7561

76-
private native void nDestroy(long ptr); /*
77-
delete (ImFontAtlas*)ptr;
62+
private native long nCreate(); /*
63+
return (intptr_t)(new ImFontConfig());
7864
*/
7965

8066
public ImFont addFont(final ImFontConfig imFontConfig) {

imgui-binding/src/main/java/imgui/ImFontConfig.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,40 @@
11
package imgui;
22

3-
public final class ImFontConfig implements ImGuiDestroyableStruct {
4-
final long ptr;
3+
import imgui.binding.ImGuiStructDestroyable;
54

5+
public final class ImFontConfig extends ImGuiStructDestroyable {
66
private short[] glyphRanges;
77

88
/**
99
* This class will create a native structure.
1010
* Call {@link #destroy()} method to manually free used memory.
1111
*/
1212
public ImFontConfig() {
13-
ImGui.touch();
14-
ptr = nCreate();
13+
super();
1514
setFontDataOwnedByAtlas(false); // Read method javadoc
1615
}
1716

1817
ImFontConfig(final long ptr) {
19-
this.ptr = ptr;
18+
super(ptr);
2019
setFontDataOwnedByAtlas(false); // Read method javadoc
2120
}
2221

23-
@Override
24-
public void destroy() {
25-
nDestroy(ptr);
26-
}
27-
2822
/*JNI
2923
#include <stdint.h>
3024
#include <imgui.h>
3125
#include "jni_common.h"
26+
#include "jni_binding_struct.h"
3227
33-
jfieldID imFontConfigPtrID;
34-
35-
#define IM_FONT_CONFIG ((ImFontConfig*)env->GetLongField(object, imFontConfigPtrID))
28+
#define IM_FONT_CONFIG ((ImFontConfig*)STRUCT_PTR)
3629
*/
3730

38-
static native void nInit(); /*
39-
jclass jImFontConfigClass = env->FindClass("imgui/ImFontConfig");
40-
imFontConfigPtrID = env->GetFieldID(jImFontConfigClass, "ptr", "J");
41-
*/
31+
@Override
32+
protected long create() {
33+
return nCreate();
34+
}
4235

4336
private native long nCreate(); /*
44-
ImFontConfig* imFontConfig = new ImFontConfig();
45-
return (intptr_t)imFontConfig;
46-
*/
47-
48-
private native void nDestroy(long ptr); /*
49-
delete (ImFontConfig*)ptr;
37+
return (intptr_t)(new ImFontConfig());
5038
*/
5139

5240
/**

imgui-binding/src/main/java/imgui/ImFontGlyph.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
11
package imgui;
22

3+
import imgui.binding.ImGuiStructDestroyable;
4+
35
/**
46
* Hold rendering data for one glyph.
57
* (Note: some language parsers may fail to convert the 31+1 bitfield members, in this case maybe drop store a single u32 or we can rework this)
68
*/
7-
public final class ImFontGlyph implements ImGuiDestroyableStruct {
8-
final long ptr;
9-
10-
/**
11-
* This class will create a native structure.
12-
* Call {@link #destroy()} method to manually free used memory.
13-
*/
9+
public final class ImFontGlyph extends ImGuiStructDestroyable {
1410
public ImFontGlyph() {
15-
ImGui.touch();
16-
ptr = nCreate();
1711
}
1812

19-
ImFontGlyph(final long ptr) {
20-
this.ptr = ptr;
21-
}
22-
23-
@Override
24-
public void destroy() {
25-
nDestroy(ptr);
13+
public ImFontGlyph(final long ptr) {
14+
super(ptr);
2615
}
2716

2817
/*JNI
2918
#include <stdint.h>
3019
#include <imgui.h>
20+
#include "jni_binding_struct.h"
3121
32-
jfieldID imFontGlyphPtrID;
33-
34-
#define IM_FONT_GLYPH ((ImFontGlyph*)env->GetLongField(object, imFontGlyphPtrID))
22+
#define IM_FONT_GLYPH ((ImFontGlyph*)STRUCT_PTR)
3523
*/
3624

37-
static native void nInit(); /*
38-
jclass jImFontGlyphClass = env->FindClass("imgui/ImFontGlyph");
39-
imFontGlyphPtrID = env->GetFieldID(jImFontGlyphClass, "ptr", "J");
40-
*/
25+
@Override
26+
protected long create() {
27+
return nCreate();
28+
}
4129

4230
private native long nCreate(); /*
43-
ImFontConfig* imFontConfig = new ImFontConfig();
44-
return (intptr_t)imFontConfig;
45-
*/
46-
47-
private native void nDestroy(long ptr); /*
48-
delete (ImFontConfig*)ptr;
31+
return (intptr_t)(new ImFontGlyph());
4932
*/
5033

5134
/**

0 commit comments

Comments
 (0)