diff --git a/README.md b/README.md index 661c6ac..aa3f36d 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,10 @@ Dear ImGui by default uses a stb_strutype library to render a fonts atlas. It's * To get an input/output to/from Dear ImGui - use primitive wrappers: `ImBool`, `ImInt` etc. * Due to the Java and JNI restrictions we can't provide a fully fledged callbacks to the `ImGui::InputText` methods. To replace some features use an ImGuiInputTextData class. +* All classes which are represent Dear ImGui structs have a field with a pointer to those structs. + 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 + native structs. Commonly you don't need to mind about that. + Just keep in mind that you are able to do advanced stuff with it like: save pointers to the structs to modify them later. * Read [javadoc](https://javadoc.io/doc/io.imgui.java/binding) and sources comments to get more info. ## How to Build diff --git a/imgui-binding/src/main/java/imgui/ImDrawData.java b/imgui-binding/src/main/java/imgui/ImDrawData.java index 9529789..03fb2d0 100644 --- a/imgui-binding/src/main/java/imgui/ImDrawData.java +++ b/imgui-binding/src/main/java/imgui/ImDrawData.java @@ -1,5 +1,7 @@ package imgui; +import imgui.binding.ImGuiStruct; + import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -11,36 +13,28 @@ import java.nio.ByteOrder; * BINDING NOTICE: Since it's impossible to do a 1:1 mapping with JNI, current class provides "getCmdList*()" methods. * Those are used to get the data needed to do a rendering. */ -public final class ImDrawData { +public final class ImDrawData extends ImGuiStruct { public static final int SIZEOF_IM_DRAW_IDX = 2; public static final int SIZEOF_IM_DRAW_VERT = (4 + 1) * 4; private static final int FACTOR = 5_000; - final long ptr; - private ByteBuffer idxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_IDX * FACTOR).order(ByteOrder.nativeOrder()); private ByteBuffer vtxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_VERT * FACTOR).order(ByteOrder.nativeOrder()); - ImDrawData(final long ptr) { - this.ptr = ptr; + public ImDrawData(final long ptr) { + super(ptr); } /*JNI #include #include #include "jni_common.h" + #include "jni_binding_struct.h" - jfieldID imDrawDataPtrID; - - #define IM_DRAW_DATA ((ImDrawData*)env->GetLongField(object, imDrawDataPtrID)) + #define IM_DRAW_DATA ((ImDrawData*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImDrawDataClass = env->FindClass("imgui/ImDrawData"); - imDrawDataPtrID = env->GetFieldID(jImDrawDataClass, "ptr", "J"); - */ - ///////// Start of Render Methods | Binding /** diff --git a/imgui-binding/src/main/java/imgui/ImDrawList.java b/imgui-binding/src/main/java/imgui/ImDrawList.java index d004198..1bd79fb 100644 --- a/imgui-binding/src/main/java/imgui/ImDrawList.java +++ b/imgui-binding/src/main/java/imgui/ImDrawList.java @@ -1,5 +1,7 @@ package imgui; +import imgui.binding.ImGuiStruct; + /** * Draw command list * This is the low-level list of polygons that ImGui:: functions are filling. At the end of the frame, @@ -11,28 +13,20 @@ package imgui; * 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) * 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. */ -public final class ImDrawList { - long ptr; - - ImDrawList(final long ptr) { - this.ptr = ptr; +public final class ImDrawList extends ImGuiStruct { + public ImDrawList(final long ptr) { + super(ptr); } /*JNI #include #include #include "jni_common.h" + #include "jni_binding_struct.h" - jfieldID imDrawListPtrID; - - #define IM_DRAW_LIST ((ImDrawList*)env->GetLongField(object, imDrawListPtrID)) + #define IM_DRAW_LIST ((ImDrawList*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImDrawListClass = env->FindClass("imgui/ImDrawList"); - imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J"); - */ - /** * Flags, you may poke into these to adjust anti-aliasing settings per-primitive. */ diff --git a/imgui-binding/src/main/java/imgui/ImFont.java b/imgui-binding/src/main/java/imgui/ImFont.java index 3aebfe6..2ebf82a 100644 --- a/imgui-binding/src/main/java/imgui/ImFont.java +++ b/imgui-binding/src/main/java/imgui/ImFont.java @@ -1,54 +1,37 @@ package imgui; +import imgui.binding.ImGuiStructDestroyable; + /** * Font runtime data and rendering * ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32(). */ -public final class ImFont implements ImGuiDestroyableStruct { - final long ptr; - +public final class ImFont extends ImGuiStructDestroyable { private ImFontGlyph fallbackGlyph = null; - /** - * This class will create a native structure. - * Call {@link #destroy()} method to manually free used memory. - */ public ImFont() { - ImGui.touch(); - ptr = nCreate(); } - ImFont(final long ptr) { - this.ptr = ptr; - } - - @Override - public void destroy() { - nDestroy(ptr); + public ImFont(final long ptr) { + super(ptr); } /*JNI #include #include #include "jni_common.h" + #include "jni_binding_struct.h" - jfieldID imFontPtrID; - - #define IM_FONT ((ImFont*)env->GetLongField(object, imFontPtrID)) + #define IM_FONT ((ImFont*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImFontClass = env->FindClass("imgui/ImFont"); - imFontPtrID = env->GetFieldID(jImFontClass, "ptr", "J"); - */ + @Override + protected long create() { + return nCreate(); + } private native long nCreate(); /* - ImFont* imFont = new ImFont(); - return (intptr_t)imFont; - */ - - private native void nDestroy(long ptr); /* - delete (ImFont*)ptr; + return (intptr_t)(new ImFont()); */ // TODO IndexAdvanceX diff --git a/imgui-binding/src/main/java/imgui/ImFontAtlas.java b/imgui-binding/src/main/java/imgui/ImFontAtlas.java index 99ca730..f903f48 100644 --- a/imgui-binding/src/main/java/imgui/ImFontAtlas.java +++ b/imgui-binding/src/main/java/imgui/ImFontAtlas.java @@ -1,5 +1,6 @@ package imgui; +import imgui.binding.ImGuiStructDestroyable; import imgui.type.ImInt; import java.nio.ByteBuffer; @@ -24,37 +25,23 @@ import java.nio.ByteOrder; * - Even though many functions are suffixed with "TTF", OTF data is supported just as well. * - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future! */ -public final class ImFontAtlas implements ImGuiDestroyableStruct { - final long ptr; - +public final class ImFontAtlas extends ImGuiStructDestroyable { private ByteBuffer alpha8pixels = null; private ByteBuffer rgba32pixels = null; - /** - * This class will create a native structure. - * Call {@link #destroy()} method to manually free used memory. - */ public ImFontAtlas() { - ImGui.touch(); - ptr = nCreate(); } - ImFontAtlas(final long ptr) { - this.ptr = ptr; - } - - @Override - public void destroy() { - nDestroy(ptr); + public ImFontAtlas(final long ptr) { + super(ptr); } /*JNI #include #include + #include "jni_binding_struct.h" - jfieldID imFontAtlasPtrID; - - #define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID)) + #define IM_FONT_ATLAS ((ImFontAtlas*)STRUCT_PTR) jmethodID jImFontAtlasCreateAlpha8PixelsMID; jmethodID jImFontAtlasCreateRgba32PixelsMID; @@ -62,19 +49,18 @@ public final class ImFontAtlas implements ImGuiDestroyableStruct { static native void nInit(); /* jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas"); - imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J"); jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;"); jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;"); */ - private native long nCreate(); /* - ImFontAtlas* imFontAtlas = new ImFontAtlas(); - return (intptr_t)imFontAtlas; - */ + @Override + protected long create() { + return nCreate(); + } - private native void nDestroy(long ptr); /* - delete (ImFontAtlas*)ptr; + private native long nCreate(); /* + return (intptr_t)(new ImFontConfig()); */ public ImFont addFont(final ImFontConfig imFontConfig) { diff --git a/imgui-binding/src/main/java/imgui/ImFontConfig.java b/imgui-binding/src/main/java/imgui/ImFontConfig.java index 7d90d00..58e65e5 100644 --- a/imgui-binding/src/main/java/imgui/ImFontConfig.java +++ b/imgui-binding/src/main/java/imgui/ImFontConfig.java @@ -1,8 +1,8 @@ package imgui; -public final class ImFontConfig implements ImGuiDestroyableStruct { - final long ptr; +import imgui.binding.ImGuiStructDestroyable; +public final class ImFontConfig extends ImGuiStructDestroyable { private short[] glyphRanges; /** @@ -10,43 +10,31 @@ public final class ImFontConfig implements ImGuiDestroyableStruct { * Call {@link #destroy()} method to manually free used memory. */ public ImFontConfig() { - ImGui.touch(); - ptr = nCreate(); + super(); setFontDataOwnedByAtlas(false); // Read method javadoc } ImFontConfig(final long ptr) { - this.ptr = ptr; + super(ptr); setFontDataOwnedByAtlas(false); // Read method javadoc } - @Override - public void destroy() { - nDestroy(ptr); - } - /*JNI #include #include #include "jni_common.h" + #include "jni_binding_struct.h" - jfieldID imFontConfigPtrID; - - #define IM_FONT_CONFIG ((ImFontConfig*)env->GetLongField(object, imFontConfigPtrID)) + #define IM_FONT_CONFIG ((ImFontConfig*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImFontConfigClass = env->FindClass("imgui/ImFontConfig"); - imFontConfigPtrID = env->GetFieldID(jImFontConfigClass, "ptr", "J"); - */ + @Override + protected long create() { + return nCreate(); + } private native long nCreate(); /* - ImFontConfig* imFontConfig = new ImFontConfig(); - return (intptr_t)imFontConfig; - */ - - private native void nDestroy(long ptr); /* - delete (ImFontConfig*)ptr; + return (intptr_t)(new ImFontConfig()); */ /** diff --git a/imgui-binding/src/main/java/imgui/ImFontGlyph.java b/imgui-binding/src/main/java/imgui/ImFontGlyph.java index 29f6b35..b501015 100644 --- a/imgui-binding/src/main/java/imgui/ImFontGlyph.java +++ b/imgui-binding/src/main/java/imgui/ImFontGlyph.java @@ -1,51 +1,34 @@ package imgui; +import imgui.binding.ImGuiStructDestroyable; + /** * Hold rendering data for one glyph. * (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) */ -public final class ImFontGlyph implements ImGuiDestroyableStruct { - final long ptr; - - /** - * This class will create a native structure. - * Call {@link #destroy()} method to manually free used memory. - */ +public final class ImFontGlyph extends ImGuiStructDestroyable { public ImFontGlyph() { - ImGui.touch(); - ptr = nCreate(); } - ImFontGlyph(final long ptr) { - this.ptr = ptr; - } - - @Override - public void destroy() { - nDestroy(ptr); + public ImFontGlyph(final long ptr) { + super(ptr); } /*JNI #include #include + #include "jni_binding_struct.h" - jfieldID imFontGlyphPtrID; - - #define IM_FONT_GLYPH ((ImFontGlyph*)env->GetLongField(object, imFontGlyphPtrID)) + #define IM_FONT_GLYPH ((ImFontGlyph*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImFontGlyphClass = env->FindClass("imgui/ImFontGlyph"); - imFontGlyphPtrID = env->GetFieldID(jImFontGlyphClass, "ptr", "J"); - */ + @Override + protected long create() { + return nCreate(); + } private native long nCreate(); /* - ImFontConfig* imFontConfig = new ImFontConfig(); - return (intptr_t)imFontConfig; - */ - - private native void nDestroy(long ptr); /* - delete (ImFontConfig*)ptr; + return (intptr_t)(new ImFontGlyph()); */ /** diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index 69f9876..32740cb 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -56,16 +56,7 @@ public final class ImGui { IMGUI_STORAGE = new ImGuiStorage(0); nInitJni(); - ImDrawList.nInit(); - ImDrawData.nInit(); ImFontAtlas.nInit(); - ImFontConfig.nInit(); - ImFontGlyph.nInit(); - ImFont.nInit(); - ImGuiStyle.nInit(); - ImGuiWindowClass.nInit(); - ImGuiStorage.nInit(); - ImGuiViewport.nInit(); nInitInputTextData(); } @@ -113,7 +104,7 @@ public final class ImGui { * Method is used to initiate static instantiation (loading of the native libraries etc.). * Otherwise native libraries will be loaded on demand and natively mapped objects won't work. */ - static void touch() { + public static void touch() { } private ImGui() { @@ -124,11 +115,13 @@ public final class ImGui { #include #include "jni_common.h" #include "jni_callbacks.h" + #include "jni_binding_struct.h" */ private static native void nInitJni(); /* Jni::InitCommon(env); Jni::InitCallbacks(env); + Jni::InitBindingStruct(env); */ // Context creation and access diff --git a/imgui-binding/src/main/java/imgui/ImGuiDestroyableStruct.java b/imgui-binding/src/main/java/imgui/ImGuiDestroyableStruct.java deleted file mode 100644 index 46ac6e5..0000000 --- a/imgui-binding/src/main/java/imgui/ImGuiDestroyableStruct.java +++ /dev/null @@ -1,8 +0,0 @@ -package imgui; - -/** - * When implement, class could be manually destroyed (free allocated memory on the JNI side). - */ -interface ImGuiDestroyableStruct { - void destroy(); -} diff --git a/imgui-binding/src/main/java/imgui/ImGuiStorage.java b/imgui-binding/src/main/java/imgui/ImGuiStorage.java index 18b2cdf..b958e7f 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiStorage.java +++ b/imgui-binding/src/main/java/imgui/ImGuiStorage.java @@ -1,5 +1,7 @@ package imgui; +import imgui.binding.ImGuiStructDestroyable; + /** * Helper: Key-Value storage * Typically you don't have to worry about this since a storage is held within each Window. @@ -10,48 +12,29 @@ package imgui; * - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient) * Types are NOT stored, so it is up to you to make sure your Key don't collide with different types. */ -public final class ImGuiStorage implements ImGuiDestroyableStruct { - long ptr; - - /** - * This class will create a native structure. - * Call {@link #destroy()} method to manually free used memory. - */ +public final class ImGuiStorage extends ImGuiStructDestroyable { public ImGuiStorage() { - ImGui.touch(); - ptr = nCreate(); } - ImGuiStorage(final long ptr) { - this.ptr = ptr; - } - - @Override - public void destroy() { - nDestroy(ptr); + public ImGuiStorage(final long ptr) { + super(ptr); } /*JNI #include #include + #include "jni_binding_struct.h" - jfieldID imGuiStoragePtrID; - - #define IMGUI_STORAGE ((ImGuiStorage*)env->GetLongField(object, imGuiStoragePtrID)) + #define IMGUI_STORAGE ((ImGuiStorage*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImGuiStorageClass = env->FindClass("imgui/ImGuiStorage"); - imGuiStoragePtrID = env->GetFieldID(jImGuiStorageClass, "ptr", "J"); - */ + @Override + protected long create() { + return nCreate(); + } private native long nCreate(); /* - ImGuiStorage* imGuiStorage = new ImGuiStorage(); - return (intptr_t)imGuiStorage; - */ - - private native void nDestroy(long ptr); /* - delete (ImGuiStorage*)ptr; + return (intptr_t)(new ImGuiStorage()); */ // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N) diff --git a/imgui-binding/src/main/java/imgui/ImGuiStyle.java b/imgui-binding/src/main/java/imgui/ImGuiStyle.java index 2d6f266..3ca867b 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiStyle.java +++ b/imgui-binding/src/main/java/imgui/ImGuiStyle.java @@ -1,53 +1,36 @@ package imgui; +import imgui.binding.ImGuiStructDestroyable; + /** * You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame(). * During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values, * and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors. */ -public final class ImGuiStyle implements ImGuiDestroyableStruct { - final long ptr; - - /** - * This class will create a native structure. - * Call {@link #destroy()} method to manually free used memory. - */ +public final class ImGuiStyle extends ImGuiStructDestroyable { public ImGuiStyle() { - ImGui.touch(); - ptr = nCreate(); } - ImGuiStyle(final long ptr) { - this.ptr = ptr; - } - - @Override - public void destroy() { - nDestroy(ptr); + public ImGuiStyle(final long ptr) { + super(ptr); } /*JNI #include #include #include "jni_common.h" + #include "jni_binding_struct.h" - jfieldID imGuiStylePtrID; - - #define IMGUI_STYLE ((ImGuiStyle*)env->GetLongField(object, imGuiStylePtrID)) + #define IMGUI_STYLE ((ImGuiStyle*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImGuiStyleClass = env->FindClass("imgui/ImGuiStyle"); - imGuiStylePtrID = env->GetFieldID(jImGuiStyleClass, "ptr", "J"); - */ + @Override + protected long create() { + return nCreate(); + } private native long nCreate(); /* - ImGuiStyle* imGuiStyle = new ImGuiStyle(); - return (intptr_t)imGuiStyle; - */ - - private native void nDestroy(long ptr); /* - delete (ImGuiStyle*)ptr; + return (intptr_t)(new ImGuiStyle()); */ /** diff --git a/imgui-binding/src/main/java/imgui/ImGuiViewport.java b/imgui-binding/src/main/java/imgui/ImGuiViewport.java index 8fcd99c..d093ee9 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiViewport.java +++ b/imgui-binding/src/main/java/imgui/ImGuiViewport.java @@ -1,32 +1,26 @@ package imgui; +import imgui.binding.ImGuiStruct; + /** * The viewports created and managed by Dear ImGui. The role of the platform back-end is to create the platform/OS windows corresponding to each viewport. * - Main Area = entire viewport. * - Work Area = entire viewport minus sections optionally used by menu bars, status bars. Some positioning code will prefer to use this. Window are also trying to stay within this area. */ -public final class ImGuiViewport { - final long ptr; - - ImGuiViewport(final long ptr) { - this.ptr = ptr; +public final class ImGuiViewport extends ImGuiStruct { + public ImGuiViewport(final long ptr) { + super(ptr); } /*JNI #include #include #include "jni_common.h" + #include "jni_binding_struct.h" - jfieldID imGuiViewportPtrID; - - #define IMGUI_VIEWPORT ((ImGuiViewport*)env->GetLongField(object, imGuiViewportPtrID)) + #define IMGUI_VIEWPORT ((ImGuiViewport*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImGuiViewportClass = env->FindClass("imgui/ImGuiViewport"); - imGuiViewportPtrID = env->GetFieldID(jImGuiViewportClass, "ptr", "J"); - */ - /** * Unique identifier for the viewport. */ diff --git a/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java b/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java index 8e77846..f69b175 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java +++ b/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java @@ -1,5 +1,7 @@ package imgui; +import imgui.binding.ImGuiStructDestroyable; + /** * [ALPHA] Rarely used / very advanced uses only. Use with SetNextWindowClass() and DockSpace() functions. * Important: the content of this class is still highly WIP and likely to change and be refactored @@ -9,48 +11,29 @@ package imgui; * - To the platform back-end for OS level parent/child relationships of viewport. * - To the docking system for various options and filtering. */ -public final class ImGuiWindowClass implements ImGuiDestroyableStruct { - final long ptr; - - /** - * This class will create a native structure. - * Call {@link #destroy()} method to manually free used memory. - */ +public final class ImGuiWindowClass extends ImGuiStructDestroyable { public ImGuiWindowClass() { - ImGui.touch(); - ptr = nCreate(); } - ImGuiWindowClass(final long ptr) { - this.ptr = ptr; - } - - @Override - public void destroy() { - nDestroy(ptr); + public ImGuiWindowClass(final long ptr) { + super(ptr); } /*JNI #include #include + #include "jni_binding_struct.h" - jfieldID imGuiWindowClassPtrID; - - #define IMGUI_WINDOW_CLASS ((ImGuiWindowClass*)env->GetLongField(object, imGuiWindowClassPtrID)) + #define IMGUI_WINDOW_CLASS ((ImGuiWindowClass*)STRUCT_PTR) */ - static native void nInit(); /* - jclass jImGuiWindowClassClass = env->FindClass("imgui/ImGuiWindowClass"); - imGuiWindowClassPtrID = env->GetFieldID(jImGuiWindowClassClass, "ptr", "J"); - */ + @Override + protected long create() { + return nCreate(); + } private native long nCreate(); /* - ImGuiWindowClass* imGuiWindowClass = new ImGuiWindowClass(); - return (intptr_t)imGuiWindowClass; - */ - - private native void nDestroy(long ptr); /* - delete (ImGuiWindowClass*)ptr; + return (intptr_t)(new ImGuiWindowClass()); */ /** diff --git a/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java b/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java new file mode 100644 index 0000000..a60df08 --- /dev/null +++ b/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java @@ -0,0 +1,18 @@ +package imgui.binding; + +import imgui.ImGui; + +/** + * Class provides a communication layer between binding and Dear ImGui library though the "pointer" field. + */ +public abstract class ImGuiStruct { + /** + * Pointer to the natively allocated object, + */ + public long ptr; + + public ImGuiStruct(final long ptr) { + ImGui.touch(); + this.ptr = ptr; + } +} diff --git a/imgui-binding/src/main/java/imgui/binding/ImGuiStructDestroyable.java b/imgui-binding/src/main/java/imgui/binding/ImGuiStructDestroyable.java new file mode 100644 index 0000000..a65488d --- /dev/null +++ b/imgui-binding/src/main/java/imgui/binding/ImGuiStructDestroyable.java @@ -0,0 +1,31 @@ +package imgui.binding; + +/** + * Class extends {@link ImGuiStruct} and provides additional API to allocated and de-allocate native objects. + */ +public abstract class ImGuiStructDestroyable extends ImGuiStruct { + /** + * Constructor, which creates natively allocated struct. Those structs should be manually disposed with {@link #destroy()} method. + */ + public ImGuiStructDestroyable() { + this(0); + ptr = create(); + } + + public ImGuiStructDestroyable(final long ptr) { + super(ptr); + } + + protected abstract long create(); + + /** + * Method to free natively allocated memory for the struct mapped with pointer. + */ + public void destroy() { + nDestroy(ptr); + } + + private native void nDestroy(long ptr); /* + delete (void*)ptr; + */ +} diff --git a/imgui-binding/src/main/native/jni_binding_struct.cpp b/imgui-binding/src/main/native/jni_binding_struct.cpp new file mode 100644 index 0000000..3553a0d --- /dev/null +++ b/imgui-binding/src/main/native/jni_binding_struct.cpp @@ -0,0 +1,17 @@ +#include "jni_binding_struct.h" + +jfieldID imGuiStructPtrID; + +namespace Jni +{ + void InitBindingStruct(JNIEnv* env) + { + jclass jImGuiStructClass = env->FindClass("imgui/binding/ImGuiStruct"); + imGuiStructPtrID = env->GetFieldID(jImGuiStructClass, "ptr", "J"); + } + + jfieldID GetBindingStructPtrID() + { + return imGuiStructPtrID; + } +} diff --git a/imgui-binding/src/main/native/jni_binding_struct.h b/imgui-binding/src/main/native/jni_binding_struct.h new file mode 100644 index 0000000..2475f93 --- /dev/null +++ b/imgui-binding/src/main/native/jni_binding_struct.h @@ -0,0 +1,17 @@ +#include + +#ifndef JNI_BINDING_STRUCT_H +#define JNI_BINDING_STRUCT_H + +#ifndef STRUCT_PTR +#define STRUCT_PTR env->GetLongField(object, Jni::GetBindingStructPtrID()) +#endif + +namespace Jni +{ + void InitBindingStruct(JNIEnv* env); + + jfieldID GetBindingStructPtrID(); +} + +#endif