Skip to content

Commit

Permalink
[ fix #71 ] By adding JImGuiUtil.cacheStringToBytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Dec 17, 2020
1 parent 0262527 commit fa6d440
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Unreleased

+ Enable `USE_IMGUI_TABLES` and `IMGUI_DISABLE_OBSOLETE_FUNCTIONS` back
+ Add bookmarks and extension-info support from file dialog
+ Add `JImGuiUtil.cacheStringToBytes()` from #29

## v0.13.0

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ This is twofold.
Only arrays and primitive types are passed from Java to C++,
and only primitive types are returned.
+ Optimization for strings. That jimgui by default uses an inefficient way to convert `java.lang.String` into byte arrays that C++ is happy with.
You can customize the string-to-bytes function yourself by using `org.ice1000.jimgui.util.JImGuiUtil.setStringToBytes`,
or use the more efficient alternative to `java.lang.String` -- `org.ice1000.jimgui.JImStr`, which is supposed to be created as global constants.
You can customize the string-to-bytes function yourself by using `org.ice1000.jimgui.util.JImGuiUtil.setStringToBytes`
the default caching `JImGuiUtil.cacheStringToBytes()`, or use the more efficient alternative to `java.lang.String` --
`org.ice1000.jimgui.JImStr`, which is supposed to be created as global constants.

### IDE-friendliness

Expand Down
10 changes: 9 additions & 1 deletion core/src/org/ice1000/jimgui/util/JImGuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.ice1000.jimgui.JImGui;
import org.jetbrains.annotations.*;

import java.util.WeakHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.LongSupplier;
Expand All @@ -13,7 +14,6 @@
*/
public interface JImGuiUtil {
byte @NotNull [] EMPTY_BYTES = new byte[0];

/** defined in C++ float.h */
float FLT_MAX = 3.402823466e+38F;
/**
Expand Down Expand Up @@ -113,6 +113,14 @@ static void runPer(@NotNull LongSupplier millisSupplier, @NotNull Consumer<@NotN
static void setStringToBytes(@NotNull Function<@Nullable String, byte @Nullable []> stringToBytes) {
SharedState.STRING_TO_BYTES = stringToBytes;
}
/**
* The string-to-byte provided in <a href="https://github.com/ice1000/jimgui/issues/29">#29</a>,
* by <a href="https://github.com/Mr00Anderson">@Mr00Anderson</a>.
*/
static void cacheStringToBytes() {
WeakHashMap<String, byte[]> cachedBytes = new WeakHashMap<>();
setStringToBytes(s -> cachedBytes.computeIfAbsent(s, SharedState::getBytesDefaultImpl));
}
@Contract(value = "!null -> !null; null -> null", pure = true)
static byte @Nullable [] getBytes(@Nullable String text) {
return SharedState.STRING_TO_BYTES != null ?
Expand Down
4 changes: 3 additions & 1 deletion core/test/org/ice1000/jimgui/tests/Issue66.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

import org.ice1000.jimgui.JImGui;
import org.ice1000.jimgui.NativeBool;
import org.ice1000.jimgui.util.JImGuiUtil;
import org.ice1000.jimgui.util.JniLoader;

public class Issue66 {
public static void main(String[] args) {
JniLoader.load();
JImGuiUtil.cacheStringToBytes();
try (JImGui imgui = new JImGui(); NativeBool bool = new NativeBool()) {
byte[] b = new byte[100];
imgui.initBeforeMainLoop();
while (!imgui.windowShouldClose()) {
imgui.initNewFrame();
imgui.checkbox("Check to test input, uncheck to test ID", bool);
if (imgui.button("System.gc()")) System.gc();
for (int i = 0; i < 50; i++) {
for (int i = 0; i < 150; i++) {
if (bool.accessValue()) {
imgui.inputText("Label " + i, b);
} else {
Expand Down

0 comments on commit fa6d440

Please sign in to comment.