SDL_Thread and Android

Hi all

Some context first: Im using SDL 2.0.9, VS2015 and Android Studio 3.2, all of this on windows; Android Studio has been configured following this guide: Building SDL2.0.9 in Android Studio 3.2 in Windows . If you are seeing this, thanks for the help DaSkuggo, that guide really helped me.

My issue has to deal with SDL_Thread. I have a scene that when its created, starts an SDL_Thread that runs a function that creates all the objects I’m going to need in the scene. Those objects themselves load their respective images using IMG_Load and SDL_CreateTextureFromSurface to create textures.

On the windows build, everything works fine. When i try to run on Android, although the objects are created, the images are all black squares. This only happens to objects created by the SDL_Thread.

I’m have just started working on Android, so I have no idea if its just a case of configurations or to even start. Any help would be much appreciated.

Cheers.

1 Like

Does the same thing happen if you call the function without using SDL_Thread?
Another thing would be to make sure if it can access the resources.
A third thing is to insert some SDL_Log() in the function to see if everything is called.

In some environments, interacting with the graphics driver (e.g. OpenGL ES for Android) can only be done on the main thread. SDL_CreateTextureFromSurface() would need to be called on the main thread in that case. Depending on your code structure, you might implement a thread-safe queue to communicate things like this back to the main thread.

No, if its called on the main thread, everything goes fine and the images are loaded correctly - i even had some images loaded before the thread was launched and it only happened to the objects that were created on thread. I tried to log everything and everything seemed to be called fine. It really threw me for a loop since on the windows version it worked fine.

That sounds like a very probable scenario. I will have to look into this and will come back with results.

This is not specifically an Android issue: on most platforms other than Windows textures may only be accessed from the thread which created the renderer. The best you can do is to load your objects into surfaces in your separate thread, but the conversion from a surface to a texture must be done in your main thread.

When developing cross-platform SDL2 applications in Windows I prefer to use the OpenGL backend (rather than the default Direct3D) so you are alerted to this kind of issue sooner:

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");

Took me a lot longer than i wanted to get back to this but didn’t want to leave this topic unanswered:

Yes, the problem i was having was indeed because i was running SDL_CreateTextureFromSurface on a worker thread instead of the main thread. Thanks JonnyD :slight_smile: