SFML community forums

Bindings - other languages => Java => Topic started by: pdinklag on January 29, 2012, 04:34:29 am

Title: Java integration features
Post by: pdinklag on January 29, 2012, 04:34:29 am
While the first release of JSFML will be more like a direct binding to SFML standing by itself, in later versions I would like to build up a range of features to integrate JSFML into Java environments.

Generally, to be able to tell apart between pure SFML bindings and Java integration features, new classes for this task will get a different root package. I am still split about whether to call it org.sfml.java or simply org.jsfml, but I'm leaning towards the latter.

What follows below is a list of goals I want to achieve, a todo list so to speak, which I would like to discuss and possibly extend using this discussion thread.
Title: Java integration features
Post by: Laurent on January 29, 2012, 09:17:43 am
Quote
RenderCanvas
This feature will be a major milestone. It is a Java canvas that can be used as an SFML render window, thus making it possible to have OpenGL contexts in any Java AWT / Swing user interface, including browser applets.

SFML, in C++ and other languages, provide a Window/RenderWindow constructor which can take a handle to an external canvas. This is usually enough. Why do you need to provide a dedicated class in Java? Does it require more complicated code?
A potential problem that I can see, is that you will have to provide two versions: a canvas that is a Window, for OpenGL only, and one that is a RenderWindow, for using the graphics module.

Quote
The idea is to make it possible to render and possibly even manage AWT / Swing components within an SFML render window.

Good luck :P
It is usually not allowed to draw on top of an OpenGL area with non-OpenGL functions.
Title: Java integration features
Post by: pdinklag on January 29, 2012, 09:38:50 am
Quote from: "Laurent"
SFML, in C++ and other languages, provide a Window/RenderWindow constructor which can take a handle to an external canvas. This is usually enough. Why do you need to provide a dedicated class in Java? Does it require more complicated code?
A potential problem that I can see, is that you will have to provide two versions: a canvas that is a Window, for OpenGL only, and one that is a RenderWindow, for using the graphics module.

On a Java level, there is no way to get a window's handle. Also, not every Swing window or panel has a window handle. It's what I had worked on before, if you remember. The problem was getting the actual handle, and then there were a good amount of issues concerning event handling (because both SFML and AWT override GWLP_USERDATA, I was able to fix this and it already worked fine on Windows).

Quote
Good luck :P
It is usually not allowed to draw on top of an OpenGL area with non-OpenGL functions.

The only idea I have so far is to let Swing render its controls and then convert the result to a SFML texture. Rendering them to an image is perfectly doable, so there should be nothing speaking against this idea other than possible performance issues. I will definitely give this a try.
Title: Java integration features
Post by: Laurent on January 29, 2012, 09:43:53 am
Quote
Also, not every Swing window or panel has a window handle. It's what I had worked on before, if you remember. The problem was getting the actual handle, and then there were a good amount of issues concerning event handling (because both SFML and AWT override GWLP_USERDATA, I was able to fix this and it already worked fine on Windows).

Yeah, I remember now :)

Quote
The only idea I have so far is to let Swing render its controls and then convert the result to a SFML texture. Rendering them to an image is perfectly doable, so there should be nothing speaking against this idea other than possible performance issues. I will definitely give this a try.

Sounds good. But will user inputs still work if the widgets are rendered to an image instead of being directly integrated to the application window? How will focus/keyboard/mouse things work?
Title: Java integration features
Post by: pdinklag on January 29, 2012, 11:35:18 am
Quote from: "Laurent"
How will focus/keyboard/mouse things work?

I guess the only way to accomplish that is to eat SFML input events and somehow emulate AWT events and give them to the components. This will need a good amount of experimentation, I'm not sure how well this will work. But I can't really imagine that it isn't doable. :)
Title: Java integration features
Post by: pdinklag on January 29, 2012, 08:16:23 pm
I just did a little experimenting with synthesizing mouse events and passing them to a root component. It works perfectly! Even focus traversal seems to work correctly (that is, focus within the Swing focus manager, I am not yet sure what happens to the actual OS focus). I believe keyboard events (in the end it only comes down to mouse and keyboard events) will not be any more problematic.

Translating SFML input events to AWT events should be easy and not performance expensive. So I think this feature might become reality. All I need to do is find a quick way to convert AWT images to SFML textures. :)
Title: Java integration features
Post by: pdinklag on March 02, 2012, 04:55:50 pm
I've been doing some more concrete experiments with having Swing GUIs rendered by SFML, and as expected, I hit on a bunch of problems.

The awesome news is, the concept works and it will be possible! :) But there is quite some work ahead.

Event dispatching will probably require me to implement my own simple focus manager, or get the existing Java one to work with JSFML somehow. The latter will be extremely tough, especially if you consider that you can add Swing GUIs to SFML windows inside another Swing GUI...

Another problem is painting. Apparently, after dispatching events (such as mouse enter events), control repainting does not have an effect immediately. I can't quite explain this behaviour yet, but I know that repainting the GUI every frame seems to be quite a performance killer and should not be the way to accomplish things.

I also failed to get the contents of JScrollPanes (and probably any other pane with embedded views) to render, but I'm fairly certain that there's some tricks to get them to work.