ice1000 / jimgui

:sparkling_heart: Pure Java binding for dear-imgui
Apache License 2.0
186 stars 13 forks source link

Drawing an image #9

Closed wrymn closed 6 years ago

wrymn commented 6 years ago

I am trying to draw an image. The only parameter I can pass is an JImTextureID.

JniLoader.load();
URL resource =  this.getClass().getResource("img.png");
JImTextureID texture = JImTextureID.fromFile(resource);

This is my code, resource is loaded correctly.

Would you point me to a problem?

Error:

Exception in thread "main" java.lang.IllegalStateException: cannot load /D:/Development/Projects/Java/GradleEditor/out/production/resources/img.png
    at org.ice1000.jimgui.JImTextureID.fromFile(JImTextureID.java:41)
    at org.ice1000.jimgui.JImTextureID.fromFile(JImTextureID.java:53)
    at Application.init(Application.java:32)
    at Application.run(Application.java:23)
    at Application.main(Application.java:14)
ice1000 commented 6 years ago

Could you please try replacing JImTextureID.fromFile(resource) with JImTextureID.fromFile(resource.getFile().substring(1))?

This doesn't happen on Linux. Maybe I should re-impl the API.

ice1000 commented 6 years ago

You can see the path is converted to /D:/Development/Projects/Java/GradleEditor/out/production/resources/img.png, and the leading / is unexpected.

wrymn commented 6 years ago

@ice1000 thank you very much for the reply

Sadly, still the same problem

code is now

        URL resource =  this.getClass().getResource("img.png");
        String path = resource.getPath().substring(1);
        System.err.println(path);
        JImTextureID texture = JImTextureID.fromFile(path);

but the issue persists

D:/Development/Projects/Java/GradleEditor/out/production/resources/img.png
Exception in thread "main" java.lang.IllegalStateException: cannot load D:/Development/Projects/Java/GradleEditor/out/production/resources/img.png
    at org.ice1000.jimgui.JImTextureID.fromFile(JImTextureID.java:41)
    at Application.init(Application.java:29)
    at Application.run(Application.java:22)
    at Application.main(Application.java:14)
ice1000 commented 6 years ago

That should be a bug, or your image cannot be loaded by DirectX.

Could you plz upload your image here? You can just drag-and-drop the image to the comment and wait for a while.

ice1000 commented 6 years ago

Could you please try loading this image?

https://avatars2.githubusercontent.com/u/16398479

wrymn commented 6 years ago

img

that was the test image. Your image did not load either.

Here is full java code, just to be safe: https://pastebin.com/e6zD5amk

ice1000 commented 6 years ago

image

Tested on my friend's laptop, it works..

ice1000 commented 6 years ago

image

Your image...

Which operating system are you using? Do you have DirectX9 installed?

wrymn commented 6 years ago

@ice1000 now I see the issue. I have not created new JImGui() instance.

There should be some tutorial snippet code in github main page.

This is finally working

JniLoader.load();
        JImGui gui = new JImGui();
        URL resource =  this.getClass().getResource("/img.png");
        String path = resource.getPath().substring(1);
        System.err.println(path);
        JImTextureID texture = JImTextureID.fromFile(path);

        while(!gui.windowShouldClose()){
            gui.initNewFrame();
            gui.text("Hello JImGui");
            gui.image(texture, 100, 100);
            gui.render();
        }
ice1000 commented 6 years ago

I see. It's due to a design problem of the API.

You should create the texture after the JImGui instance is created.

Try this code, I promise this will work! (flag

public class Main {
    @SuppressWarnings("AccessStaticViaInstance")
    public static void main(String[] args) throws URISyntaxException {
        JniLoader.load();
        try (JImGui gui = new JImGui()) {
            JImTextureID imTextureID = JImTextureID
                    .fromFile(Paths.get(Main.class.getResource([path to your image]).toURI()));
            while (!gui.windowShouldClose()) {
                gui.initNewFrame();
                gui.image(imTextureID, imTextureID.width, imTextureID.height);
                gui.render();
            }
        }
    }
}
ice1000 commented 6 years ago

My API is badly designed -- I've put the rendering framework (dx9 on win and ogl on *nix) initialization code inside the constructor of JImGui, while the creation of the textures should be done after the initialization of the rendering framework.

My initial goal is to prevent the user from worrying about the rendering framework, this is why I've mixed the initialization together. AFAIK a small tutorial should be written, the API is somewhat misleading.

ice1000 commented 6 years ago

A new idea: make createFromFile a method of JImGui!

ice1000 commented 6 years ago

@wrymn Create a new issue first, this issue should be closed.

ice1000 commented 6 years ago

@wrymn Short answer: it's production ready if you just want to use the imgui api only.

To interact with the low-level API of the rendering framework, you need some hacking, but let's first move to another issue.