# SDL\_Thread and Android

**URL:** <https://discourse.libsdl.org/t/sdl-thread-and-android/25775>\
**Category:** SDL Development\
**Created:** [February 20, 2019, 9:20pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775 "2019-02-20T21:20:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tab](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tab](https://discourse.libsdl.org/u/Tab)\
**Post date:** [February 20, 2019, 9:20pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/1 "2019-02-20T21:20:54Z")

</div>

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](https://discourse.libsdl.org/t/building-sdl2-0-9-in-android-studio-3-2-in-windows/25454) . 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.

---

<div class="post-metadata">

**Author:** ![DaSkuggo](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@DaSkuggo](https://discourse.libsdl.org/u/DaSkuggo)\
**Post date:** [February 20, 2019, 9:40pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/2 "2019-02-20T21:40:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JonnyD](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/jonnyd/32/1604_2.png) [@JonnyD](https://discourse.libsdl.org/u/JonnyD)\
**Post date:** [February 20, 2019, 10:42pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/3 "2019-02-20T22:42:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tab](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tab](https://discourse.libsdl.org/u/Tab)\
**Post date:** [February 20, 2019, 11:20pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/4 "2019-02-20T23:20:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tab](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tab](https://discourse.libsdl.org/u/Tab)\
**Post date:** [February 20, 2019, 11:21pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/5 "2019-02-20T23:21:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rtrussell](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/rtrussell/32/4768_2.png) [@rtrussell](https://discourse.libsdl.org/u/rtrussell)\
**Post date:** [February 21, 2019, 9:50am UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/6 "2019-02-21T09:50:19Z")

</div>

> [@Tab](#):
>
> When i try to run on Android, although the objects are created, the images are all black squares.

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");`

---

<div class="post-metadata">

**Author:** ![Tab](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tab](https://discourse.libsdl.org/u/Tab)\
**Post date:** [March 6, 2019, 9:11pm UTC](https://discourse.libsdl.org/t/sdl-thread-and-android/25775/7 "2019-03-06T21:11:04Z")

</div>

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 🙂
