[Solved] [LWJGL] [GLFW] ImageIO.read stalling and glitching with GLFW use

I have been making a lwjgl object-oriented wrapper just for fun and have hit a bit of a snag.

To test setting the cursor i have been trying to load a cursor image, but it isn’t working.

When i try to load the image after creating my window, it stalls on ImageIO.read.

When i try to load the image before creating my window, it glitches and doesn’t let me focus on the window. One thing i have noticed is that the application doesn’t show up in my open applications bar (don’t know the name, i’m on a mac) when i do this, but eclipse still shows it as running.

When i try to load the image in another thread started before i create the window it does the same as the first time, it stalls and never loads. The window is also never created.

When i try to load the image in another thread started after i create the window it does the same thing as the first time, it stalls and never loads.

It seems like using

new Thread() {...}.start()

Isn’t actually starting a new thread.

There are no errors, i do have a GLFWErrorCallback set up, and i do know that is working, but it is not throwing any errors either.

Here is my code to load the image:

new Thread() {
			public void run() {
				try {
					System.out.println("Testing... Testing...");
					
					System.out.println("1");
					URL url = new URL("http://orig13.deviantart.net/410c/f/2012/010/a/4/sniper_cross_hairs_cursor_by_parodyskillz-d4lxmpf.png");
					
					System.out.println("2");
					ImageIO.read(url);
					
					System.out.println("3");
					System.out.println("success");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();

Do note, this does work when i run this code in a fresh project with nothing in it.

My other code is too large to post here, so i will post what i think is relevant.

on startup:

if(glfwInit() != GL_TRUE) {
			Error.fatal("Error initalizing GLFW", this);
		}
		
		Error.registerGLFWErrorCallback();
		Monitor.registerMonitorCallback();
		
		ShutdownHandler.addDisposable("Game", this);
		
		window = Window.create(640, 400, "Cool Window", null, null, null);
		
		window.getCreationHints().setResizable(true);
		
		window.init();
		
		start();

window.init()

if(exists()) {
			throw new UnsupportedOperationException("Window is already initialized");
		}
		
		creationHints.apply();
		
		windowId = glfwCreateWindow(creationWidth, creationHeight, title, creationMonitor.getId(), share.getId());
		
		if(windowId == NULL) {
			Error.fatal("Error creating window", this);
		}
		
		add(this);
		
		makeCurrent();
		glfwSwapInterval(1);
		
		this.keyInput = new KeyInput(this);
		this.mouseInput = new MouseInput(this);
		this.windowInput = new WindowInput(this);

I am using the VM Argument -XstartOnFirstThread.

I would really appreciate if someone could help me figure this out, it is extremely frustrating.

ImageIO depends on AWT and initializes it when you try to do anything. This means that you cannot use it with GLFW on OS X (for the same reasons you cannot use GLFW with AWT/Swing or JavaFX).

LWJGL 3 has bindings to stb_image from the stb library, see the STBImage class. It’s as simple to use as ImageIO, supports common image formats (including HDR) and does some useful processing at load time (e.g. loading a 3-channel image as a 4-channel one).

Ah cool, thankyou, works perfectly now! :slight_smile:

I know this will probably be extremely newbie but could you explain to me/link me to another post explaining why AWT and GLFW cannot both be used simultaneously? Is there a conflict issue with the natives?

It’s due to how Cocoa works, JGLFW has similar limitations.

[quote=“sothatsit,post:3,topic:55861”]
No, it has to do with fundamental issues in the design of OS X. There can be only one NSApplication instance and one main event loop that has to run in the main thread. In addition, the main thread must be the first thread in the process (that’s why you need -XstartOnFirstThread). As you can imagine, both AWT and GLFW try to take over that first thread, that’s why everything freezes when you try to use them together.

Ah ok, good to know :slight_smile:

Sort of. I made some minor changes to GLFW to get JGLFW to work with AWT on OSX. Spine uses JGLFW and needs AWT for a few things (file dialog, imagio, etc). I submitted PRs to GLFW but they were only partially accepted. Unfortunately this was before the GLFW GitHub repo changed and the discussion was lost.

That was for OSX. I vaguely remember there being some issues with either Linux or Windows with JGLFW, but I don’t remember what it was. Spine uses LWJGL on those platforms.

You can use some other APIs for image loading, I don’t need AWT to do that. Even Apache Commons Imaging can help.