Skip to content

Commit fbe0ed5

Browse files
committed
Fix getBackgroundDrawList() and getForegroundDrawList(). Add methods to create draw list objects with constant pointer
1 parent 7a0e9ec commit fbe0ed5

File tree

11 files changed

+69
-42
lines changed

11 files changed

+69
-42
lines changed

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* BINDING NOTICE: Since it's impossible to do a 1:1 mapping with JNI, current class provides "getCmdList*()" methods.
1212
* Those are used to get the data needed to do a rendering.
1313
*/
14-
public final class ImDrawData implements ImDestroyable {
14+
public final class ImDrawData implements ImGuiDestroyableStruct {
1515
public static final int SIZEOF_IM_DRAW_IDX = 2;
1616
public static final int SIZEOF_IM_DRAW_VERT = (4 + 1) * 4;
1717

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111
* 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)
1212
* 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.
1313
*/
14-
public final class ImDrawList implements ImDestroyable {
15-
final long ptr;
14+
public final class ImDrawList {
15+
// BINDING NOTICE:
16+
// For proper usage in getWindowDrawList(), getBackgroundDrawList(), getForegroundDrawList() we should be able to change object pointer.
17+
long ptr;
1618

1719
ImDrawList(final long ptr) {
1820
this.ptr = ptr;
1921
}
2022

21-
@Override
22-
public void destroy() {
23-
nDestroy(ptr);
24-
}
25-
2623
/*JNI
2724
#include <stdint.h>
2825
#include <imgui.h>
@@ -38,10 +35,6 @@ public void destroy() {
3835
imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J");
3936
*/
4037

41-
private native void nDestroy(long ptr); /*
42-
delete (ImDrawList*)ptr;
43-
*/
44-
4538
/**
4639
* Flags, you may poke into these to adjust anti-aliasing settings per-primitive.
4740
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Font runtime data and rendering
55
* ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().
66
*/
7-
public final class ImFont implements ImDestroyable {
7+
public final class ImFont implements ImGuiDestroyableStruct {
88
final long ptr;
99

1010
private ImFontGlyph fallbackGlyph = null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* - Even though many functions are suffixed with "TTF", OTF data is supported just as well.
2323
* - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future!
2424
*/
25-
public final class ImFontAtlas implements ImDestroyable {
25+
public final class ImFontAtlas implements ImGuiDestroyableStruct {
2626
final long ptr;
2727

2828
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package imgui;
22

3-
public final class ImFontConfig implements ImDestroyable {
3+
public final class ImFontConfig implements ImGuiDestroyableStruct {
44
final long ptr;
55

66
private short[] glyphRanges;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Hold rendering data for one glyph.
55
* (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)
66
*/
7-
public final class ImFontGlyph implements ImDestroyable {
7+
public final class ImFontGlyph implements ImGuiDestroyableStruct {
88
final long ptr;
99

1010
/**

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public final class ImGui {
1717
private static final String LIB_TMP_DIR_PREFIX = "imgui-java-bin_" + System.getProperty("user.name", "user");
1818

1919
private static final ImGuiIO IMGUI_IO;
20+
private static final ImDrawList WINDOW_DRAW_LIST;
21+
private static final ImDrawList BACKGROUND_DRAW_LIST;
22+
private static final ImDrawList FOREGROUND_DRAW_LIST;
2023

2124
private static ImDrawData drawData;
2225
private static ImFont font;
2326
private static ImGuiStyle style;
24-
private static ImDrawList windowDrawList;
25-
private static ImDrawList backgroundDrawList;
26-
private static ImDrawList foregroundDrawList;
2727

2828
static {
2929
final String libPath = System.getProperty(LIB_PATH_PROP);
@@ -41,6 +41,9 @@ public final class ImGui {
4141
}
4242

4343
IMGUI_IO = new ImGuiIO();
44+
WINDOW_DRAW_LIST = new ImDrawList(0);
45+
BACKGROUND_DRAW_LIST = new ImDrawList(0);
46+
FOREGROUND_DRAW_LIST = new ImDrawList(0);
4447

4548
nInitJni();
4649
ImDrawList.nInit();
@@ -467,12 +470,22 @@ public static boolean begin(String title, ImBool pOpen, int imGuiWindowFlags) {
467470

468471
/**
469472
* Get draw list associated to the current window, to append your own drawing primitives
473+
* <p>
474+
* BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer.
475+
* If you need to get an object with constant pointer (which will point to the same window all the time) use {@link #getWindowDrawListNew()}.
470476
*/
471477
public static ImDrawList getWindowDrawList() {
472-
if (windowDrawList == null) {
473-
windowDrawList = new ImDrawList(nGetWindowDrawList());
474-
}
475-
return windowDrawList;
478+
WINDOW_DRAW_LIST.ptr = nGetWindowDrawList();
479+
return WINDOW_DRAW_LIST;
480+
}
481+
482+
/**
483+
* Get draw list associated to the current window, to append your own drawing primitives
484+
* <p>
485+
* BINDING NOTICE: returns {@link ImDrawList} for current window with constant pointer to it. Prefer to use {@link #getWindowDrawList()}.
486+
*/
487+
public static ImDrawList getWindowDrawListNew() {
488+
return new ImDrawList(nGetWindowDrawList());
476489
}
477490

478491
private static native long nGetWindowDrawList(); /*
@@ -4514,12 +4527,23 @@ public static byte[] acceptDragDropPayload(String type, int imGuiDragDropFlags)
45144527
/**
45154528
* Get background draw list for the viewport associated to the current window.
45164529
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
4530+
* <p>
4531+
* BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer.
4532+
* If you need to get an object with constant pointer (which will point to the same background all the time) use {@link #getBackgroundDrawListNew()}.
45174533
*/
45184534
public static ImDrawList getBackgroundDrawList() {
4519-
if (backgroundDrawList == null) {
4520-
backgroundDrawList = new ImDrawList(nGetBackgroundDrawList());
4521-
}
4522-
return backgroundDrawList;
4535+
BACKGROUND_DRAW_LIST.ptr = nGetBackgroundDrawList();
4536+
return BACKGROUND_DRAW_LIST;
4537+
}
4538+
4539+
/**
4540+
* Get background draw list for the viewport associated to the current window.
4541+
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
4542+
* <p>
4543+
* BINDING NOTICE: returns {@link ImDrawList} for current background with constant pointer to it. Prefer to use {@link #getBackgroundDrawList()}.
4544+
*/
4545+
public static ImDrawList getBackgroundDrawListNew() {
4546+
return new ImDrawList(nGetBackgroundDrawList());
45234547
}
45244548

45254549
private static native long nGetBackgroundDrawList(); /*
@@ -4529,12 +4553,23 @@ public static ImDrawList getBackgroundDrawList() {
45294553
/**
45304554
* Get foreground draw list for the viewport associated to the current window.
45314555
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
4556+
* <p>
4557+
* BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer.
4558+
* If you need to get an object with constant pointer (which will point to the same foreground all the time) use {@link #getForegroundDrawListNew()}.
45324559
*/
45334560
public static ImDrawList getForegroundDrawList() {
4534-
if (foregroundDrawList == null) {
4535-
foregroundDrawList = new ImDrawList(nGetForegroundDrawList());
4536-
}
4537-
return foregroundDrawList;
4561+
FOREGROUND_DRAW_LIST.ptr = nGetForegroundDrawList();
4562+
return FOREGROUND_DRAW_LIST;
4563+
}
4564+
4565+
/**
4566+
* Get foreground draw list for the viewport associated to the current window.
4567+
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
4568+
* <p>
4569+
* BINDING NOTICE: returns {@link ImDrawList} for current foreground with constant pointer to it. Prefer to use {@link #getForegroundDrawList()}.
4570+
*/
4571+
public static ImDrawList getForegroundDrawListNew() {
4572+
return new ImDrawList(nGetForegroundDrawList());
45384573
}
45394574

45404575
private static native long nGetForegroundDrawList(); /*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package imgui;
2+
3+
/**
4+
* When implement, class could be manually destroyed (free allocated memory on the JNI side).
5+
*/
6+
interface ImGuiDestroyableStruct {
7+
void destroy();
8+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values,
66
* and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors.
77
*/
8-
public final class ImGuiStyle implements ImDestroyable {
8+
public final class ImGuiStyle implements ImGuiDestroyableStruct {
99
final long ptr;
1010

1111
/**

0 commit comments

Comments
 (0)