SFML community forums

General => SFML projects => Topic started by: pdinklag on May 29, 2011, 11:43:02 am

Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 29, 2011, 11:43:02 am
After a good while of hidden progress, I can finally give a first sneak peek at JSFML, a Java binding for SFML.

(http://stuff.pdinklag.de/jsfml/jsfml-small.png)

The goal of this project is to allow Java developers like myself to make use of SFML, which I find to be the most straightforward multimedia library out there. A project like this happened to exist already, but has seemingly died ( http://www.sfml-dev.org/forum/viewtopic.php?t=1256 ). Therefore I am also allowing myself to use the same name, even though my version has nothing to do with that cancelled attempt.

The idea behind JSFML is not to implement all the SFML features in Java from scratch, but instead to define wrapper classes that delegate their functionality to the actual SFML. This is done using native code and has been working very well, however I have not been able to test the native part on any OS other than Win32 so far.

JSFML is based on the latest SFML2 snapshots. Since SFML2 is still in development, I will not yet release JSFML, however, I might release some builds in the near future for people to try.

So sorry, this is no release thread, but an announcement only. :)

The following is an overview of JSFML's features and status concerning the SFML modules.

SFML modules

Graphics
Most of the SFML Graphics module will be available for JSFML. Almost all classes mentioned in the SFML2 documenation (http://www.sfml-dev.org/documentation/2.0/group__graphics.php) are already supported. One exception is the Glyph class which I don't see any use for in JSFML (if you know any reason why I should make it available, let me know).

(http://stuff.pdinklag.de/jsfml/jsfml-shot-small.jpg) (http://stuff.pdinklag.de/jsfml/jsfml-shot.jpg)

A more notable exception is the Renderer class. Using JSFML, you can not yet create custom Drawables, that's also why the Renderer class has not yet been bound. This feature will require quite some work and therefore is planned for a future release, set for when the basic stuff is proven to work.

Audio
Most of SFML's Audio module is already available. You can load and play sounds and musics SFML style.

What's not really done yet is the ability to implement custom sound streams (e.g. for MP3 support). The basic foundation for that is already done, but I have not been able to test this yet. Much like custom Drawables, this is set for a future release.

Network
The Network module of SFML will not be available in JSFML, frankly because there's absolutely no need for it. The Java runtime library provides everything you need for networking.

System
Much like the Network module, SFML's System module is mostly unneeded for JSFML. Java provides multithreading, timing and unicode supprt.

However, the Vector2f and Vector3f classes will be defined as minimal data holders to simplify communication between JSFML and SFML. I do not think vector mathematics should be part of a multimedia library, instead people can extend these minimal vector classes and stuff in as much functionality as they like.

Window
The Window module port will fully include Event and Input handling and also provide the ContextSettings and VideoMode classes. The Window class itself will not be available, you will be forced to use the (fully supported) RenderWindow class of the Graphics module in the first releases.

In future releases, I plan to make it possible to have a Java Swing control wrapper around a RenderWindow so you can perfectly integrate a render targetinto your Swing GUI. So far I have only done some research on this, and this will be tough...




I am currently writing a Javadoc for this which I will upload as quickly as possible. In late June, you can expect a sneak peek release (most likely Win32 only).

In the meantime, feel free to share your comments, ideas and ask questions. :)
Title: JSFML - A Java binding for SFML
Post by: Nexus on May 29, 2011, 11:56:55 am
Looks very interesting! :)

Do you re-implement everything on your own? Maybe you could reuse some code of the older jSFML project, I don't know how evolved it is however.
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 29, 2011, 02:58:33 pm
Wow, what a good suprise :)

Quote
The idea behind JSFML is not to implement all the SFML features in Java from scratch, but instead to define wrapper classes that delegate their functionality to the actual SFML

Are you using CSFML or SFML directly? Do you use JNI?

Quote
One exception is the Glyph class which I don't see any use for in JSFML (if you know any reason why I should make it available, let me know).

It's not used a lot, but since Font and Text are available there's no reason not to implement Glyph as well. It may be useful for custom calculations on text, for example.

Quote
A more notable exception is the Renderer class. Using JSFML, you can not yet create custom Drawables, that's also why the Renderer class has not yet been bound. This feature will require quite some work and therefore is planned for a future release, set for when the basic stuff is proven to work.

Don't bother with that, the Renderer class won't exist anymore in a few weeks. So as the Drawable base class.

Quote
The Network module of SFML will not be available in JSFML, frankly because there's absolutely no need for it. The Java runtime library provides everything you need for networking.

Good thing. Most of the languages have a decent standard framework, and the network and system modules usually don't need to be ported.

Quote
The Window module port will fully include Event and Input handling and also provide the ContextSettings and VideoMode classes. The Window class itself will not be available, you will be forced to use the (fully supported) RenderWindow class of the Graphics module in the first releases.

Why can't you provide the Window class? It's very useful for people who just want an OpenGL context.

Quote
In future releases, I plan to make it possible to have a Java Swing control wrapper around a RenderWindow so you can perfectly integrate a render targetinto your Swing GUI.

If you can manage to retrieve the system handle of a SWIG control, you can directly use the Window constructor that takes a native window handle. I don't know if it's possible with Java, but for example it works with .Net.

And I love the logo ;)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 29, 2011, 03:23:59 pm
Quote from: "Nexus"
Do you re-implement everything on your own? Maybe you could reuse some code of the older jSFML project, I don't know how evolved it is however.

Yep, did it from scratch. I have no idea how that older jSFML project worked and didn't look into it. I would believe our approaches are different, but I don't know and frankly I don't really care too much. :)

Quote from: "Laurent"
Are you using CSFML or SFML directly? Do you use JNI?

I'm using SFML2 (not CSFML) and the binding is done using the JNI, yes. Right now JSFML consists of a jar (the Java library) and a dll (for Windows), the dll was supposed to simply contain delegation functions but now I also statically linked SFML into it (because of that sf::Text problem I had, you'll remember my "bug" report a few days ago).

Quote from: "Laurent"
the Renderer class won't exist anymore in a few weeks. So as the Drawable base class.

OK, gonna be interesting to see what the new class layout looks like then.

Quote from: "Laurent"
Why can't you provide the Window class? It's very useful for people who just want an OpenGL context.

I think I could provide it, but I laid focus on the Graphics and Audio modules really because that's where Java lacks possibilities greatly.

If you simply need an OpenGL context, you could use LWJGL (http://lwjgl.org/), which is a raw Java binding to OpenGL, OpenAL, OpenCL and some other libraries. Works well, but if you were to create a game engine or similar you'd still have to do all the work from ground up, which I became sick of and thus looked for something that features the most common things out of the box while being flexible at the same time. :D

Anyway, another reason I didn't include it yet is because Java doesn't support multiple class inheritance (one of the greatest drawbacks really). It'd be a pain to get Window and RenderTarget connected into one class. I'm sure there is a Java way to approach this, but like many things done the Java way it'd probably be very ugly...

Quote from: "Laurent"
If you can manage to retrieve the system handle of a SWIG control, you can directly use the Window constructor that takes a native window handle. I don't know if it's possible with Java, but for example it works with .Net.

Yep, that's exactly where I'm heading. I've been getting crashes trying to retrieve an AWT Canvas' window handle though, so I moved this into the future for now.

Quote from: "Laurent"
And I love the logo ;)

Thanks :lol:
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 29, 2011, 06:08:39 pm
Another question: do you plan to maintain this binding in the long term? I'm asking because if it's serious enough, I can make it the "official" Java binding -- with packages on the download page of the website, and a forum here. Tell me what you think about it.

And did you already setup a public repository for the project?
Title: JSFML - A Java binding for SFML
Post by: Groogy on May 29, 2011, 06:48:13 pm
Quote from: "pdinklag"
Anyway, another reason I didn't include it yet is because Java doesn't support multiple class inheritance (one of the greatest drawbacks really). It'd be a pain to get Window and RenderTarget connected into one class. I'm sure there is a Java way to approach this, but like many things done the Java way it'd probably be very ugly...


I had the same problem.
 
I did get it to work in Ruby actually using Mixin modules. I think it's called interfaces in Java? I think I made RenderTarget just simply an mixin module( Interface in Java) an included it into my RenderWindow and in RenderImage.

Not an expert in Java so there might be a huge difference between the two :)
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 29, 2011, 06:57:00 pm
Actually, I think I'm doing the same thing in SFML.Net (using pure interfaces to allow multiple inheritance).
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 30, 2011, 03:38:31 pm
Quote from: "Laurent"
Another question: do you plan to maintain this binding in the long term? I'm asking because if it's serious enough, I can make it the "official" Java binding -- with packages on the download page of the website, and a forum here. Tell me what you think about it.
And did you already setup a public repository for the project?

As for the time being, I'm all up for it!
Before we go there I'd suggest that I make a usable version first.

So far I'm using a private SVN repository, I think it'd be no trouble to move it onto a github on this page.

It all might need some integration work, as I fully rely on Apache Ant to automate building right now. Not sure what standard you'd like to see if any. Also no idea yet how it's gonna look on Linux or Mac. The C++ code should work for any OS, it's mainly the includes and some very special code parts (retrieving a Java canvas' handle) that are platform dependant.

I say we should care about all that when I got something that works, especially considering you're updating the whole Drawable tree soon.

Quote from: "Laurent"
Actually, I think I'm doing the same thing in SFML.Net (using pure interfaces to allow multiple inheritance).

Yep, Java classes can implement multiple interfaces. This will mean some duplicate code but it's better than not having it at all. I'll look into this in the near future.


(<- I believe there's a phpBB extension that introduces this horizontal rule tag, very useful thing to make posts more readable :) )

Meanwhile, good progress getting a RenderWindow onto a Java AWT canvas!

(http://stuff.pdinklag.de/jsfml/swing1-small.jpg) (http://stuff.pdinklag.de/jsfml/swing1.jpg) (http://stuff.pdinklag.de/jsfml/swing2-small.jpg) (http://stuff.pdinklag.de/jsfml/swing2.jpg) (click to enlarge)

As you see, the basic stuff works, just a few details don't.
[list=1]
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 30, 2011, 05:46:16 pm
Quote
The RenderWindow's resizing behaviour is a little weird, as you can see on the second picture. The area filled by Clear actually got enlarged with the canvas, however the render area is still clipped to the initial size, also the render area seems to be bound to the bottom left corner of the canvas. Any ideas?

It seems like the RenderWindow is not getting OS events. Are you able to use an SFML event loop in this example, and get all events properly?
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 30, 2011, 07:35:01 pm
Quote from: "Laurent"
It seems like the RenderWindow is not getting OS events. Are you able to use an SFML event loop in this example, and get all events properly?

I only get these event types:


MouseWheel and keyboard events don't get caught, neither does the resize one.

What I actually tried is override the "setBounds" method in the JSFML canvas, which gets called everytime the control's size and position changes. In that function, I use the RenderWindow's "setSize" function, which results in the issue I described above.
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 30, 2011, 08:05:34 pm
Quote
What I actually tried is override the "setBounds" method in the JSFML canvas, which gets called everytime the control's size and position changes. In that function, I use the RenderWindow's "setSize" function, which results in the issue I described above.

You shouldn't have to do that, the render window should catch the resize event and adjust accordingly.

So you must find why it's only getting mouse events :?
Title: JSFML - A Java binding for SFML
Post by: Hiura on May 30, 2011, 08:06:21 pm
Quote from: "pdinklag"
Also no idea yet how it's gonna look on Linux or Mac.
If you want some screenshot, just send me a .jar file.  :wink:

BTW, I think Ant is a great build tool; I used it last year for a school project and it went perfect. So I've no objection, on the contrary.

Good luck with this project!

EDIT : about the resize problem, couldn't you use something like http://download.oracle.com/javase/tutorial/uiswing/events/componentlistener.html ?
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 30, 2011, 08:39:13 pm
Quote from: "Hiura"
Good luck with this project!

EDIT : about the resize problem, couldn't you use something like http://download.oracle.com/javase/tutorial/uiswing/events/componentlistener.html ?

Thanks. :)
Gonna get back to you about Mac / Linux (whatever you can provide).

I don't need to be using a ComponentListener really, because I'm using a java.awt.Canvas subclass called JSFMLCanvas, I can directly react to resizes etc.

Keyboard events are most likely caught by the main window, I might try setting the focus on the JSFML canvas. Gonna try what Input reports as for which keys are pressed etc, to see whether that works fine.

Concerning the resize, those Java Canvases do have a window handle, but they are no ordinary windows, so to speak. So whatever RenderWindow does to detect a resize, I'm not surprised it doesn't work.

My question is, how can I modify the RenderWindow upon a resizing event? Simply setting its size causes this weird behaviour with a clipped area and it being bound to the bottom left.
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 30, 2011, 08:47:09 pm
Quote
Concerning the resize, those Java Canvases do have a window handle, but they are no ordinary windows, so to speak. So whatever RenderWindow does to detect a resize, I'm not surprised it doesn't work

If it has a window handle, it probably generates native resize events. A native window is not necessarily a top-level window, it may be a canvas, a button, a label, or whatever. So if you can get a native window handle from a control, you're not supposed to do anything else, SFML handles it.

What happens sometimes is that there are flags that control the behaviour of the canvas, and you may need to adjust them to get the expected results.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 30, 2011, 08:49:32 pm
Quote from: "Laurent"
If it has a window handle, it probably generates native resize events. A native window is not necessarily a top-level window, it may be a canvas, a button, a label, or whatever. So if you can get a native window handle from a control, you're not supposed to do anything else, SFML handles it.

What happens sometimes is that there are flags that control the behaviour of the canvas, and you may need to adjust them to get the expected results.

I am aware that "window" doesn't mean a window in the Windows sense only. :)

What are the flags I need to adjust? I suppose I do that using SendMessage in Windows?
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 30, 2011, 09:04:10 pm
Found out a little about the events.

All input events work fine if the JSFMLCanvas does have the focus on itself. The problem is that text fields and areas always request focus on themselves, so as soon as a text field or area is involved, the JSFMLCanvas will not receive any keyboard or mouse wheel events. The Input class will not work either in that case.

I'll have to play with this a little and see what could be done about it.

Concerning the resizing, no luck so far.

EDIT:
OK, interestingly, if the canvas does have the focus, the RenderWindow will not receive any resize events, but my custom resizing will work just fine!

So I'll need to somehow make sure that the RenderWindow has the focus in as many situations as possible...

I hope this page helps me to learn more: http://download.oracle.com/javase/6/docs/api/java/awt/doc-files/FocusSpec.html

EDIT 2:
OK, for the matter of resizing, it's enough if the canvas is focusable at all. The position popping doesn't happen anymore either, so even though the RenderWindow doesn't get any resize events, this seems to work just fine now.

EDIT 3:
The sizing issues are completely fixed now. I even added an option: you can have the image in the JSFMLCanvas scale with the canvas itself, or have its viewport extend so it can display more.
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 30, 2011, 10:10:38 pm
Quote
I am aware that "window" doesn't mean a window in the Windows sense only.

So what did you mean with "no ordinary windows"? :P

Quote
What are the flags I need to adjust? I suppose I do that using SendMessage in Windows?

Nop, I'm talking about whatever flags your specific API (in this case SWIG) may offer -- which usually have a direct effect on the OS-specific low-level.

Quote
All input events work fine if the JSFMLCanvas does have the focus on itself. The problem is that text fields and areas always request focus on themselves, so as soon as a text field or area is involved, the JSFMLCanvas will not receive any keyboard or mouse wheel events. The Input class will not work either in that case.

This seems quite normal to me, that's how all controls work. If everything could react to keyboard events without having the focus, it would be a big mess :)

Quote
I even added an option: you can have the image in the JSFMLCanvas scale with the canvas itself, or have its viewport extend so it can display more.

I'm not sure that you should provide such an extension to the SFML default behaviour, it may confuse people. Especially since it can be achieved without Java/internal specific code, it's just a matter of SFML views.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 30, 2011, 10:27:56 pm
Quote from: "Laurent"
Nop, I'm talking about whatever flags your specific API (in this case SWIG) may offer -- which usually have a direct effect on the OS-specific low-level.

It's called Java Swing, not SWIG. :)
Swing is built upon AWT, Java's old GUI builder. Unlike AWT, Swing components have no actual window handle anymore but are lightweight, meaning that Swing itself draws them itself. This JSFMLCanvas is an AWT component, I need to use the old AWT one here ("old" does not in any way mean bad or deprecated) because those still get their own window handle.

Sorry if that's confusing, just wanted to clarify the terms "Swing" and "AWT". You're probably no Java developer, so why would you even need to know. :)

Anyway, I'm not sure how exactly those AWT components are linked to their actual OS window counterparts, what events they take, etc. That's why I personally call them "non-ordinary", no special meaning really.

While I couldn't make the RenderWindow receive resize events on its own, I could catch them on a Java level and the RenderWindow's size from the Java level. This works just fine, I don't see any problem with this. And luckily, since the RenderWindow's SetSize method also creates a "Resized" event, people can catch that fine.

Quote from: "Laurent"
This seems quite normal to me, that's how all controls work. If everything could react to keyboard events without having the focus, it would be a big mess :)

True that. So I guess this does not need any extra work, except that the JSFMLCanvas should be focusable by clicking on it, I suppose. Does it work this way in the .NET binding?

Quote from: "Laurent"
I'm not sure that you should provide such an extension to the SFML default behaviour, it may confuse people. Especially since it can be achieved without Java/internal specific code, it's just a matter of SFML views.

What's the default behaviour, say in .NET, when the control containing the RenderWindow gets resized? In my case, the default behaviour is that the contents get scaled with the control.

You're right, the viewport extension is simply done using Views and could be left to the users by reacting on resize events.
Title: JSFML - A Java binding for SFML
Post by: Laurent on May 30, 2011, 10:45:33 pm
Quote
It's called Java Swing, not SWIG.  
Swing is built upon AWT, Java's old GUI builder. Unlike AWT, Swing components have no actual window handle anymore but are lightweight, meaning that Swing itself draws them itself. This JSFMLCanvas is an AWT component, I need to use the old AWT one here ("old" does not in any way mean bad or deprecated) because those still get their own window handle.

Sorry if that's confusing, just wanted to clarify the terms "Swing" and "AWT". You're probably no Java developer, so why would you even need to know.

Sorry I always mix SWIG and Swing :lol:
Thanks for the clarification, and don't worry I know a little about Swing and AWT, so what you say is not chinese to me :)

Quote
While I couldn't make the RenderWindow receive resize events on its own, I could catch them on a Java level and the RenderWindow's size from the Java level. This works just fine, I don't see any problem with this. And luckily, since the RenderWindow's SetSize method also creates a "Resized" event, people can catch that fine.

Ok, sounds good to me.

Quote
So I guess this does not need any extra work, except that the JSFMLCanvas should be focusable by clicking on it, I suppose. Does it work this way in the .NET binding?

Absolutely. It even works this way with a C++ SFML window --> if the window loses the focus it doesn't receive keyboard events anymore (whether it is a top-level window or not).

Quote
What's the default behaviour, say in .NET, when the control containing the RenderWindow gets resized? In my case, the default behaviour is that the contents get scaled with the control.

This is the expected default behaviour regardless the language, it's implemented like this in the core C++ code.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on May 31, 2011, 02:33:14 pm
Quote from: "Lauent"
Quote
So I guess this does not need any extra work, except that the JSFMLCanvas should be focusable by clicking on it, I suppose. Does it work this way in the .NET binding?

Absolutely. It even works this way with a C++ SFML window --> if the window loses the focus it doesn't receive keyboard events anymore (whether it is a top-level window or not).

Alright, I have some trouble with this, further detailed in here: http://stackoverflow.com/questions/6187472/awt-component-cannot-gain-focus-in-the-presence-of-a-text-field
I hope I can find a solution to this or somebody knows one.

Quote from: "Lauent"
Quote
What's the default behaviour, say in .NET, when the control containing the RenderWindow gets resized? In my case, the default behaviour is that the contents get scaled with the control.

This is the expected default behaviour regardless the language, it's implemented like this in the core C++ code.

Alright, I removed the option again, works this way by default in Java as well. :)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on June 08, 2011, 11:37:09 am
I was able to figure out (but not yet fix) the focus problem.

Java uses the GWLP_USERDATA field to store a pointer to an AWTComponent object, which apparently is required so the Canvas can cooperate with AWT correctly. SFML overrides that field to store its own Window object pointer, which will of course break AWT functionality.

I'm not yet sure how to solve this conflict, but I'll find a way.
I also have no idea yet whether this problem even exists in any environment other than Windows.

I'd appreciate any hints you can give me.
Title: JSFML - A Java binding for SFML
Post by: Laurent on June 08, 2011, 11:49:24 am
The usual alternative to user data is to have a global hash/map that associates Swing windows to their SFML window.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on June 08, 2011, 12:46:00 pm
Looked at the X11 and Cocoa code, I don't think this issue will occur on Linux and Mac.

My idea for Windows is as follows upon creation of the JSFMLCanvas:
[list=1]
Title: JSFML - A Java binding for SFML
Post by: pdinklag on June 08, 2011, 03:12:13 pm
Done.
The JSFMLCanvas is now focusable and receives all events correctly. :)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on June 16, 2011, 03:38:51 pm
Just in case people are wondering what's going on here.

The next scheduled task was to allow to create custom Drawable classes on a Java level, but since the whole Drawable, Image and Texture system is going to be changed entirely for what I know, I am kindof waiting for that update.

If somebody is interested giving this a shot on Linux or Mac ("giving a shot" means compiling the native Java library, which might need some adjustments in the C++ code), please let me know.
Title: JSFML - A Java binding for SFML
Post by: Hiura on June 16, 2011, 04:56:56 pm
If you want I can test JSFML on Mac in about three weeks. Just remind me then. ;-) (I've got some final exams coming up soon.)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 14, 2011, 09:28:15 am
Updated JSFML to the new input system (http://www.sfml-dev.org/forum/viewtopic.php?t=5250).
Still waiting for the big Graphics update to come. :)

Hiura, I gather you're currently busy implementing the new input system for MacOSX. If you're free, let me know.

General question about MacOSX: Is it possible to compile C++ on a Mac as well? I saw that the Mac sources of SFML are written in ObjectiveC. I'd hate to maintain the same code in two languages, talking about the native part of JSFML...
Title: JSFML - A Java binding for SFML
Post by: Hiura on July 14, 2011, 09:43:26 am
Quote from: "pdinklag"
If you're free, let me know.
I will be very soon. Next week let's say.

Quote from: "pdinklag"
General question about MacOSX: Is it possible to compile C++ on a Mac as well? I saw that the Mac sources of SFML are written in ObjectiveC. I'd hate to maintain the same code in two languages, talking about the native part of JSFML...
Compiling C++ code is possible, of course, but the problem is that the APIs provided by Apple are mostly written in Obj-C. Some are in C, however. In fact in the Mac port of SFML I'm using some combination of C++ and Obj-C (called Obj-C++).

What do you want to achieve exactly ? Depending on that there might be some alternatives to Obj-C APIs, however this is not common and most of the time you have to deal with it.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 14, 2011, 10:05:48 am
Quote from: "Hiura"
What do you want to achieve exactly ?.

Are you familiar with Java and the JNI?

To use SFML in Java, I wrote JNI compatible C++ code which is then compiled into a DLL (with SFML linked statically). That DLL can be loaded and used in Java, it serves as a binding between the JVM and SFML. On Linux, this would be a Shared Object (.so file). I have not compiled JSFML for Linux yet, but I have experience with that and it won't be any problem. However, I never used a Mac in my life and got no idea how I would go about it there. I guess what's needed is a dylib, but I'm honestly not sure at all. Anyway, a dynamic library that the JVM can load and use will be required.

The core jsfml C++ source, which contains nothing but JNI compatible delegates and bindings to SFML methods and objects, should be platform independent and even compile on a Mac.

The only thing that definitely needs platform specific code is (currently) the JSFMLCanvas implementation. It's that Java AWT component I blogged about earlier in this thread, it can be used to seamlessly integrate an SFML render window into a Swing GUI. In the best case, all that'll be needed is a Mac-specific method to retrieve a Cocoa handle for the Java canvas.
Title: JSFML - A Java binding for SFML
Post by: Hiura on July 14, 2011, 11:17:03 am
Quote from: "pdinklag"
Are you familiar with Java and the JNI?
I know how it basically works but I've never used it.

Quote from: "pdinklag"
However, I never used a Mac in my life and got no idea how I would go about it there. I guess what's needed is a dylib, but I'm honestly not sure at all.
Dylibs are the equivalent of .so so I guess you're right. Moreover, chown33 confirms it here (http://forums.macrumors.com/showthread.php?t=875804).

Quote from: "pdinklag"
In the best case, all that'll be needed is a Mac-specific method to retrieve a Cocoa handle for the Java canvas.
Well, I don't know how to do that so good luck.  :wink:
(I googled a bit but I could only find stuff for Windows.)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 14, 2011, 11:31:49 am
I took the info on how to do it from the actual Java AWT C++ sources. :D
Code: [Select]

sf::WindowHandle getHandle(JNIEnv *env, jobject canvas) {
sf::WindowHandle handle = 0;

JAWT awt;
awt.version = JAWT_VERSION_1_4;

if(JAWT_GetAWT(env, &awt)) {
JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);

if(ds) {
if(!(ds->Lock(ds) & JAWT_LOCK_ERROR)) {
JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
if(dsi) {
#if defined(SFML_SYSTEM_WINDOWS)
JAWT_Win32DrawingSurfaceInfo* dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
if(dsi_win) handle = dsi_win->hwnd;
#elif defined(SFML_SYSTEM_LINUX)
JAWT_X11DrawingSurfaceInfo* dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
if(dsi_x11) handle = dsi_x11->visualID;
#elif defined(SFML_SYSTEM_MACOS)
JAWT_MacOSXDrawingSurfaceInfo* dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
if(dsi_mac) handle = dsi_mac->cocoaViewRef;
#endif
}
ds->Unlock(ds);
}
awt.FreeDrawingSurface(ds);
}
}

return handle;
}


There's just no way for me to test this.

Mind if I send you a PM with all the stuff in the next days? Already warning ya, you'll need ant and a JDK (1.6 or higher) to build this. ;)
Title: JSFML - A Java binding for SFML
Post by: Laurent on July 14, 2011, 11:39:45 am
Quote
Code: [Select]
handle = dsi_x11->visualID;

Are you sure that it's not dsi_x11->drawable instead?
Title: JSFML - A Java binding for SFML
Post by: Hiura on July 14, 2011, 11:42:06 am
Quote from: "pdinklag"
There's just no way for me to test this.

Mind if I send you a PM with all the stuff in the next days? Already warning ya, you'll need ant and a JDK (1.6 or higher) to build this. ;)
Of course you can send me stuff to test. :wink:
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 14, 2011, 11:49:37 am
Quote from: "Laurent"
Are you sure that it's not dsi_x11->drawable instead?

Very well possible, makes more sense indeed.
The Linux and Mac part can be considered pseudo code, I haven't got to try them yet.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 18, 2011, 06:07:43 pm
Just tested music for the first time. It plays fine, but I'm having a problem when the application exits. The music would start to repeatedly hang and the Java application would never really end. It seems to me as if the SoundStream thread never gets ended, but doesn't get fed with new audio data either so it repeatedly plays the current buffer (I would guess).

Upon uninitialization, JSFML successfully invokes the sf::Music destructor and runs through. jsfml.dll (the SFML application) gets unloaded, but a dsound.dll thread keeps running, which kinda confirms my thoughts.

I have a feeling that the Stop method
Code: [Select]
   myIsStreaming = false;
    myThread.Wait();

doesn't quite do what it is supposed to be doing, but honestly I have no clue why. I know Java doesn't like threads created in native code, but that's about all I know and there must be a solution to this.

Any idea how I can investigate this problem further?
Title: JSFML - A Java binding for SFML
Post by: Laurent on July 18, 2011, 10:44:46 pm
I'm pretty sure it's not a C++ problem.


...I can't help more :lol:
Title: JSFML - A Java binding for SFML
Post by: Groogy on July 19, 2011, 03:48:02 am
Hmm really? Now I am a bit worried about rbSFML =/
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 19, 2011, 04:04:24 am
I'll study JNI and threads later on today and see whether I can find a solution.

I will also try and implement the LoadFromStream methods for Image, SoundBuffer etc. later. Since java.io.InputStream (Java's main abstract input stream class) does not support seeking, I might have to go with a custom java.io.FileInputStream wrapper. Gonna see what I can do here.

Quote from: "Groogy"
Now I am a bit worried about rbSFML =/

Are there similar problems in ruby then?
Title: JSFML - A Java binding for SFML
Post by: Groogy on July 19, 2011, 05:26:58 am
I haven't used audio yet so I don't know. But rbSFML per say don't have problems with native threads, on the other hand I think they more encourage them than Ruby threads because the latest versions of Ruby has a global lock so only one thread at a time can run ruby code. Which is pretty bad.

Anyway you never know I might have the same behaviour on rbSFML, I got one user I know use the audio library so I asked him to look at it.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 20, 2011, 06:42:28 pm
There is no way to have non-Java threads operate properly in the JVM space. This also explains the crashes I had trying to have custom SoundStream sublcasses interoperating with Java.

I will have to implement the SoundStream class in Java, which shouldn't be too much of a problem. For the Music class I'll have to to a wild mix between Java and C++ code so I can take advantage of sndfile.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on July 31, 2011, 12:41:18 pm
This audio problem seems to go deeper. Apparently the AL context is never destroyed.

It gets initialized by EnsureALInit, which will invoke the AudioDevice constructor. The destructor, however, is never invoked, I can honestly not tell why, but I doubt there is any way for me to fix it. I have no control over how the JVM manages space allocated by native code, if it can and does at all.

I would need a way to force the destruction of the AudioDevice, which is not possible with the methods currently provided (especially since the AudioDevice is merely a static object inside a method). Any chance some sort of "UnInitAL" could be added to ALCheck?

Or maybe does or did this problem occur in the C# binding? If yes, how did you solve it?
Title: JSFML - A Java binding for SFML
Post by: Laurent on July 31, 2011, 03:00:52 pm
The audio module will be fixed (in a future minor 2.x release) so that the audio device won't be destroyed at global exit anymore.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on August 01, 2011, 07:18:43 am
Hm, that would mean I'd have to carry this (not to be underestimated) problem around until then. Would you mind if I created a solution proposal myself, or is there anything that needs to be done in conjunction with that change?
Title: JSFML - A Java binding for SFML
Post by: Laurent on August 01, 2011, 07:55:18 am
I already know how to implement it, don't bother with that.

I don't think it's a major issue if your binding has a memory leak until it's fixed, people may not even notice it.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on August 01, 2011, 05:37:48 pm
A simple memory leak wouldn't hurt, but the application never exits unless killed manually (via task explorer / kill). It might still not be a major issue, but it is serious.
Title: JSFML - A Java binding for SFML
Post by: Laurent on August 01, 2011, 06:28:51 pm
And it is the AudioDevice which prevents the application from exiting??
Title: JSFML - A Java binding for SFML
Post by: pdinklag on August 06, 2011, 09:11:46 am
Quote from: "Laurent"
And it is the AudioDevice which prevents the application from exiting??

Yes.
I just created a Destroy() method for myself to make exactly sure of this.
When I manually destroy the AudioDevice from my Java application, the OpenAL thread will be stopped and the application will exit properly.

I don't know why the global exit destructor does not work inside the JVM heap space, but even if I knew I doubt there would be anything I could do about it. So if it doesn't take too much time from other things, I'd appreciate this to be a 2.0 feature already.
Title: JSFML - A Java binding for SFML
Post by: Laurent on August 06, 2011, 09:58:29 am
Ok, I see :?
Title: JSFML - A Java binding for SFML
Post by: pdinklag on August 24, 2011, 05:07:11 pm
Ported the new / changed sf::Texture, sf::RenderTexture and sf::Image classes.
Title: JSFML - A Java binding for SFML
Post by: coolhome on August 31, 2011, 04:38:29 am
Quote from: "pdinklag"
Ported the new / changed sf::Texture, sf::RenderTexture and sf::Image classes.


Very nice! Keep up the good work! Can't wait to play with this when its released.

-Slightly off topic below-
I just started a test project to use JNI and I figured why not use SFML for fun. I got the sf::Clock usable in java. Then I googled jsfml and found this! Man I thought I was being original! Oh well, at least I can say I played with JNI ;)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on August 31, 2011, 11:16:43 pm
Heh. :)
I'm not the first doing this either, but whoever worked on it first cancelled it.

I should definitely do some "demo" source and binary releases soon, been saying this for months. :roll: Things are working well, but I've still only tested on Win32, and besides there's one large SFML2 change still coming up (new Drawable API) which I'm waiting for.

Recently I've mostly worked on the javadoc, the latest version of that is currently up here: http://stuff.pdinklag.de/jsfml/javadoc/
Title: JSFML - A Java binding for SFML
Post by: coolhome on September 01, 2011, 01:45:31 am
Quote from: "pdinklag"
Heh. :)
I'm not the first doing this either, but whoever worked on it first cancelled it.

I should definitely do some "demo" source and binary releases soon, been saying this for months. :roll: Things are working well, but I've still only tested on Win32, and besides there's one large SFML2 change still coming up (new Drawable API) which I'm waiting for.

Recently I've mostly worked on the javadoc, the latest version of that is currently up here: http://stuff.pdinklag.de/jsfml/javadoc/


I seen that thread a long time ago and noticed it died. So yesterday I got bored and I've always wanted to try JNI so I had some fun.

I'm excited for the demo. If you need anything just let me know I would love to help. Totally reasonable for waiting. I had to change so much of my code in the past few months with these internal changes.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on September 29, 2011, 01:08:34 pm
Still waiting for the new graphics API to be finished before I continue. However, that seems to be the case soon. :)

Meanwhile, I tried to build and run JSFML on Linux, and except for a few problems (especially with the Swing integration), everything seems to work fine. Linux support is not too far away. :)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on September 30, 2011, 12:25:57 pm
I started refactoring the chaos that is my C++ code and sorting stuff in general.  :lol:
Once that's done, I will create a github repository for this project.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on October 02, 2011, 01:12:09 pm
Here's a website prototype: http://jsfml.pdinklag.de/
The logo is greatly inspired by Haikarainen's new SFML logo (http://www.sfml-dev.org/forum/viewtopic.php?p=37161#37161), the color scheme by Oracle's decision to pick red for Java. :)

I also decided to release this under the zlib/png license like SFML itself. I already created a github repository, which is still empty. I'm still refactoring and moving around here and there before I will fill it.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on October 02, 2011, 09:40:29 pm
Worked on "self-containment" and loading resources from InputStreams today. With self-containment I mean that JSFML will extract the required binaries from its own jar file into the user's home directory, much like Minecraft does it. What this ultimately means is that JSFML can be used in Java web applets! For Windows this already works perfectly, I have yet to fix some AWT integration problems in Linux.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on October 15, 2011, 11:35:48 am
Here's a first little demonstration:
http://jsfml.pdinklag.de/?page=demo

You'll have to trust the publisher. I'm not entirely sure why it's "UNKNOWN" as I entered my name in the certificate, but I'll try to fix this soon.

Would be cool if somebody could try this and report possible problems. Note the focus is on that it works at all, not the sloppy game. ;) You'll have to refresh the page if you want to restart. There should be sounds and a waving JSFML logo in the background (shaders!), also input should work.

It's currently Windows 32-bit only. Windows 64-bit users should be able to run it as well, because most browsers are 32-bit and have a 32-bit Java plugin installed.

KNOWN PROBLEMS:
Title: JSFML - A Java binding for SFML
Post by: jscombs91 on October 17, 2011, 05:05:10 am
Looks great. Only problem I've seen was on refresh the applet was white and the audio was "frozen" and kept repeating the same sound, but that only happened once. Definitely following this project and hopefully going to use it soon. Thanks a lot, pdinklag.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on October 17, 2011, 07:24:45 pm
Thank you for the support! :)

Ah yes, I had that problem a few times when testing, but never in the web browser applet, good to know it occurs there as well. For some reason the JSFML "game loop" thread misses that the applet becomes visible. I hope I can fix this.

A friend of mine reported a crash in the JVM when the graphics system was unloaded, I will try and investigate.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on December 04, 2011, 07:43:47 am
Just noticed the new graphics API (or at least some kind of release candidate of it) has been pushed to github. I have little time at the moment, life is keeping me busy, but I'll continue work on JSFML later this month. Just so nobody thinks this is dead - it's not at all! :)
Title: JSFML - A Java binding for SFML
Post by: StormWingDelta on December 29, 2011, 12:55:24 am
Just wondering what Java Compilers are you setting up JSFML for.

I'm using BlueJ and was wondering if what JSFML would be usable on it. :?:
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 14, 2012, 09:24:17 am
BlueJ is simply the IDE you use, the compiler is (carefully saying this) always the same. Any compiler in JDK 1.6 or higher will work, that should include the latest versions of BlueJ.

I started re-setting up the project for github-driven development.
You can follow up on the project here: https://github.com/pdinklag/JSFML

I'm refactoring and revisiting most of the code, as the previous version was quite a mess (all stuffed into one cpp file and that sort of thing). :roll: In the process, I'm also documenting it properly. Watch it grow slowly from here. :)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 20, 2012, 09:27:51 pm
I uploaded a first test package for everyone to try out. :)

This is what I call a "self-contained" package. It contains the compiled SFML and JSFML binaries (dll / so) for all supported systems and extracts whatever is needed to the directory ".jsfml" in the user directory. I'm not entirely sure about this kind of distribution yet, but it has clear advantages. People will not have to tinker with packaging them all up properly or set the java library path, but JSFML does all that automatically, at the cost of a larger file (jsfml.jar).

The test package contains binaries for Windows 32-bit and Linux 32-bit. However, it should also work for 64 bit systems as long as a 32-bit Java is installed (which is the case most of the time ;) ). I tested it on Windows 7 Professional and Ubuntu 10.04 LTS.

If all goes well, you should see something like this:
(http://stuff.pdinklag.de/jsfml-test1-thumb.jpg) (http://stuff.pdinklag.de/jsfml-test1.png)

It would be cool if some people could give this a try. It's available on github:
https://github.com/pdinklag/JSFML/downloads

Quote from: "Laurent"
Another question: do you plan to maintain this binding in the long term? I'm asking because if it's serious enough, I can make it the "official" Java binding -- with packages on the download page of the website, and a forum here. Tell me what you think about it.

I believe we can go with that. :)

If this test works out well, JSFML can probably be released very soon after SFML 2.0. The Swing integration feature might not make it into the first release, but it's definitely a top-priority goal once everything else is stable. I will start writing tutorials on how to use JSFML soon and update the website ( http://jsfml.pdinklag.de/ ).
Title: JSFML - A Java binding for SFML
Post by: Laurent on January 21, 2012, 09:23:11 am
Great job! :)

I've created the Java sub-forum so that people can post their feedback there directly.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 21, 2012, 10:03:30 am
OK, thanks :) I'll post an info thread later, since the main post in here is pretty much outdates.

I added another example that allows you to add shapes floating around, sort of a little benchmark test and demonstrating shapes:
https://github.com/pdinklag/JSFML/downloads

I'm experiencing something weird here. I set the max FPS to 60, but the FPS (and I'm actually counting every frame until a second passes) stays at 32 until I add a whole bunch of shapes to the scene. Does SFML automagically hold down  the frame rate for some reason? I don't really get it.
Title: JSFML - A Java binding for SFML
Post by: Laurent on January 21, 2012, 10:40:30 am
Quote
Does SFML automagically hold down the frame rate for some reason?

No. But since I recently changed the sf::Clock implementation, as well as the time API, there might be new bugs there.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 21, 2012, 12:21:35 pm
Well, I didn't update to the new Time API yet, will do that now.
Title: JSFML - A Java binding for SFML
Post by: Nexus on January 21, 2012, 12:51:21 pm
Is there an online documentation (JavaDoc or something) available? I always like to browse through APIs to get an overview :)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 21, 2012, 12:58:40 pm
Yup: http://jsfml.pdinklag.de/javadoc/
The new Time API is not in there yet, working on it at this moment (and wondering where the hell sf::Window::GetFrameTime has gone  :shock: )
Title: JSFML - A Java binding for SFML
Post by: Nexus on January 21, 2012, 01:13:50 pm
Thanks! Concerning sf::Window::GetFrameTime(), I just wondered the same thing (http://www.sfml-dev.org/forum/viewtopic.php?t=6831), it has been removed.

For the C++ operators, is there a class with static methods?
Code: [Select]
Vector2f a   = new Vector2f(2.f, 4.f);
Vector2f b   = new Vector2f(3.f, 5.f);
Vector2f sum = Op.add(a, b);
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 21, 2012, 01:43:59 pm
Quote from: "Nexus"
For the C++ operators, is there a class with static methods?
Code: [Select]
Vector2f a   = new Vector2f(2.f, 4.f);
Vector2f b   = new Vector2f(3.f, 5.f);
Vector2f sum = Op.add(a, b);

I plan on starting a discussion thread on that soon. ;)
Something like that will be needed for vectors, times and colors.
Title: JSFML - A Java binding for SFML
Post by: Masthiks on January 21, 2012, 11:26:45 pm
Hello!

There is a way to use JSFML for x64 architecture? I tried to modify SFMLNative.java but I have an error when I launch my test program: "libsndfile-1.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform", and  change "${env.SFML_HOME}/extlibs/bin/x86" by "${env.SFML_HOME}/extlibs/bin/x64" at line 125 of the ant file does not work.
It's a bit annoying :/

(By the way, sorry for my english)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 22, 2012, 07:00:52 am
Not yet, sorry.
You would have to provide an actual 64-bit libsndfile (SFML should include one in the extlib folder). You cannot mix a 32-bit DLL into a 64-bit application.

Are you building JSFML yourself? If so, you might be a good test subject for a win64 ant build target. :) I have no 64-bit system here (yet), so it's not easy for me to test those things.

EDIT:
I'm switching to x64 myself now. Had it planned for quite a while, better now on a lazy sunday than never. :D
Title: JSFML - A Java binding for SFML
Post by: Masthiks on January 22, 2012, 09:58:36 am
Quote from: "pdinklag"
Are you building JSFML yourself?


Yes, otherwise it's not very interesting to modify the ant file and SFMLNative.java (of JSFML) :)
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 22, 2012, 11:54:14 am
How did you link SFML on your 64-bit system? Because I can't (using mingw-64).
Title: JSFML - A Java binding for SFML
Post by: Masthiks on January 22, 2012, 01:59:40 pm
I use MinGW-get (http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20111118/) with the latest snapshot of SFML.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 23, 2012, 06:42:13 am
Hm, but where did you get the 64-bit extlibs (freetype, OpenAL-soft, etc)? I can't build SFML in 64-bit because there are only 32-bit versions of those in the repository.
Title: JSFML - A Java binding for SFML
Post by: Masthiks on January 24, 2012, 04:05:08 pm
I get the files from the directory "extlib/bin/x64" of GIT repository, and it works fine with SFML2. I think. Maybe I should test tonight :?
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 24, 2012, 09:36:59 pm
So you build SFML using Microsoft Visual C++ and JSFML using MinGW? Mixing those is probably not a good idea at all.

Still, it should be compilable. I'll look into setting everything up for 64-bit building, but I won't be able to test it until somebody builds the SFML dependencies for MinGW as 64-bit.
Title: JSFML - A Java binding for SFML
Post by: Masthiks on January 24, 2012, 09:56:15 pm
No, I build SFML using MinGW, I don't use Visual C++.

The compilation works fine but I have an application error when I try to use the audio module :?

EDIT: Ah, it's ok with x86 binaries of libsndfile-1.dll and openal32.dll. I thought that MinGW was also a 64-bit compiler... I understand the problem now.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 28, 2012, 07:12:20 pm
I will change the build process to use Microsoft's Visual C++ compiler for Windows again, because I can't figure out how to build the SFML dependencies using mingw-w64.

The Windows SDK with C++ compilers should suffice to build JSFML then, the Visual Studio should not be required. However, it will probably be required to build SFML itself... anyway, more details will follow.
Title: JSFML - A Java binding for SFML
Post by: pdinklag on January 29, 2012, 09:02:53 am
I changed the build system now to use MSVC++ on Windows, this way 64-bit is possible and works fine. :) I got rid of the cpptasks Ant plugin, it's just of no proper use if you want to compile with MSVC++.

More details: http://www.sfml-dev.org/forum/viewtopic.php?t=6906
Working on Linux right now.