diff --git a/imgui-binding/src/main/java/imgui/ImDestroyable.java b/imgui-binding/src/main/java/imgui/ImDestroyable.java deleted file mode 100644 index 71c1ae7..0000000 --- a/imgui-binding/src/main/java/imgui/ImDestroyable.java +++ /dev/null @@ -1,9 +0,0 @@ -package imgui; - -/** - * While implementing this interface, class could be manually destroyed (free allocated memory on the JNI side). - * Read javadoc and "BINDING NOTICE" for specific cases. - */ -interface ImDestroyable { - void destroy(); -} diff --git a/imgui-binding/src/main/java/imgui/ImDrawData.java b/imgui-binding/src/main/java/imgui/ImDrawData.java index e9ae855..e49fb03 100644 --- a/imgui-binding/src/main/java/imgui/ImDrawData.java +++ b/imgui-binding/src/main/java/imgui/ImDrawData.java @@ -11,7 +11,7 @@ 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 implements ImDestroyable { +public final class ImDrawData implements ImGuiDestroyableStruct { public static final int SIZEOF_IM_DRAW_IDX = 2; public static final int SIZEOF_IM_DRAW_VERT = (4 + 1) * 4; diff --git a/imgui-binding/src/main/java/imgui/ImDrawList.java b/imgui-binding/src/main/java/imgui/ImDrawList.java index b8a2363..4fad02e 100644 --- a/imgui-binding/src/main/java/imgui/ImDrawList.java +++ b/imgui-binding/src/main/java/imgui/ImDrawList.java @@ -11,18 +11,15 @@ 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 implements ImDestroyable { - final long ptr; +public final class ImDrawList { + // BINDING NOTICE: + // For proper usage in getWindowDrawList(), getBackgroundDrawList(), getForegroundDrawList() we should be able to change object pointer. + long ptr; ImDrawList(final long ptr) { this.ptr = ptr; } - @Override - public void destroy() { - nDestroy(ptr); - } - /*JNI #include #include @@ -38,10 +35,6 @@ public final class ImDrawList implements ImDestroyable { imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J"); */ - private native void nDestroy(long ptr); /* - delete (ImDrawList*)ptr; - */ - /** * 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 5095962..7e552e0 100644 --- a/imgui-binding/src/main/java/imgui/ImFont.java +++ b/imgui-binding/src/main/java/imgui/ImFont.java @@ -4,7 +4,7 @@ package imgui; * 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 ImDestroyable { +public final class ImFont implements ImGuiDestroyableStruct { final long ptr; private ImFontGlyph fallbackGlyph = null; diff --git a/imgui-binding/src/main/java/imgui/ImFontAtlas.java b/imgui-binding/src/main/java/imgui/ImFontAtlas.java index 403b67f..e249a1d 100644 --- a/imgui-binding/src/main/java/imgui/ImFontAtlas.java +++ b/imgui-binding/src/main/java/imgui/ImFontAtlas.java @@ -22,7 +22,7 @@ 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 ImDestroyable { +public final class ImFontAtlas implements ImGuiDestroyableStruct { final long ptr; /** diff --git a/imgui-binding/src/main/java/imgui/ImFontConfig.java b/imgui-binding/src/main/java/imgui/ImFontConfig.java index 1bae50a..305c51b 100644 --- a/imgui-binding/src/main/java/imgui/ImFontConfig.java +++ b/imgui-binding/src/main/java/imgui/ImFontConfig.java @@ -1,6 +1,6 @@ package imgui; -public final class ImFontConfig implements ImDestroyable { +public final class ImFontConfig implements ImGuiDestroyableStruct { final long ptr; private short[] glyphRanges; diff --git a/imgui-binding/src/main/java/imgui/ImFontGlyph.java b/imgui-binding/src/main/java/imgui/ImFontGlyph.java index 7122f45..29f6b35 100644 --- a/imgui-binding/src/main/java/imgui/ImFontGlyph.java +++ b/imgui-binding/src/main/java/imgui/ImFontGlyph.java @@ -4,7 +4,7 @@ package imgui; * 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 ImDestroyable { +public final class ImFontGlyph implements ImGuiDestroyableStruct { final long ptr; /** diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index c6c4794..d5533f7 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -17,13 +17,13 @@ public final class ImGui { private static final String LIB_TMP_DIR_PREFIX = "imgui-java-bin_" + System.getProperty("user.name", "user"); private static final ImGuiIO IMGUI_IO; + private static final ImDrawList WINDOW_DRAW_LIST; + private static final ImDrawList BACKGROUND_DRAW_LIST; + private static final ImDrawList FOREGROUND_DRAW_LIST; private static ImDrawData drawData; private static ImFont font; private static ImGuiStyle style; - private static ImDrawList windowDrawList; - private static ImDrawList backgroundDrawList; - private static ImDrawList foregroundDrawList; static { final String libPath = System.getProperty(LIB_PATH_PROP); @@ -41,6 +41,9 @@ public final class ImGui { } IMGUI_IO = new ImGuiIO(); + WINDOW_DRAW_LIST = new ImDrawList(0); + BACKGROUND_DRAW_LIST = new ImDrawList(0); + FOREGROUND_DRAW_LIST = new ImDrawList(0); nInitJni(); ImDrawList.nInit(); @@ -467,12 +470,22 @@ public final class ImGui { /** * Get draw list associated to the current window, to append your own drawing primitives + *

+ * BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer. + * If you need to get an object with constant pointer (which will point to the same window all the time) use {@link #getWindowDrawListNew()}. */ public static ImDrawList getWindowDrawList() { - if (windowDrawList == null) { - windowDrawList = new ImDrawList(nGetWindowDrawList()); - } - return windowDrawList; + WINDOW_DRAW_LIST.ptr = nGetWindowDrawList(); + return WINDOW_DRAW_LIST; + } + + /** + * Get draw list associated to the current window, to append your own drawing primitives + *

+ * BINDING NOTICE: returns {@link ImDrawList} for current window with constant pointer to it. Prefer to use {@link #getWindowDrawList()}. + */ + public static ImDrawList getWindowDrawListNew() { + return new ImDrawList(nGetWindowDrawList()); } private static native long nGetWindowDrawList(); /* @@ -4514,12 +4527,23 @@ public final class ImGui { /** * Get background draw list for the viewport associated to the current window. * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + *

+ * BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer. + * If you need to get an object with constant pointer (which will point to the same background all the time) use {@link #getBackgroundDrawListNew()}. */ public static ImDrawList getBackgroundDrawList() { - if (backgroundDrawList == null) { - backgroundDrawList = new ImDrawList(nGetBackgroundDrawList()); - } - return backgroundDrawList; + BACKGROUND_DRAW_LIST.ptr = nGetBackgroundDrawList(); + return BACKGROUND_DRAW_LIST; + } + + /** + * Get background draw list for the viewport associated to the current window. + * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + *

+ * BINDING NOTICE: returns {@link ImDrawList} for current background with constant pointer to it. Prefer to use {@link #getBackgroundDrawList()}. + */ + public static ImDrawList getBackgroundDrawListNew() { + return new ImDrawList(nGetBackgroundDrawList()); } private static native long nGetBackgroundDrawList(); /* @@ -4529,12 +4553,23 @@ public final class ImGui { /** * Get foreground draw list for the viewport associated to the current window. * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + *

+ * BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer. + * If you need to get an object with constant pointer (which will point to the same foreground all the time) use {@link #getForegroundDrawListNew()}. */ public static ImDrawList getForegroundDrawList() { - if (foregroundDrawList == null) { - foregroundDrawList = new ImDrawList(nGetForegroundDrawList()); - } - return foregroundDrawList; + FOREGROUND_DRAW_LIST.ptr = nGetForegroundDrawList(); + return FOREGROUND_DRAW_LIST; + } + + /** + * Get foreground draw list for the viewport associated to the current window. + * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + *

+ * BINDING NOTICE: returns {@link ImDrawList} for current foreground with constant pointer to it. Prefer to use {@link #getForegroundDrawList()}. + */ + public static ImDrawList getForegroundDrawListNew() { + return new ImDrawList(nGetForegroundDrawList()); } private static native long nGetForegroundDrawList(); /* diff --git a/imgui-binding/src/main/java/imgui/ImGuiDestroyableStruct.java b/imgui-binding/src/main/java/imgui/ImGuiDestroyableStruct.java new file mode 100644 index 0000000..46ac6e5 --- /dev/null +++ b/imgui-binding/src/main/java/imgui/ImGuiDestroyableStruct.java @@ -0,0 +1,8 @@ +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/ImGuiStyle.java b/imgui-binding/src/main/java/imgui/ImGuiStyle.java index 9f05008..c1aa896 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiStyle.java +++ b/imgui-binding/src/main/java/imgui/ImGuiStyle.java @@ -5,7 +5,7 @@ package imgui; * 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 ImDestroyable { +public final class ImGuiStyle implements ImGuiDestroyableStruct { final long ptr; /** diff --git a/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java b/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java index 512c4f3..2458569 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java +++ b/imgui-binding/src/main/java/imgui/ImGuiWindowClass.java @@ -9,7 +9,7 @@ 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 ImDestroyable { +public final class ImGuiWindowClass implements ImGuiDestroyableStruct { final long ptr; /**