SFML community forums

Bindings - other languages => Java => Topic started by: Groogy on September 22, 2012, 01:45:17 pm

Title: JOGL and JSFML
Post by: Groogy on September 22, 2012, 01:45:17 pm
Do you guys know of any way to use JOGL with JSFML? I am trying right now but it doesn't seem to work like in C where OpenGL only works on the current context. In JOGL it seems to be that you have to somehow aquire a context object to work with.

Just checking if someone already has solved this or if I should go ahead and try find a way?
Title: Re: JOGL and JSFML
Post by: pdinklag on September 22, 2012, 02:33:05 pm
I did experiment with LWJGL a little in the past. LWJGL is much better known than JOGL in the independent scene. It was clearly made with game development in mind, but it's a fully functional OpenGL, OpenAL and OpenCL binding for Java. JOGL is rather used in professional applications, I believe.

In any event, I'd like both LWJGL and JOGL to work with JSFML so you can do custom rendering.

What I tried is create a window using JSFML so that a valid context exists, and then I tried to tell LWJGL to use that context so I could do custom OpenGL rendering on my JSFML window. I made a forum post here, back then: http://lwjgl.org/forum/index.php/topic,4616.msg25357.html#msg25357
The "useContext" method was executed successfully (as in no exception was thrown), but I never actually saw anything. glClear or any type of rendering would have no effect on the window.

I never investigated any further because this kind of support is planned for later versions. However, feel free to experiment with this, I'd be extremely happy if JOGL and LWJGL were supported. :)
Title: Re: JOGL and JSFML
Post by: Groogy on September 23, 2012, 08:19:02 pm
At this time, is it possible to create a AWT frame or something similar from a SFML window? That could solve it for me.
Title: Re: JOGL and JSFML
Post by: Groogy on September 23, 2012, 08:21:46 pm
Nevermind found something way more interesting in the documentation

Update: Yepp works perfectly and irght out of the box. There is a function in the context/drawable factory to create a context representing an external context. Looking around JOGL looks freaking amazing and it supplements SFML perfectly on the raw OpenGL side.

Example code to get it to work:
package game;

import javax.media.opengl.GL;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLProfile;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;

public class Game {
    static { GLProfile.initSingleton(); }

    public static void main(String[] args) {
        RenderWindow window = new RenderWindow();
        window.create(new VideoMode(640, 480), "Hello JSFML!");
       
        GLContext context = GLDrawableFactory.getFactory(GLProfile.getDefault()).createExternalGLContext();
        GL gl = context.getGL();
        gl.glClearColor(1, 1, 0, 1);

        window.setFramerateLimit(30);

        while(window.isOpen()) {
            gl.glClear(GL.GL_COLOR_BUFFER_BIT);
            window.display();

            for(Event event : window.pollEvents()) {
                if(event.getType() == Event.Type.CLOSED) {
                    window.close();
                }
            }
        }
    }
}
Title: Re: JOGL and JSFML
Post by: Groogy on September 23, 2012, 08:52:20 pm
JOGL was quite easy to get to work but it might be intimidating for newcommers. Especially if they want to compile to x86_64(that was a hell to go through) There are no tutorials or information on how to work with an external context either as far as I could find except in the documentation. So I am thinking if I maybe should write a tutorial something like next weekend to JSFML on how to get JOGL up and running.
Title: Re: JOGL and JSFML
Post by: pdinklag on September 23, 2012, 09:07:24 pm
That would be great, thanks for the help! :)
Good to see that it works this easily.

Quote from: Groogy
At this time, is it possible to create a AWT frame or something similar from a SFML window? That could solve it for me.
Code for that already exists, but is not checked in at this point. I should make it a branch really... It's supposed to be added in a future JSFML version.
Title: Re: JOGL and JSFML
Post by: Groogy on September 23, 2012, 09:21:19 pm
Code for that already exists, but is not checked in at this point. I should make it a branch really... It's supposed to be added in a future JSFML version.

Well to be honest I was looking into JSFML for the possibility to use the Java GUI together with SFML to make it easier to develop tools and so on, etc. etc. I guess this is possible with SFML.NET but... I get enough from C# at my day job so if I have to choose that would be Java that wins for me.

So I'll be looking forward to seeing Java GUI and SFML mix seamlessly :)
Wouldn't it be possible to take like the HWND or something like that from the Java Frame and create an SFML Window from that? Or you are thinking of a much wider support than just simply that?
Title: Re: JOGL and JSFML
Post by: pdinklag on September 24, 2012, 07:55:06 am
Quote from: Groogy
Wouldn't it be possible to take like the HWND or something like that from the Java Frame and create an SFML Window from that?

Simple answer: yes.
Actual answer: nope, because first off, the handle is hidden away from Java and it has to be retrieved using native code. That's the easy part. On Windows at least, there's this problem that both SFML and AWT use the GWLP_USERDATA field to store information. I had a solution to this, but there were still some flaws if I recall right. I'm not sure how well it works on X/11 and Cocoa.

I guess there's only one way to find out. ;) I'll create a branch for this some time this week and check that code back in.
Title: Re: JOGL and JSFML
Post by: pdinklag on September 25, 2012, 07:40:02 am
Try the render-canvas branch: https://github.com/pdinklag/JSFML/tree/render-canvas
The org.jsfml.graphics.RenderCanvas is an AWT control that can have a RenderWindow.

Right now, you need to initialize it manually using "initialize". This has to be done when the canvas already has a window handle, e.g. once your window has become visible. I'm not sure yet how this can be detected automatically.

Simple example:
JFrame frame = new JFrame("JFrame");
frame.setSize(640, 480);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

RenderCanvas canvas = new RenderCanvas();
frame.add(canvas, BorderLayout.CENTER);
frame.add(new JLabel("I'm a JLabel!"), BorderLayout.SOUTH);
frame.setVisible(true);

//canvas has a handle at this point
canvas.initialize();

RenderWindow window = canvas.getRenderWindow();
while (window.isOpen()) {
        window.clear(Color.RED);
        window.display();
}
Title: Re: JOGL and JSFML
Post by: Groogy on September 25, 2012, 07:25:13 pm
Seems to work ^^ Really nice! (And damn Java is fast compared to my beloved Ruby)
Title: Re: JOGL and JSFML
Post by: Groogy on September 25, 2012, 07:39:12 pm
But it seems like you are loading in AWT DLL and JOGL does that as well and this somehow crashes with the message:

Quote
Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: Native Library C:\Program Files\Java\jdk1.7.0_07\jre\bin\awt.dll already loaded in another classloader

Know how to fix this?
Title: Re: JOGL and JSFML
Post by: pdinklag on September 25, 2012, 08:19:13 pm
JOGL does that?
If so, make sure JOGL loads it first. In JSFML, this error should be caught and ignored.
Title: Re: JOGL and JSFML
Post by: Groogy on October 10, 2012, 12:09:46 am
With RenderCanvas I am wondering about a thing. I am about to start using it. I refactored so everything is placed into it's own library and am about to start working on some tools. Though I have of course rendering in it's own thread and I am wondering if I can just send in the canvas to that thread without problems on JSFML side?

I did some experimentations so I can do all the rendering to a render texture that is then copied to the intended target at synchronization. Though this means that I have to fiddle around with the context activation between the threads and I measured that to actually add 200-220 microseconds on my high end machine. doesn't seem like much but if I can spare it then I will :)

I am going to try it out but if it doesn't seem to work do you think it's fixable to render to a canvas from another thread?
Title: Re: JOGL and JSFML
Post by: pdinklag on October 10, 2012, 08:03:29 am
I had a working multi-threaded example working a good while ago. The code is lost in the nimbus but I know it's doable. I also ported the Context class a while ago, this should help with context activation. It was never tested so far...

I guess try it, if it doesn't work we'll see what we can do. :)

By the way, you do not have to do the rendering in an extra thread, you can use a main loop in the main thread without problems. All AWT / Swing events are handled in the separate "AWT Thread" already, so it won't freeze. You just have to deal with AWT events (e.g. button presses) asynchronously. A concurrent list to queue such events to be processed could be of help here.

Also, prepare for more refactoring, I've done a bunch of changes in the master branch to fix remaining design issues. Right now it should all be in its final form. I can't tell when I will merge the RenderCanvas into the master branch. It probably should be in JSFML 1.0, but it has to work properly. I will commence Linux tests some time soon.
Title: Re: JOGL and JSFML
Post by: Groogy on October 10, 2012, 08:45:06 am
By the way, you do not have to do the rendering in an extra thread, you can use a main loop in the main thread without problems. All AWT / Swing events are handled in the separate "AWT Thread" already, so it won't freeze. You just have to deal with AWT events (e.g. button presses) asynchronously. A concurrent list to queue such events to be processed could be of help here.

Ah no I mean the game graphics are done in another thread. So I want the thread to be able to directly render to the render canvas so I don't have to deal with the context activations problems that SFML has. So the game library itself will not be aware that it is using a render canvas, it thinks it is only working with a render target while the tools application which generates the actual runnable code will be the one handling the AWT interface ;)
Title: Re: JOGL and JSFML
Post by: pdinklag on October 10, 2012, 08:51:41 am
Ah no I mean the game graphics are done in another thread. So I want the thread to be able to directly render to the render canvas so I don't have to deal with the context activations problems that SFML has. So the game library itself will not be aware that it is using a render canvas, it thinks it is only working with a render target while the tools application which generates the actual runnable code will be the one handling the AWT interface ;)
But if you use a "classical" main loop, you can render to the canvas like to any other render target with no trouble at all. You can use it like a normal RenderWindow, AWT will not interfere and run happily in its own thread.

I don't know your application, so I might be missing something. But from the sound of it, you are trying to protect yourself against problems that do not exist. :D
Title: Re: JOGL and JSFML
Post by: Groogy on October 10, 2012, 11:44:56 pm
Not really protecting myself. Just don't want to throw myself out into the big scary unkown of Java directly :D

Anyway trying to use the render canvas together with Swing so I can use the GUI builder of Netbeans but it seems to be messing with me. The Render canvas only gives me the error message that it fails to initialize.

I am placing the render canvas behind a custom panel that is very basic and have more or less nothing in it:
package tool;

import org.jsfml.graphics.Color;
import org.jsfml.graphics.RenderCanvas;
import org.jsfml.graphics.RenderWindow;

/**
 *
 * @author Groogy
 */

public class MainDisplayPanel extends javax.swing.JPanel {
    RenderCanvas gameCanvas = null;
    RenderWindow gameWindow = null;
   
    /**
     * Creates new form MainDisplayPanel
     */

    public MainDisplayPanel() {
        this.initComponents();
       
        this.gameCanvas = new RenderCanvas();
        this.add(gameCanvas);
        this.gameCanvas.initialize();
        this.gameWindow = this.gameCanvas.getRenderWindow();
        this.gameWindow.clear(Color.BLUE);
        this.gameWindow.display();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setName("MainDisplayPanel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1024, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 768, Short.MAX_VALUE)
        );
    }// </editor-fold>                        
    // Variables declaration - do not modify                    
    // End of variables declaration                  
 

This panel is then put into the main window frame. Could that mess the render canvas up? How do you recommend that I approach this?

PS: Also tried moving the canvas to the frame with the same code but nothing is rendered(though I can get a label and similar to render)
Title: Re: JOGL and JSFML
Post by: pdinklag on October 11, 2012, 07:02:46 am
You call "initialize" before the canvas actually has a handle. AWT doesn't give it a handle before the canvas becomes visible. During construction, it's not visible yet, therefore it has no handle, therefore you can't initialize it.

You say you added the panel to a frame - after you made that visible (setVisible), call "initialize". I'm not sure if overriding "setVisible" in your panel could do the trick - if it does, that would be good to know. I would really like to automate the "initialize" call.
Title: Re: JOGL and JSFML
Post by: pdinklag on October 11, 2012, 07:22:57 am
Quick update: pull the newest version of the "render-canvas" branch. :)

The render canvas will now initialize itself automatically after validation. It has a handle at that point, and the manuall call can be avoided. Note, however, that before this happens, your gameWindow will be NULL.

Your option would be to override "validate" in your panel and get the window after the super call completed. "validate" works recursively, that means after the super call, all child components have been validated and the render window has been initialized.
Title: Re: JOGL and JSFML
Post by: Groogy on October 11, 2012, 08:05:40 am
Will try that when I get home from jobb, thanks.

How is the Java way of doing a frame method? In C# I just make sure that invalidate is called on the panel and that I override the OnPaint method.
Title: Re: JOGL and JSFML
Post by: pdinklag on October 11, 2012, 08:22:37 am
What do you mean by a "frame method"?

You don't have to care about repainting the RenderCanvas. The content of the canvas will always be empty from Java's point of view. SFML / OpenGL does the "painting".
Title: Re: JOGL and JSFML
Post by: Groogy on October 11, 2012, 09:39:24 am
What I am talking about is not JSFML specific but Java :P Remember this place is pretty alien to me. But anyway what I want is to have a method called every frame/update so I can synchronize my render thread that will do the actual painting on the canvas.

I could hook up some ugly thing with timers I guess but I thought it would be smart to ask a more experienced JSFML user on what is the "Java way" to do this?
Title: Re: JOGL and JSFML
Post by: pdinklag on October 11, 2012, 10:16:47 am
Quote from: Groogy
some ugly thing with timers
Well, that actually is the Java way to do it. You would create another thread that loops. It always sleeps for a certain interval and then does whatever timed action you want. Abusing validation and painting for this is not even nice in C#. :P

That's why I keep ranting about the "main loop".

In a usual SFML application, you have a "main loop" that does all the input handling, timed updates, rendering, etc while the window is open. There is absolutely no reason to not do it the same way with a render canvas. You just create the frame, add the canvas, make it visible and enter your main loop. You don't have to create another thread, AWT runs in its own thread by default.

The only thing that you need to synchronize into that main loop would be AWT input, e.g. when the user presses a button. AWT event listeners are always called in the AWT thread. I'm not sure what else you would like to synchronize between threads.
Title: Re: JOGL and JSFML
Post by: Groogy on October 11, 2012, 10:29:31 am
Well it seems like the template that Netbeans puts up will make it near to impossible to do that since it seems to launch a thread for you which does only this:

new Tool().setVisible(true);
Where Tool is the frame class that netbeans generated for me.

Forgot to push my latest changes to Github so don't have the tool up there yet so can't pastebin some more complete code to you to show how it actually looks. But I'll fiddle around with netbeans generated code to see if I can inject some of my own stuff in there.

And I am rendering my graphics in a separate thread(well it is interfaced like if it was in a separate thread though if it should run in a separate thread is configurable) and I have this cause I am doing some heavy operations there and since it's an easy task to separate out and work on in parallell with the logic. But this isn't exposed to the AWT/Swing or whatever. So don't bother with this as it works on it's own. I more or less only need an entry point to call game.update() each frame and that's what I am more or less asking about where is the best way to do this in a Java GUI environment?
Title: Re: JOGL and JSFML
Post by: pdinklag on October 11, 2012, 10:45:43 am
I would have to see exactly what NetBeans generated there, but it sure doesn't make anything impossible. ;) We should probably stop discussing this until I can have a look at it. :)

Anyway, timed functions in Java are done using background casts. The simplest approach would be your own thread that does a loop that sleeps, the "Java way" would be using Java's Timer and TimerTask. It doesn't really matter whether you run in a GUI environment or not, Timer and TimerTask are the Java way.

http://sakhatech.com/blog/2011/03/23/java-timer-task-example/
Title: Re: JOGL and JSFML
Post by: Groogy on October 11, 2012, 10:51:58 am
That actually looks quite nice, would let me also easily implement the logic to update at a fixed frame rate which would be awesome as well. The less code I have to write the better :D Why reinvent the wheel or work against the system? ;)

Thanks I'll have a look at it when I get back home. I'll also show some pictures as soon as I have anything that looks remotely pretty :)
Title: Re: JOGL and JSFML
Post by: Groogy on October 11, 2012, 10:55:40 pm
Gah this is annoying. Trying to add the canvas to my custom panel but whenever I try Netbeans refuses me to drag-n-drop the panel to the main window. Getting really annoyed. I guess you have no idea?

Adding a Label and borderlayout and everything works just fine. Gah I was doing this in order to make tool development easier... This is the damned opposite.

Update: Even found a way to add the render canvas trough the GUI builder interface since it extends the canvas class. Still doesn't work(though with a plain canvas it works just perfectly)

Update: Nevermind, unless you want to delve into it and get it to work properly. But i managed to get it to work trough bypassing the GUI builder and adding the custom panel with the canvas in it for hand in the code so it isn't displayed trough the gui builder, then I got it to work.
Title: Re: JOGL and JSFML
Post by: pdinklag on October 12, 2012, 07:09:56 am
I... have never worked with that GUI builder and never planned on doing that, sorry. :P

If I have to do something small in order for NetBeans to show it properly, let me know. I believe that somebody who calls himself a "Java developer" should be able to code up his GUI from scratch in vi, though. ;)
Title: Re: JOGL and JSFML
Post by: Groogy on October 14, 2012, 04:07:42 pm
Alright little update, got timer on running the event loop and call update and eventually got everything working around the GUI Builder. It's not optimal but it is functional and I don't want to modify the game display canvas anyway so it doesn't really matter.

Little screenshot just of it in action:
(https://legacy.sfmluploads.org/cache/pics/262_jsfml-gui.png)

Everything works with resize and so on as well so it's really SWEET to work with :)

Why the update time is so high(5ms) is because the timer is set to that, since it's just tool doesn't really matter what FPS I have there as long as I don't change the underlying game structure to support the tool that will eventually slow the game itself down.
Title: Re: JOGL and JSFML
Post by: pdinklag on October 14, 2012, 04:15:32 pm
Great to know. :)
The latest version on git changed the wav the RenderCanvas receives events entirely. It worked fine on Linux as far as I could test.

It would be good if you had a shot at it as well, ie whether events are received properly.
Title: Re: JOGL and JSFML
Post by: Groogy on October 14, 2012, 04:22:23 pm
Works just fine for me :) did a clean rebuild of the entire project and no complaints. I can't try on Linux at the moment but when I get time i am going to fix my GRUB installation so I can boot into my archlinux installation.
Title: Re: JOGL and JSFML
Post by: pdinklag on October 14, 2012, 05:37:15 pm
That's very good!
If you're running into trouble with that, a new thread would probably be better. There are still some pitfalls on Linux, I shall take care of those soon.
Title: Re: JOGL and JSFML
Post by: gouessej on February 05, 2014, 01:24:51 pm
Hi

I did experiment with LWJGL a little in the past. LWJGL is much better known than JOGL in the independent scene. It was clearly made with game development in mind, but it's a fully functional OpenGL, OpenAL and OpenCL binding for Java. JOGL is rather used in professional applications, I believe.
In any event, I'd like both LWJGL and JOGL to work with JSFML so you can do custom rendering.
I'd be extremely happy if JOGL and LWJGL were supported. :)
The best way of getting some help from JogAmp maintainers doesn't consist in disparaging any JogAmp API. JogAmp contains fully functional Java bindings for OpenGL, OpenGL-ES, OpenAL (it supports OpenAL-Soft too), OpenCL (both in desktop and mobile environments, under Android too). JogAmp is used both in games (the MMORPG Salem, Poxnora, ...) and in other kinds of applications. Most major middle and high level APIs for 2D & 3D rendering support JogAmp. In my humble opinion, promoting our main competitor when someone simply asks for help isn't very professional and ethically incorrect.

At first, JOGL allows you to create a GLContext representing the OpenGL context created by a third party library:
http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/javax/media/opengl/GLDrawableFactory.html#createExternalGLContext() (http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/javax/media/opengl/GLDrawableFactory.html#createExternalGLContext())
This is what Groogy did  :)
However, unlike what he said, there are tons of tutorials and documentations in our wiki, especially concerning how to build our APIs:
http://jogamp.org/wiki/index.php/Main_Page (http://jogamp.org/wiki/index.php/Main_Page)

Secondly, I'm not sure JOGL can check whether AWT is already loaded into another classloader and AWT isn't mandatory anyway. You can create a NEWT window if you don't really need to use AWT.

We cannot keep an eye on all forums. Don't hesitate to contact us on our official forum if you still need some help (instead of complaining about our "bad" documentation and giving us no chance to know what is "wrong"):
http://forum.jogamp.org/ (http://forum.jogamp.org/)

Good luck with JogAmp.
Title: Re: JOGL and JSFML
Post by: DarrenKelly on June 10, 2014, 11:46:52 am
Hi related to this topic I was wondering how to get the glu from opengl to work with the external sfml window.

this context code helped me greatly getting things set up thanks.
context = GLDrawableFactory.getFactory(GLProfile.getDefault()).createExternalGLContext();

Title: Re: JOGL and JSFML
Post by: gouessej on July 25, 2014, 02:30:53 pm
DarrenKelly, choose your profile carefully, the default one isn't always the best suited.