Lwjgl3 doesn't like java.awt.Color? [MACOS]

Started by orange451, November 23, 2015, 04:48:21

Previous topic - Next topic

orange451

So... I've been sitting here scratching my head at this one.

I started this little 3d rendering demo on my apple machine a couple of months ago. I normally only develop on it when I'm away from my desktop. Everything worked fine... I transferred all the files over to my desktop, and continued working.

I'm home from uni for Thanksgiving break, and as such I'm away from my desktop. So, I wanted to continue to develop my 3d engine. I put all the files on my machine, added that gay -XstartOnFirstThread run configuration, and pressed run. The game loaded, but no window displayed. Now, I thought this was quite odd, and so I quickly began googling for answers; nothing.

I spent hours disecting my code to see what the problem could be, but I never got any step closer. It seemed the window just didn't want to create, even though there were no errors.

Today when I was trying to copy some code over line-at-a-time, I noticed something quite odd. Referencing java.awt's Color class causes lwjgl3 to... well... break. Even if I don't use the color. JUST referencing it breaks lwjgl.

I thought I was going crazy, so I tried a simple example. I used this: Clickity Clack

It ran perfectly.

before the while loop I put:
Color unusedColor = Color.WHITE;


The window no longer loads. I'm about to cry.

I am on lwjgl3 3.0.0b

Kai

Simply put: AWT does not work together with GLFW.
AWT is trying to steal the "main" thread for processing window messages when it initializes. And referencing a field inside of java.awt.Color also causes AWT to initialize, since the colors are no compiletime constants, which causes the static initializer to call Toolkit.loadLibraries().
So, you cannot use AWT and GLFW together.

Cornix

Note that this will only happen on MacOS. Windows and Linux operating systems do not force GLFW or AWT to be run on the first thread.


Kai

It's not that bad. Actually, you likely don't need AWT when using LWJGL3/GLFW.
For image I/O, for example, there is support for stb in LWJGL3.
If you need widget support, you can also use SWT, which is a lot more lightweight than AWT and also works with GLFW (though you don't need that also, since SWT will create the windows and the GL context).
Maybe you can tell a bit more why exactly you need AWT.

orange451

I only used it for the Color class ;)

Oh and image loading. I forgot that used awt too. Hah.

Cornix

If you really need the AWT Color class and you dont want to roll your own or use an existing one from the internet then you can simply copy the source code from AWT into a custom class of yours. The source code for most of the JDK is publicly available. Of course you would need to remove anything that depends on other AWT classes.

kappa

If you are not creating any AWT windows, then on OS X you can use LWJGL3 with AWT when running AWT in headless mode.

I was using AWT for font loading and it worked fine (although have switched to STB since) so should work fine for using the Color class.

As for image loading using AWT it may or may not work but give it a try. STB does provide pretty good image loading support so might be worth switching if it doesn't work.

Use the following VM arguments:

-XstartOnFirstThread -Djava.awt.headless=true

orange451

Thank you guys for all your help ^^
My engine now works on mac!