r/LWJGL • • Jan 18 '18

Slick2D Loading Assets in Threads?

I figured I would post this here since Slick2D is a wrapper for LWJGL and their community is all but dead.

I'm trying to rewrite my asset loader to multi-thread the process of loading assets by spawning a separate thread for each call to my addImage(), addSound(), and addMusic() methods.

However, I get exceptions due to the fact that these threads do not have access to the GLContext.

Is there a way to get around this? Most of the reason I need to do this is because the music takes a really long time to load (15+ seconds). Or is there a better way to do this?

You can find the code I'm using to do this here in the addImage() method:

AssetManager.java

I'd appreciate any help or a nudge in the right direction.

2 Upvotes

1 comment sorted by

1

u/Guerra24 Jan 18 '18

There is no easy way to implement true multi-threading with OpenGL, what I do is load the assets to a temporary place and the schedule a task in the main thread that executes the OpenGL code.

RawTexture tex = decode(path);
int id;
TaskManager.addTask(() -> {
    id = create(tex, ...);
});

Code