The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL2, multithreading and loading screen
Naith


Joined: 03 Jul 2014
Posts: 158
Hi there!

I would like to create my textures and other objects in another thread (i.e, a thread that is not the same thread as I'm creating the window in).
For example, I have a state machine containing a menu state and a game state. Whenever I switch from the menu state to the game state I would like to create a thread which creates the textures and other objects for the game and while these objects is being created, I would like to have a loading screen which is rendering a texture or some text saying 'Loading...' (not important how the loading screen looks right now). As soon as the game is created (i.e, all the game objects has been successfully created), the loading screen stop rendering and the game state is rendered.

How can I achieve this with SDL2?

I've heard that CreateTexture and such shouldn't be executed in a thread that is not the main thread. Is this true?
Naith


Joined: 03 Jul 2014
Posts: 158
No one's got any ideas / tips?
reddog0000


Joined: 18 Aug 2014
Posts: 3
As far as I know your right about screen access routines needing to be in the same thread.

Of course you could just code a test using two threads and see what happens ...

woof
G-Shadow


Joined: 07 Sep 2014
Posts: 5
Hi!

Why don't you add one intermediate "loading" state and switch to it from the menu to render progress before loader thread start. Then the loader thread will switch state to the "game" on loading success. It could update progress variable using some atomic functions.
Naith


Joined: 03 Jul 2014
Posts: 158
I've solved it. Thanks for the answers.